Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(snap): Clarify how to use Harness #262

Merged
merged 1 commit into from
Feb 23, 2024
Merged
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
22 changes: 20 additions & 2 deletions crates/snapbox/src/harness.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//! [`Harness`] for discovering test inputs and asserting against snapshot files
//!
//! This is a custom test harness and should be put in its own test binary with
//! [`test.harness = false`](https://doc.rust-lang.org/stable/cargo/reference/cargo-targets.html#the-harness-field).
//!
//! # Examples
//!
//! ```rust,no_run
Expand Down Expand Up @@ -37,6 +40,9 @@ use crate::Action;

use libtest_mimic::Trial;

/// [`Harness`] for discovering test inputs and asserting against snapshot files
///
/// See [`harness`][crate::harness] for more details
pub struct Harness<S, T> {
root: std::path::PathBuf,
overrides: Option<ignore::overrides::Override>,
Expand All @@ -52,9 +58,15 @@ where
S: Fn(std::path::PathBuf) -> Case + Send + Sync + 'static,
T: Fn(&std::path::Path) -> Result<I, E> + Send + Sync + 'static + Clone,
{
pub fn new(root: impl Into<std::path::PathBuf>, setup: S, test: T) -> Self {
/// 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
pub fn new(input_root: impl Into<std::path::PathBuf>, setup: S, test: T) -> Self {
Self {
root: root.into(),
root: input_root.into(),
overrides: None,
setup,
test,
Expand Down Expand Up @@ -207,8 +219,14 @@ impl Default for Verifier {
}
}

/// A test case enumerated by the [`Harness`] with data from the `setup` function
///
/// See [`harness`][crate::harness] for more details
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,
}
Loading