From 75bd0914a61b55b2ef6be4f362e2ffa41428f264 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Biel?= Date: Tue, 22 Mar 2022 00:01:21 +0100 Subject: [PATCH] Fix `matches Pattern if condition` test case --- CHANGELOG.md | 4 ++++ Cargo.toml | 2 +- README.md | 2 +- acceptance_tests/basic/src/lib.rs | 12 +++++++++++- src/expr.rs | 18 ++++++++++++------ src/lib.rs | 2 +- .../acceptance__acceptance__basic.snap | 14 ++++++++------ .../acceptance__acceptance__item_reuse.snap | 4 ++-- .../acceptance__acceptance__basic.snap | 14 ++++++++------ .../acceptance__acceptance__item_reuse.snap | 4 ++-- .../acceptance__acceptance__basic.snap | 14 ++++++++------ .../acceptance__acceptance__item_reuse.snap | 4 ++-- 12 files changed, 60 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index abbe4d7..97415be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 2.0.1 +### Bug fixes +* `matches Pattern if condition` parses correctly (`if condition` part wasn't allowed) + ## 2.0.0 ### New features * `=> with |x: T| assert!(x)` custom inline test assertions diff --git a/Cargo.toml b/Cargo.toml index 4ef48eb..94686d4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "test-case" -version = "2.0.0" +version = "2.0.1" edition = "2018" authors = ["Marcin Sas-Szymanski ", "Wojciech Polak ", "Ɓukasz Biel "] description = "Provides #[test_case(...)] procedural macro attribute for generating parametrized test cases easily" diff --git a/README.md b/README.md index 5784e23..6553eab 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Crate has to be added as a dependency to `Cargo.toml`: ```toml [dev-dependencies] -test-case = "2.0.0" +test-case = "2.0.1" ``` and imported to the scope of a block where it's being called diff --git a/acceptance_tests/basic/src/lib.rs b/acceptance_tests/basic/src/lib.rs index b2d0099..97574ec 100644 --- a/acceptance_tests/basic/src/lib.rs +++ b/acceptance_tests/basic/src/lib.rs @@ -138,7 +138,7 @@ mod test_cases { String::default() } - #[derive(Debug)] + #[derive(Debug, PartialEq)] #[allow(dead_code)] enum SimpleEnum { Var1, @@ -150,6 +150,16 @@ mod test_cases { e } + #[test_case(SimpleEnum::Var1 => matches Ok(e) if e == SimpleEnum::Var1)] + #[test_case(SimpleEnum::Var2 => matches Err(e) if e == "var2")] + fn extended_pattern_matching_result(e: SimpleEnum) -> Result { + if e == SimpleEnum::Var1 { + Ok(e) + } else { + Err("var2") + } + } + #[should_panic(expected = "Expected SimpleEnum :: Var2 found Var1")] #[test_case(SimpleEnum::Var1 => matches SimpleEnum::Var2)] fn pattern_matching_result_fails(e: SimpleEnum) -> SimpleEnum { diff --git a/src/expr.rs b/src/expr.rs index ea152b1..fa861e5 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -28,8 +28,8 @@ pub struct TestCaseExpression { pub enum TestCaseResult { // test_case(a, b, c => result) Simple(Expr), - // test_case(a, b, c => matches Ok(_)) - Matching(Pat), + // test_case(a, b, c => matches Ok(_) if true) + Matching(Pat, Option), // test_case(a, b, c => panics "abcd") Panicking(Option), // test_case(a, b, c => with |v: T| assert!(v.is_nan())) @@ -46,7 +46,11 @@ impl Parse for TestCaseExpression { let extra_keywords = parse_kws(input); if input.parse::().is_ok() { - parse_with_keyword::<_, _>(input, token, extra_keywords, TestCaseResult::Matching) + Ok(TestCaseExpression { + _token: token, + extra_keywords, + result: TestCaseResult::Matching(input.parse()?, input.parse::().ok()), + }) } else if input.parse::().is_ok() || input.parse::().is_ok() { parse_with_keyword::<_, _>(input, token, extra_keywords, TestCaseResult::Complex) } else if input.parse::().is_ok() { @@ -78,7 +82,9 @@ impl Display for TestCaseResult { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match self { TestCaseResult::Simple(expr) => write!(f, "{}", fmt_syn(expr)), - TestCaseResult::Matching(expr) => write!(f, "matching {}", fmt_syn(expr)), + TestCaseResult::Matching(pat, expr) => { + write!(f, "matching {} {}", fmt_syn(pat), fmt_syn(expr)) + } TestCaseResult::Panicking(expr) => write!( f, "panicking {:?}", @@ -95,11 +101,11 @@ impl TestCaseExpression { pub fn assertion(&self) -> TokenStream2 { match &self.result { TestCaseResult::Simple(expr) => parse_quote! { assert_eq!(#expr, _result) }, - TestCaseResult::Matching(pat) => { + TestCaseResult::Matching(pat, expr) => { let pat_str = pat.to_token_stream().to_string(); parse_quote! { match _result { - #pat => (), + #pat #expr => (), e => panic!("Expected {} found {:?}", #pat_str, e) } } diff --git a/src/lib.rs b/src/lib.rs index eba928f..0663411 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,7 +7,7 @@ //! //! ```toml //! [dev-dependencies] -//! test-case = "2.0.0" +//! test-case = "2.0.1" //! ``` //! //! and imported to the scope of a block where it's being called diff --git a/tests/snapshots/rust-1.49.0/acceptance__acceptance__basic.snap b/tests/snapshots/rust-1.49.0/acceptance__acceptance__basic.snap index 1238ffe..92934fd 100644 --- a/tests/snapshots/rust-1.49.0/acceptance__acceptance__basic.snap +++ b/tests/snapshots/rust-1.49.0/acceptance__acceptance__basic.snap @@ -1,6 +1,6 @@ --- source: tests/acceptance_tests.rs -assertion_line: 58 +assertion_line: 69 expression: lines --- @@ -10,8 +10,8 @@ expression: lines ---- test_cases::panicing::_expects_panicking_some_this_should_fail_ stdout ---- failures: failures: -running 94 tests -test result: FAILED. 89 passed; 1 failed; 4 ignored; 0 measured; 0 filtered out +running 96 tests +test result: FAILED. 91 passed; 1 failed; 4 ignored; 0 measured; 0 filtered out test test_cases::abs_tests::returns_0_for_0 ... ok test test_cases::abs_tests::returns_given_number_for_positive_input ... ok test test_cases::abs_tests::returns_opposite_number_for_non_positive_input ... ok @@ -52,6 +52,8 @@ test test_cases::create_path::long_dir ... ok test test_cases::create_path::short_dir ... ok test test_cases::divide_by_zero_f64_with_lambda::_0_0_expects_with_v_f64_assert_v_is_nan_ ... ok test test_cases::divide_by_zero_f64_with_lambda::_1_0_expects_with_v_f64_assert_v_is_infinite_ ... ok +test test_cases::extended_pattern_matching_result::simpleenum_var1_expects_matching_ok_e_ ... ok +test test_cases::extended_pattern_matching_result::simpleenum_var2_expects_matching_err_e_ ... ok test test_cases::fancy_addition::some_2_3_some_4_expects_2_3_4 ... ok test test_cases::fancy_addition::some_2_some_3_expects_5 ... ok test test_cases::fancy_addition::treats_none_as_0 ... ok @@ -86,8 +88,8 @@ test test_cases::not_path::_src_parse_unwrap_expects_complex_not_path_file ... o test test_cases::panicing::_expects_panicking_some_it_has_to_panic_ ... ok test test_cases::panicing::_expects_panicking_some_this_should_fail_ ... FAILED test test_cases::panics_without_value::_expects_panicking_none ... ok -test test_cases::pattern_matching_result::simpleenum_var2_expects_matching_simpleenum_var2 ... ok -test test_cases::pattern_matching_result_fails::simpleenum_var1_expects_matching_simpleenum_var2 ... ok +test test_cases::pattern_matching_result::simpleenum_var2_expects_matching_simpleenum_var2_ ... ok +test test_cases::pattern_matching_result_fails::simpleenum_var1_expects_matching_simpleenum_var2_ ... ok test test_cases::power_of_two_with_using::_1_expects_use_assert_is_power_of_two ... ok test test_cases::power_of_two_with_using::_2_expects_use_crate_test_cases_assert_is_power_of_two ... ok test test_cases::power_of_two_with_using::_4_expects_use_some_mod_assert_is_power_of_two ... ok @@ -106,4 +108,4 @@ test test_cases::test_generics::source1_expects_1 ... ok test test_cases::test_generics::source2_expects_2 ... ok test test_cases::test_impl::source1_expects_1 ... ok test test_cases::test_impl::source2_expects_2 ... ok -thread 'test_cases::panicing::_expects_panicking_some_this_should_fail_' panicked at 'It has to panic', src/lib.rs:162:9 +thread 'test_cases::panicing::_expects_panicking_some_this_should_fail_' panicked at 'It has to panic', src/lib.rs:172:9 diff --git a/tests/snapshots/rust-1.49.0/acceptance__acceptance__item_reuse.snap b/tests/snapshots/rust-1.49.0/acceptance__acceptance__item_reuse.snap index 851df25..33f88db 100644 --- a/tests/snapshots/rust-1.49.0/acceptance__acceptance__item_reuse.snap +++ b/tests/snapshots/rust-1.49.0/acceptance__acceptance__item_reuse.snap @@ -1,6 +1,6 @@ --- source: tests/acceptance_tests.rs -assertion_line: 94 +assertion_line: 97 expression: lines --- @@ -14,7 +14,7 @@ running 6 tests test internal_tested_function1::_2_expects_4 ... ok test internal_tested_function1::_3_expects_6 ... FAILED test internal_tested_function2::_1_expects_0 ... ok -test internal_tested_function3::_1_expects_matching_3 ... ok +test internal_tested_function3::_1_expects_matching_3_ ... ok test internal_tested_function3::_2_expects_inconclusive6 ... ignored test internal_tested_function4::_2_expects_panicking_some_can_t_ ... ok test result: FAILED. 4 passed; 1 failed; 1 ignored; 0 measured; 0 filtered out diff --git a/tests/snapshots/rust-nightly/acceptance__acceptance__basic.snap b/tests/snapshots/rust-nightly/acceptance__acceptance__basic.snap index cca9f34..f11ec83 100644 --- a/tests/snapshots/rust-nightly/acceptance__acceptance__basic.snap +++ b/tests/snapshots/rust-nightly/acceptance__acceptance__basic.snap @@ -1,6 +1,6 @@ --- source: tests/acceptance_tests.rs -assertion_line: 58 +assertion_line: 69 expression: lines --- @@ -10,8 +10,8 @@ expression: lines ---- test_cases::panicing::_expects_panicking_some_this_should_fail_ stdout ---- failures: failures: -running 94 tests -test result: FAILED. 89 passed; 1 failed; 4 ignored; 0 measured; 0 filtered out +running 96 tests +test result: FAILED. 91 passed; 1 failed; 4 ignored; 0 measured; 0 filtered out test test_cases::abs_tests::returns_0_for_0 ... ok test test_cases::abs_tests::returns_given_number_for_positive_input ... ok test test_cases::abs_tests::returns_opposite_number_for_non_positive_input ... ok @@ -52,6 +52,8 @@ test test_cases::create_path::long_dir ... ok test test_cases::create_path::short_dir ... ok test test_cases::divide_by_zero_f64_with_lambda::_0_0_expects_with_v_f64_assert_v_is_nan_ ... ok test test_cases::divide_by_zero_f64_with_lambda::_1_0_expects_with_v_f64_assert_v_is_infinite_ ... ok +test test_cases::extended_pattern_matching_result::simpleenum_var1_expects_matching_ok_e_ ... ok +test test_cases::extended_pattern_matching_result::simpleenum_var2_expects_matching_err_e_ ... ok test test_cases::fancy_addition::some_2_3_some_4_expects_2_3_4 ... ok test test_cases::fancy_addition::some_2_some_3_expects_5 ... ok test test_cases::fancy_addition::treats_none_as_0 ... ok @@ -86,8 +88,8 @@ test test_cases::not_path::_src_parse_unwrap_expects_complex_not_path_file ... o test test_cases::panicing::_expects_panicking_some_it_has_to_panic_ - should panic ... ok test test_cases::panicing::_expects_panicking_some_this_should_fail_ - should panic ... FAILED test test_cases::panics_without_value::_expects_panicking_none - should panic ... ok -test test_cases::pattern_matching_result::simpleenum_var2_expects_matching_simpleenum_var2 ... ok -test test_cases::pattern_matching_result_fails::simpleenum_var1_expects_matching_simpleenum_var2 - should panic ... ok +test test_cases::pattern_matching_result::simpleenum_var2_expects_matching_simpleenum_var2_ ... ok +test test_cases::pattern_matching_result_fails::simpleenum_var1_expects_matching_simpleenum_var2_ - should panic ... ok test test_cases::power_of_two_with_using::_1_expects_use_assert_is_power_of_two ... ok test test_cases::power_of_two_with_using::_2_expects_use_crate_test_cases_assert_is_power_of_two ... ok test test_cases::power_of_two_with_using::_4_expects_use_some_mod_assert_is_power_of_two ... ok @@ -106,4 +108,4 @@ test test_cases::test_generics::source1_expects_1 ... ok test test_cases::test_generics::source2_expects_2 ... ok test test_cases::test_impl::source1_expects_1 ... ok test test_cases::test_impl::source2_expects_2 ... ok -thread 'test_cases::panicing::_expects_panicking_some_this_should_fail_' panicked at 'It has to panic', src/lib.rs:162:9 +thread 'test_cases::panicing::_expects_panicking_some_this_should_fail_' panicked at 'It has to panic', src/lib.rs:172:9 diff --git a/tests/snapshots/rust-nightly/acceptance__acceptance__item_reuse.snap b/tests/snapshots/rust-nightly/acceptance__acceptance__item_reuse.snap index 89be860..28c0fba 100644 --- a/tests/snapshots/rust-nightly/acceptance__acceptance__item_reuse.snap +++ b/tests/snapshots/rust-nightly/acceptance__acceptance__item_reuse.snap @@ -1,6 +1,6 @@ --- source: tests/acceptance_tests.rs -assertion_line: 94 +assertion_line: 97 expression: lines --- @@ -14,7 +14,7 @@ running 6 tests test internal_tested_function1::_2_expects_4 ... ok test internal_tested_function1::_3_expects_6 ... FAILED test internal_tested_function2::_1_expects_0 ... ok -test internal_tested_function3::_1_expects_matching_3 ... ok +test internal_tested_function3::_1_expects_matching_3_ ... ok test internal_tested_function3::_2_expects_inconclusive6 ... ignored test internal_tested_function4::_2_expects_panicking_some_can_t_ - should panic ... ok test result: FAILED. 4 passed; 1 failed; 1 ignored; 0 measured; 0 filtered out diff --git a/tests/snapshots/rust-stable/acceptance__acceptance__basic.snap b/tests/snapshots/rust-stable/acceptance__acceptance__basic.snap index cca9f34..f11ec83 100644 --- a/tests/snapshots/rust-stable/acceptance__acceptance__basic.snap +++ b/tests/snapshots/rust-stable/acceptance__acceptance__basic.snap @@ -1,6 +1,6 @@ --- source: tests/acceptance_tests.rs -assertion_line: 58 +assertion_line: 69 expression: lines --- @@ -10,8 +10,8 @@ expression: lines ---- test_cases::panicing::_expects_panicking_some_this_should_fail_ stdout ---- failures: failures: -running 94 tests -test result: FAILED. 89 passed; 1 failed; 4 ignored; 0 measured; 0 filtered out +running 96 tests +test result: FAILED. 91 passed; 1 failed; 4 ignored; 0 measured; 0 filtered out test test_cases::abs_tests::returns_0_for_0 ... ok test test_cases::abs_tests::returns_given_number_for_positive_input ... ok test test_cases::abs_tests::returns_opposite_number_for_non_positive_input ... ok @@ -52,6 +52,8 @@ test test_cases::create_path::long_dir ... ok test test_cases::create_path::short_dir ... ok test test_cases::divide_by_zero_f64_with_lambda::_0_0_expects_with_v_f64_assert_v_is_nan_ ... ok test test_cases::divide_by_zero_f64_with_lambda::_1_0_expects_with_v_f64_assert_v_is_infinite_ ... ok +test test_cases::extended_pattern_matching_result::simpleenum_var1_expects_matching_ok_e_ ... ok +test test_cases::extended_pattern_matching_result::simpleenum_var2_expects_matching_err_e_ ... ok test test_cases::fancy_addition::some_2_3_some_4_expects_2_3_4 ... ok test test_cases::fancy_addition::some_2_some_3_expects_5 ... ok test test_cases::fancy_addition::treats_none_as_0 ... ok @@ -86,8 +88,8 @@ test test_cases::not_path::_src_parse_unwrap_expects_complex_not_path_file ... o test test_cases::panicing::_expects_panicking_some_it_has_to_panic_ - should panic ... ok test test_cases::panicing::_expects_panicking_some_this_should_fail_ - should panic ... FAILED test test_cases::panics_without_value::_expects_panicking_none - should panic ... ok -test test_cases::pattern_matching_result::simpleenum_var2_expects_matching_simpleenum_var2 ... ok -test test_cases::pattern_matching_result_fails::simpleenum_var1_expects_matching_simpleenum_var2 - should panic ... ok +test test_cases::pattern_matching_result::simpleenum_var2_expects_matching_simpleenum_var2_ ... ok +test test_cases::pattern_matching_result_fails::simpleenum_var1_expects_matching_simpleenum_var2_ - should panic ... ok test test_cases::power_of_two_with_using::_1_expects_use_assert_is_power_of_two ... ok test test_cases::power_of_two_with_using::_2_expects_use_crate_test_cases_assert_is_power_of_two ... ok test test_cases::power_of_two_with_using::_4_expects_use_some_mod_assert_is_power_of_two ... ok @@ -106,4 +108,4 @@ test test_cases::test_generics::source1_expects_1 ... ok test test_cases::test_generics::source2_expects_2 ... ok test test_cases::test_impl::source1_expects_1 ... ok test test_cases::test_impl::source2_expects_2 ... ok -thread 'test_cases::panicing::_expects_panicking_some_this_should_fail_' panicked at 'It has to panic', src/lib.rs:162:9 +thread 'test_cases::panicing::_expects_panicking_some_this_should_fail_' panicked at 'It has to panic', src/lib.rs:172:9 diff --git a/tests/snapshots/rust-stable/acceptance__acceptance__item_reuse.snap b/tests/snapshots/rust-stable/acceptance__acceptance__item_reuse.snap index 89be860..28c0fba 100644 --- a/tests/snapshots/rust-stable/acceptance__acceptance__item_reuse.snap +++ b/tests/snapshots/rust-stable/acceptance__acceptance__item_reuse.snap @@ -1,6 +1,6 @@ --- source: tests/acceptance_tests.rs -assertion_line: 94 +assertion_line: 97 expression: lines --- @@ -14,7 +14,7 @@ running 6 tests test internal_tested_function1::_2_expects_4 ... ok test internal_tested_function1::_3_expects_6 ... FAILED test internal_tested_function2::_1_expects_0 ... ok -test internal_tested_function3::_1_expects_matching_3 ... ok +test internal_tested_function3::_1_expects_matching_3_ ... ok test internal_tested_function3::_2_expects_inconclusive6 ... ignored test internal_tested_function4::_2_expects_panicking_some_can_t_ - should panic ... ok test result: FAILED. 4 passed; 1 failed; 1 ignored; 0 measured; 0 filtered out