Added templates to support integration tests

This commit is contained in:
Ishan Jain 2019-05-19 14:33:33 +05:30
parent 4d923d2bc7
commit c5ef2202a9
3 changed files with 86 additions and 38 deletions

59
src/lib.rs Normal file
View 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)
}

View File

@ -5,7 +5,7 @@
// Utilise custom test frameworks feature in rust, Since the default one doesn't works in no_std environments
#![feature(custom_test_frameworks)]
// 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
// 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
@ -29,26 +29,13 @@ pub fn panic(info: &PanicInfo) -> ! {
#[cfg(test)]
#[panic_handler]
pub fn panic(info: &PanicInfo) -> ! {
serial_println!("[failed]\n");
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,
fn panic(info: &PanicInfo) -> ! {
neptune::test_panic_handler(info)
}
#[no_mangle] // don't change the name of function in symbol table
pub extern "C" fn _start() -> ! {
use core::fmt::Write;
vga_buffer::WRITER.lock().write_str("Hello again").unwrap();
println!(", some numbers: {} {}", 42, 1.337);
println!("Hello! Here are some numbers: {} {}", 42, 1.337);
#[cfg(test)]
test_main();
@ -56,27 +43,6 @@ pub extern "C" fn _start() -> ! {
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]
fn trivial_assertion() {
print!("trivial assertion... ");

23
tests/basic_boot.rs Normal file
View 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);
}