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_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
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() {
|
fn test_println_simple() {
|
||||||
println!("Does this work?");
|
println!("Does this work?");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user