Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/libtest2/examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use libtest2::RunError;
use libtest2::RunResult;
use libtest2::TestContext;

libtest2::libtest2_main!(
libtest2::main!(
check_toph,
check_katara,
check_sokka,
Expand Down
8 changes: 4 additions & 4 deletions crates/libtest2/examples/tidy.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use libtest2::FnCase;
use libtest2::RunError;
use libtest2::RunResult;
use libtest2::Trial;

fn main() -> std::io::Result<()> {
let harness = ::libtest2_harness::Harness::new();
Expand All @@ -23,8 +23,8 @@ fn main() -> std::io::Result<()> {

/// Creates one test for each `.rs` file in the current directory or
/// sub-directories of the current directory.
fn collect_tests() -> std::io::Result<Vec<Trial>> {
fn visit_dir(path: &std::path::Path, tests: &mut Vec<Trial>) -> std::io::Result<()> {
fn collect_tests() -> std::io::Result<Vec<FnCase>> {
fn visit_dir(path: &std::path::Path, tests: &mut Vec<FnCase>) -> std::io::Result<()> {
let current_dir = std::env::current_dir()?;
for entry in std::fs::read_dir(path)? {
let entry = entry?;
Expand All @@ -44,7 +44,7 @@ fn collect_tests() -> std::io::Result<Vec<Trial>> {
.to_string_lossy()
.into_owned();

let test = Trial::test(name, move |_| check_file(&path));
let test = FnCase::test(name, move |_| check_file(&path));
tests.push(test);
}
} else if file_type.is_dir() {
Expand Down
76 changes: 76 additions & 0 deletions crates/libtest2/src/case.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use libtest2_harness::Case;
use libtest2_harness::Source;
use libtest2_harness::TestKind;

use crate::RunResult;
use crate::TestContext;

pub struct FnCase {
name: String,
#[allow(clippy::type_complexity)]
runner: Box<dyn Fn(&TestContext) -> RunResult + Send + Sync>,
}

impl FnCase {
pub fn test(
name: impl Into<String>,
runner: impl Fn(&TestContext) -> RunResult + Send + Sync + 'static,
) -> Self {
Self {
name: name.into(),
runner: Box::new(runner),
}
}
}

impl Case for FnCase {
fn name(&self) -> &str {
&self.name
}
fn kind(&self) -> TestKind {
Default::default()
}
fn source(&self) -> Option<&Source> {
None
}
fn exclusive(&self, _: &TestContext) -> bool {
false
}

fn run(&self, context: &TestContext) -> RunResult {
(self.runner)(context)
}
}

pub fn main(cases: impl IntoIterator<Item = impl Case + 'static>) {
let harness = libtest2_harness::Harness::new();
let harness = match harness.with_env() {
Ok(harness) => harness,
Err(err) => {
eprintln!("{err}");
::std::process::exit(1);
}
};
let harness = match harness.parse() {
Ok(harness) => harness,
Err(err) => {
eprintln!("{err}");
::std::process::exit(1);
}
};
let harness = match harness.discover(cases) {
Ok(harness) => harness,
Err(err) => {
eprintln!("{err}");
::std::process::exit(libtest2_harness::ERROR_EXIT_CODE)
}
};
match harness.run() {
Ok(true) => ::std::process::exit(0),
Ok(false) => ::std::process::exit(libtest2_harness::ERROR_EXIT_CODE),
Err(err) => {
eprintln!("{err}");
::std::process::exit(libtest2_harness::ERROR_EXIT_CODE)
}
}
}
100 changes: 13 additions & 87 deletions crates/libtest2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,112 +12,38 @@
//! harness = false
//! ```
//!
//! And in `tests/mytest.rs` you would call [`libtest2_main`], passing it each of your tests:
//! And in `tests/mytest.rs` you would call [`main!`], passing it each of your tests:
//!
//! ```no_run
//! # use libtest2::RunError;
//! # use libtest2::RunResult;
//! # use libtest2::TestContext;
//! # use libtest2::libtest2_main;
//! fn check_toph(_context: &TestContext) -> RunResult {
//! Ok(())
//! }
//!
//! libtest2_main!(check_toph);
//! libtest2::main!(check_toph);
//! ```
//!

#![cfg_attr(docsrs, feature(doc_cfg))]
//#![warn(clippy::print_stderr)]
#![warn(clippy::print_stdout)]

pub use libtest2_harness::Harness;
pub use libtest2_harness::RunError;
pub use libtest2_harness::RunResult;
pub use libtest2_harness::TestContext;
pub use libtest2_harness::TestKind;
pub use libtest2_harness::ERROR_EXIT_CODE;

use libtest2_harness::Case;
use libtest2_harness::Source;
mod case;
mod macros;

pub struct Trial {
name: String,
#[allow(clippy::type_complexity)]
runner: Box<dyn Fn(&TestContext) -> Result<(), RunError> + Send + Sync>,
#[doc(hidden)]
pub mod _private {
pub use crate::_main as main;
}

impl Trial {
pub fn test(
name: impl Into<String>,
runner: impl Fn(&TestContext) -> Result<(), RunError> + Send + Sync + 'static,
) -> Self {
Self {
name: name.into(),
runner: Box::new(runner),
}
}
}

impl Case for Trial {
fn name(&self) -> &str {
&self.name
}
fn kind(&self) -> TestKind {
Default::default()
}
fn source(&self) -> Option<&Source> {
None
}
fn exclusive(&self, _: &TestContext) -> bool {
false
}

fn run(&self, context: &TestContext) -> Result<(), RunError> {
(self.runner)(context)
}
}

/// Expands to the test harness
#[macro_export]
macro_rules! libtest2_main {
( $( $test:path ),* $(,)*) => {
fn main() {
let harness = $crate::Harness::new();
let harness = match harness.with_env() {
Ok(harness) => harness,
Err(err) => {
eprintln!("{err}");
::std::process::exit(1);
}
};
let harness = match harness.parse() {
Ok(harness) => harness,
Err(err) => {
eprintln!("{err}");
::std::process::exit(1);
}
};
let harness = match harness.discover([
$($crate::Trial::test(::std::stringify!($test), $test)),*
]) {
Ok(harness) => harness,
Err(err) => {
eprintln!("{err}");
::std::process::exit($crate::ERROR_EXIT_CODE)
}
};
match harness.run() {
Ok(true) => ::std::process::exit(0),
Ok(false) => ::std::process::exit($crate::ERROR_EXIT_CODE),
Err(err) => {
eprintln!("{err}");
::std::process::exit($crate::ERROR_EXIT_CODE)
}
}
}
}
}
pub use _private::main;
pub use case::main;
pub use case::FnCase;
pub use libtest2_harness::RunError;
pub use libtest2_harness::RunResult;
pub use libtest2_harness::TestContext;

#[doc = include_str!("../README.md")]
#[cfg(doctest)]
Expand Down
11 changes: 11 additions & 0 deletions crates/libtest2/src/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// Expands to the test harness
#[macro_export]
macro_rules! _main {
( $( $test:path ),* $(,)*) => {
fn main() {
$crate::main([
$($crate::FnCase::test(::std::stringify!($test), $test)),*
]);
}
}
}
2 changes: 1 addition & 1 deletion crates/libtest2/tests/testsuite/all_passing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn test_cmd() -> snapbox::cmd::Command {
let (bin, current_dir) = BIN.get_or_init(|| {
let package_root = crate::util::new_test(
r#"
libtest2::libtest2_main!(foo, bar, barro);
libtest2::main!(foo, bar, barro);

fn foo(_context: &libtest2::TestContext) -> libtest2::RunResult {
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion crates/libtest2/tests/testsuite/argfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn test_cmd() -> snapbox::cmd::Command {
let (bin, current_dir) = BIN.get_or_init(|| {
let package_root = crate::util::new_test(
r#"
libtest2::libtest2_main!(one, two, three, one_two);
libtest2::main!(one, two, three, one_two);

fn one(_context: &libtest2::TestContext) -> libtest2::RunResult {
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion crates/libtest2/tests/testsuite/mixed_bag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn test_cmd() -> snapbox::cmd::Command {
let (bin, current_dir) = BIN.get_or_init(|| {
let package_root = crate::util::new_test(
r#"
libtest2::libtest2_main!(cat, dog, fox, bunny, frog, owl, fly, bear);
libtest2::main!(cat, dog, fox, bunny, frog, owl, fly, bear);

fn cat(_context: &libtest2::TestContext) -> libtest2::RunResult {
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion crates/libtest2/tests/testsuite/panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn test_cmd() -> snapbox::cmd::Command {
let (bin, current_dir) = BIN.get_or_init(|| {
let package_root = crate::util::new_test(
r#"
libtest2::libtest2_main!(passes, panics);
libtest2::main!(passes, panics);

fn passes(_context: &libtest2::TestContext) -> libtest2::RunResult {
Ok(())
Expand Down
Loading