Move test utils into separate module
This commit is contained in:
parent
56f003e8cc
commit
ad88a0784b
56
src/main.rs
56
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<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]
|
||||
fn basic_test() {
|
||||
assert_eq!(5, 5);
|
||||
|
50
src/test_utils.rs
Normal file
50
src/test_utils.rs
Normal 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,
|
||||
}
|
@ -168,4 +168,3 @@ lazy_static! {
|
||||
fn test_println_simple() {
|
||||
println!("Does this work?");
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user