diff --git a/agb-macros/src/lib.rs b/agb-macros/src/lib.rs index 46e491ff..87dcaf6b 100644 --- a/agb-macros/src/lib.rs +++ b/agb-macros/src/lib.rs @@ -76,6 +76,7 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream { ); quote!( + #[cfg(not(test))] #[export_name = "main"] #(#attrs)* pub fn #fn_name() -> ! { @@ -83,6 +84,19 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream { #(#stmts)* } + + #[cfg(test)] + #[export_name = "main"] + #(#attrs)* + pub fn #fn_name() -> ! { + let mut #argument_name = unsafe { #argument_type ::new_in_entry() }; + + if cfg!(test) { + agb::test_runner::agb_start_tests(#argument_name, test_main); + } else { + #(#stmts)* + } + } ) .into() } diff --git a/agb/src/lib.rs b/agb/src/lib.rs index 083e5874..c14e79c3 100644 --- a/agb/src/lib.rs +++ b/agb/src/lib.rs @@ -265,22 +265,12 @@ impl Gba { /// #![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); -/// } /// ``` /// -/// And ensure that your main does not build if testing, so do something similar to: -/// -/// ``` -/// #[cfg(not(test))] -/// #[agb::entry] -/// fn main(mut gba: agb::Gba) -> ! { -/// // ... -/// } +/// And ensure you add agb with the `testing` feature to your `dev-dependencies` +/// ```toml +/// [dev-dependencies] +/// agb = { version = "", features = ["testing"] } /// ``` /// /// With this support, you will be able to write tests which you can run using `mgba-test-runner`. @@ -365,6 +355,13 @@ pub mod test_runner { .unwrap(); } + // needed to fudge the #[entry] below + mod agb { + pub mod test_runner { + pub use super::super::agb_start_tests; + } + } + #[cfg(test)] #[entry] fn agb_test_main(gba: Gba) -> ! { diff --git a/examples/the-hat-chooses-the-wizard/src/main.rs b/examples/the-hat-chooses-the-wizard/src/main.rs index 0b458fc5..ea363269 100644 --- a/examples/the-hat-chooses-the-wizard/src/main.rs +++ b/examples/the-hat-chooses-the-wizard/src/main.rs @@ -4,12 +4,6 @@ #![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; use agb::{