From 4ea3ab476959acd6ce6c642298f250e8af611e0b Mon Sep 17 00:00:00 2001 From: Ian Chamberlain Date: Mon, 31 Jan 2022 21:45:48 -0500 Subject: [PATCH] First pass of using custom test runner It works! --- ctru-rs/src/lib.rs | 6 +++++ ctru-rs/src/test_runner.rs | 46 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 ctru-rs/src/test_runner.rs diff --git a/ctru-rs/src/lib.rs b/ctru-rs/src/lib.rs index 5f487a9..09bd316 100644 --- a/ctru-rs/src/lib.rs +++ b/ctru-rs/src/lib.rs @@ -1,5 +1,8 @@ #![crate_type = "rlib"] #![crate_name = "ctru"] +#![feature(test)] +#![feature(custom_test_frameworks)] +#![test_runner(test_runner::test_runner)] /// Call this somewhere to force Rust to link some required crates /// This is also a setup for some crate integration only available at runtime @@ -61,6 +64,9 @@ cfg_if::cfg_if! { } } +#[cfg(test)] +mod test_runner; + pub use crate::error::{Error, Result}; pub use crate::gfx::Gfx; diff --git a/ctru-rs/src/test_runner.rs b/ctru-rs/src/test_runner.rs new file mode 100644 index 0000000..6ce2955 --- /dev/null +++ b/ctru-rs/src/test_runner.rs @@ -0,0 +1,46 @@ +//! Custom test runner for building/running unit tests on the 3DS. + +extern crate test; + +use test::TestFn; + +use crate::console::Console; +use crate::gfx::Gfx; +use crate::services::hid::{Hid, KeyPad}; + +/// A custom runner to be used with `#[test_runner]`. This simple implementation +/// runs all tests in series, "failing" on the first one to panic (really, the +/// panic is just treated the same as any normal application panic). +pub(crate) fn test_runner(test_cases: &[&test::TestDescAndFn]) { + crate::init(); + + let gfx = Gfx::default(); + let hid = Hid::init().expect("Couldn't obtain HID controller"); + let _console = Console::init(gfx.top_screen.borrow_mut()); + + // TODO: may want to use some more features of standard testing framework, + // like output capture, filtering, panic handling, etc. + // For now this is works without too much setup. + for test_info in test_cases { + if let TestFn::StaticTestFn(testfn) = test_info.testfn { + println!("Running test {}", test_info.desc.name); + testfn(); + } else { + println!( + "unsupported test type for {}: {:?}", + test_info.desc.name, test_info.testfn + ); + } + } + + println!("All tests passed! Press START to exit."); + + // TODO: do we need apt.main_loop() here? + loop { + hid.scan_input(); + + if hid.keys_down().contains(KeyPad::KEY_START) { + break; + } + } +}