From f98145d556665098ee0cef8b731bd79d0ca80d69 Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Wed, 7 Aug 2019 10:24:48 +0200 Subject: [PATCH 1/3] Delete panic handler test --- Cargo.toml | 4 --- tests/panic_handler.rs | 74 ------------------------------------------ 2 files changed, 78 deletions(-) delete mode 100644 tests/panic_handler.rs diff --git a/Cargo.toml b/Cargo.toml index 76dae168..a4da7a6e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,10 +4,6 @@ version = "0.1.0" authors = ["Philipp Oppermann "] edition = "2018" -[[test]] -name = "panic_handler" -harness = false - [dependencies] bootloader = "0.6.4" volatile = "0.2.3" diff --git a/tests/panic_handler.rs b/tests/panic_handler.rs deleted file mode 100644 index ecb8fd9d..00000000 --- a/tests/panic_handler.rs +++ /dev/null @@ -1,74 +0,0 @@ -#![no_std] -#![no_main] -#![feature(panic_info_message)] - -use blog_os::{exit_qemu, serial_print, serial_println, QemuExitCode}; -use core::{ - fmt::{self, Write}, - panic::PanicInfo, -}; - -const MESSAGE: &str = "Example panic message from panic_handler test"; -const PANIC_LINE: u32 = 17; // adjust this when moving the `panic!` call - -#[no_mangle] -pub extern "C" fn _start() -> ! { - serial_print!("panic_handler... "); - panic!(MESSAGE); // must be in line `PANIC_LINE` -} - -#[panic_handler] -fn panic(info: &PanicInfo) -> ! { - check_location(info); - check_message(info); - - serial_println!("[ok]"); - exit_qemu(QemuExitCode::Success); - loop {} -} - -fn fail(error: &str) -> ! { - serial_println!("[failed]"); - serial_println!("{}", error); - exit_qemu(QemuExitCode::Failed); - loop {} -} - -fn check_location(info: &PanicInfo) { - let location = info.location().unwrap_or_else(|| fail("no location")); - if location.file() != file!() { - fail("file name wrong"); - } - if location.line() != PANIC_LINE { - fail("file line wrong"); - } -} - -fn check_message(info: &PanicInfo) { - let message = info.message().unwrap_or_else(|| fail("no message")); - let mut compare_message = CompareMessage { expected: MESSAGE }; - write!(&mut compare_message, "{}", message).unwrap_or_else(|_| fail("write failed")); - if compare_message.expected.len() != 0 { - fail("message shorter than expected message"); - } -} - -/// Compares a `fmt::Arguments` instance with the `MESSAGE` string -/// -/// To use this type, write the `fmt::Arguments` instance to it using the -/// `write` macro. If the message component matches `MESSAGE`, the `expected` -/// field is the empty string. -struct CompareMessage { - expected: &'static str, -} - -impl fmt::Write for CompareMessage { - fn write_str(&mut self, s: &str) -> fmt::Result { - if self.expected.starts_with(s) { - self.expected = &self.expected[s.len()..]; - } else { - fail("message not equal to expected message"); - } - Ok(()) - } -} From aad98c0611a8eb5780b68f819b1b923b288f841b Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Wed, 7 Aug 2019 11:00:01 +0200 Subject: [PATCH 2/3] Add a should_panic test --- tests/should_panic.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 tests/should_panic.rs diff --git a/tests/should_panic.rs b/tests/should_panic.rs new file mode 100644 index 00000000..3fe187cd --- /dev/null +++ b/tests/should_panic.rs @@ -0,0 +1,38 @@ +#![no_std] +#![no_main] +#![feature(custom_test_frameworks)] +#![test_runner(test_runner)] +#![reexport_test_harness_main = "test_main"] + +use blog_os::{exit_qemu, serial_print, serial_println, QemuExitCode}; +use core::panic::PanicInfo; + +#[no_mangle] +pub extern "C" fn _start() -> ! { + test_main(); + + loop {} +} + +pub fn test_runner(tests: &[&dyn Fn()]) { + serial_println!("Running {} tests", tests.len()); + if let Some(test) = tests.first() { + test(); + serial_println!("[test did not panic]"); + exit_qemu(QemuExitCode::Failed); + } + exit_qemu(QemuExitCode::Success); +} + +#[panic_handler] +fn panic(_info: &PanicInfo) -> ! { + serial_println!("[ok]"); + exit_qemu(QemuExitCode::Success); + loop {} +} + +#[test_case] +fn should_fail() { + serial_print!("should_fail... "); + assert_eq!(0, 1); +} From f1ae82b220dc6a383dcb6c23a4940e33557b2434 Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Wed, 7 Aug 2019 11:02:19 +0200 Subject: [PATCH 3/3] Disable the test harness for the should_panic test --- Cargo.toml | 4 ++++ tests/should_panic.rs | 25 ++++++------------------- 2 files changed, 10 insertions(+), 19 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a4da7a6e..6eeea60f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,10 @@ version = "0.1.0" authors = ["Philipp Oppermann "] edition = "2018" +[[test]] +name = "should_panic" +harness = false + [dependencies] bootloader = "0.6.4" volatile = "0.2.3" diff --git a/tests/should_panic.rs b/tests/should_panic.rs index 3fe187cd..d624bcbe 100644 --- a/tests/should_panic.rs +++ b/tests/should_panic.rs @@ -1,27 +1,20 @@ #![no_std] #![no_main] -#![feature(custom_test_frameworks)] -#![test_runner(test_runner)] -#![reexport_test_harness_main = "test_main"] use blog_os::{exit_qemu, serial_print, serial_println, QemuExitCode}; use core::panic::PanicInfo; #[no_mangle] pub extern "C" fn _start() -> ! { - test_main(); - + should_fail(); + serial_println!("[test did not panic]"); + exit_qemu(QemuExitCode::Failed); loop {} } -pub fn test_runner(tests: &[&dyn Fn()]) { - serial_println!("Running {} tests", tests.len()); - if let Some(test) = tests.first() { - test(); - serial_println!("[test did not panic]"); - exit_qemu(QemuExitCode::Failed); - } - exit_qemu(QemuExitCode::Success); +fn should_fail() { + serial_print!("should_fail... "); + assert_eq!(0, 1); } #[panic_handler] @@ -30,9 +23,3 @@ fn panic(_info: &PanicInfo) -> ! { exit_qemu(QemuExitCode::Success); loop {} } - -#[test_case] -fn should_fail() { - serial_print!("should_fail... "); - assert_eq!(0, 1); -}