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