Skip to content

Commit

Permalink
Merge pull request #326 from epage/tryfn
Browse files Browse the repository at this point in the history
feat(tryfn)!: Future-proof the API a bit
  • Loading branch information
epage authored May 17, 2024
2 parents 029bd07 + 2e3d702 commit 95f4564
Showing 1 changed file with 67 additions and 28 deletions.
95 changes: 67 additions & 28 deletions crates/tryfn/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
//!
//! fn setup(input_path: std::path::PathBuf) -> tryfn::Case {
//! let name = input_path.file_name().unwrap().to_str().unwrap().to_owned();
//! let expected = input_path.with_extension("out");
//! let expected = tryfn::Data::read_from(&input_path.with_extension("out"), None);
//! tryfn::Case {
//! name,
//! fixture: input_path,
Expand All @@ -41,39 +41,51 @@
use libtest_mimic::Trial;

pub use snapbox::assert::Action;
pub use snapbox::data::DataFormat;
pub use snapbox::Data;

/// [`Harness`] for discovering test inputs and asserting against snapshot files
pub struct Harness<S, T> {
pub struct Harness<S, T, I, E> {
root: std::path::PathBuf,
overrides: Option<ignore::overrides::Override>,
setup: S,
test: T,
config: snapbox::Assert,
test_output: std::marker::PhantomData<I>,
test_error: std::marker::PhantomData<E>,
}

impl<S, T, I, E> Harness<S, T>
impl<S, T, I, E> Harness<S, T, I, E>
where
S: Setup + Send + Sync + 'static,
T: Test<I, E> + Clone + Send + Sync + 'static,
I: std::fmt::Display,
E: std::fmt::Display,
S: Fn(std::path::PathBuf) -> Case + Send + Sync + 'static,
T: Fn(&std::path::Path) -> Result<I, E> + Send + Sync + 'static + Clone,
{
/// Specify where the test scenarios
///
/// - `input_root`: where to find the files. See [`Self::select`] for restricting what files
/// are considered
/// - `setup`: Given a path, choose the test name and the output location
/// - `test`: Given a path, return the actual output value
///
/// By default filters are applied, including:
/// - `...` is a line-wildcard when on a line by itself
/// - `[..]` is a character-wildcard when inside a line
/// - `[EXE]` matches `.exe` on Windows
/// - `\` to `/`
/// - Newlines
///
/// To limit this to newline normalization for text, have [`Setup`] call [`Data::raw`][snapbox::Data::raw] on `expected`.
pub fn new(input_root: impl Into<std::path::PathBuf>, setup: S, test: T) -> Self {
Self {
root: input_root.into(),
overrides: None,
setup,
test,
config: snapbox::Assert::new().action_env(snapbox::assert::DEFAULT_ACTION_ENV),
test_output: Default::default(),
test_error: Default::default(),
}
}

Expand All @@ -89,18 +101,6 @@ where
self
}

/// Read the failure action from an environment variable
pub fn action_env(mut self, var_name: &str) -> Self {
self.config = self.config.action_env(var_name);
self
}

/// Override the failure action
pub fn action(mut self, action: Action) -> Self {
self.config = self.config.action(action);
self
}

/// Customize the assertion behavior
///
/// Includes
Expand Down Expand Up @@ -133,18 +133,23 @@ where
let tests: Vec<_> = tests
.into_iter()
.map(|path| {
let case = (self.setup)(path);
let case = self.setup.setup(path);
assert!(
case.expected.source().map(|s| s.is_path()).unwrap_or(false),
"`Case::expected` must be from a file"
);
let test = self.test.clone();
let config = shared_config.clone();
Trial::test(case.name.clone(), move || {
let expected = crate::Data::read_from(&case.expected, Some(DataFormat::Text));
let actual = (test)(&case.fixture)?;
let actual = test.run(&case.fixture)?;
let actual = actual.to_string();
let actual = crate::Data::text(actual);
config.try_eq(Some(&case.name), actual, expected.raw())?;
let actual = snapbox::Data::text(actual);
config.try_eq(Some(&case.name), actual, case.expected.clone())?;
Ok(())
})
.with_ignored_flag(shared_config.selected_action() == Action::Ignore)
.with_ignored_flag(
shared_config.selected_action() == snapbox::assert::Action::Ignore,
)
})
.collect();

Expand All @@ -153,14 +158,48 @@ where
}
}

/// A test case enumerated by the [`Harness`] with data from the `setup` function
///
/// See [`harness`][crate] for more details
/// Function signature for generating a test [`Case`] from a path fixture
pub trait Setup {
fn setup(&self, fixture: std::path::PathBuf) -> Case;
}

impl<F> Setup for F
where
F: Fn(std::path::PathBuf) -> Case,
{
fn setup(&self, fixture: std::path::PathBuf) -> Case {
(self)(fixture)
}
}

/// Function signature for running a test [`Case`]
pub trait Test<S, E>
where
S: std::fmt::Display,
E: std::fmt::Display,
{
fn run(&self, fixture: &std::path::Path) -> Result<S, E>;
}

impl<F, S, E> Test<S, E> for F
where
F: Fn(&std::path::Path) -> Result<S, E>,
S: std::fmt::Display,
E: std::fmt::Display,
{
fn run(&self, fixture: &std::path::Path) -> Result<S, E> {
(self)(fixture)
}
}

/// A test case enumerated by the [`Harness`] with data from the [`Setup`] function
pub struct Case {
/// Display name
pub name: String,
/// Input for the test
pub fixture: std::path::PathBuf,
/// What the actual output should be compared against or updated
pub expected: std::path::PathBuf,
///
/// Generally derived from `fixture` and loaded with [`Data::read_from`]
pub expected: Data,
}

0 comments on commit 95f4564

Please sign in to comment.