Remove the need for a special test implementation

This commit is contained in:
Gwilym Kuiper 2022-07-03 17:52:47 +01:00
parent 9231d16071
commit 2e505f9684
3 changed files with 25 additions and 20 deletions

View file

@ -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()
}

View file

@ -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 = "<same as in dependencies>", 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) -> ! {

View file

@ -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::{