Skip to content

Commit

Permalink
Fix matches Pattern if condition test case
Browse files Browse the repository at this point in the history
  • Loading branch information
luke-biel committed Mar 21, 2022
1 parent 6301fbd commit 75bd091
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 34 deletions.
4 changes: 4 additions & 0 deletions 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
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "test-case"
version = "2.0.0"
version = "2.0.1"
edition = "2018"
authors = ["Marcin Sas-Szymanski <marcin.sas-szymanski@anixe.pl>", "Wojciech Polak <frondeus@gmail.com>", "Łukasz Biel <lukasz.p.biel@gmail.com>"]
description = "Provides #[test_case(...)] procedural macro attribute for generating parametrized test cases easily"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion acceptance_tests/basic/src/lib.rs
Expand Up @@ -138,7 +138,7 @@ mod test_cases {
String::default()
}

#[derive(Debug)]
#[derive(Debug, PartialEq)]
#[allow(dead_code)]
enum SimpleEnum {
Var1,
Expand All @@ -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<SimpleEnum, &'static str> {
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 {
Expand Down
18 changes: 12 additions & 6 deletions src/expr.rs
Expand Up @@ -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<Expr>),
// test_case(a, b, c => panics "abcd")
Panicking(Option<Expr>),
// test_case(a, b, c => with |v: T| assert!(v.is_nan()))
Expand All @@ -46,7 +46,11 @@ impl Parse for TestCaseExpression {
let extra_keywords = parse_kws(input);

if input.parse::<kw::matches>().is_ok() {
parse_with_keyword::<_, _>(input, token, extra_keywords, TestCaseResult::Matching)
Ok(TestCaseExpression {
_token: token,
extra_keywords,
result: TestCaseResult::Matching(input.parse()?, input.parse::<Expr>().ok()),
})
} else if input.parse::<kw::it>().is_ok() || input.parse::<kw::is>().is_ok() {
parse_with_keyword::<_, _>(input, token, extra_keywords, TestCaseResult::Complex)
} else if input.parse::<kw::using>().is_ok() {
Expand Down Expand Up @@ -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 {:?}",
Expand All @@ -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)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Expand Up @@ -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
Expand Down
14 changes: 8 additions & 6 deletions 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

---
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
@@ -1,6 +1,6 @@
---
source: tests/acceptance_tests.rs
assertion_line: 94
assertion_line: 97
expression: lines

---
Expand All @@ -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
Expand Down
14 changes: 8 additions & 6 deletions 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

---
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
@@ -1,6 +1,6 @@
---
source: tests/acceptance_tests.rs
assertion_line: 94
assertion_line: 97
expression: lines

---
Expand All @@ -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
Expand Down
14 changes: 8 additions & 6 deletions 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

---
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
@@ -1,6 +1,6 @@
---
source: tests/acceptance_tests.rs
assertion_line: 94
assertion_line: 97
expression: lines

---
Expand All @@ -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
Expand Down

0 comments on commit 75bd091

Please sign in to comment.