Skip to content

Commit

Permalink
Migrate internal tests to the new syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
la10736 committed Mar 21, 2021
1 parent 91906aa commit e58dd6c
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 134 deletions.
15 changes: 6 additions & 9 deletions src/parse/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,15 +348,12 @@ mod should {
assert_eq!(expected, data);
}

#[rstest(
input,
expected,
case("first(42),", 1),
case("first(42), second=42,", 2),
case(r#"fixture(42, "other"), :: trace"#, 1),
case(r#"second=42, fixture(42, "other"), :: trace"#, 2)
)]
fn should_accept_trailing_comma(input: &str, expected: usize) {
#[rstest]
#[case("first(42),", 1)]
#[case("first(42), second=42,", 2)]
#[case(r#"fixture(42, "other"), :: trace"#, 1)]
#[case(r#"second=42, fixture(42, "other"), :: trace"#, 2)]
fn should_accept_trailing_comma(#[case] input: &str, #[case] expected: usize) {
let info: FixtureInfo = input.ast();

assert_eq!(
Expand Down
22 changes: 14 additions & 8 deletions src/render/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,18 @@ mod should {
}

#[template]
#[rstest(is_async,
case::async_fn(true),
case::not_async_fn(false),
method => ["default", "get", "partial_1", "partial_2", "partial_3"]
)]
fn async_fixture_cases(is_async: bool, method: &str) {}
#[rstest(
method => ["default", "get", "partial_1", "partial_2", "partial_3"])
]
#[case::async_fn(true)]
#[case::not_async_fn(false)]
fn async_fixture_cases(#[case] is_async: bool, method: &str) {}

#[apply(async_fixture_cases)]
fn fixture_method_should_be_async_if_fixture_function_is_async(is_async: bool, method: &str) {
fn fixture_method_should_be_async_if_fixture_function_is_async(
#[case] is_async: bool,
method: &str,
) {
let prefix = if is_async { "async" } else { "" };
let (_, out) = parse_fixture(&format!(
r#"
Expand All @@ -206,7 +209,10 @@ mod should {
}

#[apply(async_fixture_cases)]
fn fixture_method_should_use_await_if_fixture_function_is_async(is_async: bool, method: &str) {
fn fixture_method_should_use_await_if_fixture_function_is_async(
#[case] is_async: bool,
method: &str,
) {
let prefix = if is_async { "async" } else { "" };
let (_, out) = parse_fixture(&format!(
r#"
Expand Down
206 changes: 107 additions & 99 deletions src/render/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,17 @@ mod single_test_should {
assert_eq!(inner_fn, input_fn);
}

#[rstest(
attributes => [
"#[test]",
#[rstest]
fn not_copy_any_attributes(
#[values(
"#[test]",
"#[very::complicated::path]",
"#[test]#[should_panic]",
"#[should_panic]#[test]",
"#[a]#[b]#[c]",
]
)]
fn not_copy_any_attributes(attributes: &str) {
"#[test]#[should_panic]",
"#[should_panic]#[test]",
"#[a]#[b]#[c]"
)]
attributes: &str,
) {
let attributes = attrs(attributes);
let mut input_fn: ItemFn = r#"pub fn test(_s: String){}"#.ast();
input_fn.attrs = attributes;
Expand All @@ -172,19 +173,21 @@ mod single_test_should {
assert!(inner_fn.attrs.is_empty());
}

#[rstest(is_async,
case::sync(false),
case::async_fn(true),
attributes => [
"#[test]",
"#[other::test]",
#[rstest]
#[case::sync(false)]
#[case::async_fn(true)]
fn use_injected_test_attribute_to_mark_test_functions_if_any(
#[case] is_async: bool,
#[values(
"#[test]",
"#[other::test]",
"#[very::complicated::path::test]",
"#[prev]#[test]",
"#[test]#[after]",
"#[prev]#[other::test]",
]
)]
fn use_injected_test_attribute_to_mark_test_functions_if_any(is_async: bool, attributes: &str) {
"#[prev]#[test]",
"#[test]#[after]",
"#[prev]#[other::test]"
)]
attributes: &str,
) {
let attributes = attrs(attributes);
let mut input_fn: ItemFn = r#"fn test(_s: String) {} "#.ast();
input_fn.set_async(is_async);
Expand Down Expand Up @@ -247,18 +250,21 @@ mod single_test_should {
);
}

#[rstest(prefix, test_attribute,
case::sync("", parse_quote! { #[test] }),
case::async_fn("async", parse_quote! { #[async_std::test] }),
attributes => [
"",
#[rstest]
#[case::sync("", parse_quote! { #[test] })]
#[case::async_fn("async", parse_quote! { #[async_std::test] })]
fn add_default_test_attribute(
#[case] prefix: &str,
#[case] test_attribute: Attribute,
#[values(
"",
"#[no_one]",
"#[should_panic]",
"#[should_panic]#[other]",
"#[a::b::c]#[should_panic]",
]
)]
fn add_default_test_attribute(prefix: &str, test_attribute: Attribute, attributes: &str) {
"#[a::b::c]#[should_panic]"
)]
attributes: &str,
) {
let attributes = attrs(attributes);
let mut input_fn: ItemFn = format!(r#"{} fn test(_s: String) {{}} "#, prefix).ast();
input_fn.attrs = attributes.clone();
Expand All @@ -269,13 +275,10 @@ mod single_test_should {
assert_eq!(&result.attrs[1..], attributes.as_slice());
}

#[rstest(
is_async,
use_await,
case::sync(false, false),
case::async_fn(true, true)
)]
fn use_await_for_no_async_test_function(is_async: bool, use_await: bool) {
#[rstest]
#[case::sync(false, false)]
#[case::async_fn(true, true)]
fn use_await_for_no_async_test_function(#[case] is_async: bool, #[case] use_await: bool) {
let mut input_fn: ItemFn = r#"fn test(_s: String) {} "#.ast();
input_fn.set_async(is_async);

Expand Down Expand Up @@ -597,12 +600,13 @@ mod cases_should {
}
}

#[rstest(fnattrs,
case::empty(""),
case::some_attrs("#[a]#[b::c]#[should_panic]"),
case_attrs => ["", "#[should_panic]", "#[first]#[second(arg)]"]
)]
fn should_add_attributes_given_in_the_test_case(fnattrs: &str, case_attrs: &str) {
#[rstest]
#[case::empty("")]
#[case::some_attrs("#[a]#[b::c]#[should_panic]")]
fn should_add_attributes_given_in_the_test_case(
#[case] fnattrs: &str,
#[values("", "#[should_panic]", "#[first]#[second(arg)]")] case_attrs: &str,
) {
let given_attrs = attrs(fnattrs);
let case_attrs = attrs(case_attrs);
let (mut item_fn, info) = TestCaseBuilder::from(r#"fn test(v: i32){}"#)
Expand Down Expand Up @@ -773,19 +777,21 @@ mod cases_should {
.ends_with(&format!("_{}", description)));
}

#[rstest(is_async,
case::sync(false),
case::async_fn(true),
attributes => [
"#[test]",
"#[other::test]",
#[rstest]
#[case::sync(false)]
#[case::async_fn(true)]
fn use_injected_test_attribute_to_mark_test_functions_if_any(
#[case] is_async: bool,
#[values(
"#[test]",
"#[other::test]",
"#[very::complicated::path::test]",
"#[prev]#[test]",
"#[test]#[after]",
"#[prev]#[other::test]",
]
)]
fn use_injected_test_attribute_to_mark_test_functions_if_any(is_async: bool, attributes: &str) {
"#[prev]#[test]",
"#[test]#[after]",
"#[prev]#[other::test]"
)]
attributes: &str,
) {
let attributes = attrs(attributes);
let (mut item_fn, info) = TestCaseBuilder::from(r#"fn test(s: String){}"#)
.push_case(r#"String::from("3")"#)
Expand All @@ -801,18 +807,21 @@ mod cases_should {
assert_eq!(attributes, test.attrs);
}

#[rstest(is_async, test_attribute,
case::sync(false, parse_quote! { #[test] }),
case::async_fn(true, parse_quote! { #[async_std::test] }),
attributes => [
"",
#[rstest]
#[case::sync(false, parse_quote! { #[test] })]
#[case::async_fn(true, parse_quote! { #[async_std::test] })]
fn add_default_test_attribute(
#[case] is_async: bool,
#[case] test_attribute: Attribute,
#[values(
"",
"#[no_one]",
"#[should_panic]",
"#[should_panic]#[other]",
"#[a::b::c]#[should_panic]",
]
)]
fn add_default_test_attribute(is_async: bool, test_attribute: Attribute, attributes: &str) {
"#[a::b::c]#[should_panic]"
)]
attributes: &str,
) {
let attributes = attrs(attributes);
let (mut item_fn, info) = TestCaseBuilder::from(
r#"fn should_be_the_module_name(mut fix: String) { println!("user code") }"#,
Expand All @@ -830,13 +839,10 @@ mod cases_should {
assert_eq!(&tests[0].attrs[1..], attributes.as_slice());
}

#[rstest(
is_async,
use_await,
case::sync(false, false),
case::async_fn(true, true)
)]
fn use_await_for_async_test_function(is_async: bool, use_await: bool) {
#[rstest]
#[case::sync(false, false)]
#[case::async_fn(true, true)]
fn use_await_for_async_test_function(#[case] is_async: bool, #[case] use_await: bool) {
let (item_fn, info) =
TestCaseBuilder::from(r#"fn test(mut fix: String) { println!("user code") }"#)
.set_async(is_async)
Expand Down Expand Up @@ -1123,19 +1129,21 @@ mod matrix_cases_should {
assert!(&tests[0].sig.ident.to_string().starts_with("fix_"))
}

#[rstest(is_async,
case::sync(false),
case::async_fn(true),
attributes => [
"#[test]",
"#[other::test]",
#[rstest]
#[case::sync(false)]
#[case::async_fn(true)]
fn use_injected_test_attribute_to_mark_test_functions_if_any(
#[case] is_async: bool,
#[values(
"#[test]",
"#[other::test]",
"#[very::complicated::path::test]",
"#[prev]#[test]",
"#[test]#[after]",
"#[prev]#[other::test]",
]
)]
fn use_injected_test_attribute_to_mark_test_functions_if_any(is_async: bool, attributes: &str) {
"#[prev]#[test]",
"#[test]#[after]",
"#[prev]#[other::test]"
)]
attributes: &str,
) {
let attributes = attrs(attributes);
let data = RsTestData {
items: vec![values_list("v", &["1", "2", "3"]).into()].into(),
Expand All @@ -1156,18 +1164,21 @@ mod matrix_cases_should {
}
}

#[rstest(is_async, test_attribute,
case::sync(false, parse_quote! { #[test] }),
case::async_fn(true, parse_quote! { #[async_std::test] }),
attributes => [
"",
#[rstest]
#[case::sync(false, parse_quote! { #[test] })]
#[case::async_fn(true, parse_quote! { #[async_std::test] })]
fn add_default_test_attribute(
#[case] is_async: bool,
#[case] test_attribute: Attribute,
#[values(
"",
"#[no_one]",
"#[should_panic]",
"#[should_panic]#[other]",
"#[a::b::c]#[should_panic]",
]
)]
fn add_default_test_attribute(is_async: bool, test_attribute: Attribute, attributes: &str) {
"#[a::b::c]#[should_panic]"
)]
attributes: &str,
) {
let attributes = attrs(attributes);
let data = RsTestData {
items: vec![values_list("v", &["1", "2", "3"]).into()].into(),
Expand All @@ -1190,13 +1201,10 @@ mod matrix_cases_should {
}
}

#[rstest(
is_async,
use_await,
case::sync(false, false),
case::async_fn(true, true)
)]
fn use_await_for_async_test_function(is_async: bool, use_await: bool) {
#[rstest]
#[case::sync(false, false)]
#[case::async_fn(true, true)]
fn use_await_for_async_test_function(#[case] is_async: bool, #[case] use_await: bool) {
let data = RsTestData {
items: vec![values_list("v", &["1", "2", "3"]).into()].into(),
};
Expand Down

0 comments on commit e58dd6c

Please sign in to comment.