embedded_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 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
// Copied from https://github.com/knurling-rs/defmt/blob/main/firmware/defmt-test/macros/src/lib.rs
extern crate proc_macro;
use darling::{ast::NestedMeta, FromMeta};
use proc_macro::TokenStream;
use quote::{format_ident, quote};
use syn::{parse, spanned::Spanned, Attribute, Item, ItemFn, ItemMod, ReturnType, Type};
/// Attribute to be placed on the test suite's module.
///
/// ## Arguments
/// - `default-timeout`: The default timeout in seconds for all tests in the suite. This can be overridden on a per-test basis. If not specified here or on a per-test basis, the default timeout is 60 seconds.
/// - `executor`: The custom executor to use for running async tests. This is only required if the features `embassy` and `external-executor` are enabled.
/// - `setup`: A function that will be called before running the tests. This can be used to setup logging or other global state.
///
/// ## Examples
///
/// Define a test suite with a single test:
///
/// ```rust,no_run
/// #[embedded_test::tests]
/// mod tests {
/// #[init]
/// fn init() {
/// // Initialize the hardware
/// }
///
/// #[test]
/// fn test() {
/// // Test the hardware
/// }
/// }
/// ```
///
/// Define a test suite and customize everything:
///
/// ```rust,no_run
/// #[embedded_test::tests(default_timeout = 10, executor = embassy::executor::Executor::new(), setup = rtt_target::rtt_init_log!())]
/// mod tests {
/// #[init]
/// fn init() {
/// // Initialize the hardware
/// }
///
/// #[test]
/// fn test() {
/// log::info("Start....")
/// // Test the hardware
/// }
///
/// #[test]
/// #[timeout(5)]
/// fn test2() {
/// // Test the hardware
/// }
/// }
/// ```
#[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> {
#[derive(Debug, FromMeta)]
struct MacroArgs {
executor: Option<syn::Expr>,
default_timeout: Option<u32>,
setup: Option<syn::Expr>,
}
let attr_args = match NestedMeta::parse_meta_list(args.into()) {
Ok(v) => v,
Err(e) => {
return Ok(TokenStream::from(darling::Error::from(e).write_errors()));
}
};
let macro_args = match MacroArgs::from_list(&attr_args) {
Ok(v) => v,
Err(e) => {
return Ok(TokenStream::from(e.write_errors()));
}
};
#[cfg(not(all(feature = "embassy", feature = "external-executor")))]
if macro_args.executor.is_some() {
return Err(parse::Error::new(
proc_macro2::Span::call_site(),
"`#[embedded_test::tests]` attribute doesn't take an executor unless the features `embassy` and `external-executor` are enabled",
));
}
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 init = None;
let mut tests = vec![];
let mut untouched_tokens = vec![];
for item in items {
match item {
Item::Fn(mut f) => {
let mut test_kind = None;
let mut should_panic = false;
let mut ignore = false;
let mut timeout = None;
f.attrs.retain(|attr| {
if attr.path().is_ident("init") {
test_kind = Some(Attr::Init);
false
} else if attr.path().is_ident("test") {
test_kind = Some(Attr::Test);
false
} else if attr.path().is_ident("should_panic") {
should_panic = true;
false
} else if attr.path().is_ident("ignore") {
ignore = true;
false
} else if attr.path().is_ident("timeout") {
timeout = Some(attr.clone());
false
} else {
true
}
});
let timeout = if let Some(attr) = timeout {
match attr.parse_args::<TimeoutAttribute>() {
Ok(TimeoutAttribute { value }) => Some(value),
Err(e) => {
return Err(parse::Error::new(
attr.span(),
format!("failed to parse `timeout` attribute. Must be of the form #[timeout(10)] where 10 is the timeout in seconds. Error: {}", e)
));
}
}
} else {
None
};
let attr = match test_kind {
Some(it) => it,
None => {
return Err(parse::Error::new(
f.span(),
"function requires `#[init]` or `#[test]` attribute",
));
}
};
match attr {
Attr::Init => {
if init.is_some() {
return Err(parse::Error::new(
f.sig.ident.span(),
"only a single `#[init]` function can be defined",
));
}
if should_panic {
return Err(parse::Error::new(
f.sig.ident.span(),
"`#[should_panic]` is not allowed on the `#[init]` function",
));
}
if ignore {
return Err(parse::Error::new(
f.sig.ident.span(),
"`#[ignore]` is not allowed on the `#[init]` function",
));
}
if timeout.is_some() {
return Err(parse::Error::new(
f.sig.ident.span(),
"`#[timeout]` is not allowed on the `#[init]` function",
));
}
if check_fn_sig(&f.sig).is_err() || !f.sig.inputs.is_empty() {
return Err(parse::Error::new(
f.sig.ident.span(),
"`#[init]` function must have signature `async fn() [-> Type]` (async/return type are optional)",
));
}
#[cfg(not(feature = "embassy"))]
if f.sig.asyncness.is_some() {
return Err(parse::Error::new(
f.sig.ident.span(),
"`#[init]` function can only be async if an async executor is enabled via feature",
));
}
let state = match &f.sig.output {
ReturnType::Default => None,
ReturnType::Type(.., ty) => Some(ty.clone()),
};
let asyncness = f.sig.asyncness.is_some();
init = Some(Init {
func: f,
state,
asyncness,
});
}
Attr::Test => {
if check_fn_sig(&f.sig).is_err() || f.sig.inputs.len() > 1 {
return Err(parse::Error::new(
f.sig.ident.span(),
"`#[test]` function must have signature `async fn(state: &mut Type)` (async/parameter are optional)",
));
}
#[cfg(not(feature = "embassy"))]
if f.sig.asyncness.is_some() {
return Err(parse::Error::new(
f.sig.ident.span(),
"`#[test]` function can only be async if an async executor is enabled via feature",
));
}
let input = if f.sig.inputs.len() == 1 {
let arg = &f.sig.inputs[0];
// NOTE we cannot check the argument type matches `init.state` at this
// point
if let Some(ty) = get_arg_type(arg).cloned() {
Some(Input { ty })
} else {
// was not `&mut T`
return Err(parse::Error::new(
arg.span(),
"parameter must be of the type that init() returns",
));
}
} else {
None
};
let asyncness = f.sig.asyncness.is_some();
tests.push(Test {
cfgs: extract_cfgs(&f.attrs),
func: f,
asyncness,
input,
should_panic,
ignore,
timeout: timeout.or(macro_args.default_timeout),
});
}
}
}
_ => {
untouched_tokens.push(item);
}
}
}
let krate = format_ident!("embedded_test");
let ident = module.ident;
let mut state_ty = None;
let (init_fn, init_expr, init_is_async) = if let Some(ref init) = init {
let init_func = &init.func;
let init_ident = &init.func.sig.ident;
state_ty = init.state.clone();
(
Some(quote!(#init_func)),
invoke(init_ident, vec![], init.asyncness),
init.asyncness,
)
} else {
(None, quote!(()), false)
};
let mut unit_test_calls = vec![];
let mut test_function_invokers = vec![];
for test in &tests {
let should_panic = test.should_panic;
let ignore = test.ignore;
let ident = &test.func.sig.ident;
let ident_invoker = format_ident!("__{}_invoker", ident);
let span = test.func.sig.ident.span();
let timeout = match test.timeout {
Some(timeout) => quote!(Some(#timeout)),
None => quote!(None),
};
//TODO: if init func returns something, all functions must accept it as input?
let mut args = vec![];
if let Some(input) = test.input.as_ref() {
if let Some(state) = &state_ty {
if input.ty != **state {
return Err(parse::Error::new(
input.ty.span(),
format!(
"this type must match `#[init]`s return type: {}",
type_ident(state)
),
));
}
args.push(quote!(state));
} else {
return Err(parse::Error::new(
span,
"no state was initialized by `#[init]`; signature must be `fn()`",
));
}
}
let run_call = invoke(ident, args, test.asyncness);
let init_run_and_check = quote!(
{
let outcome;
{
let state = #init_expr; // either init() or init().await or ()
outcome = #run_call; // either test(state), test(state).await, test(), or test().await
}
#krate::export::check_outcome(outcome);
}
);
// The closure that will be called, if the test should be runned.
// This closure has the signature () -> !, so it will never return.
// The closure will signal the test result via semihosting exit/abort instead
let entrypoint = if test.asyncness || init_is_async {
// We need a utility function, so that embassy can create a task for us
let cfgs = &test.cfgs;
test_function_invokers.push(quote!(
#(#cfgs)*
#[#krate::export::task]
async fn #ident_invoker() {
#init_run_and_check
}
));
let executor = if let Some(executor) = ¯o_args.executor {
quote! {
#executor
}
} else {
quote! {
#krate::export::Executor::new()
}
};
quote!(|| {
let mut executor = #executor;
let executor = unsafe { __make_static(&mut executor) };
executor.run(|spawner| {
spawner.must_spawn(#ident_invoker());
})
})
} else {
quote!(|| {
#init_run_and_check
})
};
unit_test_calls.push(quote! {
const FULLY_QUALIFIED_FN_NAME: &str = concat!(module_path!(), "::", stringify!(#ident));
test_funcs.push(#krate::export::Test{name: FULLY_QUALIFIED_FN_NAME, ignored: #ignore, should_panic: #should_panic, function: #entrypoint, timeout: #timeout}).unwrap();
});
}
let test_functions = tests.iter().map(|test| &test.func);
let test_cfgs = tests.iter().map(|test| &test.cfgs);
let test_count = {
let test_cfgs = test_cfgs.clone();
quote!(
{
let mut counter = 0;
#(
#(#test_cfgs)*
{ counter += 1; }
)*
counter
}
)
};
let test_names_strlen = {
let test_cfgs = test_cfgs.clone();
let test_names = tests.iter().map(|test| test.func.sig.ident.clone());
quote!(
{
// The idea of this code is to calculate the overall string length of all test names,
// so that inside `run_tests` we can allocate a json buffer of the right size,
// without doing a heap allocation (requiring a global allocator)
let mut counter = 0;
#(
#(#test_cfgs)*
{
const FULLY_QUALIFIED_FN_NAME: &str = concat!(module_path!(), "::", stringify!(#test_names));
counter += FULLY_QUALIFIED_FN_NAME.len();
}
)*
counter
}
)
};
let setup = macro_args.setup;
Ok(quote!(
#[cfg(test)]
mod #ident {
#(#untouched_tokens)*
// Used by probe-rs to detect that the binary runs embedded-test
#[used]
#[no_mangle]
#[link_section = ".embedded_test.meta"]
static EMBEDDED_TEST_VERSION: usize = 0;
unsafe fn __make_static<T>(t: &mut T) -> &'static mut T {
::core::mem::transmute(t)
}
#[export_name = "main"]
unsafe extern "C" fn __embedded_test_entry() -> ! {
// The linker file will redirect this call to the function below.
// This trick ensures that we get a compile error, if the linker file was not added to the rustflags.
#krate::export::ensure_linker_file_was_added_to_rustflags();
}
#[no_mangle]
unsafe extern "C" fn __embedded_test_start() -> ! {
{
#setup
}
const TEST_COUNT : usize = #test_count;
const TEST_NAMES_STRLEN : usize = #test_names_strlen;
let mut test_funcs: #krate::export::Vec<#krate::export::Test, TEST_COUNT> = #krate::export::Vec::new();
#(
#(#test_cfgs)*
{
#unit_test_calls // pushes Test to test_funcs
}
)*
const JSON_SIZE_TOTAL : usize = #krate::export::JSON_SIZE_HEADER + TEST_NAMES_STRLEN
+ #krate::export::JSON_SIZE_PER_TEST_WITHOUT_TESTNAME * TEST_COUNT;
#krate::export::run_tests::<JSON_SIZE_TOTAL>(&mut test_funcs[..]);
}
#init_fn
#(
#test_functions
)*
#(
#test_function_invokers
)*
})
.into())
}
#[derive(Clone, Copy)]
enum Attr {
Init,
Test,
}
struct Init {
func: ItemFn,
state: Option<Box<Type>>,
asyncness: bool,
}
struct Test {
func: ItemFn,
cfgs: Vec<Attribute>,
input: Option<Input>,
should_panic: bool,
ignore: bool,
asyncness: bool,
timeout: Option<u32>,
}
struct Input {
ty: Type,
}
struct TimeoutAttribute {
value: u32,
}
impl syn::parse::Parse for TimeoutAttribute {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let value_lit: syn::LitInt = input.parse()?;
let value = value_lit.base10_parse::<u32>()?;
Ok(TimeoutAttribute { value })
}
}
// NOTE doesn't check the parameters or the return type
fn check_fn_sig(sig: &syn::Signature) -> Result<(), ()> {
if sig.constness.is_none()
&& sig.unsafety.is_none()
&& sig.abi.is_none()
&& sig.generics.params.is_empty()
&& sig.generics.where_clause.is_none()
&& sig.variadic.is_none()
{
Ok(())
} else {
Err(())
}
}
fn get_arg_type(arg: &syn::FnArg) -> Option<&Type> {
if let syn::FnArg::Typed(pat) = arg {
match &*pat.ty {
syn::Type::Reference(_) => None,
_ => Some(&pat.ty),
}
} else {
None
}
}
fn extract_cfgs(attrs: &[Attribute]) -> Vec<Attribute> {
let mut cfgs = vec![];
for attr in attrs {
if attr.path().is_ident("cfg") {
cfgs.push(attr.clone());
}
}
cfgs
}
fn type_ident(ty: impl AsRef<syn::Type>) -> String {
let mut ident = String::new();
let ty = ty.as_ref();
let ty = format!("{}", quote!(#ty));
ty.split_whitespace().for_each(|t| ident.push_str(t));
ident
}
fn invoke(
func: &proc_macro2::Ident,
args: Vec<proc_macro2::TokenStream>,
asyncness: bool,
) -> proc_macro2::TokenStream {
if asyncness {
quote!(#func(#(#args),*).await)
} else {
quote!(#func(#(#args),*))
}
}