use std::panic::Location;
use crate::prelude::*;
use crate::testing::utils::*;
use test::TestDescAndFn;
use test::TestFn;
#[track_caller]
pub fn new_auto(
func: impl 'static + Send + FnOnce() -> Result<(), String>,
) -> TestDescAndFn {
let desc = new_auto_desc();
TestDescAndFn {
desc,
testfn: TestFn::DynTestFn(Box::new(func)),
}
}
#[track_caller]
pub fn new_auto_static(func: fn() -> Result<(), String>) -> TestDescAndFn {
let desc = new_auto_desc();
TestDescAndFn {
desc,
testfn: TestFn::StaticTestFn(func),
}
}
#[track_caller]
pub fn new_auto_desc() -> TestDesc {
let caller = Location::caller();
let name = caller.file().split('/').last().unwrap_or("").to_string();
TestDesc {
name: test::TestName::DynTestName(format!(
"libtest::test_ext::{}#{}",
name,
caller.line()
)),
ignore: false,
ignore_message: None,
source_file: caller.file(),
start_line: caller.line() as usize,
start_col: caller.column() as usize,
end_line: caller.line() as usize,
end_col: caller.column() as usize,
compile_fail: false,
no_run: false,
should_panic: ShouldPanic::No,
test_type: TestType::UnitTest,
}
}
pub fn new(
name: &str,
file: &'static str,
func: impl 'static + Send + FnOnce() -> Result<(), String>,
) -> TestDescAndFn {
TestDescAndFn {
desc: test_ext::new_desc(name, file),
testfn: TestFn::DynTestFn(Box::new(func)),
}
}
pub fn clone_static(test: &TestDescAndFn) -> TestDescAndFn {
match test.testfn {
TestFn::StaticTestFn(f) => TestDescAndFn {
testfn: TestFn::StaticTestFn(f),
desc: test.desc.clone(),
},
TestFn::StaticBenchFn(f) => TestDescAndFn {
testfn: TestFn::StaticBenchFn(f),
desc: test.desc.clone(),
},
_ => panic!("non-static tests cannot be cloned"),
}
}
#[deprecated]
pub fn func(test: &TestDescAndFn) -> fn() -> Result<(), String> {
match test.testfn {
TestFn::StaticTestFn(func) => func,
_ => panic!("non-static tests are not supported"),
}
}
pub fn run(test: TestFn) -> Result<(), String> {
match test {
TestFn::StaticTestFn(func) => func(),
TestFn::DynTestFn(func) => func(),
_ => panic!("benches not yet supported"),
}
}
use test::ShouldPanic;
use test::TestDesc;
use test::TestType;
pub fn is_equal_location(a: &TestDesc, b: &TestDesc) -> bool {
a.source_file == b.source_file && a.start_line == b.start_line
}
pub fn new_desc(name: &str, file: &'static str) -> TestDesc {
TestDesc {
name: test::TestName::DynTestName(name.into()),
ignore: false,
ignore_message: None,
source_file: file,
start_line: 0,
start_col: 0,
end_line: 0,
end_col: 0,
compile_fail: false,
no_run: false,
should_panic: ShouldPanic::No,
test_type: TestType::UnitTest,
}
}
#[deprecated]
pub fn result_to_panic<T, E>(result: Result<T, E>) {
match result {
Ok(_) => {}
Err(_) => {
panic!(
"test returned an Err(). Use `unwrap()` instead to see the contents of the error"
);
}
}
}
pub fn short_name(test: &TestDesc) -> String {
let path = test.name.to_string();
path.split("::")
.last()
.map(|p| p.to_string())
.unwrap_or(path)
}