| 1 |
use syn::{ |
| 2 |
parse::{Error, Parse, ParseStream, Result}, |
| 3 |
punctuated::Punctuated, |
| 4 |
Attribute, Expr, Ident, Token, |
| 5 |
}; |
| 6 |
|
| 7 |
use proc_macro2::TokenStream; |
| 8 |
use quote::ToTokens; |
| 9 |
|
| 10 |
#[derive(PartialEq, Debug, Clone)] |
| 11 |
/// A test case instance data. Contains a list of arguments. It is parsed by parametrize |
| 12 |
/// attributes. |
| 13 |
pub(crate) struct TestCase { |
| 14 |
pub(crate) args: Vec<Expr>, |
| 15 |
pub(crate) attrs: Vec<Attribute>, |
| 16 |
pub(crate) description: Option<Ident>, |
| 17 |
} |
| 18 |
|
| 19 |
impl Parse for TestCase { |
| 20 |
fn parse(input: ParseStream) -> Result<Self> { |
| 21 |
let attrs = Attribute::parse_outer(input)?; |
| 22 |
let case: Ident = input.parse()?; |
| 23 |
if case == "case" { |
| 24 |
let mut description = None; |
| 25 |
if input.peek(Token![::]) { |
| 26 |
let _ = input.parse::<Token![::]>(); |
| 27 |
description = Some(input.parse()?); |
| 28 |
} |
| 29 |
let content; |
| 30 |
let _ = syn::parenthesized!(content in input); |
| 31 |
let args = Punctuated::<Expr, Token![,]>::parse_terminated(&content)? |
| 32 |
.into_iter() |
| 33 |
.collect(); |
| 34 |
Ok(TestCase { |
| 35 |
args, |
| 36 |
attrs, |
| 37 |
description, |
| 38 |
}) |
| 39 |
} else { |
| 40 |
Err(Error::new(case.span(), "expected a test case")) |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
impl ToTokens for TestCase { |
| 46 |
fn to_tokens(&self, tokens: &mut TokenStream) { |
| 47 |
self.args.iter().for_each(|c| c.to_tokens(tokens)) |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
#[cfg(test)] |
| 52 |
mod should { |
| 53 |
use super::*; |
| 54 |
use crate::test::{assert_eq, *}; |
| 55 |
|
| 56 |
fn parse_test_case<S: AsRef<str>>(test_case: S) -> TestCase { |
| 57 |
parse_meta(test_case) |
| 58 |
} |
| 59 |
|
| 60 |
#[test] |
| 61 |
fn two_literal_args() { |
| 62 |
let test_case = parse_test_case(r#"case(42, "value")"#); |
| 63 |
let args = test_case.args(); |
| 64 |
|
| 65 |
let expected = to_args!(["42", r#""value""#]); |
| 66 |
|
| 67 |
assert_eq!(expected, args); |
| 68 |
} |
| 69 |
|
| 70 |
#[test] |
| 71 |
fn some_literals() { |
| 72 |
let args_expressions = literal_expressions_str(); |
| 73 |
let test_case = parse_test_case(&format!("case({})", args_expressions.join(", "))); |
| 74 |
let args = test_case.args(); |
| 75 |
|
| 76 |
assert_eq!(to_args!(args_expressions), args); |
| 77 |
} |
| 78 |
|
| 79 |
#[test] |
| 80 |
fn accept_arbitrary_rust_code() { |
| 81 |
let test_case = parse_test_case(r#"case(vec![1,2,3])"#); |
| 82 |
let args = test_case.args(); |
| 83 |
|
| 84 |
assert_eq!(to_args!(["vec![1, 2, 3]"]), args); |
| 85 |
} |
| 86 |
|
| 87 |
#[test] |
| 88 |
#[should_panic] |
| 89 |
fn raise_error_on_invalid_rust_code() { |
| 90 |
parse_test_case(r#"case(some:<>(1,2,3))"#); |
| 91 |
} |
| 92 |
|
| 93 |
#[test] |
| 94 |
fn get_description_if_any() { |
| 95 |
let test_case = parse_test_case(r#"case::this_test_description(42)"#); |
| 96 |
let args = test_case.args(); |
| 97 |
|
| 98 |
assert_eq!( |
| 99 |
"this_test_description", |
| 100 |
&test_case.description.unwrap().to_string() |
| 101 |
); |
| 102 |
assert_eq!(to_args!(["42"]), args); |
| 103 |
} |
| 104 |
|
| 105 |
#[test] |
| 106 |
fn get_description_also_with_more_args() { |
| 107 |
let test_case = parse_test_case(r#"case :: this_test_description (42, 24)"#); |
| 108 |
let args = test_case.args(); |
| 109 |
|
| 110 |
assert_eq!( |
| 111 |
"this_test_description", |
| 112 |
&test_case.description.unwrap().to_string() |
| 113 |
); |
| 114 |
assert_eq!(to_args!(["42", "24"]), args); |
| 115 |
} |
| 116 |
|
| 117 |
#[test] |
| 118 |
fn parse_arbitrary_rust_code_as_expression() { |
| 119 |
let test_case = parse_test_case( |
| 120 |
r##" |
| 121 |
case(42, -42, |
| 122 |
pippo("pluto"), |
| 123 |
Vec::new(), |
| 124 |
String::from(r#"prrr"#), |
| 125 |
{ |
| 126 |
let mut sum=0; |
| 127 |
for i in 1..3 { |
| 128 |
sum += i; |
| 129 |
} |
| 130 |
sum |
| 131 |
}, |
| 132 |
vec![1,2,3] |
| 133 |
)"##, |
| 134 |
); |
| 135 |
|
| 136 |
let args = test_case.args(); |
| 137 |
|
| 138 |
assert_eq!( |
| 139 |
to_args!([ |
| 140 |
"42", |
| 141 |
"-42", |
| 142 |
r#"pippo("pluto")"#, |
| 143 |
"Vec::new()", |
| 144 |
r##"String::from(r#"prrr"#)"##, |
| 145 |
r#"{let mut sum=0;for i in 1..3 {sum += i;}sum}"#, |
| 146 |
"vec![1,2,3]" |
| 147 |
]), |
| 148 |
args |
| 149 |
); |
| 150 |
} |
| 151 |
|
| 152 |
#[test] |
| 153 |
fn save_attributes() { |
| 154 |
let test_case = parse_test_case(r#"#[should_panic]#[other_attr(x)]case(42)"#); |
| 155 |
|
| 156 |
let content = format!("{:?}", test_case.attrs); |
| 157 |
|
| 158 |
assert_eq!(2, test_case.attrs.len()); |
| 159 |
assert!(content.contains("should_panic")); |
| 160 |
assert!(content.contains("other_attr")); |
| 161 |
} |
| 162 |
} |