Mkae it possible to run tests in target crates (and add a basic tests in hat-wizard)

This commit is contained in:
Gwilym Kuiper 2022-06-25 22:09:57 +01:00
parent 79e78b7fc3
commit 553a625c91
6 changed files with 70 additions and 10 deletions

View file

@ -19,6 +19,7 @@ debug = true
default = [] default = []
freq18157 = ["agb_sound_converter/freq18157"] freq18157 = ["agb_sound_converter/freq18157"]
freq32768 = ["agb_sound_converter/freq32768"] freq32768 = ["agb_sound_converter/freq32768"]
testing = []
[dependencies] [dependencies]
bitflags = "1" bitflags = "1"

View file

@ -58,7 +58,8 @@ impl BlockAllocator {
} }
} }
#[cfg(test)] #[doc(hidden)]
#[cfg(any(test, feature = "testing"))]
pub unsafe fn number_of_blocks(&self) -> u32 { pub unsafe fn number_of_blocks(&self) -> u32 {
free(|key| { free(|key| {
let mut state = self.state.borrow(key).borrow_mut(); let mut state = self.state.borrow(key).borrow_mut();

View file

@ -42,7 +42,7 @@ static GLOBAL_ALLOC: BlockAllocator = unsafe {
}) })
}; };
#[cfg(test)] #[cfg(any(test, feature = "testing"))]
pub unsafe fn number_of_blocks() -> u32 { pub unsafe fn number_of_blocks() -> u32 {
GLOBAL_ALLOC.number_of_blocks() GLOBAL_ALLOC.number_of_blocks()
} }

View file

@ -1,9 +1,15 @@
#![no_std] #![no_std]
// This appears to be needed for testing to work // This appears to be needed for testing to work
#![cfg_attr(test, no_main)] #![cfg_attr(any(test, feature = "testing"), no_main)]
#![cfg_attr(test, feature(custom_test_frameworks))] #![cfg_attr(any(test, feature = "testing"), feature(custom_test_frameworks))]
#![cfg_attr(test, test_runner(crate::test_runner::test_runner))] #![cfg_attr(
#![cfg_attr(test, reexport_test_harness_main = "test_main")] 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)] #![feature(alloc_error_handler)]
#![warn(clippy::all)] #![warn(clippy::all)]
#![deny(clippy::must_use_candidate)] #![deny(clippy::must_use_candidate)]
@ -183,7 +189,7 @@ pub mod syscall;
/// Interactions with the internal timers /// Interactions with the internal timers
pub mod timer; pub mod timer;
#[cfg(not(test))] #[cfg(not(any(test, feature = "testing")))]
#[panic_handler] #[panic_handler]
#[allow(unused_must_use)] #[allow(unused_must_use)]
fn panic_implementation(info: &core::panic::PanicInfo) -> ! { fn panic_implementation(info: &core::panic::PanicInfo) -> ! {
@ -249,8 +255,9 @@ impl Gba {
} }
} }
#[cfg(test)] #[cfg(any(test, feature = "testing"))]
mod test_runner { #[doc(hidden)]
pub mod test_runner {
use super::*; use super::*;
#[doc(hidden)] #[doc(hidden)]
@ -320,8 +327,13 @@ mod test_runner {
.unwrap(); .unwrap();
} }
#[cfg(test)]
#[entry] #[entry]
fn agb_test_main(gba: Gba) -> ! { 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) }; unsafe { TEST_GBA = Some(gba) };
test_main(); test_main();
#[allow(clippy::empty_loop)] #[allow(clippy::empty_loop)]

View file

@ -9,6 +9,9 @@ edition = "2018"
[dependencies] [dependencies]
agb = { version = "0.9.2", path = "../../agb" } agb = { version = "0.9.2", path = "../../agb" }
[dev-dependencies]
agb = { version = "0.9.2", path = "../../agb", features = ["testing"] }
[build-dependencies] [build-dependencies]
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"

View file

@ -1,5 +1,14 @@
#![no_std] #![no_std]
#![no_main] #![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; extern crate alloc;
@ -775,8 +784,13 @@ impl<'a, 'b> PlayingLevel<'a, 'b> {
} }
} }
#[cfg(not(test))]
#[agb::entry] #[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(); let (tiled, mut vram) = agb.display.video.tiled0();
vram.set_background_palettes(tile_sheet::background.palettes); vram.set_background_palettes(tile_sheet::background.palettes);
let mut splash_screen = tiled.background(Priority::P0, RegularBackgroundSize::Background32x32); 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],
);
}
}
}