Compare commits

..

No commits in common. "cda022e6f09c81781559fc8c3e4ea2678e08c712" and "f6a8a78b6c5dbda0d1c86e61547abffb1be30622" have entirely different histories.

5 changed files with 27 additions and 104 deletions

View file

@ -1,85 +1,28 @@
extern crate proc_macro; extern crate proc_macro;
extern crate quote; extern crate quote;
extern crate syn; extern crate syn;
use { use {
proc_macro::TokenStream, proc_macro::TokenStream,
quote::quote, quote::quote,
syn::{parse::Parse, parse_macro_input, Expr, ItemFn, Token} syn::{parse_macro_input, ItemFn}
}; };
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)
}
#[proc_macro_attribute] #[proc_macro_attribute]
pub fn ktest(_attr: TokenStream, item: TokenStream) -> TokenStream { pub fn ktest(_attr: TokenStream, item: TokenStream) -> TokenStream {
let input = parse_macro_input!(item as ItemFn); let input = parse_macro_input!(item as ItemFn);
let test_name = &input.sig.ident; let test_name = &input.sig.ident;
let test_string = test_name.to_string();
let static_var_name = syn::Ident::new( let static_var_name = syn::Ident::new(
&format!("__ktest_{}", test_name).to_uppercase(), &format!("__ktest_{}", test_name),
test_name.span(), test_name.span(),
); );
let block = &input.block;
let out = quote! { let out = quote! {
#[cfg(feature = "ktest")] // #[cfg(feature = "ktest")]
fn #test_name() -> Result<String, String> { #input
use crate::alloc::string::ToString;
let name = #test_string.to_string();
#block // #[cfg(feature = "ktest")]
return Ok(name);
}
#[cfg(feature = "ktest")]
#[unsafe(link_section = ".note.ktest")] #[unsafe(link_section = ".note.ktest")]
#[used] #[used]
pub static #static_var_name: fn() -> Result<String, String> = #test_name; pub static #static_var_name: fn() = #test_name;
}; };
TokenStream::from(out) TokenStream::from(out)

View file

@ -6,11 +6,6 @@ SECTIONS
.text.boot : { *(.text.boot) } .text.boot : { *(.text.boot) }
.text : { *(.text) } .text : { *(.text) }
.data : { *(.data) } .data : { *(.data) }
.note.ktest : {
__ktest_start = .;
*(.note.ktest)
__ktest_end = .;
}
.rodata : { *(.rodata) } .rodata : { *(.rodata) }
.bss : { .bss : {
*(COMMON) *(COMMON)

View file

@ -24,11 +24,8 @@ pub fn kmain(_cmdline: &str, boot_modules: BootModules) -> ! {
#[cfg(feature = "ktest")] #[cfg(feature = "ktest")]
{ {
use { use crate::ktest;
crate::ktest, debug!("TESTING");
log::info,
};
info!("Running tests");
ktest::test_main(); ktest::test_main();
loop {} loop {}

View file

@ -1,51 +1,38 @@
pub use ktest_macro::*; pub use ktest_macro::ktest;
use log::debug;
use {
alloc::string::String,
log::{error, info},
};
extern "C" { extern "C" {
static __ktest_start: fn() -> Result<String, String>; static __ktest_start: fn();
static __ktest_end: fn() -> Result<String, String>; static __ktest_end: fn();
} }
// TODO: Implement ktest for arm and riscv (Later problems, see below) // TODO: Get test_fn linker name (may require no_mangle in macro)
// More info on tests (run the rest even if panic)
// Implement ktest for arm and riscv (Later problems, see below)
// Allow for arch specific tests (Leave for now) // Allow for arch specific tests (Leave for now)
// Should panic tests // Allow for ktest test name attr
// Test specific panic handler // Usefull message at the end of testing
pub fn test_main() { pub fn test_main() {
unsafe { unsafe {
let mut current_test = &__ktest_start as *const fn() -> Result<String, String>; let mut current_test = &__ktest_start as *const fn();
let test_end = &__ktest_end as *const fn() -> Result<String, String>; let mut current = 1;
let test_end = &__ktest_end as *const fn();
let mut pass = 0;
let mut fail = 0;
while current_test < test_end { while current_test < test_end {
let test_fn = *current_test; let test_fn = *current_test;
let test_name = test_fn(); debug!("Running test {}", current);
match test_name {
Ok(name) => { test_fn();
info!("Test: {} passed", name); debug!("Test {} passed", current);
pass += 1;
},
Err(name) => {
error!("Test: {} failed", name);
fail += 1;
}
}
current_test = current_test.add(1); current_test = current_test.add(1);
current += 1;
} }
info!("{}/{} tests passed", pass, pass + fail);
} }
} }
#[ktest] #[ktest]
pub fn trivial_assertion() { pub fn trivial_assertion() {
ktest_eq!(1, 1); assert_eq!(1, 1);
ktest_neq!(0, 1);
} }

View file

@ -34,6 +34,7 @@ mod memory;
mod task; mod task;
mod utils; mod utils;
// #[cfg(feature = "tests")]
#[allow(improper_ctypes, non_upper_case_globals)] #[allow(improper_ctypes, non_upper_case_globals)]
mod ktest; mod ktest;