diff --git a/compiler/rustc_builtin_macros/src/test_harness.rs b/compiler/rustc_builtin_macros/src/test_harness.rs index 677db66530ecb..bd2ea5a3e6696 100644 --- a/compiler/rustc_builtin_macros/src/test_harness.rs +++ b/compiler/rustc_builtin_macros/src/test_harness.rs @@ -364,7 +364,12 @@ fn add_main(cx: &mut TestCtxt<'_>, c: &mut ast::Crate) { }); // Integrate the new item into existing module structures. - let items = AstFragment::Items(smallvec![test_extern_stmt, main]); + // `extern crate test;` is only needed with the default runner. + let items = AstFragment::Items(if cx.test_runner.is_none() { + smallvec![test_extern_stmt, main] + } else { + smallvec![main] + }); c.items.extend(cx.ext_cx.monotonic_expander().fully_expand_fragment(items).make_items()); } diff --git a/tests/pretty/custom-test-runner.pp b/tests/pretty/custom-test-runner.pp new file mode 100644 index 0000000000000..5965d76bbc8a5 --- /dev/null +++ b/tests/pretty/custom-test-runner.pp @@ -0,0 +1,32 @@ +#![feature(prelude_import)] +#![no_std] +//@ compile-flags: --crate-type=lib --test --remap-path-prefix={{src-base}}/=/the/src/ --remap-path-prefix={{src-base}}\=/the/src/ +//@ pretty-compare-only +//@ pretty-mode:expanded +//@ pp-exact:custom-test-runner.pp + +// Example taken from the unstable book. + +#![feature(custom_test_frameworks)] +#![test_runner(my_runner)] +extern crate std; +#[prelude_import] +use ::std::prelude::rust_2015::*; + +fn my_runner(tests: &[&i32]) { + for t in tests { + if **t == 0 { + + + { ::std::io::_print(format_args!("PASSED\n")); }; + } else { { ::std::io::_print(format_args!("FAILED\n")); }; } + } +} +#[rustc_test_marker = "WILL_PASS"] +pub const WILL_PASS: i32 = 0; +#[rustc_test_marker = "WILL_FAIL"] +pub const WILL_FAIL: i32 = 4; +#[rustc_main] +#[coverage(off)] +#[doc(hidden)] +pub fn main() -> () { my_runner(&[&WILL_FAIL, &WILL_PASS]) } diff --git a/tests/pretty/custom-test-runner.rs b/tests/pretty/custom-test-runner.rs new file mode 100644 index 0000000000000..b0d7360bcd879 --- /dev/null +++ b/tests/pretty/custom-test-runner.rs @@ -0,0 +1,25 @@ +//@ compile-flags: --crate-type=lib --test --remap-path-prefix={{src-base}}/=/the/src/ --remap-path-prefix={{src-base}}\=/the/src/ +//@ pretty-compare-only +//@ pretty-mode:expanded +//@ pp-exact:custom-test-runner.pp + +// Example taken from the unstable book. + +#![feature(custom_test_frameworks)] +#![test_runner(my_runner)] + +fn my_runner(tests: &[&i32]) { + for t in tests { + if **t == 0 { + println!("PASSED"); + } else { + println!("FAILED"); + } + } +} + +#[test_case] +const WILL_PASS: i32 = 0; + +#[test_case] +const WILL_FAIL: i32 = 4;