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
//! `test!` macro for individual test cases
//!
//! Creates tests with optional TestDatabase parameter and async support,
//! similar to Jest's test/it blocks.
use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use quote::{format_ident, quote};
use syn::parse::{Parse, ParseStream};
use syn::{braced, parenthesized, Ident, LitStr, Token, Type};
/// Convert a string to snake_case for function names
fn to_snake_case(name: &str) -> String {
let mut result = String::new();
let mut prev_is_uppercase = false;
for (i, c) in name.chars().enumerate() {
if c.is_alphanumeric() {
if c.is_uppercase() {
if i > 0 && !prev_is_uppercase && !result.ends_with('_') {
result.push('_');
}
result.push(c.to_ascii_lowercase());
prev_is_uppercase = true;
} else {
result.push(c);
prev_is_uppercase = false;
}
} else if (c.is_whitespace() || c == '-' || c == '_')
&& !result.ends_with('_')
&& !result.is_empty()
{
result.push('_');
}
}
// Remove trailing underscore
while result.ends_with('_') {
result.pop();
}
result
}
/// Parameter in the function signature
struct FnParam {
name: Ident,
ty: Type,
}
/// Arguments for the test! macro
/// Supports: test!("name", async fn(db: TestDatabase) { ... })
/// test!("name", async fn() { ... })
/// test!("name", fn() { ... })
struct TestArgs {
name: LitStr,
is_async: bool,
params: Vec<FnParam>,
body: TokenStream2,
}
impl Parse for TestArgs {
fn parse(input: ParseStream) -> syn::Result<Self> {
// Parse the test name string
let name: LitStr = input.parse()?;
input.parse::<Token![,]>()?;
// Check for async keyword
let is_async = if input.peek(Token![async]) {
input.parse::<Token![async]>()?;
true
} else {
false
};
// Parse 'fn'
input.parse::<Token![fn]>()?;
// Parse parameters (param: Type, ...)
let content;
parenthesized!(content in input);
let mut params = Vec::new();
while !content.is_empty() {
let param_name: Ident = content.parse()?;
content.parse::<Token![:]>()?;
let param_type: Type = content.parse()?;
params.push(FnParam {
name: param_name,
ty: param_type,
});
if content.peek(Token![,]) {
content.parse::<Token![,]>()?;
}
}
// Parse the body
let body_content;
braced!(body_content in input);
let body: TokenStream2 = body_content.parse()?;
Ok(Self {
name,
is_async,
params,
body,
})
}
}
/// Check if a type path ends with "TestDatabase"
fn is_test_database(ty: &Type) -> bool {
if let Type::Path(type_path) = ty {
if let Some(segment) = type_path.path.segments.last() {
return segment.ident == "TestDatabase";
}
}
false
}
pub fn test_impl(input: TokenStream) -> TokenStream {
let args = match syn::parse::<TestArgs>(input) {
Ok(args) => args,
Err(e) => return e.to_compile_error().into(),
};
let ferro = quote!(::ferro);
let name_str = args.name.value();
let fn_name = format_ident!("{}", to_snake_case(&name_str));
let body = args.body;
// Check if any parameter is TestDatabase
let has_db_param = args.params.iter().any(|p| is_test_database(&p.ty));
// Find the TestDatabase parameter name if it exists
let db_param = args.params.iter().find(|p| is_test_database(&p.ty));
if args.is_async {
if has_db_param {
// Async with TestDatabase - use ferro_test
let db_param_name = &db_param.unwrap().name;
let output = quote! {
#[#ferro::ferro_test]
async fn #fn_name(#db_param_name: #ferro::testing::TestDatabase) {
// Set the test name for expect! macro output
#ferro::testing::set_current_test_name(Some(#name_str.to_string()));
// Run the test body
let __test_result = async {
#body
}.await;
// Clear the test name
#ferro::testing::set_current_test_name(None);
__test_result
}
};
output.into()
} else {
// Async without TestDatabase - still use ferro_test for consistency
let output = quote! {
#[#ferro::ferro_test]
async fn #fn_name() {
// Set the test name for expect! macro output
#ferro::testing::set_current_test_name(Some(#name_str.to_string()));
// Run the test body
let __test_result = async {
#body
}.await;
// Clear the test name
#ferro::testing::set_current_test_name(None);
__test_result
}
};
output.into()
}
} else {
// Sync test - use regular #[test]
let output = quote! {
#[test]
fn #fn_name() {
// Set the test name for expect! macro output
#ferro::testing::set_current_test_name(Some(#name_str.to_string()));
// Run the test body
let __test_result = {
#body
};
// Clear the test name
#ferro::testing::set_current_test_name(None);
__test_result
}
};
output.into()
}
}