Skip to content

Commit 3ac846d

Browse files
authored
Merge pull request #321 from epage/macro
feat(assert): Add assert_data_eq!
2 parents 490dc89 + 1f5c4dd commit 3ac846d

File tree

7 files changed

+95
-42
lines changed

7 files changed

+95
-42
lines changed

crates/snapbox/src/assert/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ impl Assert {
291291
}
292292

293293
let checks: Vec<_> =
294-
crate::path::PathDiff::subset_eq_iter_inner(expected_root, actual_root).collect();
294+
crate::dir::PathDiff::subset_eq_iter_inner(expected_root, actual_root).collect();
295295
self.verify(checks);
296296
}
297297

@@ -319,7 +319,7 @@ impl Assert {
319319
Action::Ignore | Action::Verify | Action::Overwrite => {}
320320
}
321321

322-
let checks: Vec<_> = crate::path::PathDiff::subset_matches_iter_inner(
322+
let checks: Vec<_> = crate::dir::PathDiff::subset_matches_iter_inner(
323323
expected_root,
324324
actual_root,
325325
&self.substitutions,
@@ -332,7 +332,7 @@ impl Assert {
332332
#[track_caller]
333333
fn verify(
334334
&self,
335-
mut checks: Vec<Result<(std::path::PathBuf, std::path::PathBuf), crate::path::PathDiff>>,
335+
mut checks: Vec<Result<(std::path::PathBuf, std::path::PathBuf), crate::dir::PathDiff>>,
336336
) {
337337
if checks.iter().all(Result::is_ok) {
338338
for check in checks {

crates/snapbox/src/data/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use filters::FilterSet;
3838
///
3939
/// let actual = some_function();
4040
/// let expected = snapbox::str![["5"]];
41-
/// snapbox::assert_eq(actual.to_debug(), expected);
41+
/// snapbox::assert_data_eq!(actual.to_debug(), expected);
4242
/// ```
4343
pub trait ToDebug {
4444
fn to_debug(&self) -> Data;
@@ -64,7 +64,7 @@ impl<D: std::fmt::Debug> ToDebug for D {
6464
///
6565
/// let actual = some_function();
6666
/// let expected = snapbox::str![["5"]];
67-
/// snapbox::assert_eq(actual.into_json(), expected);
67+
/// snapbox::assert_data_eq!(actual.into_json(), expected);
6868
/// ```
6969
#[cfg(feature = "json")]
7070
pub trait IntoJson {

crates/snapbox/src/data/runtime.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -372,41 +372,41 @@ impl PathRuntime {
372372
#[cfg(test)]
373373
mod tests {
374374
use super::*;
375-
use crate::assert_eq;
375+
use crate::assert_data_eq;
376376
use crate::prelude::*;
377377
use crate::str;
378378

379379
#[test]
380380
fn test_format_patch() {
381381
let patch = format_patch(None, "hello\nworld\n");
382382

383-
assert_eq(
383+
assert_data_eq!(
384+
patch,
384385
str![[r##"
385386
[r#"
386387
hello
387388
world
388389
"#]"##]],
389-
patch,
390390
);
391391

392392
let patch = format_patch(None, r"hello\tworld");
393-
assert_eq(str![[r##"[r#"hello\tworld"#]"##]], patch);
393+
assert_data_eq!(patch, str![[r##"[r#"hello\tworld"#]"##]].raw());
394394

395395
let patch = format_patch(None, "{\"foo\": 42}");
396-
assert_eq(str![[r##"[r#"{"foo": 42}"#]"##]], patch);
396+
assert_data_eq!(patch, str![[r##"[r#"{"foo": 42}"#]"##]]);
397397

398398
let patch = format_patch(Some(0), "hello\nworld\n");
399-
assert_eq(
399+
assert_data_eq!(
400+
patch,
400401
str![[r##"
401402
[r#"
402403
hello
403404
world
404405
"#]"##]],
405-
patch,
406406
);
407407

408408
let patch = format_patch(Some(4), "single line");
409-
assert_eq(str![[r#""single line""#]], patch);
409+
assert_data_eq!(patch, str![[r#""single line""#]]);
410410
}
411411

412412
#[test]
@@ -415,7 +415,8 @@ mod tests {
415415
patchwork.patch(4..7, "zwei");
416416
patchwork.patch(0..3, "один");
417417
patchwork.patch(8..13, "3");
418-
assert_eq(
418+
assert_data_eq!(
419+
patchwork.to_debug(),
419420
str![[r#"
420421
Patchwork {
421422
text: "один zwei 3",
@@ -435,7 +436,6 @@ mod tests {
435436
],
436437
}
437438
"#]],
438-
patchwork.to_debug(),
439439
);
440440
}
441441

crates/snapbox/src/filter/redactions.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ impl Redactions {
1717
}
1818

1919
pub(crate) fn with_exe() -> Self {
20-
let mut substitutions = Self::new();
21-
substitutions
20+
let mut redactions = Self::new();
21+
redactions
2222
.insert("[EXE]", std::env::consts::EXE_SUFFIX)
2323
.unwrap();
24-
substitutions
24+
redactions
2525
}
2626

2727
/// Insert an additional match pattern
@@ -172,7 +172,7 @@ fn validate_placeholder(placeholder: &'static str) -> crate::assert::Result<&'st
172172
Ok(placeholder)
173173
}
174174

175-
fn normalize(input: &str, pattern: &str, substitutions: &Redactions) -> String {
175+
fn normalize(input: &str, pattern: &str, redactions: &Redactions) -> String {
176176
if input == pattern {
177177
return input.to_owned();
178178
}
@@ -191,7 +191,7 @@ fn normalize(input: &str, pattern: &str, substitutions: &Redactions) -> String {
191191
input_lines[input_index..]
192192
.iter()
193193
.copied()
194-
.map(|s| substitutions.substitute(s)),
194+
.map(|s| redactions.substitute(s)),
195195
);
196196
break 'outer;
197197
};
@@ -204,7 +204,7 @@ fn normalize(input: &str, pattern: &str, substitutions: &Redactions) -> String {
204204
};
205205
let next_input_index = input_index + 1;
206206

207-
if line_matches(input_line, pattern_line, substitutions) {
207+
if line_matches(input_line, pattern_line, redactions) {
208208
pattern_index = next_pattern_index;
209209
input_index = next_input_index;
210210
normalized.push(Cow::Borrowed(pattern_line));
@@ -232,7 +232,7 @@ fn normalize(input: &str, pattern: &str, substitutions: &Redactions) -> String {
232232
input_lines[input_index..]
233233
.iter()
234234
.copied()
235-
.map(|s| substitutions.substitute(s)),
235+
.map(|s| redactions.substitute(s)),
236236
);
237237
break 'outer;
238238
}
@@ -250,7 +250,7 @@ fn normalize(input: &str, pattern: &str, substitutions: &Redactions) -> String {
250250
input_lines[input_index..future_input_index]
251251
.iter()
252252
.copied()
253-
.map(|s| substitutions.substitute(s)),
253+
.map(|s| redactions.substitute(s)),
254254
);
255255
pattern_index = future_pattern_index;
256256
input_index = future_input_index;
@@ -262,7 +262,7 @@ fn normalize(input: &str, pattern: &str, substitutions: &Redactions) -> String {
262262
input_lines[input_index..]
263263
.iter()
264264
.copied()
265-
.map(|s| substitutions.substitute(s)),
265+
.map(|s| redactions.substitute(s)),
266266
);
267267
break 'outer;
268268
}
@@ -275,15 +275,15 @@ fn is_line_elide(line: &str) -> bool {
275275
line == "...\n" || line == "..."
276276
}
277277

278-
fn line_matches(line: &str, pattern: &str, substitutions: &Redactions) -> bool {
278+
fn line_matches(line: &str, pattern: &str, redactions: &Redactions) -> bool {
279279
if line == pattern {
280280
return true;
281281
}
282282

283-
let subbed = substitutions.substitute(line);
283+
let subbed = redactions.substitute(line);
284284
let mut line = subbed.as_ref();
285285

286-
let pattern = substitutions.clear(pattern);
286+
let pattern = redactions.clear(pattern);
287287

288288
let mut sections = pattern.split("[..]").peekable();
289289
while let Some(section) = sections.next() {

crates/snapbox/src/lib.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//! ## Which tool is right
1313
//!
1414
//! - [cram](https://bitheap.org/cram/): End-to-end CLI snapshotting agnostic of any programming language
15+
//! - See also [scrut](https://github.com/facebookincubator/scrut)
1516
//! - [trycmd](https://crates.io/crates/trycmd): For running a lot of blunt tests (limited test predicates)
1617
//! - Particular attention is given to allow the test data to be pulled into documentation, like
1718
//! with [mdbook](https://rust-lang.github.io/mdBook/)
@@ -25,7 +26,7 @@
2526
//! ## Getting Started
2627
//!
2728
//! Testing Functions:
28-
//! - [`assert_eq`][crate::assert_eq()] and [`assert_matches`] for reusing diffing / pattern matching for non-snapshot testing
29+
//! - [`assert_data_eq!`] for quick and dirty snapshotting
2930
//! - [`harness::Harness`] for discovering test inputs and asserting against snapshot files:
3031
//!
3132
//! Testing Commands:
@@ -44,17 +45,17 @@
4445
//!
4546
//! # Examples
4647
//!
47-
//! [`assert_matches`]
48+
//! [`assert_data_eq!`]
4849
//! ```rust
49-
//! snapbox::assert_matches("Hello [..] people!", "Hello many people!");
50+
//! snapbox::assert_data_eq!("Hello many people!", "Hello [..] people!");
5051
//! ```
5152
//!
5253
//! [`Assert`]
5354
//! ```rust,no_run
5455
//! let actual = "...";
5556
//! snapbox::Assert::new()
5657
//! .action_env("SNAPSHOTS")
57-
//! .matches(snapbox::file!["help_output_is_clean.txt"], actual);
58+
//! .eq_(actual, snapbox::file!["help_output_is_clean.txt"]);
5859
//! ```
5960
//!
6061
//! [`harness::Harness`]
@@ -160,6 +161,10 @@ pub mod prelude {
160161
/// assert_eq(file!["output.txt"], actual);
161162
/// ```
162163
#[track_caller]
164+
#[deprecated(
165+
since = "0.5.11",
166+
note = "Replaced with `assert_data_eq!(actual, expected.raw())`"
167+
)]
163168
pub fn assert_eq(expected: impl Into<crate::Data>, actual: impl Into<crate::Data>) {
164169
Assert::new()
165170
.action_env(assert::DEFAULT_ACTION_ENV)
@@ -192,6 +197,7 @@ pub fn assert_eq(expected: impl Into<crate::Data>, actual: impl Into<crate::Data
192197
/// assert_matches(file!["output.txt"], actual);
193198
/// ```
194199
#[track_caller]
200+
#[deprecated(since = "0.5.11", note = "Replaced with `assert_data_eq!(actual)`")]
195201
pub fn assert_matches(pattern: impl Into<crate::Data>, actual: impl Into<crate::Data>) {
196202
Assert::new()
197203
.action_env(assert::DEFAULT_ACTION_ENV)

crates/snapbox/src/macros.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,50 @@
1+
/// Check if a value is the same as an expected value
2+
///
3+
/// By default [`filters`][crate::filter] are applied, including:
4+
/// - `...` is a line-wildcard when on a line by itself
5+
/// - `[..]` is a character-wildcard when inside a line
6+
/// - `[EXE]` matches `.exe` on Windows
7+
/// - `\` to `/`
8+
/// - Newlines
9+
///
10+
/// To limit this to newline normalization for text, call [`Data::raw`][crate::Data] on `expected`.
11+
///
12+
/// # Effective signature
13+
///
14+
/// ```rust
15+
/// # use snapbox::IntoData;
16+
/// fn assert_data_eq(actual: impl IntoData, expected: impl IntoData) {
17+
/// // ...
18+
/// }
19+
/// ```
20+
///
21+
/// # Examples
22+
///
23+
/// ```rust
24+
/// # use snapbox::assert_data_eq;
25+
/// let output = "something";
26+
/// let expected = "so[..]g";
27+
/// assert_data_eq!(output, expected);
28+
/// ```
29+
///
30+
/// Can combine this with [`file!`]
31+
/// ```rust,no_run
32+
/// # use snapbox::assert_data_eq;
33+
/// # use snapbox::file;
34+
/// let actual = "something";
35+
/// assert_data_eq!(actual, file!["output.txt"]);
36+
/// ```
37+
#[macro_export]
38+
macro_rules! assert_data_eq {
39+
($actual: expr, $expected: expr $(,)?) => {{
40+
let actual = $crate::IntoData::into_data($actual);
41+
let expected = $crate::IntoData::into_data($expected);
42+
$crate::Assert::new()
43+
.action_env($crate::assert::DEFAULT_ACTION_ENV)
44+
.eq_(actual, expected);
45+
}};
46+
}
47+
148
/// Find the directory for your source file
249
#[doc(hidden)] // forced to be visible in intended location
350
#[macro_export]
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
1-
use snapbox::assert_eq;
1+
use snapbox::assert_data_eq;
22
use snapbox::file;
33
use snapbox::str;
44

55
#[test]
66
fn test_trivial_assert() {
7-
assert_eq(str!["5"], "5");
7+
assert_data_eq!("5", str!["5"]);
88
}
99

1010
#[test]
1111
fn smoke_test_indent() {
12-
assert_eq(
12+
assert_data_eq!(
13+
"\
14+
line1
15+
line2
16+
",
1317
str![[r#"
1418
line1
1519
line2
1620
"#]]
1721
.indent(true),
22+
);
23+
24+
assert_data_eq!(
1825
"\
1926
line1
2027
line2
2128
",
22-
);
23-
24-
assert_eq(
2529
str![[r#"
2630
line1
2731
line2
2832
"#]]
2933
.indent(false),
30-
"\
31-
line1
32-
line2
33-
",
3434
);
3535
}
3636

3737
#[test]
3838
fn test_expect_file() {
39-
assert_eq(file!["../../README.md"], include_str!("../../README.md"))
39+
assert_data_eq!(include_str!("../../README.md"), file!["../../README.md"]);
4040
}

0 commit comments

Comments
 (0)