Skip to content

Commit bf0ad21

Browse files
cramertjcopybara-github
authored andcommitted
Allow fixtures to be used with async tests
PiperOrigin-RevId: 802212419
1 parent 6c792bf commit bf0ad21

File tree

8 files changed

+227
-131
lines changed

8 files changed

+227
-131
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ jobs:
9393
name: test (no default features) / ubuntu / ${{ matrix.toolchain }}
9494
strategy:
9595
matrix:
96-
toolchain: [stable, 1.70.0]
96+
toolchain: [stable, 1.85.0]
9797
steps:
9898
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
9999
- name: Install ${{ matrix.toolchain }}
@@ -108,7 +108,7 @@ jobs:
108108
name: integration-test / ubuntu / ${{ matrix.toolchain }}
109109
strategy:
110110
matrix:
111-
toolchain: [stable, 1.70.0, nightly, beta]
111+
toolchain: [stable, 1.85.0, nightly, beta]
112112
steps:
113113
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
114114
- name: Install ${{ matrix.toolchain }}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ This library brings the rich assertion types of Google's C++ testing library
2828
* A new set of assertion macros offering similar functionality to those of
2929
[GoogleTest](https://google.github.io/googletest/primer.html#assertions).
3030

31-
**The minimum supported Rust version is 1.70**.
31+
**The minimum supported Rust version is 1.85**.
3232

3333
> :warning: The API is not fully stable and may still be changed until we
3434
> publish version 1.0.

googletest/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ repository = "https://github.com/google/googletest-rust"
2222
readme = "../README.md"
2323
license = "Apache-2.0"
2424
edition = "2021"
25-
rust-version = "1.70.0"
25+
rust-version = "1.85.0" # Needed for async closures.
2626
authors = [
2727
"Bradford Hovinen <[email protected]>",
2828
"Bastien Jacot-Guillarmod <[email protected]>",

googletest/src/fixtures.rs

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use std::{
2424
/// test function.
2525
///
2626
/// ```ignore
27-
/// strct MyFixture { ... }
27+
/// struct MyFixture { ... }
2828
///
2929
/// impl Fixture for MyFixture { ... }
3030
///
@@ -54,7 +54,7 @@ pub trait Fixture: Sized {
5454
/// a test function.
5555
///
5656
/// ```ignore
57-
/// strct MyFixture { ... }
57+
/// struct MyFixture { ... }
5858
///
5959
/// impl ConsumableFixture for MyFixture { ... }
6060
///
@@ -100,7 +100,7 @@ impl<T> DerefMut for FixtureOf<T> {
100100
/// argument to a test function.
101101
///
102102
/// ```ignore
103-
/// strct MyFixture{ ... }
103+
/// struct MyFixture{ ... }
104104
///
105105
/// impl StaticFixture for MyFixture { ... }
106106
///
@@ -214,22 +214,40 @@ mod tests {
214214

215215
#[test]
216216
#[should_panic(expected = "Whoooops")]
217-
fn fixture_teardown_called_even_if_test_fail(_: &PanickyFixture) {
218-
panic!("Test failed");
217+
fn fixture_teardown_called_even_if_test_fail(_: &PanickyFixture) -> Result<()> {
218+
Err(googletest::TestAssertionFailure::create("It must fail!".into()))
219219
}
220220

221-
struct FailingTearDown;
221+
struct AbortIfNotTornDownFixture {
222+
has_been_torn_down: bool,
223+
}
222224

223-
impl Fixture for FailingTearDown {
225+
impl Fixture for AbortIfNotTornDownFixture {
224226
fn set_up() -> crate::Result<Self> {
225-
Ok(Self)
227+
Ok(Self { has_been_torn_down: false })
226228
}
229+
fn tear_down(mut self) -> crate::Result<()> {
230+
self.has_been_torn_down = true;
231+
Ok(())
232+
}
233+
}
227234

228-
fn tear_down(self) -> crate::Result<()> {
229-
Err(googletest::TestAssertionFailure::create("It must fail!".into()))
235+
impl Drop for AbortIfNotTornDownFixture {
236+
fn drop(&mut self) {
237+
if !self.has_been_torn_down {
238+
eprintln!("AbortIfNotTornDownFixture was not torn down");
239+
std::process::abort();
240+
}
230241
}
231242
}
232243

244+
#[test]
245+
#[should_panic(expected = "simple_fail")]
246+
fn fixture_torn_down_after_test_panics(f: &AbortIfNotTornDownFixture) {
247+
expect_that!(f.has_been_torn_down, eq(false));
248+
panic!("simple_fail");
249+
}
250+
233251
struct OnlyOnce;
234252

235253
impl StaticFixture for OnlyOnce {
@@ -263,9 +281,9 @@ mod tests {
263281
#[test]
264282
fn static_fixture_two_different_static_fixtures(_: &&OnlyOnce, _: &&AnotherStaticFixture) {}
265283

266-
struct FailingFixture;
284+
struct FailingSetUp;
267285

268-
impl Fixture for FailingFixture {
286+
impl Fixture for FailingSetUp {
269287
fn set_up() -> crate::Result<Self> {
270288
Err(googletest::TestAssertionFailure::create("sad fixture".into()))
271289
}
@@ -277,5 +295,23 @@ mod tests {
277295

278296
#[test]
279297
#[should_panic(expected = "See failure output above")]
280-
fn failing_fixture_causes_test_failure(_: &FailingFixture) {}
298+
fn failing_fixture_causes_test_failure(_: &FailingSetUp) {
299+
unreachable!()
300+
}
301+
302+
struct FailingTearDown;
303+
304+
impl Fixture for FailingTearDown {
305+
fn set_up() -> crate::Result<Self> {
306+
Ok(Self)
307+
}
308+
309+
fn tear_down(self) -> crate::Result<()> {
310+
Err(googletest::TestAssertionFailure::create("It must fail!".into()))
311+
}
312+
}
313+
314+
#[test]
315+
#[should_panic(expected = "See failure output above")]
316+
fn failing_teardown_causes_test_failure(_: &FailingTearDown) {}
281317
}

googletest/src/lib.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,3 +324,45 @@ impl<T> OrFail for Option<T> {
324324
}
325325
}
326326
}
327+
328+
#[doc(hidden)]
329+
pub mod __internal_macro_support {
330+
use crate::fixtures::Fixture;
331+
332+
pub struct FixtureTearDownOnDrop<T: Fixture> {
333+
fixture: Option<T>,
334+
}
335+
336+
impl<T: Fixture> FixtureTearDownOnDrop<T> {
337+
pub fn new(fixture: T) -> Self {
338+
Self { fixture: Some(fixture) }
339+
}
340+
pub fn tear_down(mut self) -> crate::Result<()> {
341+
if let Some(fixture) = self.fixture.take() {
342+
fixture.tear_down()
343+
} else {
344+
Ok(())
345+
}
346+
}
347+
}
348+
349+
impl<T: Fixture> AsRef<T> for FixtureTearDownOnDrop<T> {
350+
fn as_ref(&self) -> &T {
351+
self.fixture.as_ref().unwrap()
352+
}
353+
}
354+
355+
impl<T: Fixture> AsMut<T> for FixtureTearDownOnDrop<T> {
356+
fn as_mut(&mut self) -> &mut T {
357+
self.fixture.as_mut().unwrap()
358+
}
359+
}
360+
361+
impl<T: Fixture> Drop for FixtureTearDownOnDrop<T> {
362+
fn drop(&mut self) {
363+
if let Some(fixture) = self.fixture.take() {
364+
fixture.tear_down().unwrap();
365+
}
366+
}
367+
}
368+
}

0 commit comments

Comments
 (0)