diff --git a/resources/parametrize/errors.rs b/resources/parametrize/errors.rs index 4ec9d2af..4d43bb70 100644 --- a/resources/parametrize/errors.rs +++ b/resources/parametrize/errors.rs @@ -15,3 +15,10 @@ fn error_fixture_wrong_type(fixture: String, f: u32) {} #[rstest_parametrize(f, case(42))] fn error_param_wrong_type(f: &str) {} + +#[rstest_parametrize(condition, + case(r(r#"vec![1,2,3].contains(2)"#))) +] +fn example(condition: bool) { + assert!(condition) +} diff --git a/src/lib.rs b/src/lib.rs index 74b95850..279e027b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -87,14 +87,6 @@ fn respan>(t: T, span: Span) -> proc_macro2::Tok t } -fn respan_stream(t: proc_macro2::TokenStream, span: Span) -> proc_macro2::TokenStream { - t.into_iter().map(|tt| respan(tt, span)).collect() -} - -fn is_arbitrary_rust_code(meta: &MetaList) -> bool { - ["Unwrap", "r"].iter().any(|&n| meta.ident == n) -} - fn parse_case_arg(a: &NestedMeta) -> Result { match a { NestedMeta::Literal(l) => @@ -104,7 +96,7 @@ fn parse_case_arg(a: &NestedMeta) -> Result { Meta::List(arg) if is_arbitrary_rust_code(arg) => match arg.nested.first().unwrap().value() { NestedMeta::Literal(Lit::Str(inner_unwrap)) => - parse_expression(inner_unwrap.value()), + inner_unwrap.parse().or(Err(format!("Cannot parse '{}'", inner_unwrap.value()))), _ => panic!("Unexpected case argument: {:?}", opt), }, Meta::Word(term) => { @@ -113,11 +105,8 @@ fn parse_case_arg(a: &NestedMeta) -> Result { nested_case => panic!("Unexpected case attribute: {:?}", nested_case) } } - }.map(|t| - { - let e = respan_stream(t.into_token_stream(), a.span()); - CaseArg::from(syn::parse2::(e).unwrap()).respan(a.span()) - }).map_err(|m| Error::new(a.span(), m)) + }.map(|t| CaseArg::from(t).respan(a.span())) + .map_err(|m| Error::new(a.span(), m)) } trait TryFrom: Sized diff --git a/tests/parametrize.rs b/tests/parametrize.rs index 1a58f16c..cf0071a7 100644 --- a/tests/parametrize.rs +++ b/tests/parametrize.rs @@ -162,9 +162,6 @@ mod not_compile_if_missed_arguments { mod not_compile_if_a_case_has_a_wrong_signature { use super::*; - // TODO: - // - [ ] Test case for less case args - #[test] fn with_too_much_arguments() { let (output, _) = run_test("case_with_too_much_args.rs"); @@ -293,17 +290,35 @@ mod dump_input_values { } } -#[test] -fn should_show_correct_errors() { - let (output, name) = run_test("errors.rs"); +mod should_show_correct_errors { + use super::*; + use lazy_static::lazy_static; + use std::process::Output; + + fn execute() -> &'static (Output, String) { + lazy_static! { + static ref OUTPUT: (Output, String) = + run_test("errors.rs"); + } + &OUTPUT + } - assert_in!(output.stderr.str(), format!(" + #[test] + fn if_no_fixture() { + let (output, name) = execute(); + + assert_in!(output.stderr.str(), format!(" error[E0425]: cannot find function `no_fixture` in this scope --> {}/src/lib.rs:10:1 | 10 | #[rstest_parametrize(f, case(42))]", name).deindent()); + } + + #[test] + fn if_wrong_type() { + let (output, name) = execute(); - assert_in!(output.stderr.str(), format!(r#" + assert_in!(output.stderr.str(), format!(r#" error[E0308]: mismatched types --> {}/src/lib.rs:7:18 | @@ -313,8 +328,13 @@ fn should_show_correct_errors() { = note: expected type `u32` found type `&'static str` "#, name).deindent()); + } - assert_in!(output.stderr.str(), format!(" + #[test] + fn if_wrong_type_fixture() { + let (output, name) = execute(); + + assert_in!(output.stderr.str(), format!(" error[E0308]: mismatched types --> {}/src/lib.rs:14:29 | @@ -327,12 +347,37 @@ fn should_show_correct_errors() { = note: expected type `std::string::String` found type `u32` ", name).deindent()); + } - assert_in!(output.stderr.str(), format!(" + #[test] + fn if_wrong_type_param() { + let (output, name) = execute(); + + assert_in!(output.stderr.str(), format!(" error[E0308]: mismatched types --> {}/src/lib.rs:17:27 | 17 | fn error_param_wrong_type(f: &str) {{}}", name).deindent()); + } + + #[test] + fn if_arbitrary_rust_code_has_some_errors() { + let (output, name) = execute(); + + assert_in!(output.stderr.str(), format!(r##" + error[E0308]: mismatched types + --> {}/src/lib.rs:20:12 + | + 20 | case(r(r#"vec![1,2,3].contains(2)"#))) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | expected &{{integer}}, found integer + | help: consider borrowing here: `&r#"vec![1,2,3].contains(2)"#` + | + = note: expected type `&{{integer}}` + found type `{{integer}}`"##, + name).deindent()); + } } #[test]