Skip to content

Commit 93eebf9

Browse files
committed
support tests that load files as strings or Vec<u8>
I realized that we could use a variant of autoref specialization to achieve this, and this seemed like a good way for me to learn how to do that as well. This is neat!
1 parent e707ccf commit 93eebf9

File tree

6 files changed

+370
-45
lines changed

6 files changed

+370
-45
lines changed

README.md

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
[![License](https://img.shields.io/badge/license-Apache-green.svg)](../LICENSE-APACHE)
77
[![License](https://img.shields.io/badge/license-MIT-green.svg)](../LICENSE-MIT)
88

9-
`datatest-stable` is a very simple test harness intended to write data-driven tests, where
10-
individual test cases are specified as files and not as code. Given:
11-
* a test `my_test` that accepts a path as input
9+
`datatest-stable` is a test harness intended to write *file-driven* or *data-driven* tests,
10+
where individual test cases are specified as files and not as code.
11+
12+
Given:
13+
14+
* a test `my_test` that accepts a path, and optionally the contents as input
1215
* a directory to look for files in
1316
* a pattern to match files on
1417

@@ -29,10 +32,18 @@ harness = false
2932

3033
2. Call the `datatest_stable::harness!(testfn, root, pattern)` macro with the following
3134
parameters:
32-
* `testfn` - The test function to be executed on each matching input. This function can be one of:
35+
36+
* `testfn` - The test function to be executed on each matching input. This function can be one
37+
of:
3338
* `fn(&Path) -> datatest_stable::Result<()>`
3439
* `fn(&Utf8Path) -> datatest_stable::Result<()>` (`Utf8Path` is part of the
3540
[`camino`](https://docs.rs/camino) library, and is re-exported here for convenience.)
41+
* `fn(&P, String) -> datatest_stable::Result<()>` where `P` is `Path` or `Utf8Path`. If the
42+
extra `String` parameter is specified, the contents of the file will be loaded and passed in
43+
as a string (erroring out if that failed).
44+
* `fn(&P, Vec<u8>) -> datatest_stable::Result<()>` where `P` is `Path` or `Utf8Path`. If the
45+
extra `Vec<u8>` parameter is specified, are specified, the contents of the file will be
46+
loaded and passed in as a `Vec<u8>` (erroring out if that failed).
3647
* `root` - The path to the root directory where the input files (fixtures) live. This path is
3748
relative to the root of the crate.
3849
* `pattern` - the regex used to match against and select each file to be tested.
@@ -54,7 +65,7 @@ fn my_test(path: &Path) -> datatest_stable::Result<()> {
5465
Ok(())
5566
}
5667

57-
fn my_test_utf8(path: &Utf8Path) -> datatest_stable::Result<()> {
68+
fn my_test_utf8(path: &Utf8Path, contents: String) -> datatest_stable::Result<()> {
5869
// ... write test here
5970

6071
Ok(())

src/lib.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33

44
#![forbid(unsafe_code)]
55

6-
//! `datatest-stable` is a very simple test harness intended to write data-driven tests, where
7-
//! individual test cases are specified as files and not as code. Given:
8-
//! * a test `my_test` that accepts a path as input
6+
//! `datatest-stable` is a test harness intended to write *file-driven* or *data-driven* tests,
7+
//! where individual test cases are specified as files and not as code.
8+
//!
9+
//! Given:
10+
//!
11+
//! * a test `my_test` that accepts a path, and optionally the contents as input
912
//! * a directory to look for files in
1013
//! * a pattern to match files on
1114
//!
@@ -26,10 +29,18 @@
2629
//!
2730
//! 2. Call the `datatest_stable::harness!(testfn, root, pattern)` macro with the following
2831
//! parameters:
29-
//! * `testfn` - The test function to be executed on each matching input. This function can be one of:
32+
//!
33+
//! * `testfn` - The test function to be executed on each matching input. This function can be one
34+
//! of:
3035
//! * `fn(&Path) -> datatest_stable::Result<()>`
3136
//! * `fn(&Utf8Path) -> datatest_stable::Result<()>` (`Utf8Path` is part of the
3237
//! [`camino`](https://docs.rs/camino) library, and is re-exported here for convenience.)
38+
//! * `fn(&P, String) -> datatest_stable::Result<()>` where `P` is `Path` or `Utf8Path`. If the
39+
//! extra `String` parameter is specified, the contents of the file will be loaded and passed in
40+
//! as a string (erroring out if that failed).
41+
//! * `fn(&P, Vec<u8>) -> datatest_stable::Result<()>` where `P` is `Path` or `Utf8Path`. If the
42+
//! extra `Vec<u8>` parameter is specified, are specified, the contents of the file will be
43+
//! loaded and passed in as a `Vec<u8>` (erroring out if that failed).
3344
//! * `root` - The path to the root directory where the input files (fixtures) live. This path is
3445
//! relative to the root of the crate.
3546
//! * `pattern` - the regex used to match against and select each file to be tested.
@@ -51,7 +62,7 @@
5162
//! Ok(())
5263
//! }
5364
//!
54-
//! fn my_test_utf8(path: &Utf8Path) -> datatest_stable::Result<()> {
65+
//! fn my_test_utf8(path: &Utf8Path, contents: String) -> datatest_stable::Result<()> {
5566
//! // ... write test here
5667
//!
5768
//! Ok(())
@@ -85,7 +96,7 @@ pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
8596

8697
/// Not part of the public API, just used for macros.
8798
#[doc(hidden)]
88-
pub use self::runner::{runner, Requirements, TestFn};
99+
pub use self::runner::{runner, test_kinds, Requirements, TestFn};
89100
/// A re-export of this type from the `camino` crate, since it forms part of function signatures.
90101
#[doc(no_inline)]
91102
pub use camino::Utf8Path;

src/macros.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ macro_rules! harness {
1010
( $( $name:path, $root:expr, $pattern:expr ),+ $(,)* ) => {
1111
fn main() -> ::std::process::ExitCode {
1212
let mut requirements = Vec::new();
13+
use $crate::test_kinds::*;
1314

1415
$(
1516
requirements.push(
1617
$crate::Requirements::new(
17-
$name,
18+
$name.kind().resolve($name),
1819
stringify!($name).to_string(),
1920
$root.to_string().into(),
2021
$pattern.to_string()

0 commit comments

Comments
 (0)