flipperzero_test_macros/
lib.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
321
322
323
324
325
use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::{quote, ToTokens};
use syn::{
    parse::{self, Parse},
    punctuated::Punctuated,
    spanned::Spanned,
    token, Expr, ExprArray, Ident, Item, ItemMod, ReturnType, Stmt, Token,
};

mod deassert;

struct TestRunner {
    manifest_args: Punctuated<TestRunnerArg, Token![,]>,
    test_suites: ExprArray,
}

impl Parse for TestRunner {
    fn parse(input: parse::ParseStream) -> syn::Result<Self> {
        let mut manifest_args = Punctuated::new();
        if !input.peek(token::Bracket) {
            loop {
                if input.is_empty() || input.peek(token::Bracket) {
                    break;
                }
                let value = input.parse()?;
                manifest_args.push_value(value);
                if input.is_empty() || input.peek(token::Bracket) {
                    break;
                }
                let punct = input.parse()?;
                manifest_args.push_punct(punct);
            }
        };

        let test_suites = input.parse()?;

        Ok(TestRunner {
            manifest_args,
            test_suites,
        })
    }
}

struct TestRunnerArg {
    ident: Ident,
    eq_token: Token![=],
    value: Box<Expr>,
}

impl Parse for TestRunnerArg {
    fn parse(input: parse::ParseStream) -> syn::Result<Self> {
        let ident = input.parse()?;
        let eq_token = input.parse()?;
        let value = input.parse()?;
        Ok(TestRunnerArg {
            ident,
            eq_token,
            value,
        })
    }
}

#[proc_macro]
pub fn tests_runner(args: TokenStream) -> TokenStream {
    match tests_runner_impl(args) {
        Ok(ts) => ts,
        Err(e) => e.to_compile_error().into(),
    }
}

