/
inject.rs
238 lines (207 loc) · 7.11 KB
/
inject.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
use std::borrow::Cow;
use proc_macro2::TokenStream;
use quote::quote;
use syn::{parse_quote, Expr, FnArg, Ident, Stmt, Type};
use crate::{
refident::{MaybeIdent, MaybeType},
resolver::Resolver,
utils::IsLiteralExpression,
};
pub(crate) fn resolve_aruments<'a>(
args: impl Iterator<Item = &'a FnArg>,
resolver: &impl Resolver,
generic_types: &[Ident],
) -> TokenStream {
let define_vars = args.map(|arg| ArgumentResolver::new(resolver, generic_types).resolve(arg));
quote! {
#(#define_vars)*
}
}
struct ArgumentResolver<'resolver, 'idents, 'f, R>
where
R: Resolver + 'resolver,
{
resolver: &'resolver R,
generic_types_names: &'idents [Ident],
magic_conversion: &'f dyn Fn(Cow<Expr>, &Type) -> Expr,
}
impl<'resolver, 'idents, 'f, R> ArgumentResolver<'resolver, 'idents, 'f, R>
where
R: Resolver + 'resolver,
{
fn new(resolver: &'resolver R, generic_types_names: &'idents [Ident]) -> Self {
Self {
resolver,
generic_types_names,
magic_conversion: &handling_magic_conversion_code,
}
}
fn resolve(&self, arg: &FnArg) -> Option<Stmt> {
let ident = arg.maybe_ident()?;
let arg_type = arg.maybe_type()?;
let fixture_name = self.fixture_name(ident);
let mut fixture = self
.resolver
.resolve(&fixture_name)
.map(|e| e.clone())
.unwrap_or_else(|| default_fixture_resolve(&fixture_name));
if fixture.is_literal() && self.type_can_be_get_from_literal_str(arg_type) {
fixture = Cow::Owned((self.magic_conversion)(fixture, arg_type));
}
Some(parse_quote! {
let #ident = #fixture;
})
}
fn fixture_name<'a>(&self, ident: &'a Ident) -> Cow<'a, Ident> {
let id_str = ident.to_string();
if id_str.starts_with("_") && !id_str.starts_with("__") {
Cow::Owned(Ident::new(&id_str[1..], ident.span()))
} else {
Cow::Borrowed(ident)
}
}
fn type_can_be_get_from_literal_str(&self, t: &Type) -> bool {
// Check valid type to apply magic conversion
match t {
Type::ImplTrait(_)
| Type::TraitObject(_)
| Type::Infer(_)
| Type::Group(_)
| Type::Macro(_)
| Type::Never(_)
| Type::Paren(_)
| Type::Verbatim(_)
| Type::Slice(_) => return false,
_ => {}
}
match t.maybe_ident() {
Some(id) => !self.generic_types_names.contains(id),
None => true,
}
}
}
fn default_fixture_resolve(ident: &Ident) -> Cow<Expr> {
Cow::Owned(parse_quote! { #ident::default() })
}
fn handling_magic_conversion_code(fixture: Cow<Expr>, arg_type: &Type) -> Expr {
parse_quote! {
{
struct __Wrap<T>(std::marker::PhantomData<T>);
trait __ViaParseDebug<'a, T> {
fn magic_conversion(&self, input: &'a str) -> T;
}
impl<'a, T> __ViaParseDebug<'a, T> for &&__Wrap<T>
where
T: std::str::FromStr,
T::Err: std::fmt::Debug,
{
fn magic_conversion(&self, input: &'a str) -> T {
T::from_str(input).unwrap()
}
}
trait __ViaParse<'a, T> {
fn magic_conversion(&self, input: &'a str) -> T;
}
impl<'a, T> __ViaParse<'a, T> for &__Wrap<T>
where
T: std::str::FromStr,
{
fn magic_conversion(&self, input: &'a str) -> T {
match T::from_str(input) {
Ok(v) => v,
Err(_) => {
panic!("Cannot parse '{}' to get {}", input, std::stringify!(#arg_type));
}
}
}
}
trait __ViaIdent<'a, T> {
fn magic_conversion(&self, input: &'a str) -> T;
}
impl<'a> __ViaIdent<'a, &'a str> for &&__Wrap<&'a str> {
fn magic_conversion(&self, input: &'a str) -> &'a str {
input
}
}
(&&&__Wrap::<#arg_type>(std::marker::PhantomData)).magic_conversion(#fixture)
}
}
}
#[cfg(test)]
mod should {
use super::*;
use crate::{
test::{assert_eq, *},
utils::fn_args,
};
use mytest::*;
#[rstest]
#[case::as_is("fix: String", "let fix = fix::default();")]
#[case::without_underscore("_fix: String", "let _fix = fix::default();")]
#[case::do_not_remove_inner_underscores("f_i_x: String", "let f_i_x = f_i_x::default();")]
#[case::do_not_remove_double_underscore("__fix: String", "let __fix = __fix::default();")]
#[case::without_mut("mut fix: String", "let fix = fix::default();")]
fn call_fixture(#[case] arg_str: &str, #[case] expected: &str) {
let arg = arg_str.ast();
let injected = ArgumentResolver::new(&EmptyResolver {}, &[])
.resolve(&arg)
.unwrap();
assert_eq!(injected, expected.ast());
}
#[rstest]
#[case::as_is("mut fix: String", ("fix", expr("bar()")), "let fix = bar();")]
#[case::without_undescore("_fix: String", ("fix", expr("bar()")), "let _fix = bar();")]
fn call_given_fixture(
#[case] arg_str: &str,
#[case] rule: (&str, Expr),
#[case] expected: &str,
) {
let arg = arg_str.ast();
let mut resolver = std::collections::HashMap::new();
resolver.insert(rule.0.to_owned(), &rule.1);
let injected = ArgumentResolver::new(&resolver, &[]).resolve(&arg).unwrap();
assert_eq!(injected, expected.ast());
}
fn _mock_conversion_code(fixture: Cow<Expr>, arg_type: &Type) -> Expr {
parse_quote! {
#fixture as #arg_type
}
}
#[rstest]
#[case::implement_it(
"fn test(arg: MyType){}",
0,
r#"let arg = "value to convert" as MyType;"#
)]
#[case::discard_impl(
"fn test(arg: impl AsRef<str>){}",
0,
r#"let arg = "value to convert";"#
)]
#[case::discard_generic_type(
"fn test<S: AsRef<str>>(arg: S){}",
0,
r#"let arg = "value to convert";"#
)]
fn handle_magic_conversion(#[case] fn_str: &str, #[case] n_arg: usize, #[case] expected: &str) {
let function = fn_str.ast();
let arg = fn_args(&function).nth(n_arg).unwrap();
let generics = function
.sig
.generics
.type_params()
.map(|tp| &tp.ident)
.cloned()
.collect::<Vec<_>>();
let mut resolver = std::collections::HashMap::new();
let expr = expr(r#""value to convert""#);
resolver.insert(arg.maybe_ident().unwrap().to_string(), &expr);
let ag = ArgumentResolver {
resolver: &resolver,
generic_types_names: &generics,
magic_conversion: &_mock_conversion_code,
};
let injected = ag.resolve(&arg).unwrap();
assert_eq!(injected, expected.ast());
}
}