Added templates to support integration tests
This commit is contained in:
parent
4d923d2bc7
commit
c5ef2202a9
59
src/lib.rs
Normal file
59
src/lib.rs
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
#![no_std]
|
||||||
|
#![cfg_attr(test, no_main)]
|
||||||
|
#![feature(custom_test_frameworks)]
|
||||||
|
#![test_runner(crate::test_runner)]
|
||||||
|
#![reexport_test_harness_main = "test_main"]
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
extern crate lazy_static;
|
||||||
|
|
||||||
|
pub mod serial;
|
||||||
|
pub mod vga_buffer;
|
||||||
|
|
||||||
|
use core::panic::PanicInfo;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
#[repr(u32)]
|
||||||
|
// u32 because iosize is set to 4 bytes
|
||||||
|
pub enum QemuExitCode {
|
||||||
|
Success = 0x10,
|
||||||
|
Failed = 0x11,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn exit_qemu(exit_code: QemuExitCode) {
|
||||||
|
use x86_64::instructions::port::Port;
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
// iobase address
|
||||||
|
let mut port = Port::new(0xf4);
|
||||||
|
port.write(exit_code as u32);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn test_runner(tests: &[&dyn Fn()]) {
|
||||||
|
serial_println!("Running {} test", tests.len());
|
||||||
|
for test in tests {
|
||||||
|
test();
|
||||||
|
}
|
||||||
|
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 {}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern "C" fn _start() -> ! {
|
||||||
|
test_main();
|
||||||
|
loop {}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
#[panic_handler]
|
||||||
|
fn panic(info: &PanicInfo) -> ! {
|
||||||
|
test_panic_handler(info)
|
||||||
|
}
|
42
src/main.rs
42
src/main.rs
|
@ -5,7 +5,7 @@
|
||||||
// Utilise custom test frameworks feature in rust, Since the default one doesn't works in no_std environments
|
// Utilise custom test frameworks feature in rust, Since the default one doesn't works in no_std environments
|
||||||
#![feature(custom_test_frameworks)]
|
#![feature(custom_test_frameworks)]
|
||||||
// Specify the runner, aka, The function that will execute all the tests
|
// Specify the runner, aka, The function that will execute all the tests
|
||||||
#![test_runner(crate::test_runner)]
|
#![test_runner(neptune::test_runner)]
|
||||||
// custom test frameworks creates an entry point called `main`. Since we are using no_main and have
|
// custom test frameworks creates an entry point called `main`. Since we are using no_main and have
|
||||||
// set a custom entry point, The entry point created by custom test framework gets ignored.
|
// set a custom entry point, The entry point created by custom test framework gets ignored.
|
||||||
// This changes the name of the entry point to test_main which can then be called in _start
|
// This changes the name of the entry point to test_main which can then be called in _start
|
||||||
|
@ -29,26 +29,13 @@ pub fn panic(info: &PanicInfo) -> ! {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
#[panic_handler]
|
#[panic_handler]
|
||||||
pub fn panic(info: &PanicInfo) -> ! {
|
fn panic(info: &PanicInfo) -> ! {
|
||||||
serial_println!("[failed]\n");
|
neptune::test_panic_handler(info)
|
||||||
serial_println!("Error: {}\n", info);
|
|
||||||
exit_qemu(QemuExitCode::Failed);
|
|
||||||
loop {}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
||||||
#[repr(u32)]
|
|
||||||
// u32 because iosize is set to 4 bytes
|
|
||||||
pub enum QemuExitCode {
|
|
||||||
Success = 0x10,
|
|
||||||
Failed = 0x11,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle] // don't change the name of function in symbol table
|
#[no_mangle] // don't change the name of function in symbol table
|
||||||
pub extern "C" fn _start() -> ! {
|
pub extern "C" fn _start() -> ! {
|
||||||
use core::fmt::Write;
|
println!("Hello! Here are some numbers: {} {}", 42, 1.337);
|
||||||
vga_buffer::WRITER.lock().write_str("Hello again").unwrap();
|
|
||||||
println!(", some numbers: {} {}", 42, 1.337);
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
test_main();
|
test_main();
|
||||||
|
@ -56,27 +43,6 @@ pub extern "C" fn _start() -> ! {
|
||||||
loop {}
|
loop {}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn exit_qemu(exit_code: QemuExitCode) {
|
|
||||||
use x86_64::instructions::port::Port;
|
|
||||||
|
|
||||||
unsafe {
|
|
||||||
// iobase address
|
|
||||||
let mut port = Port::new(0xf4);
|
|
||||||
port.write(exit_code as u32);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
fn test_runner(tests: &[&dyn Fn()]) {
|
|
||||||
println!("Running {} tests", tests.len());
|
|
||||||
serial_println!("Running {} tests", tests.len());
|
|
||||||
|
|
||||||
for test in tests {
|
|
||||||
test();
|
|
||||||
}
|
|
||||||
exit_qemu(QemuExitCode::Success);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test_case]
|
#[test_case]
|
||||||
fn trivial_assertion() {
|
fn trivial_assertion() {
|
||||||
print!("trivial assertion... ");
|
print!("trivial assertion... ");
|
||||||
|
|
23
tests/basic_boot.rs
Normal file
23
tests/basic_boot.rs
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
#![no_std]
|
||||||
|
#![no_main]
|
||||||
|
#![feature(custom_test_frameworks)]
|
||||||
|
#![test_runner(neptune::test_runner)]
|
||||||
|
#![reexport_test_harness_main = "test_main"]
|
||||||
|
|
||||||
|
use core::panic::PanicInfo;
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern "C" fn _start() -> ! {
|
||||||
|
test_main();
|
||||||
|
|
||||||
|
loop {}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_runner(tests: &[&dyn Fn()]) {
|
||||||
|
unimplemented!();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[panic_handler]
|
||||||
|
fn panic(info: &PanicInfo) -> ! {
|
||||||
|
neptune::test_panic_handler(info);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user