2024-11-23 18:21:39 -06:00
|
|
|
extern crate proc_macro;
|
|
|
|
extern crate quote;
|
|
|
|
extern crate syn;
|
2024-11-27 02:09:15 -06:00
|
|
|
|
2024-11-23 18:21:39 -06:00
|
|
|
use {
|
|
|
|
proc_macro::TokenStream,
|
|
|
|
quote::quote,
|
2024-11-27 02:09:15 -06:00
|
|
|
syn::{parse::Parse, parse_macro_input, Expr, ItemFn, Token}
|
2024-11-23 18:21:39 -06:00
|
|
|
};
|
|
|
|
|
2024-11-27 02:09:15 -06:00
|
|
|
struct KtestInput {
|
|
|
|
lhs: Expr,
|
|
|
|
_comma: Token![,],
|
|
|
|
rhs: Expr,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Parse for KtestInput {
|
|
|
|
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
|
|
|
|
Ok(Self {
|
|
|
|
lhs: input.parse()?,
|
|
|
|
_comma: input.parse()?,
|
|
|
|
rhs: input.parse()?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[proc_macro]
|
|
|
|
pub fn ktest_eq(item: TokenStream) -> TokenStream {
|
|
|
|
let input = parse_macro_input!(item as KtestInput);
|
|
|
|
|
|
|
|
let lhs = input.lhs;
|
|
|
|
let rhs = input.rhs;
|
|
|
|
|
|
|
|
let out = quote! {
|
|
|
|
if #lhs != #rhs {
|
|
|
|
return Err(name);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
TokenStream::from(out)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[proc_macro]
|
|
|
|
pub fn ktest_neq(item: TokenStream) -> TokenStream {
|
|
|
|
let input = parse_macro_input!(item as KtestInput);
|
|
|
|
|
|
|
|
let lhs = input.lhs;
|
|
|
|
let rhs = input.rhs;
|
|
|
|
|
|
|
|
let out = quote! {
|
|
|
|
if #lhs == #rhs {
|
|
|
|
return Err(name);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
TokenStream::from(out)
|
|
|
|
}
|
|
|
|
|
2024-11-23 18:21:39 -06:00
|
|
|
#[proc_macro_attribute]
|
|
|
|
pub fn ktest(_attr: TokenStream, item: TokenStream) -> TokenStream {
|
|
|
|
let input = parse_macro_input!(item as ItemFn);
|
|
|
|
let test_name = &input.sig.ident;
|
2024-11-27 02:09:15 -06:00
|
|
|
let test_string = test_name.to_string();
|
2024-11-23 18:21:39 -06:00
|
|
|
let static_var_name = syn::Ident::new(
|
2024-11-27 02:09:15 -06:00
|
|
|
&format!("__ktest_{}", test_name).to_uppercase(),
|
2024-11-23 18:21:39 -06:00
|
|
|
test_name.span(),
|
|
|
|
);
|
2024-11-27 02:09:15 -06:00
|
|
|
|
|
|
|
let block = &input.block;
|
2024-11-23 18:21:39 -06:00
|
|
|
let out = quote! {
|
2024-11-27 02:09:15 -06:00
|
|
|
#[cfg(feature = "ktest")]
|
|
|
|
fn #test_name() -> Result<String, String> {
|
|
|
|
use crate::alloc::string::ToString;
|
|
|
|
let name = #test_string.to_string();
|
|
|
|
|
|
|
|
#block
|
|
|
|
|
|
|
|
return Ok(name);
|
|
|
|
}
|
2024-11-23 18:21:39 -06:00
|
|
|
|
2024-11-27 02:09:15 -06:00
|
|
|
#[cfg(feature = "ktest")]
|
2024-11-23 18:21:39 -06:00
|
|
|
#[unsafe(link_section = ".note.ktest")]
|
|
|
|
#[used]
|
2024-11-27 02:09:15 -06:00
|
|
|
pub static #static_var_name: fn() -> Result<String, String> = #test_name;
|
2024-11-23 18:21:39 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
TokenStream::from(out)
|
|
|
|
}
|