Skip to content

Commit 6736b8b

Browse files
authored
Merge pull request #251 from epage/rust-snap
feat(snap): Allow inline snapshotting
2 parents 0865e04 + da308b1 commit 6736b8b

File tree

9 files changed

+663
-5
lines changed

9 files changed

+663
-5
lines changed

crates/snapbox/src/assert.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ impl Assert {
6262

6363
#[track_caller]
6464
fn eq_inner(&self, expected: crate::Data, actual: crate::Data) {
65+
if expected.source().is_none() && actual.source().is_some() {
66+
panic!("received `(actual, expected)`, expected `(expected, actual)`");
67+
}
6568
match self.action {
6669
Action::Skip => {
6770
return;
@@ -108,6 +111,9 @@ impl Assert {
108111

109112
#[track_caller]
110113
fn matches_inner(&self, pattern: crate::Data, actual: crate::Data) {
114+
if pattern.source().is_none() && actual.source().is_some() {
115+
panic!("received `(actual, expected)`, expected `(expected, actual)`");
116+
}
111117
match self.action {
112118
Action::Skip => {
113119
return;

crates/snapbox/src/data/mod.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod format;
22
mod normalize;
3+
mod runtime;
34
mod source;
45
#[cfg(test)]
56
mod tests;
@@ -10,6 +11,18 @@ pub use normalize::NormalizeMatches;
1011
pub use normalize::NormalizeNewlines;
1112
pub use normalize::NormalizePaths;
1213
pub use source::DataSource;
14+
pub use source::Inline;
15+
pub use source::Position;
16+
17+
pub trait ToDebug {
18+
fn to_debug(&self) -> Data;
19+
}
20+
21+
impl<D: std::fmt::Debug> ToDebug for D {
22+
fn to_debug(&self) -> Data {
23+
Data::text(format!("{:#?}\n", self))
24+
}
25+
}
1326

1427
/// Declare an expected value for an assert from a file
1528
///
@@ -53,6 +66,37 @@ macro_rules! file {
5366
}};
5467
}
5568

69+
/// Declare an expected value from within Rust source
70+
///
71+
/// ```
72+
/// # use snapbox::str;
73+
/// str![["
74+
/// Foo { value: 92 }
75+
/// "]];
76+
/// str![r#"{"Foo": 92}"#];
77+
/// ```
78+
///
79+
/// Leading indentation is stripped.
80+
#[macro_export]
81+
macro_rules! str {
82+
[$data:literal] => { $crate::str![[$data]] };
83+
[[$data:literal]] => {{
84+
let position = $crate::data::Position {
85+
file: $crate::path::current_rs!(),
86+
line: line!(),
87+
column: column!(),
88+
};
89+
let inline = $crate::data::Inline {
90+
position,
91+
data: $data,
92+
indent: true,
93+
};
94+
inline
95+
}};
96+
[] => { $crate::str![[""]] };
97+
[[]] => { $crate::str![[""]] };
98+
}
99+
56100
/// Test fixture, actual output, or expected result
57101
///
58102
/// This provides conveniences for tracking the intended format (binary vs text).
@@ -165,6 +209,9 @@ impl Data {
165209
pub fn write_to(&self, source: &DataSource) -> Result<(), crate::Error> {
166210
match &source.inner {
167211
source::DataSourceInner::Path(p) => self.write_to_path(p),
212+
source::DataSourceInner::Inline(p) => runtime::get()
213+
.write(self, p)
214+
.map_err(|err| err.to_string().into()),
168215
}
169216
}
170217

0 commit comments

Comments
 (0)