From ad88a0784b1d49bd0eb654a0330ac3f0a1747e4a Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Fri, 25 Feb 2022 19:39:09 -0800 Subject: [PATCH] Move test utils into separate module --- src/main.rs | 56 +++-------------------------------------------- src/test_utils.rs | 50 ++++++++++++++++++++++++++++++++++++++++++ src/vga_buffer.rs | 1 - 3 files changed, 53 insertions(+), 54 deletions(-) create mode 100644 src/test_utils.rs diff --git a/src/main.rs b/src/main.rs index f831e6f..0b6f0bb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,32 +2,22 @@ #![no_std] #![no_main] #![feature(custom_test_frameworks)] -#![test_runner(crate::test_runner)] +#![test_runner(crate::test_utils::test_runner)] #![reexport_test_harness_main = "test_main"] #[macro_use] mod vga_buffer; #[macro_use] mod serial; - -use core::panic::PanicInfo; +mod test_utils; #[cfg(not(test))] #[panic_handler] -fn panic(info: &PanicInfo) -> ! { +fn panic(info: &core::panic::PanicInfo) -> ! { println!("{info}"); loop {} } -#[cfg(test)] -#[panic_handler] -fn panic(info: &PanicInfo) -> ! { - serial_println!("[failed]\n"); - serial_println!("Error: {}", info); - exit_qemu(QemuExitCode::Failed); - loop {} -} - #[no_mangle] pub extern "C" fn _start() -> ! { #[cfg(test)] @@ -37,46 +27,6 @@ pub extern "C" fn _start() -> ! { 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 TestFunction for T -where - T: Fn(), -{ - fn run(&self) { - serial_print!("{}...\t", core::any::type_name::()); - 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] fn basic_test() { assert_eq!(5, 5); diff --git a/src/test_utils.rs b/src/test_utils.rs new file mode 100644 index 0000000..63142f4 --- /dev/null +++ b/src/test_utils.rs @@ -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 TestFunction for T +where + T: Fn(), +{ + fn run(&self) { + serial_print!("{}...\t", core::any::type_name::()); + 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, +} diff --git a/src/vga_buffer.rs b/src/vga_buffer.rs index aaa9e41..f64180b 100644 --- a/src/vga_buffer.rs +++ b/src/vga_buffer.rs @@ -168,4 +168,3 @@ lazy_static! { fn test_println_simple() { println!("Does this work?"); } -