From 553a625c910dc4e18a6f062b9eb418fa8396232e Mon Sep 17 00:00:00 2001 From: Gwilym Kuiper Date: Sat, 25 Jun 2022 22:09:57 +0100 Subject: [PATCH] Mkae it possible to run tests in target crates (and add a basic tests in hat-wizard) --- agb/Cargo.toml | 1 + agb/src/agb_alloc/block_allocator.rs | 3 +- agb/src/agb_alloc/mod.rs | 2 +- agb/src/lib.rs | 26 ++++++++--- .../the-hat-chooses-the-wizard/Cargo.toml | 3 ++ .../the-hat-chooses-the-wizard/src/main.rs | 45 ++++++++++++++++++- 6 files changed, 70 insertions(+), 10 deletions(-) diff --git a/agb/Cargo.toml b/agb/Cargo.toml index 403da505..2b03d9a9 100644 --- a/agb/Cargo.toml +++ b/agb/Cargo.toml @@ -19,6 +19,7 @@ debug = true default = [] freq18157 = ["agb_sound_converter/freq18157"] freq32768 = ["agb_sound_converter/freq32768"] +testing = [] [dependencies] bitflags = "1" diff --git a/agb/src/agb_alloc/block_allocator.rs b/agb/src/agb_alloc/block_allocator.rs index a3e51cbd..a601131e 100644 --- a/agb/src/agb_alloc/block_allocator.rs +++ b/agb/src/agb_alloc/block_allocator.rs @@ -58,7 +58,8 @@ impl BlockAllocator { } } - #[cfg(test)] + #[doc(hidden)] + #[cfg(any(test, feature = "testing"))] pub unsafe fn number_of_blocks(&self) -> u32 { free(|key| { let mut state = self.state.borrow(key).borrow_mut(); diff --git a/agb/src/agb_alloc/mod.rs b/agb/src/agb_alloc/mod.rs index 7357c17a..ecffeb72 100644 --- a/agb/src/agb_alloc/mod.rs +++ b/agb/src/agb_alloc/mod.rs @@ -42,7 +42,7 @@ static GLOBAL_ALLOC: BlockAllocator = unsafe { }) }; -#[cfg(test)] +#[cfg(any(test, feature = "testing"))] pub unsafe fn number_of_blocks() -> u32 { GLOBAL_ALLOC.number_of_blocks() } diff --git a/agb/src/lib.rs b/agb/src/lib.rs index 97f1159c..c1647fc4 100644 --- a/agb/src/lib.rs +++ b/agb/src/lib.rs @@ -1,9 +1,15 @@ #![no_std] // This appears to be needed for testing to work -#![cfg_attr(test, no_main)] -#![cfg_attr(test, feature(custom_test_frameworks))] -#![cfg_attr(test, test_runner(crate::test_runner::test_runner))] -#![cfg_attr(test, reexport_test_harness_main = "test_main")] +#![cfg_attr(any(test, feature = "testing"), no_main)] +#![cfg_attr(any(test, feature = "testing"), feature(custom_test_frameworks))] +#![cfg_attr( + any(test, feature = "testing"), + test_runner(crate::test_runner::test_runner) +)] +#![cfg_attr( + any(test, feature = "testing"), + reexport_test_harness_main = "test_main" +)] #![feature(alloc_error_handler)] #![warn(clippy::all)] #![deny(clippy::must_use_candidate)] @@ -183,7 +189,7 @@ pub mod syscall; /// Interactions with the internal timers pub mod timer; -#[cfg(not(test))] +#[cfg(not(any(test, feature = "testing")))] #[panic_handler] #[allow(unused_must_use)] fn panic_implementation(info: &core::panic::PanicInfo) -> ! { @@ -249,8 +255,9 @@ impl Gba { } } -#[cfg(test)] -mod test_runner { +#[cfg(any(test, feature = "testing"))] +#[doc(hidden)] +pub mod test_runner { use super::*; #[doc(hidden)] @@ -320,8 +327,13 @@ mod test_runner { .unwrap(); } + #[cfg(test)] #[entry] fn agb_test_main(gba: Gba) -> ! { + agb_start_tests(gba, test_main); + } + + pub fn agb_start_tests(gba: Gba, test_main: impl Fn()) -> ! { unsafe { TEST_GBA = Some(gba) }; test_main(); #[allow(clippy::empty_loop)] diff --git a/examples/the-hat-chooses-the-wizard/Cargo.toml b/examples/the-hat-chooses-the-wizard/Cargo.toml index bc9e160c..4c41d7d3 100644 --- a/examples/the-hat-chooses-the-wizard/Cargo.toml +++ b/examples/the-hat-chooses-the-wizard/Cargo.toml @@ -9,6 +9,9 @@ edition = "2018" [dependencies] agb = { version = "0.9.2", path = "../../agb" } +[dev-dependencies] +agb = { version = "0.9.2", path = "../../agb", features = ["testing"] } + [build-dependencies] serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" diff --git a/examples/the-hat-chooses-the-wizard/src/main.rs b/examples/the-hat-chooses-the-wizard/src/main.rs index 80071419..0b458fc5 100644 --- a/examples/the-hat-chooses-the-wizard/src/main.rs +++ b/examples/the-hat-chooses-the-wizard/src/main.rs @@ -1,5 +1,14 @@ #![no_std] #![no_main] +#![cfg_attr(test, feature(custom_test_frameworks))] +#![cfg_attr(test, reexport_test_harness_main = "test_main")] +#![cfg_attr(test, test_runner(agb::test_runner::test_runner))] + +#[cfg(test)] +#[agb::entry] +fn main(mut gba: agb::Gba) -> ! { + agb::test_runner::agb_start_tests(gba, test_main); +} extern crate alloc; @@ -775,8 +784,13 @@ impl<'a, 'b> PlayingLevel<'a, 'b> { } } +#[cfg(not(test))] #[agb::entry] -fn main(mut agb: agb::Gba) -> ! { +fn agb_main(mut gba: agb::Gba) -> ! { + main(gba); +} + +pub fn main(mut agb: agb::Gba) -> ! { let (tiled, mut vram) = agb.display.video.tiled0(); vram.set_background_palettes(tile_sheet::background.palettes); let mut splash_screen = tiled.background(Priority::P0, RegularBackgroundSize::Background32x32); @@ -958,3 +972,32 @@ fn main(mut agb: agb::Gba) -> ! { ); } } + +#[cfg(test)] +mod tests { + use super::*; + use agb::Gba; + + #[test_case] + fn test_ping_pong(_gba: &mut Gba) { + let test_cases = [ + [0, 2, 0], + [0, 7, 0], + [1, 2, 1], + [2, 2, 0], + [3, 2, 1], + [4, 2, 0], + ]; + + for test_case in test_cases { + assert_eq!( + ping_pong(test_case[0], test_case[1]), + test_case[2], + "Expected ping_pong({}, {}) to equal {}", + test_case[0], + test_case[1], + test_case[2], + ); + } + } +}