-
Notifications
You must be signed in to change notification settings - Fork 39
/
test.rs
320 lines (269 loc) · 7.19 KB
/
test.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#![macro_use]
/// Unit testing utility module. Collect a bunch of functions¯o and impls to simplify unit
/// testing bolilerplate.
///
use std::borrow::Cow;
use std::iter::FromIterator;
pub(crate) use mytest::{fixture, rstest};
pub(crate) use pretty_assertions::assert_eq;
use proc_macro2::TokenTree;
use quote::quote;
use syn::{parse::Parse, parse2, parse_str, Error, Expr, Ident, ItemFn, Stmt};
use super::*;
use crate::parse::{
fixture::{FixtureData, FixtureItem},
rstest::{RsTestData, RsTestItem},
testcase::TestCase,
vlist::ValueList,
Attribute, Fixture, Positional,
};
use crate::resolver::Resolver;
use crate::utils::fn_args_idents;
use parse::fixture::ArgumentValue;
macro_rules! to_args {
($e:expr) => {{
$e.iter()
.map(|s| s as &dyn AsRef<str>)
.map(expr)
.collect::<Vec<_>>()
}};
}
macro_rules! to_exprs {
($e:expr) => {
$e.iter().map(|s| expr(s)).collect::<Vec<_>>()
};
}
macro_rules! to_strs {
($e:expr) => {
$e.iter().map(ToString::to_string).collect::<Vec<_>>()
};
}
macro_rules! to_idents {
($e:expr) => {
$e.iter().map(|s| ident(s)).collect::<Vec<_>>()
};
}
struct Outer<T>(T);
impl<T: Parse> Parse for Outer<T> {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let outer: Ident = input.parse()?;
if outer == "outer" {
let content;
let _ = syn::parenthesized!(content in input);
content.parse().map(Outer)
} else {
Err(Error::new(outer.span(), "Expected 'outer'"))
}
}
}
pub(crate) fn parse_meta<T: syn::parse::Parse, S: AsRef<str>>(test_case: S) -> T {
let to_parse = format!(
r#"
#[outer({})]
fn to_parse() {{}}
"#,
test_case.as_ref()
);
let item_fn = parse_str::<ItemFn>(&to_parse).expect(&format!("Cannot parse '{}'", to_parse));
let tokens = quote!(
#item_fn
);
let tt = tokens.into_iter().skip(1).next().unwrap();
if let TokenTree::Group(g) = tt {
let ts = g.stream();
parse2::<Outer<T>>(ts).unwrap().0
} else {
panic!("Cannot find group in {:#?}", tt)
}
}
pub(crate) trait ToAst {
fn ast<T: Parse>(self) -> T;
}
impl ToAst for &str {
fn ast<T: Parse>(self) -> T {
parse_str(self).unwrap()
}
}
impl ToAst for String {
fn ast<T: Parse>(self) -> T {
parse_str(&self).unwrap()
}
}
impl ToAst for proc_macro2::TokenStream {
fn ast<T: Parse>(self) -> T {
parse2(self).unwrap()
}
}
pub(crate) fn ident(s: impl AsRef<str>) -> syn::Ident {
s.as_ref().ast()
}
pub(crate) fn expr(s: impl AsRef<str>) -> syn::Expr {
s.as_ref().ast()
}
pub(crate) fn attrs(s: impl AsRef<str>) -> Vec<syn::Attribute> {
parse_str::<ItemFn>(&format!(
r#"{}
fn _no_name_() {{}}
"#,
s.as_ref()
))
.unwrap()
.attrs
}
pub(crate) fn fixture(name: impl AsRef<str>, args: &[&str]) -> Fixture {
Fixture::new(ident(name), None, Positional(to_exprs!(args)))
}
pub(crate) fn arg_value(name: impl AsRef<str>, value: impl AsRef<str>) -> ArgumentValue {
ArgumentValue::new(ident(name), expr(value))
}
pub(crate) fn values_list<S: AsRef<str>>(arg: &str, values: &[S]) -> ValueList {
ValueList {
arg: ident(arg),
values: values.into_iter().map(|s| expr(s)).collect(),
}
}
pub(crate) fn first_arg_ident(ast: &ItemFn) -> &Ident {
fn_args_idents(&ast).next().unwrap()
}
pub(crate) fn extract_inner_functions(block: &syn::Block) -> impl Iterator<Item = &syn::ItemFn> {
block.stmts.iter().filter_map(|s| match s {
syn::Stmt::Item(syn::Item::Fn(f)) => Some(f),
_ => None,
})
}
pub(crate) fn literal_expressions_str() -> Vec<&'static str> {
vec![
"42",
"42isize",
"1.0",
"-1",
"-1.0",
"true",
"1_000_000u64",
"0b10100101u8",
r#""42""#,
"b'H'",
]
}
pub(crate) trait ExtractArgs {
fn args(&self) -> Vec<Expr>;
}
impl ExtractArgs for TestCase {
fn args(&self) -> Vec<Expr> {
self.args.iter().cloned().collect()
}
}
impl ExtractArgs for ValueList {
fn args(&self) -> Vec<Expr> {
self.values.iter().cloned().collect()
}
}
impl Attribute {
pub fn attr<S: AsRef<str>>(s: S) -> Self {
Attribute::Attr(ident(s))
}
pub fn tagged<SI: AsRef<str>, SA: AsRef<str>>(tag: SI, attrs: Vec<SA>) -> Self {
Attribute::Tagged(ident(tag), attrs.into_iter().map(|a| ident(a)).collect())
}
pub fn typed<S: AsRef<str>, T: AsRef<str>>(tag: S, inner: T) -> Self {
Attribute::Type(ident(tag), parse_str(inner.as_ref()).unwrap())
}
}
impl RsTestInfo {
pub fn push_case(&mut self, case: TestCase) {
self.data.items.push(RsTestItem::TestCase(case));
}
pub fn extend(&mut self, cases: impl Iterator<Item = TestCase>) {
self.data.items.extend(cases.map(RsTestItem::TestCase));
}
}
impl Fixture {
pub fn with_resolve(mut self, resolve_ident: &str) -> Self {
self.resolve = Some(ident(resolve_ident));
self
}
}
impl TestCase {
pub fn with_description(mut self, description: &str) -> Self {
self.description = Some(ident(description));
self
}
pub fn with_attrs(mut self, attrs: Vec<syn::Attribute>) -> Self {
self.attrs = attrs;
self
}
}
impl<A: AsRef<str>> FromIterator<A> for TestCase {
fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self {
TestCase {
args: iter.into_iter().map(expr).collect(),
attrs: Default::default(),
description: None,
}
}
}
impl<'a> From<&'a str> for TestCase {
fn from(argument: &'a str) -> Self {
std::iter::once(argument).collect()
}
}
impl From<Vec<RsTestItem>> for RsTestData {
fn from(items: Vec<RsTestItem>) -> Self {
Self { items }
}
}
impl From<RsTestData> for RsTestInfo {
fn from(data: RsTestData) -> Self {
Self {
data,
attributes: Default::default(),
}
}
}
impl From<Vec<Expr>> for Positional {
fn from(data: Vec<Expr>) -> Self {
Positional(data)
}
}
impl From<Vec<FixtureItem>> for FixtureData {
fn from(fixtures: Vec<FixtureItem>) -> Self {
Self { items: fixtures }
}
}
pub(crate) struct EmptyResolver;
impl<'a> Resolver for EmptyResolver {
fn resolve(&self, _ident: &Ident) -> Option<Cow<Expr>> {
None
}
}
pub(crate) trait IsAwait {
fn is_await(&self) -> bool;
}
impl IsAwait for Stmt {
fn is_await(&self) -> bool {
match self {
Stmt::Expr(Expr::Await(_)) => true,
_ => false,
}
}
}
pub(crate) trait DisplayCode {
fn display_code(&self) -> String;
}
impl<T: ToTokens> DisplayCode for T {
fn display_code(&self) -> String {
self.to_token_stream().to_string()
}
}
impl crate::parse::fixture::FixtureInfo {
pub(crate) fn with_once(mut self) -> Self {
self.attributes = self.attributes.with_once();
self
}
}
impl crate::parse::fixture::FixtureModifiers {
pub(crate) fn with_once(mut self) -> Self {
self.append(Attribute::attr("once"));
self
}
}