Skip to content

Commit

Permalink
1. Test also matrix case
Browse files Browse the repository at this point in the history
2. add module path
3. add start time
4. avoid destructoring for longer compatibility
  • Loading branch information
la10736 committed Jan 1, 2025
1 parent d77d7c6 commit 4a9763f
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 19 deletions.
31 changes: 31 additions & 0 deletions rstest/src/context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/// A test context.
#[non_exhaustive]
pub struct Context {
/// The complete module test path
pub module: &'static str,
/// The complete test name
pub name: &'static str,
/// The optional test description
pub description: Option<&'static str>,
/// The optional case number
pub case: Option<usize>,
/// Start time
pub start: std::time::Instant,
}

impl Context {
pub fn new(
module: &'static str,
name: &'static str,
description: Option<&'static str>,
case: Option<usize>,
) -> Self {
Self {
module,
name,
description,
case,
start: std::time::Instant::now(),
}
}
}
19 changes: 3 additions & 16 deletions rstest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ pub mod __std {
}
}

mod context;
pub use crate::context::Context;

/// Define a fixture that you can use in all `rstest`'s test arguments. You should just mark your
/// function as `#[fixture]` and then use it as a test's argument. Fixture functions can also
/// use other fixtures.
Expand Down Expand Up @@ -1469,19 +1472,3 @@ pub use rstest_macros::fixture;
/// ```
///
pub use rstest_macros::rstest;

pub struct Context {
pub name: &'static str,
pub description: Option<&'static str>,
pub case: Option<usize>,
}

impl Context {
pub fn new(name: &'static str, description: Option<&'static str>, case: Option<usize>) -> Self {
Self {
name,
description,
case,
}
}
}
17 changes: 17 additions & 0 deletions rstest/tests/resources/rstest/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,20 @@ fn without_case(#[context] ctx: Context) {
assert_eq!(None, ctx.description);
assert_eq!(None, ctx.case);
}

mod first {
mod inner {
use rstest::*;

#[rstest]
fn test(#[context] ctx: Context) {
assert!(ctx.module.ends_with("first::inner"));
}
}
}

#[rstest]
fn measure_time(#[context] ctx: Context) {
std::thread::sleep(std::time::Duration::from_millis(100));
assert!(ctx.start.elapsed() >= std::time::Duration::from_millis(100));
}
2 changes: 2 additions & 0 deletions rstest/tests/rstest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,8 @@ fn context() {
TestResults::new()
.ok("with_case::case_1_description")
.ok("without_case")
.ok("first::inner::test")
.ok("measure_time")
.assert(output);
}

Expand Down
2 changes: 1 addition & 1 deletion rstest_macros/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ fn single_test_case(
.map(|p| {
(p.clone(), {
let e: Expr = parse_quote! {
Context::new(#test_fn_name_str, #description, #pos)
Context::new(module_path!(), #test_fn_name_str, #description, #pos)
};
e
})
Expand Down
38 changes: 36 additions & 2 deletions rstest_macros/src/render/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1085,13 +1085,13 @@ mod cases_should {

assert_in!(
code(&tests, 0),
r#"let ctx = Context::new("test_with_context", Some("my_description"), Some(0usize));"#
r#"let ctx = Context::new(module_path!(), "test_with_context", Some("my_description"), Some(0usize));"#
.ast::<Stmt>()
.display_code()
);
assert_in!(
code(&tests, 1),
r#"let ctx = Context::new("test_with_context", Some("other_description"), Some(1usize));"#
r#"let ctx = Context::new(module_path!(), "test_with_context", Some("other_description"), Some(1usize));"#
.ast::<Stmt>()
.display_code()
);
Expand Down Expand Up @@ -1774,6 +1774,40 @@ mod matrix_cases_should {
assert_in!(functions[0], "third_1");
assert_in!(functions[1], "third_2");
}

#[test]
fn render_context() {
let item_fn: ItemFn =
r#"fn matrix_with_context(ctx: Context, first: u32, second: u32) { println!("user code") }"#.ast();
let values = vec!["1".to_string(), "2".to_string()];
let mut info = RsTestInfo {
data: RsTestData {
items: vec![
values_list("first", values.as_ref()).into(),
values_list("second", values.as_ref()).into(),
],
},
..Default::default()
};
info.arguments.add_context(pat("ctx"));

let tokens = matrix(item_fn.clone(), info);

let tests = TestsGroup::from(tokens);

fn code(tests: &TestsGroup, id: usize) -> String {
tests.module.get_all_tests()[id].block.display_code()
}

for t in 0..tests.module.get_all_tests().len() {
assert_in!(
code(&tests, t),
r#"let ctx = Context::new(module_path!(), "matrix_with_context", None, None);"#
.ast::<Stmt>()
.display_code()
);
}
}
}

mod complete_should {
Expand Down

0 comments on commit 4a9763f

Please sign in to comment.