Skip to content

Commit f1808e0

Browse files
committed
Add support for googletest matchers.
1 parent 5a5eb4f commit f1808e0

File tree

5 files changed

+57
-3
lines changed

5 files changed

+57
-3
lines changed

rustest-testing/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ categories.workspace = true
1313
[dependencies]
1414

1515
[dev-dependencies]
16-
rustest = { path = "../rustest" }
16+
googletest = "0.14.0"
17+
rustest = { path = "../rustest", features = ["googletest"] }
1718

1819

1920
[lib]

rustest-testing/src/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,20 @@ pub fn add(left: u64, right: u64) -> u64 {
99
#[cfg(test)]
1010
mod tests {
1111
use super::*;
12+
use googletest::prelude::*;
1213
use rustest::test;
1314

1415
#[test]
1516
fn it_works() {
1617
let result = add(2, 2);
1718
assert!(result == 4);
1819
}
20+
21+
#[test]
22+
fn it_works_with_gtest() {
23+
let result = add(2, 2);
24+
assert_that!(result, eq(4));
25+
}
1926
}
2027

2128
#[cfg(test)]

rustest-testing/src/other_mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ pub fn addition(a: u32, b: u32) -> u32 {
55
#[cfg(test)]
66
pub(crate) mod tests {
77
use super::*;
8+
use googletest::prelude::*;
89
use rustest::test;
910

1011
#[test(params:(u32, u32, u32)=[
@@ -13,7 +14,8 @@ pub(crate) mod tests {
1314
(598318, 54876521, 55474839)
1415
])]
1516
fn test_addition_ok(Param((a, b, expected)): Param) {
16-
assert_eq!(addition(a, b), expected);
17+
expect_that!(addition(a, b), eq(expected));
18+
expect_that!(addition(b, a), eq(expected));
1719
}
1820

1921
#[test(params:(u32, u32, u32)=[
@@ -23,6 +25,6 @@ pub(crate) mod tests {
2325
])]
2426
#[xfail]
2527
fn test_addition_fail(Param((a, b, expected)): Param) {
26-
assert_eq!(addition(a, b), expected);
28+
assert_that!(addition(a, b), eq(expected));
2729
}
2830
}

rustest/Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ keywords.workspace = true
1212

1313
[dependencies]
1414
ctor = { version = "0.4.1", features = ["__no_warn_on_missing_unsafe"] }
15+
googletest = { version = "0.14.0", optional = true }
1516
libtest-mimic = "0.8.1"
1617
rustest-macro = { version = "0.1.0", path = "../rustest-macro" }
1718

19+
[features]
20+
googletest = ["dep:googletest"]
21+
1822
[[test]]
1923
name = "test"
2024
harness = false

rustest/src/test.rs

+40
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
use core::fmt::Display;
12
use libtest_mimic::Failed;
23
use std::error::Error;
34

45
/// Result of a test.
56
pub type Result = std::result::Result<(), Box<dyn Error>>;
67

78
#[doc(hidden)]
9+
810
pub struct InnerTestError {
911
msg: String,
1012
}
@@ -21,6 +23,13 @@ impl Display for InnerTestError {
2123
}
2224
}
2325

26+
#[cfg(feature = "googletest")]
27+
impl From<googletest::internal::test_outcome::TestFailure> for InnerTestError {
28+
fn from(e: googletest::internal::test_outcome::TestFailure) -> Self {
29+
Self { msg: e.to_string() }
30+
}
31+
}
32+
2433
/// The result of test runned by rustest.
2534
///
2635
/// InnerTestResult is necessary as we are somehow between user (`Result`) and libtest_mimic (`LibTestResult`).
@@ -58,13 +67,42 @@ impl IntoError for Result {
5867
}
5968
}
6069

70+
#[cfg(feature = "googletest")]
71+
impl<T> IntoError for googletest::Result<T> {
72+
fn into_error(self) -> InnerTestResult {
73+
self.map(|_v| ())
74+
.map_err(|e| InnerTestError::new(e.to_string()))
75+
}
76+
}
77+
6178
/// An actual test run by rustest
6279
pub struct Test {
6380
name: String,
6481
runner: Box<dyn FnOnce() -> InnerTestResult + Send + std::panic::UnwindSafe>,
6582
xfail: bool,
6683
}
6784

85+
fn setup_gtest() {
86+
#[cfg(feature = "googletest")]
87+
{
88+
use googletest::internal::test_outcome::TestOutcome;
89+
TestOutcome::init_current_test_outcome();
90+
}
91+
}
92+
93+
fn collect_gtest(test_result: InnerTestResult) -> InnerTestResult {
94+
#[cfg(not(feature = "googletest"))]
95+
{
96+
test_result
97+
}
98+
99+
#[cfg(feature = "googletest")]
100+
{
101+
use googletest::internal::test_outcome::TestOutcome;
102+
TestOutcome::close_current_test_outcome(test_result).map_err(|e| e.into())
103+
}
104+
}
105+
68106
impl Test {
69107
/// Build a new test.
70108
pub fn new<F>(name: impl Into<String>, xfail: bool, runner: F) -> Self
@@ -78,6 +116,7 @@ impl Test {
78116
}
79117
}
80118
fn run(self) -> LibTestResult {
119+
setup_gtest();
81120
let unwind_result = std::panic::catch_unwind(self.runner);
82121
let test_result = match unwind_result {
83122
Ok(Ok(())) => Ok(()),
@@ -92,6 +131,7 @@ impl Test {
92131
Err(InnerTestError::new(payload))
93132
}
94133
};
134+
let test_result = collect_gtest(test_result);
95135
if self.xfail {
96136
match test_result {
97137
Ok(_) => Err("Test should fail".into()),

0 commit comments

Comments
 (0)