From 38af9d6ff0c0210b845bd8b2663cc8badc65bb00 Mon Sep 17 00:00:00 2001 From: Armin Becher Date: Wed, 28 Aug 2019 18:51:38 +0200 Subject: [PATCH 1/4] Update version patch --- ntest/Cargo.toml | 6 +++--- ntest_test_cases/Cargo.toml | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ntest/Cargo.toml b/ntest/Cargo.toml index c35e5b4..0c86ca5 100644 --- a/ntest/Cargo.toml +++ b/ntest/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ntest" -version = "0.1.6" +version = "0.1.7" authors = [ "Armin Becher ",] edition = "2018" description = "Testing framework for rust which enhances the built-in library with some useful features." @@ -25,9 +25,9 @@ status = "actively-developed" panic = "unwind" [dependencies.ntest_test_cases] -version = "0.1.6" +version = "0.1.7" path = "../ntest_test_cases" [dev-dependencies.ntest_test_cases] -version = "0.1.6" +version = "0.1.7" path = "../ntest_test_cases" diff --git a/ntest_test_cases/Cargo.toml b/ntest_test_cases/Cargo.toml index bc9d9fb..563b19d 100644 --- a/ntest_test_cases/Cargo.toml +++ b/ntest_test_cases/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ntest_test_cases" -version = "0.1.6" +version = "0.1.7" authors = [ "Armin Becher ",] edition = "2018" description = "Test cases for ntest framwork." -- GitLab From a79de2f2fcfcb076540c5e1da04d245c8a1907fe Mon Sep 17 00:00:00 2001 From: Armin Becher Date: Wed, 28 Aug 2019 23:21:49 +0200 Subject: [PATCH 2/4] WIP version update --- ntest_test_cases/Cargo.toml | 6 ++--- ntest_test_cases/src/lib.rs | 45 ++++++++++++++++++++----------------- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/ntest_test_cases/Cargo.toml b/ntest_test_cases/Cargo.toml index 563b19d..9822908 100644 --- a/ntest_test_cases/Cargo.toml +++ b/ntest_test_cases/Cargo.toml @@ -16,9 +16,9 @@ name = "ntest_test_cases" proc-macro = true [dependencies] -quote = "0.6" -proc-macro2 = "0.4" +quote = "1.0" +proc-macro2 = "1.0" [dependencies.syn] -version = "0.15.44" +version = "1.0" features = [ "full",] diff --git a/ntest_test_cases/src/lib.rs b/ntest_test_cases/src/lib.rs index 0675bba..2679fc8 100644 --- a/ntest_test_cases/src/lib.rs +++ b/ntest_test_cases/src/lib.rs @@ -38,25 +38,28 @@ use syn::export::TokenStream2; #[proc_macro_attribute] pub fn test_case(attr: TokenStream, item: TokenStream) -> TokenStream { let mut test_case_descriptions: Vec = vec![]; - let attr = parse_macro_input!(attr as syn::AttributeArgs); + let attributes_first_test_case = parse_macro_input!(attr as syn::AttributeArgs); let input = parse_macro_input!(item as syn::ItemFn); // Collect test case descriptions - test_case_descriptions.push(parse_test_case_attributes(&attr)); - for at in &input.attrs { - let meta = at.parse_meta(); + test_case_descriptions.push(parse_test_case_attributes(&attributes_first_test_case)); + for attribute in &input.attrs { + let meta = attribute.parse_meta(); match meta { Ok(m) => { match m { - syn::Meta::List(ml) => { - if ml.ident != "test_case" { - panic!("Only test_case attributes expected, but found {:?}", ml.ident); + syn::Meta::Path(p) => { + let identifier = p.get_ident().expect("Expected identifier"); + if identifier != "test_case" { + panic!("Only test_case attributes expected, but found {:?}", identifier); } - let argument_args: syn::AttributeArgs = ml.nested.into_iter().collect(); - test_case_descriptions.push(parse_test_case_attributes(&argument_args)); + //let attribute_args = syn::parse2(tokens as syn::AttributeArgs).expect("Could not parse arguments"); + //let attribute_args : syn::AttributeArgs = attribute.parse_args_with(NestedMeta::Parse).unwrap(); + let attribute_args = (attribute as syn::AttributeArgs).parse_args(); + test_case_descriptions.push(parse_test_case_attributes(&attribute_args)); } - syn::Meta::Word(i) => { - panic!("Wrong input {:?} for test cases", i) + syn::Meta::List(_) => { + unimplemented!("Need to check for other types"); } syn::Meta::NameValue(_) => { unimplemented!("Need to check for named values"); @@ -67,28 +70,28 @@ pub fn test_case(attr: TokenStream, item: TokenStream) -> TokenStream { } } - let fn_args = &input.decl.inputs; + let fn_args = &input.sig.inputs; let fn_body = &input.block; let mut fn_args_idents: Vec = vec![]; for i in fn_args { match i { - syn::FnArg::Captured(c) => { - match &c.pat { - syn::Pat::Ident(ident) => { - fn_args_idents.push(ident.ident.clone()); + syn::FnArg::Typed(t) => { + match *t.pat { + syn::Pat::Ident(i) => { + fn_args_idents.push(i.ident.clone()); } _ => panic!("Unexpected function identifier.") } } - _ => panic!("Unexpected function identifier.") + syn::FnArg::Receiver(t) => panic!("Receiver function not expected for test case attribute.") } } let mut result = TokenStream2::new(); for test_case_description in test_case_descriptions { let test_case_name = syn::Ident::new( - &format!("{}{}", &input.ident.to_string(), &test_case_description.name), + &format!("{}{}", &input.sig.ident, &test_case_description.name), Span::call_site(), ); let literals = test_case_description.literals; @@ -128,9 +131,9 @@ fn parse_test_case_attributes(attr: &syn::AttributeArgs) -> TestCaseDescription syn::NestedMeta::Meta(_) => { unimplemented!("Need to check for named values"); } - syn::NestedMeta::Literal(lit) => { + syn::NestedMeta::Lit(lit) => { literals.push((*lit).clone()); - name.push_str(&format!("_{}", lit_to_str(lit))); + name.push_str(&format!("_{:?}", lit_to_str(lit))); } } } @@ -145,7 +148,7 @@ fn lit_to_str(lit: &syn::Lit) -> String { match lit { syn::Lit::Bool(s) => s.value.to_string(), syn::Lit::Str(s) => s.value().to_string(), - syn::Lit::Int(s) => s.value().to_string(), + syn::Lit::Int(s) => s.base10_parse().expect("Expected valid int"), _ => unimplemented!(), } } \ No newline at end of file -- GitLab From a43211766ddf23298b4ef5433397c9c15e9bd077 Mon Sep 17 00:00:00 2001 From: Armin Becher Date: Thu, 29 Aug 2019 12:08:36 +0200 Subject: [PATCH 3/4] Fix compile errors --- ntest_test_cases/src/lib.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/ntest_test_cases/src/lib.rs b/ntest_test_cases/src/lib.rs index 2679fc8..5403796 100644 --- a/ntest_test_cases/src/lib.rs +++ b/ntest_test_cases/src/lib.rs @@ -53,13 +53,10 @@ pub fn test_case(attr: TokenStream, item: TokenStream) -> TokenStream { if identifier != "test_case" { panic!("Only test_case attributes expected, but found {:?}", identifier); } - //let attribute_args = syn::parse2(tokens as syn::AttributeArgs).expect("Could not parse arguments"); - //let attribute_args : syn::AttributeArgs = attribute.parse_args_with(NestedMeta::Parse).unwrap(); - let attribute_args = (attribute as syn::AttributeArgs).parse_args(); - test_case_descriptions.push(parse_test_case_attributes(&attribute_args)); } - syn::Meta::List(_) => { - unimplemented!("Need to check for other types"); + syn::Meta::List(ml) => { + let argument_args: syn::AttributeArgs = ml.nested.into_iter().collect(); + test_case_descriptions.push(parse_test_case_attributes(&argument_args)); } syn::Meta::NameValue(_) => { unimplemented!("Need to check for named values"); @@ -77,14 +74,15 @@ pub fn test_case(attr: TokenStream, item: TokenStream) -> TokenStream { for i in fn_args { match i { syn::FnArg::Typed(t) => { - match *t.pat { + let ubox_t = *(t.pat.clone()); + match ubox_t { syn::Pat::Ident(i) => { fn_args_idents.push(i.ident.clone()); } _ => panic!("Unexpected function identifier.") } } - syn::FnArg::Receiver(t) => panic!("Receiver function not expected for test case attribute.") + syn::FnArg::Receiver(_) => panic!("Receiver function not expected for test case attribute.") } } -- GitLab From a030117bb481ff43046662380877fb79ddf05b88 Mon Sep 17 00:00:00 2001 From: Armin Becher Date: Thu, 29 Aug 2019 17:17:27 +0200 Subject: [PATCH 4/4] Fix wrong test case function identifier --- ntest_test_cases/src/lib.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/ntest_test_cases/src/lib.rs b/ntest_test_cases/src/lib.rs index 5403796..a13664e 100644 --- a/ntest_test_cases/src/lib.rs +++ b/ntest_test_cases/src/lib.rs @@ -103,7 +103,6 @@ pub fn test_case(attr: TokenStream, item: TokenStream) -> TokenStream { let test_case_quote = quote! { #[test] fn #test_case_name() { - let x = 42; #(let #fn_args_idents = #literals;)* #fn_body } @@ -131,7 +130,7 @@ fn parse_test_case_attributes(attr: &syn::AttributeArgs) -> TestCaseDescription } syn::NestedMeta::Lit(lit) => { literals.push((*lit).clone()); - name.push_str(&format!("_{:?}", lit_to_str(lit))); + name.push_str(&format!("_{}", lit_to_str(lit))); } } } @@ -146,7 +145,7 @@ fn lit_to_str(lit: &syn::Lit) -> String { match lit { syn::Lit::Bool(s) => s.value.to_string(), syn::Lit::Str(s) => s.value().to_string(), - syn::Lit::Int(s) => s.base10_parse().expect("Expected valid int"), + syn::Lit::Int(s) => s.base10_digits().to_string(), _ => unimplemented!(), } } \ No newline at end of file -- GitLab