Skip to content

Commit dff9cba

Browse files
authored
Merge pull request #296 from epage/macro
feat(assert): Add assert_data_eq!
2 parents 0a9e06b + 2ebd198 commit dff9cba

File tree

5 files changed

+67
-52
lines changed

5 files changed

+67
-52
lines changed

crates/snapbox/src/data/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use filters::FilterSet;
3030
///
3131
/// let actual = some_function();
3232
/// let expected = snapbox::str![["5"]];
33-
/// snapbox::assert_eq(actual.to_debug(), expected);
33+
/// snapbox::assert_data_eq!(actual.to_debug(), expected);
3434
/// ```
3535
pub trait ToDebug {
3636
fn to_debug(&self) -> Data;
@@ -54,7 +54,7 @@ impl<D: std::fmt::Debug> ToDebug for D {
5454
///
5555
/// let actual = some_function();
5656
/// let expected = snapbox::str![["5"]];
57-
/// snapbox::assert_eq(actual.into_json(), expected);
57+
/// snapbox::assert_data_eq!(actual.into_json(), expected);
5858
/// ```
5959
#[cfg(feature = "json")]
6060
pub trait IntoJson {

crates/snapbox/src/data/runtime.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -348,30 +348,30 @@ impl PathRuntime {
348348
#[cfg(test)]
349349
mod tests {
350350
use super::*;
351-
use crate::assert_eq;
351+
use crate::assert_data_eq;
352352
use crate::prelude::*;
353353
use crate::str;
354354

355355
#[test]
356356
fn test_format_patch() {
357357
let patch = format_patch("hello\nworld\n");
358358

359-
assert_eq(
359+
assert_data_eq!(
360+
patch,
360361
str![[r##"
361362
[r#"
362363
hello
363364
world
364365
365366
"#]
366367
"##]],
367-
patch,
368368
);
369369

370370
let patch = format_patch(r"hello\tworld");
371-
assert_eq(str![[r##"[r#"hello\tworld"#]"##]].raw(), patch);
371+
assert_data_eq!(patch, str![[r##"[r#"hello\tworld"#]"##]].raw());
372372

373373
let patch = format_patch("{\"foo\": 42}");
374-
assert_eq(str![[r##"[r#"{"foo": 42}"#]"##]].raw(), patch);
374+
assert_data_eq!(patch, str![[r##"[r#"{"foo": 42}"#]"##]].raw());
375375
}
376376

377377
#[test]
@@ -380,7 +380,8 @@ world
380380
patchwork.patch(4..7, "zwei");
381381
patchwork.patch(0..3, "один");
382382
patchwork.patch(8..13, "3");
383-
assert_eq(
383+
assert_data_eq!(
384+
patchwork.to_debug(),
384385
str![[r#"
385386
Patchwork {
386387
text: "один zwei 3",
@@ -402,7 +403,6 @@ Patchwork {
402403
403404
"#]]
404405
.raw(),
405-
patchwork.to_debug(),
406406
);
407407
}
408408

crates/snapbox/src/lib.rs

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
//! ## Getting Started
2727
//!
2828
//! Testing Functions:
29-
//! - [`assert_eq`][crate::assert_eq()] for quick and dirty snapshotting
29+
//! - [`assert_data_eq`] for quick and dirty snapshotting
3030
//!
3131
//! Testing Commands:
3232
//! - [`cmd::Command`]: Process spawning for testing of non-interactive commands
@@ -44,9 +44,9 @@
4444
//!
4545
//! # Examples
4646
//!
47-
//! [`assert_eq`][crate::assert_eq()]
47+
//! [`assert_data_eq`]
4848
//! ```rust
49-
//! snapbox::assert_eq("Hello [..] people!", "Hello many people!");
49+
//! snapbox::assert_data_eq!("Hello many people!", "Hello [..] people!");
5050
//! ```
5151
//!
5252
//! [`Assert`]
@@ -89,38 +89,6 @@ pub mod prelude {
8989
pub use crate::ToDebug;
9090
}
9191

92-
/// Check if a value is the same as an expected value
93-
///
94-
/// By default [`filters`] are applied, including:
95-
/// - `...` is a line-wildcard when on a line by itself
96-
/// - `[..]` is a character-wildcard when inside a line
97-
/// - `[EXE]` matches `.exe` on Windows
98-
/// - `\` to `/`
99-
/// - Newlines
100-
///
101-
/// To limit this to newline normalization for text, call [`Data::raw`] on `expected`.
102-
///
103-
/// ```rust
104-
/// # use snapbox::assert_eq;
105-
/// let output = "something";
106-
/// let expected = "so[..]g";
107-
/// assert_eq(expected, output);
108-
/// ```
109-
///
110-
/// Can combine this with [`file!`]
111-
/// ```rust,no_run
112-
/// # use snapbox::assert_eq;
113-
/// # use snapbox::file;
114-
/// let actual = "something";
115-
/// assert_eq(file!["output.txt"], actual);
116-
/// ```
117-
#[track_caller]
118-
pub fn assert_eq(expected: impl IntoData, actual: impl IntoData) {
119-
Assert::new()
120-
.action_env(assert::DEFAULT_ACTION_ENV)
121-
.eq(expected, actual);
122-
}
123-
12492
/// Check if a path matches the content of another path, recursively
12593
///
12694
/// When the content is text, newlines are normalized.

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::filters] 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(expected, actual);
45+
}};
46+
}
47+
148
/// Find the directory for your source file
249
#[doc(hidden)] // forced to be visible in intended location
350
#[macro_export]

crates/snapbox/tests/assert.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
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
"#]],
18-
"\
19-
line1
20-
line2
21-
",
2222
);
2323
}
2424

2525
#[test]
2626
fn test_expect_file() {
27-
assert_eq(file!["../README.md"], include_str!("../README.md"))
27+
assert_data_eq!(include_str!("../README.md"), file!["../README.md"])
2828
}

0 commit comments

Comments
 (0)