Skip to content

Commit ddf5a79

Browse files
committed
Migrate test helper binaries to run in the Rust test harness.
This simplifies the integration tests and makes them more realistic by asserting on the actual output when the test is run by the test harness rather than faking the output by manually running the test function. PiperOrigin-RevId: 497906957
1 parent d4f1942 commit ddf5a79

16 files changed

+167
-147
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Google Rust testing library
1+
# GoogleTest Rust
22

33
This library brings the rich assertion types of Google's C++ testing library
44
[GoogleTest](https://github.com/google/googletest) to Rust. It provides:
@@ -47,6 +47,9 @@ fn more_than_one_failure() -> Result<()> {
4747
}
4848
```
4949

50+
> In case one wants behaviour closer to other Rust test libraries, the macro
51+
> [`assert_that!`] has the same [`verify_that!`] but panics on failure.
52+
5053
This library includes a rich set of matchers, covering:
5154

5255
* Equality, numeric inequality, and approximate equality;
@@ -268,6 +271,7 @@ Please read [CONTRIBUTING.md](CONTRIBUTING.md) for details on how to contribute
268271
to this project.
269272

270273
[`and_log_failure()`]: https://docs.rs/googletest/*/googletest/trait.GoogleTestSupport.html#tymethod.and_log_failure
274+
[`assert_that!`]: https://docs.rs/googletest/*/googletest/macro.assert_that.html
271275
[`fail!`]: https://docs.rs/googletest/*/googletest/macro.fail.html
272276
[`google_test`]: https://docs.rs/googletest/*/googletest/attr.google_test.html
273277
[`matches_pattern!`]: https://docs.rs/googletest/*/googletest/macro.matches_pattern.html

googletest/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
[package]
1616
name = "googletest"
17-
version = "0.1.1"
17+
version = "0.2.0"
1818
keywords = ["unit", "matcher", "testing", "assertions"]
1919
categories = ["development-tools", "development-tools::testing"]
2020
description = "A rich assertion and matcher library inspired by GoogleTest for C++"
@@ -32,7 +32,7 @@ authors = [
3232
doctest = false
3333

3434
[dependencies]
35-
googletest_macro = { path = "../googletest_macro", version = "0.1.0" }
35+
googletest_macro = { path = "../googletest_macro", version = "0.2.0" }
3636
num-traits = "0.2.15"
3737
regex = "1.6.0"
3838

googletest/integration_tests/assertion_failure_in_subroutine.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#[cfg(not(google3))]
16-
use googletest::matchers;
17-
use googletest::{verify_that, Result};
18-
use googletest_macro::google_test_wrapper;
19-
use matchers::eq;
15+
fn main() {}
2016

21-
fn main() -> std::result::Result<(), ()> {
22-
should_fail_in_subroutine()
23-
}
17+
#[cfg(test)]
18+
mod tests {
19+
#[cfg(not(google3))]
20+
use googletest::matchers;
21+
use googletest::{google_test, verify_that, Result};
22+
use matchers::eq;
2423

25-
#[google_test_wrapper]
26-
fn should_fail_in_subroutine() -> Result<()> {
27-
assert_that_things_are_okay(2)
28-
}
24+
#[google_test]
25+
fn should_fail_in_subroutine() -> Result<()> {
26+
assert_that_things_are_okay(2)
27+
}
2928

30-
fn assert_that_things_are_okay(value: i32) -> Result<()> {
31-
verify_that!(value, eq(3))
29+
fn assert_that_things_are_okay(value: i32) -> Result<()> {
30+
verify_that!(value, eq(3))
31+
}
3232
}

googletest/integration_tests/custom_error_message.rs

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,31 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#[cfg(not(google3))]
16-
use googletest::matchers;
17-
use googletest::{verify_that, GoogleTestSupport, Result};
18-
use googletest_macro::google_test_wrapper;
19-
use matchers::eq;
15+
fn main() {}
2016

21-
fn main() {
22-
let _ = should_fail_with_custom_error_message();
23-
let _ = should_fail_with_custom_error_message_in_string();
24-
let _ = should_fail_with_custom_error_message_in_closure();
25-
}
17+
#[cfg(test)]
18+
mod tests {
19+
#[cfg(not(google3))]
20+
use googletest::matchers;
21+
use googletest::{verify_that, GoogleTestSupport, Result};
22+
use matchers::eq;
2623

27-
#[google_test_wrapper]
28-
fn should_fail_with_custom_error_message() -> Result<()> {
29-
let value = 2;
30-
verify_that!(value, eq(3)).failure_message("A custom error message")
31-
}
24+
#[test]
25+
fn should_fail_with_custom_error_message() -> Result<()> {
26+
let value = 2;
27+
verify_that!(value, eq(3)).failure_message("A custom error message")
28+
}
3229

33-
#[google_test_wrapper]
34-
fn should_fail_with_custom_error_message_in_string() -> Result<()> {
35-
let value = 2;
36-
verify_that!(value, eq(3)).failure_message("A custom error message in a String".to_string())
37-
}
30+
#[test]
31+
fn should_fail_with_custom_error_message_in_string() -> Result<()> {
32+
let value = 2;
33+
verify_that!(value, eq(3)).failure_message("A custom error message in a String".to_string())
34+
}
3835

39-
#[google_test_wrapper]
40-
fn should_fail_with_custom_error_message_in_closure() -> Result<()> {
41-
let value = 2;
42-
verify_that!(value, eq(3))
43-
.with_failure_message(|| "A custom error message from a closure".to_string())
36+
#[test]
37+
fn should_fail_with_custom_error_message_in_closure() -> Result<()> {
38+
let value = 2;
39+
verify_that!(value, eq(3))
40+
.with_failure_message(|| "A custom error message from a closure".to_string())
41+
}
4442
}

googletest/integration_tests/failure_due_to_fail_macro.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use googletest::{fail, Result};
16-
use googletest_macro::google_test_wrapper;
15+
fn main() {}
1716

18-
fn main() -> std::result::Result<(), ()> {
19-
just_fails()
20-
}
17+
#[cfg(test)]
18+
mod tests {
19+
use googletest::{fail, Result};
2120

22-
#[google_test_wrapper]
23-
fn just_fails() -> Result<()> {
24-
fail!("Expected test failure")
21+
#[test]
22+
fn just_fails() -> Result<()> {
23+
fail!("Expected test failure")
24+
}
2525
}

googletest/integration_tests/failure_due_to_fail_macro_with_empty_message.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use googletest::{fail, GoogleTestSupport};
15+
fn main() {}
1616

17-
fn main() {
18-
just_fails();
19-
}
17+
#[cfg(test)]
18+
mod tests {
19+
use googletest::{fail, google_test, GoogleTestSupport, Result};
2020

21-
fn just_fails() {
22-
fail!().and_log_failure();
21+
#[google_test]
22+
fn just_fails() -> Result<()> {
23+
fail!().and_log_failure();
24+
Ok(())
25+
}
2326
}

googletest/integration_tests/failure_due_to_fail_macro_with_format_arguments.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use googletest::{fail, GoogleTestSupport};
15+
fn main() {}
1616

17-
fn main() {
18-
just_fails();
19-
}
17+
#[cfg(test)]
18+
mod tests {
19+
use googletest::{fail, google_test, GoogleTestSupport, Result};
2020

21-
fn just_fails() {
22-
fail!("Failure message with argument: {}", "An argument").and_log_failure();
21+
#[google_test]
22+
fn just_fails() -> Result<()> {
23+
fail!("Failure message with argument: {}", "An argument").and_log_failure();
24+
Ok(())
25+
}
2326
}

googletest/integration_tests/failure_due_to_returned_error.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use googletest_macro::google_test_wrapper;
15+
fn main() {}
1616

17-
fn main() -> Result<(), ()> {
18-
should_fail_due_to_returned_error()
19-
}
17+
#[cfg(test)]
18+
mod tests {
19+
use googletest::google_test;
2020

21-
#[google_test_wrapper]
22-
fn should_fail_due_to_returned_error() -> Result<(), i32> {
23-
Err(123)
21+
#[google_test]
22+
fn should_fail_due_to_returned_error() -> Result<(), i32> {
23+
Err(123)
24+
}
2425
}

googletest/integration_tests/first_failure_aborts.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#[cfg(not(google3))]
16-
use googletest::matchers;
17-
use googletest::{verify_that, GoogleTestSupport, Result};
18-
use googletest_macro::google_test_wrapper;
19-
use matchers::eq;
15+
fn main() {}
2016

21-
fn main() {
22-
let _ = first_failure_aborts();
23-
}
17+
#[cfg(test)]
18+
mod tests {
19+
#[cfg(not(google3))]
20+
use googletest::matchers;
21+
use googletest::{google_test, verify_that, GoogleTestSupport, Result};
22+
use matchers::eq;
2423

25-
#[google_test_wrapper]
26-
fn first_failure_aborts() -> Result<()> {
27-
let value = 2;
28-
verify_that!(value, eq(3))?;
29-
verify_that!(value, eq(4)).and_log_failure();
30-
Ok(())
24+
#[google_test]
25+
fn first_failure_aborts() -> Result<()> {
26+
let value = 2;
27+
verify_that!(value, eq(3))?;
28+
verify_that!(value, eq(4)).and_log_failure();
29+
Ok(())
30+
}
3131
}

googletest/integration_tests/integration_tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ mod tests {
2424
use googletest_macro::google_test_wrapper;
2525
#[cfg(google3)]
2626
use matchers::all;
27-
use matchers::{contains_regex, contains_substring, eq, matches_regex, not};
27+
use matchers::{contains_regex, contains_substring, eq, not};
2828
use std::process::Command;
2929

3030
#[google_test]
@@ -356,10 +356,10 @@ a_submodule :: A_STRUCT_IN_SUBMODULE.eq_predicate_as_method(a, b) was false with
356356

357357
verify_that!(
358358
output,
359-
matches_regex(
359+
contains_regex(
360360
"\
361361
Expected test failure
362-
at .*googletest/integration_tests/failure_due_to_fail_macro.rs:[0-9]+:5
362+
at .*googletest/integration_tests/failure_due_to_fail_macro.rs:[0-9]+:9
363363
"
364364
)
365365
)

0 commit comments

Comments
 (0)