Skip to content

Commit b31e6c6

Browse files
committed
Unify handling of file & inline snapshots
This is the code for mitsuhiko#456 (comment), as mentioned in mitsuhiko#466. There's a very small change in yaml inline snapshots — shown here in the tests. In return, it makes the macros simpler & more maintainable.
1 parent 8379841 commit b31e6c6

File tree

5 files changed

+17
-63
lines changed

5 files changed

+17
-63
lines changed

src/macros.rs

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -223,59 +223,39 @@ macro_rules! assert_compact_json_snapshot {
223223
#[doc(hidden)]
224224
#[macro_export]
225225
macro_rules! _assert_serialized_snapshot {
226-
// If there are redaction expressions and an inline snapshot, capture
227-
// the redactions expressions and pass to `_assert_snapshot_base`
228-
//
229-
// Note that if we could unify the Inline & File represenations of snapshots
230-
// redactions we could unify some of these branches.
231-
232-
(format=$format:ident, $value:expr, $(match ..)? {$($k:expr => $v:expr),* $(,)?}, @$snapshot:literal) => {{
226+
// If there are redaction expressions, capture the redactions expressions
227+
// and pass to `_assert_snapshot_base`
228+
(format=$format:ident, $value:expr, $(match ..)? {$($k:expr => $v:expr),* $(,)?} $($arg:tt)*) => {{
233229
let transform = |value| {
234-
let (_, value) = $crate::_prepare_snapshot_for_redaction!(value, {$($k => $v),*}, $format, Inline);
230+
let (_, value) = $crate::_prepare_snapshot_for_redaction!(value, {$($k => $v),*}, $format);
235231
value
236232
};
237-
$crate::_assert_snapshot_base!(transform=transform, $value, @$snapshot);
233+
$crate::_assert_snapshot_base!(transform=transform, $value $($arg)*);
238234
}};
239-
// If there are redaction expressions and no name, add a auto-generated name, call self
240-
(format=$format:ident, $value:expr, $(match ..)? {$($k:expr => $v:expr),* $(,)?} $(,)?) => {{
241-
$crate::_assert_serialized_snapshot!(format=$format, $crate::_macro_support::AutoName, $value, {$($k => $v),*});
242-
}};
243-
// If there are redaction expressions, capture and pass to `_assert_snapshot_base`
235+
// If there's a name and redaction expressions, capture and pass to `_assert_snapshot_base`
244236
(format=$format:ident, $name:expr, $value:expr, $(match ..)? {$($k:expr => $v:expr),* $(,)?} $(,)?) => {{
245237
let transform = |value| {
246-
let (_, value) = $crate::_prepare_snapshot_for_redaction!(value, {$($k => $v),*}, $format, File);
238+
let (_, value) = $crate::_prepare_snapshot_for_redaction!(value, {$($k => $v),*}, $format);
247239
value
248240
};
249241
$crate::_assert_snapshot_base!(transform=transform, $name, $value);
250242
}};
251-
// If there's an inline snapshot, capture serialization function and pass to
252-
// `_assert_snapshot_base`, specifying `Inline`
243+
// Capture serialization function and pass to `_assert_snapshot_base`
253244
//
254-
(format=$format:ident, $($arg:expr),*, @$snapshot:literal) => {{
255-
let transform = |value| {$crate::_macro_support::serialize_value(
256-
&value,
257-
$crate::_macro_support::SerializationFormat::$format,
258-
$crate::_macro_support::SnapshotLocation::Inline
259-
)};
260-
$crate::_assert_snapshot_base!(transform = transform, $($arg),*, @$snapshot);
261-
}};
262-
// Capture serialization function and pass to `_assert_snapshot_base`,
263-
// specifying `File`
264-
(format=$format:ident, $($arg:expr),* $(,)?) => {{
245+
(format=$format:ident, $($arg:tt)*) => {{
265246
let transform = |value| {$crate::_macro_support::serialize_value(
266247
&value,
267248
$crate::_macro_support::SerializationFormat::$format,
268-
$crate::_macro_support::SnapshotLocation::File
269249
)};
270-
$crate::_assert_snapshot_base!(transform = transform, $($arg),*);
250+
$crate::_assert_snapshot_base!(transform = transform, $($arg)*);
271251
}};
272252
}
273253

274254
#[cfg(feature = "redactions")]
275255
#[doc(hidden)]
276256
#[macro_export]
277257
macro_rules! _prepare_snapshot_for_redaction {
278-
($value:expr, {$($k:expr => $v:expr),*}, $format:ident, $location:ident) => {
258+
($value:expr, {$($k:expr => $v:expr),*}, $format:ident) => {
279259
{
280260
let vec = vec![
281261
$((
@@ -287,7 +267,6 @@ macro_rules! _prepare_snapshot_for_redaction {
287267
&$value,
288268
&vec,
289269
$crate::_macro_support::SerializationFormat::$format,
290-
$crate::_macro_support::SnapshotLocation::$location
291270
);
292271
(vec, value)
293272
}
@@ -298,7 +277,7 @@ macro_rules! _prepare_snapshot_for_redaction {
298277
#[doc(hidden)]
299278
#[macro_export]
300279
macro_rules! _prepare_snapshot_for_redaction {
301-
($value:expr, {$($k:expr => $v:expr),*}, $format:ident, $location:ident) => {
280+
($value:expr, {$($k:expr => $v:expr),*}, $format:ident) => {
302281
compile_error!("insta was compiled without redaction support.");
303282
};
304283
}

src/serialization.rs

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@ pub enum SnapshotLocation {
2222
File,
2323
}
2424

25-
pub fn serialize_content(
26-
mut content: Content,
27-
format: SerializationFormat,
28-
location: SnapshotLocation,
29-
) -> String {
25+
pub fn serialize_content(mut content: Content, format: SerializationFormat) -> String {
3026
content = Settings::with(|settings| {
3127
if settings.sort_maps() {
3228
content.sort_maps();
@@ -41,13 +37,7 @@ pub fn serialize_content(
4137
});
4238

4339
match format {
44-
SerializationFormat::Yaml => {
45-
let serialized = yaml::to_string(&content);
46-
match location {
47-
SnapshotLocation::Inline => serialized,
48-
SnapshotLocation::File => serialized[4..].to_string(),
49-
}
50-
}
40+
SerializationFormat::Yaml => yaml::to_string(&content)[4..].to_string(),
5141
SerializationFormat::Json => json::to_string_pretty(&content),
5242
SerializationFormat::JsonCompact => json::to_string_compact(&content),
5343
#[cfg(feature = "csv")]
@@ -98,29 +88,24 @@ pub fn serialize_content(
9888
}
9989
}
10090

101-
pub fn serialize_value<S: Serialize>(
102-
s: &S,
103-
format: SerializationFormat,
104-
location: SnapshotLocation,
105-
) -> String {
91+
pub fn serialize_value<S: Serialize>(s: &S, format: SerializationFormat) -> String {
10692
let serializer = ContentSerializer::<ValueError>::new();
10793
let content = Serialize::serialize(s, serializer).unwrap();
108-
serialize_content(content, format, location)
94+
serialize_content(content, format)
10995
}
11096

11197
#[cfg(feature = "redactions")]
11298
pub fn serialize_value_redacted<S: Serialize>(
11399
s: &S,
114100
redactions: &[(crate::redaction::Selector, crate::redaction::Redaction)],
115101
format: SerializationFormat,
116-
location: SnapshotLocation,
117102
) -> String {
118103
let serializer = ContentSerializer::<ValueError>::new();
119104
let mut content = Serialize::serialize(s, serializer).unwrap();
120105
for (selector, redaction) in redactions {
121106
content = selector.redact(content, redaction);
122107
}
123-
serialize_content(content, format, location)
108+
serialize_content(content, format)
124109
}
125110

126111
#[test]
@@ -140,7 +125,6 @@ fn test_yaml_serialization() {
140125
),
141126
]),
142127
SerializationFormat::Yaml,
143-
SnapshotLocation::File,
144128
);
145129
crate::assert_snapshot!(&yaml, @r###"
146130
env:
@@ -166,10 +150,8 @@ fn test_yaml_serialization() {
166150
),
167151
]),
168152
SerializationFormat::Yaml,
169-
SnapshotLocation::Inline,
170153
);
171154
crate::assert_snapshot!(&inline_yaml, @r###"
172-
---
173155
env:
174156
- ENVIRONMENT
175157
- production

tests/test_inline.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@ fn test_yaml_inline() {
183183
username: "peter-pan".into(),
184184
email: "peterpan@wonderland.invalid".into()
185185
}, @r###"
186-
---
187186
id: 42
188187
username: peter-pan
189188
email: peterpan@wonderland.invalid
@@ -207,7 +206,6 @@ fn test_yaml_inline_redacted() {
207206
}, {
208207
".id" => "[user-id]"
209208
}, @r###"
210-
---
211209
id: "[user-id]"
212210
username: peter-pan
213211
email: peterpan@wonderland.invalid

tests/test_redaction.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,6 @@ fn test_redact_newtype_enum() {
331331
assert_yaml_snapshot!(visitor, {
332332
r#".id"# => "[id]",
333333
}, @r###"
334-
---
335334
Visitor:
336335
id: "[id]"
337336
name: my-name
@@ -346,7 +345,6 @@ fn test_redact_newtype_enum() {
346345
assert_yaml_snapshot!(admin, {
347346
r#".id"# => "[id]",
348347
}, @r###"
349-
---
350348
Admin:
351349
id: "[id]"
352350
username: john_doe

tests/test_settings.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ fn test_simple() {
1717
settings.set_sort_maps(true);
1818
settings.bind(|| {
1919
assert_yaml_snapshot!(&map, @r###"
20-
---
2120
a: first value
2221
b: second value
2322
c: third value
@@ -40,7 +39,6 @@ fn test_bound_to_scope() {
4039
settings.set_sort_maps(true);
4140
let _guard = settings.bind_to_scope();
4241
assert_yaml_snapshot!(&map, @r###"
43-
---
4442
a: first value
4543
b: second value
4644
c: third value
@@ -62,7 +60,6 @@ fn test_settings_macro() {
6260

6361
with_settings!({sort_maps => true}, {
6462
insta::assert_yaml_snapshot!(&map, @r###"
65-
---
6663
a: first value
6764
b: second value
6865
c: third value

0 commit comments

Comments
 (0)