Skip to content

Commit

Permalink
Close #14: Use LitStr's parse do the job. Removed unused respan funct…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
Michele d'Amico committed Feb 19, 2019
1 parent 795af81 commit 5c30a96
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 24 deletions.
7 changes: 7 additions & 0 deletions resources/parametrize/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
17 changes: 3 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,6 @@ fn respan<T: Into<proc_macro2::TokenTree>>(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<CaseArg, Error> {
match a {
NestedMeta::Literal(l) =>
Expand All @@ -104,7 +96,7 @@ fn parse_case_arg(a: &NestedMeta) -> Result<CaseArg, Error> {
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) => {
Expand All @@ -113,11 +105,8 @@ fn parse_case_arg(a: &NestedMeta) -> Result<CaseArg, Error> {
nested_case => panic!("Unexpected case attribute: {:?}", nested_case)
}
}
}.map(|t|
{
let e = respan_stream(t.into_token_stream(), a.span());
CaseArg::from(syn::parse2::<Expr>(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<T>: Sized
Expand Down
65 changes: 55 additions & 10 deletions tests/parametrize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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
|
Expand All @@ -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
|
Expand All @@ -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]
Expand Down

1 comment on commit 5c30a96

@la10736
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commit on a wrong ticket ... it was #16 instad

Please sign in to comment.