integration testing

This commit is contained in:
clizia 2025-08-31 21:26:59 +02:00
parent 386522208e
commit a3005ff146
3 changed files with 98 additions and 61 deletions

75
src/lib.rs Normal file
View file

@ -0,0 +1,75 @@
#![no_std]
#![cfg_attr(test, no_main)]
#![feature(custom_test_frameworks)]
#![test_runner(crate::test_runner)]
#![reexport_test_harness_main = "test_main"]
use core::panic::PanicInfo;
pub mod vga_buffer;
pub mod serial;
pub trait Testable {
fn run(&self) -> ();
}
impl<T> Testable for T
where
T: Fn(),
{
fn run(&self) -> () {
serial_print!("{}...\t", core::any::type_name::<T>());
self();
serial_println!("[ok]");
}
}
pub fn test_runner(tests: &[&dyn Testable]) {
serial_println!("Running {} tests", tests.len());
for test in tests {
test.run();
}
exit_qemu(QemuExitCode::Success);
}
pub fn test_panic_handler(info: &PanicInfo) -> ! {
serial_println!("[failed]\n");
serial_println!("Error: {}\n", info);
exit_qemu(QemuExitCode::Failed);
loop {}
}
/// entry point for `cargo test`
#[cfg(test)]
#[unsafe(no_mangle)]
pub extern "C" fn _start() -> ! {
test_main();
loop {}
}
#[cfg(test)]
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
test_panic_handler(info);
}
#[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);
port.write(exit_code as u32);
}
}

View file

@ -1,47 +1,15 @@
#![no_std] // dont link the standard library
#![no_main] // disable all rust-level entry points
#![feature(custom_test_frameworks)]
#![test_runner(crate::test_runner)]
#![test_runner(totos::test_runner)]
#![reexport_test_harness_main = "test_main"]
use core::panic::PanicInfo;
use totos::println;
mod vga_buffer;
mod serial;
pub trait Testable {
fn run(&self) -> ();
}
impl<T> Testable for T
where
T: Fn(),
{
fn run(&self) -> () {
serial_print!("{}...\t", core::any::type_name::<T>());
self();
serial_println!("[ok]");
}
}
// this is here because i dont want to remove it :>
static HELLO: &[u8] = b"Hello toto :3";
#[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);
port.write(exit_code as u32);
}
}
// this function is the entry point since the linker
// looks for a function named `_start` by default
#[unsafe(no_mangle)]
@ -54,35 +22,10 @@ pub extern "C" fn _start() -> ! {
loop {}
}
// panic handler
#[cfg(not(test))]
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
println!("{}", info);
loop {}
}
// panic handler in test mode
#[cfg(test)]
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
serial_println!("[failed]\n");
serial_println!("Error: {}\n", info);
exit_qemu(QemuExitCode::Failed);
loop {}
}
#[cfg(test)]
pub fn test_runner(tests: &[&dyn Testable]) {
serial_println!("Running {} tests", tests.len());
for test in tests {
test.run();
}
exit_qemu(QemuExitCode::Success);
totos::test_panic_handler(info)
}
#[test_case]

19
tests/basic_boot.rs Normal file
View file

@ -0,0 +1,19 @@
#![no_std]
#![no_main]
#![feature(custom_test_frameworks)]
#![test_runner(totos::test_runner)]
#![reexport_test_harness_main = "test_main"]
use core::panic::PanicInfo;
#[unsafe(no_mangle)]
pub extern "C" fn _start() -> ! {
test_main();
loop {}
}
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
totos::test_panic_handler(info)
}