syntax: Don't assume std exists for tests

This commit removes the injection of `std::env::args()` from `--test` expanded
code, relying on the test runner itself to call this funciton. This is more
hygienic because we can't assume that `std` exists at the top layer all the
time, and it meaks the injected test module entirely self contained.
This commit is contained in:
Alex Crichton
2015-07-30 08:53:22 -07:00
parent 5cccf3cd25
commit 0d8340327c
7 changed files with 23 additions and 33 deletions

View File

@@ -197,7 +197,8 @@ pub struct Bencher {
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum ShouldPanic {
No,
Yes(Option<&'static str>)
Yes,
YesWithMessage(&'static str)
}
// The definition of a single test. A test runner will run a list of
@@ -262,8 +263,8 @@ pub fn test_main(args: &[String], tests: Vec<TestDescAndFn> ) {
// a Vec<TestDescAndFn> is used in order to effect ownership-transfer
// semantics into parallel test runners, which in turn requires a Vec<>
// rather than a &[].
pub fn test_main_static(args: env::Args, tests: &[TestDescAndFn]) {
let args = args.collect::<Vec<_>>();
pub fn test_main_static(tests: &[TestDescAndFn]) {
let args = env::args().collect::<Vec<_>>();
let owned_tests = tests.iter().map(|t| {
match t.testfn {
StaticTestFn(f) => TestDescAndFn { testfn: StaticTestFn(f), desc: t.desc.clone() },
@@ -1027,8 +1028,8 @@ pub fn run_test(opts: &TestOpts,
fn calc_result(desc: &TestDesc, task_result: Result<(), Box<Any+Send>>) -> TestResult {
match (&desc.should_panic, task_result) {
(&ShouldPanic::No, Ok(())) |
(&ShouldPanic::Yes(None), Err(_)) => TrOk,
(&ShouldPanic::Yes(Some(msg)), Err(ref err))
(&ShouldPanic::Yes, Err(_)) => TrOk,
(&ShouldPanic::YesWithMessage(msg), Err(ref err))
if err.downcast_ref::<String>()
.map(|e| &**e)
.or_else(|| err.downcast_ref::<&'static str>().map(|e| *e))
@@ -1276,7 +1277,7 @@ mod tests {
desc: TestDesc {
name: StaticTestName("whatever"),
ignore: false,
should_panic: ShouldPanic::Yes(None)
should_panic: ShouldPanic::Yes,
},
testfn: DynTestFn(Box::new(move|| f())),
};
@@ -1293,7 +1294,7 @@ mod tests {
desc: TestDesc {
name: StaticTestName("whatever"),
ignore: false,
should_panic: ShouldPanic::Yes(Some("error message"))
should_panic: ShouldPanic::YesWithMessage("error message"),
},
testfn: DynTestFn(Box::new(move|| f())),
};
@@ -1310,7 +1311,7 @@ mod tests {
desc: TestDesc {
name: StaticTestName("whatever"),
ignore: false,
should_panic: ShouldPanic::Yes(Some("foobar"))
should_panic: ShouldPanic::YesWithMessage("foobar"),
},
testfn: DynTestFn(Box::new(move|| f())),
};
@@ -1327,7 +1328,7 @@ mod tests {
desc: TestDesc {
name: StaticTestName("whatever"),
ignore: false,
should_panic: ShouldPanic::Yes(None)
should_panic: ShouldPanic::Yes,
},
testfn: DynTestFn(Box::new(move|| f())),
};