Move test utils into separate module

This commit is contained in:
Greg Shuflin 2022-02-25 19:39:09 -08:00
parent 56f003e8cc
commit ad88a0784b
3 changed files with 53 additions and 54 deletions

View File

@ -2,32 +2,22 @@
#![no_std] #![no_std]
#![no_main] #![no_main]
#![feature(custom_test_frameworks)] #![feature(custom_test_frameworks)]
#![test_runner(crate::test_runner)] #![test_runner(crate::test_utils::test_runner)]
#![reexport_test_harness_main = "test_main"] #![reexport_test_harness_main = "test_main"]
#[macro_use] #[macro_use]
mod vga_buffer; mod vga_buffer;
#[macro_use] #[macro_use]
mod serial; mod serial;
mod test_utils;
use core::panic::PanicInfo;
#[cfg(not(test))] #[cfg(not(test))]
#[panic_handler] #[panic_handler]
fn panic(info: &PanicInfo) -> ! { fn panic(info: &core::panic::PanicInfo) -> ! {
println!("{info}"); println!("{info}");
loop {} loop {}
} }
#[cfg(test)]
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
serial_println!("[failed]\n");
serial_println!("Error: {}", info);
exit_qemu(QemuExitCode::Failed);
loop {}
}
#[no_mangle] #[no_mangle]
pub extern "C" fn _start() -> ! { pub extern "C" fn _start() -> ! {
#[cfg(test)] #[cfg(test)]
@ -37,46 +27,6 @@ pub extern "C" fn _start() -> ! {
loop {} loop {}
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u32)]
pub enum QemuExitCode {
Success = 0x10,
Failed = 0x11,
}
pub fn exit_qemu(exit_code: QemuExitCode) {
use x86_64::instructions::port::Port;
unsafe {
let mut port = Port::new(0xf4); // isa-debug-exit port, see Cargo.toml
port.write(exit_code as u32);
}
}
pub trait TestFunction {
fn run(&self) -> ();
}
impl<T> TestFunction for T
where
T: Fn(),
{
fn run(&self) {
serial_print!("{}...\t", core::any::type_name::<T>());
self();
serial_println!("[ok]");
}
}
#[cfg(test)]
fn test_runner(tests: &[&dyn TestFunction]) {
serial_println!("Running {} test(s)", tests.len());
for test in tests {
test.run();
}
exit_qemu(QemuExitCode::Success);
}
#[test_case] #[test_case]
fn basic_test() { fn basic_test() {
assert_eq!(5, 5); assert_eq!(5, 5);

50
src/test_utils.rs Normal file
View File

@ -0,0 +1,50 @@
#![cfg(test)]
use core::panic::PanicInfo;
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
serial_println!("[failed]\n");
serial_println!("Error: {}", info);
exit_qemu(QemuExitCode::Failed);
loop {}
}
pub fn test_runner(tests: &[&dyn TestFunction]) {
serial_println!("Running {} test(s)", tests.len());
for test in tests {
test.run();
}
exit_qemu(QemuExitCode::Success);
}
pub trait TestFunction {
fn run(&self) -> ();
}
impl<T> TestFunction for T
where
T: Fn(),
{
fn run(&self) {
serial_print!("{}...\t", core::any::type_name::<T>());
self();
serial_println!("[ok]");
}
}
pub fn exit_qemu(exit_code: QemuExitCode) {
use x86_64::instructions::port::Port;
unsafe {
let mut port = Port::new(0xf4); // isa-debug-exit port, see Cargo.toml
port.write(exit_code as u32);
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u32)]
pub enum QemuExitCode {
Success = 0x10,
Failed = 0x11,
}

View File

@ -168,4 +168,3 @@ lazy_static! {
fn test_println_simple() { fn test_println_simple() {
println!("Does this work?"); println!("Does this work?");
} }