fn tests_runner_impl(args: TokenStream) -> parse::Result<TokenStream> {
    let TestRunner {
        manifest_args,
        test_suites,
    } = syn::parse(args)?;

    let test_suites = test_suites
        .elems
        .into_iter()
        .map(|attr| {
            let mut module = String::new();
            for token in attr.to_token_stream() {
                module.push_str(&token.to_string());
            }
            let module = module.trim_start_matches("crate::");

            (
                quote!(#attr::__test_list().len()),
                quote!(#attr::__test_list().iter().copied().map(|(name, test_fn)| (#module, name, test_fn))),
            )
        })
        .collect::<Vec<_>>();

    let test_counts = test_suites.iter().map(|(count, _)| count);
    let test_lists = test_suites.iter().map(|(_, list)| list);

    let manifest_args = manifest_args.into_iter().map(
        |TestRunnerArg {
             ident,
             eq_token,
             value,
         }| { quote!(#ident #eq_token #value) },
    );

    Ok(quote!(
        #[cfg(test)]
        mod __test_runner {
            // Required for panic handler
            extern crate flipperzero_rt;

            // Required for allocator
            #[cfg(feature = "alloc")]
            extern crate flipperzero_alloc;

            use flipperzero_rt::{entry, manifest};

            manifest!(#(#manifest_args),*);
            entry!(main);

            const fn test_count() -> usize {
                let ret = 0;
                #( let ret = ret + #test_counts; )*
                ret
            }

            fn test_list() -> impl Iterator<Item = (&'static str, &'static str, ::flipperzero_test::TestFn)> + Clone {
                let ret = ::core::iter::empty();
                #( let ret = ret.chain(#test_lists); )*
                ret
            }

            // Test runner entry point
            pub(super) fn main(args: Option<&::core::ffi::CStr>) -> i32 {
                let args = ::flipperzero_test::__macro_support::Args::parse(args);
                match ::flipperzero_test::__macro_support::run_tests(test_count(), test_list(), args) {
                    Ok(()) => 0,
                    Err(e) => e,
                }
            }
        }

        #[cfg(all(test, miri))]
        #[start]
        fn main(argc: isize, argv: *const *const u8) -> isize {
            // TODO: Is there any benefit to Miri in hooking up the binary arguments to
            // the test runner?
            let ret = __test_runner::main(None);

            // Clean up app state.
            ::flipperzero_rt::__macro_support::__wait_for_thread_completion();

            ret.try_into().unwrap_or(isize::MAX)
        }
    )
    .into())
}

#[proc_macro_attribute]
pub fn tests(args: TokenStream, input: TokenStream) -> TokenStream {
    match tests_impl(args, input) {
        Ok(ts) => ts,
        Err(e) => e.to_compile_error().into(),
    }
}

fn tests_impl(args: TokenStream, input: TokenStream) -> parse::Result<TokenStream> {
    if !args.is_empty() {
        return Err(parse::Error::new(
            Span::call_site(),
            "`#[tests]` attribute takes no arguments",
        ));
    }

    let module: ItemMod = syn::parse(input)?;

    let items = if let Some(content) = module.content {
        content.1
    } else {
        return Err(parse::Error::new(
            module.span(),
            "module must be inline (e.g. `mod foo {}`)",
        ));
    };

    let mut tests = vec![];
    let mut test_cfgs = vec![];
    let mut untouched_tokens = vec![];
    for item in items {
        match item {
            Item::Fn(mut f) => {
                let mut is_test = false;
                let mut cfg = vec![];

                // Find and extract the `#[test]` and `#[cfg(..)] attributes, if present.
                f.attrs.retain(|attr| {
                    if attr.path.is_ident("test") {
                        is_test = true;
                        false
                    } else {
                        if attr.path.is_ident("cfg") {
                            cfg.push(attr.clone());
                        }
                        true
                    }
                });

                if is_test {
                    // Enforce expected function signature.
                    if !f.sig.inputs.is_empty() {
                        return Err(parse::Error::new(
                            f.sig.inputs.span(),
                            "`#[test]` function must have signature `fn()`",
                        ));
                    }
                    if !matches!(f.sig.output, ReturnType::Default) {
                        return Err(parse::Error::new(
                            f.sig.output.span(),
                            "`#[test]` function must have signature `fn()`",
                        ));
                    }

                    // Add a `TestResult` return type.
                    f.sig.output = syn::parse(quote!(-> ::flipperzero_test::TestResult).into())?;

                    // Replace `assert` macros in the test with `TestResult` functions.
                    f.block = deassert::box_block(f.block)?;

                    // Enforce that the test doesn't return anything. This is somewhat
                    // redundant with the function signature check above, as the compiler
                    // will enforce that no value is returned by the unmodified function.
                    // However, in certain cases this results in better errors, due to us
                    // appending an `Ok(())` that can interfere with the previous expression.
                    check_ret_block(&mut f.block.stmts)?;

                    // Append an `Ok(())` to the test.
                    f.block.stmts.push(Stmt::Expr(syn::parse(
                        quote!(::core::result::Result::Ok(())).into(),
                    )?));

                    tests.push(f);
                    test_cfgs.push(cfg);
                } else {
                    untouched_tokens.push(Item::Fn(f));
                }
            }
            _ => {
                untouched_tokens.push(item);
            }
        }
    }

    let ident = module.ident;
    let test_names = tests.iter().zip(test_cfgs).map(|(test, cfg)| {
        let ident = &test.sig.ident;
        let name = ident.to_string();
        quote! {
            #(#cfg)*
            (#name, #ident)
        }
    });

    Ok(quote!(
        #[cfg(test)]
        pub(crate) mod #ident {
            #(#untouched_tokens)*

            #(#tests)*

            pub(crate) const fn __test_list() -> &'static [(&'static str, ::flipperzero_test::TestFn)] {
                &[#(#test_names), *]
            }
        }
    )
    .into())
}

fn check_ret_block(stmts: &mut [Stmt]) -> parse::Result<()> {
    if let Some(stmt) = stmts.last_mut() {
        if let Stmt::Expr(expr) = stmt {
            if let Some(new_stmt) = check_ret_expr(expr)? {
                *stmt = new_stmt;
            }
        }
    }
    Ok(())
}

fn check_ret_expr(expr: &mut Expr) -> parse::Result<Option<Stmt>> {
    match expr {
        // If `expr` is a block that implicitly returns `()`, do nothing.
        Expr::ForLoop(_) | Expr::While(_) => Ok(None),
        // If `expr` is a block, recurse into it.
        Expr::Async(e) => check_ret_block(&mut e.block.stmts).map(|()| None),
        Expr::Block(e) => check_ret_block(&mut e.block.stmts).map(|()| None),
        Expr::If(e) => {
            // Checking the first branch is sufficient; the compiler will enforce that the
            // other branches match.
            check_ret_block(&mut e.then_branch.stmts).map(|()| None)
        }
        Expr::Loop(e) => check_ret_block(&mut e.body.stmts).map(|()| None),
        Expr::Match(e) => {
            if let Some(arm) = e.arms.first_mut() {
                if let Some(stmt) = check_ret_expr(&mut arm.body)? {
                    *arm.body = Expr::Block(syn::parse(quote!({#stmt}).into())?);
                }
            }
            Ok(None)
        }
        Expr::TryBlock(e) => check_ret_block(&mut e.block.stmts).map(|()| None),
        Expr::Unsafe(e) => check_ret_block(&mut e.block.stmts).map(|()| None),
        // If `expr` implicitly returns `()`, append a semicolon.
        Expr::Assign(_) | Expr::AssignOp(_) => {
            Ok(Some(Stmt::Semi(expr.clone(), Token!(;)(expr.span()))))
        }
        Expr::Break(brk) if brk.expr.is_none() => {
            Ok(Some(Stmt::Semi(expr.clone(), Token!(;)(expr.span()))))
        }
        // For all other expressions, raise an error.
        _ => Err(parse::Error::new(
            expr.span(),
            "`#[test]` function must not return anything",
        )),
    }
}