1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/// Alias for `assert_yaml_snapshot_matches`.
#[macro_export]
#[deprecated(since = "0.6.0", note = "Replaced by assert_yaml_snapshot_matches")]
macro_rules! assert_serialized_snapshot_matches {
    ($value:expr, @$snapshot:literal) => {{
        $crate::_assert_serialized_snapshot_matches!($value, Yaml, @$snapshot);
    }};
    ($value:expr, {$($k:expr => $v:expr),*}, @$snapshot:literal) => {{
        $crate::_assert_serialized_snapshot_matches!($value, {$($k => $v),*}, Yaml, @$snapshot);
    }};
    ($name:expr, $value:expr) => {{
        $crate::_assert_serialized_snapshot_matches!($name, $value, Yaml);
    }};
    ($name:expr, $value:expr, {$($k:expr => $v:expr),*}) => {{
        $crate::_assert_serialized_snapshot_matches!($name, $value, {$($k => $v),*}, Yaml);
    }}
}

/// Assets a `Serialize` snapshot in YAML format.
///
/// The value needs to implement the `serde::Serialize` trait and the snapshot
/// will be serialized in YAML format.  This does mean that unlike the debug
/// snapshot variant the type of the value does not appear in the output.
/// You can however use the `assert_ron_snapshot_matches!` macro to dump out
/// the value in [RON](https://github.com/ron-rs/ron/) format which retains some
/// type information for more accurate comparisions.
///
/// Example:
///
/// ```no_run,ignore
/// assert_yaml_snapshot_matches!("snapshot_name", vec[1, 2, 3]);
/// ```
///
/// Unlike the `assert_debug_snapshot_matches` macro, this one has a secondary
/// mode where redactions can be defined.
///
/// The third argument to the macro can be an object expression for redaction.
/// It's in the form `{ selector => replacement }`.  For more information
/// about redactions see [redactions](index.html#redactions).
///
/// Example:
///
/// ```no_run,ignore
/// assert_yaml_snapshot_matches!("name", value, {
///     ".key.to.redact" => "[replacement value]",
///     ".another.key.*.to.redact" => 42
/// });
/// ```
///
/// The replacement value can be a string, integer or any other primitive value.
///
/// For inline usage the format is `(expression, @reference_value)` where the
/// reference value must be a string literal.  If you make the initial snapshot
/// just use an empty string (`@""`).  For more information see
/// [inline snapshots](index.html#inline-snapshots).
#[macro_export]
macro_rules! assert_yaml_snapshot_matches {
    ($value:expr, @$snapshot:literal) => {{
        $crate::_assert_serialized_snapshot_matches!($value, Yaml, @$snapshot);
    }};
    ($value:expr, {$($k:expr => $v:expr),*}, @$snapshot:literal) => {{
        $crate::_assert_serialized_snapshot_matches!($value, {$($k => $v),*}, Yaml, @$snapshot);
    }};
    ($name:expr, $value:expr) => {{
        $crate::_assert_serialized_snapshot_matches!($name, $value, Yaml);
    }};
    ($name:expr, $value:expr, {$($k:expr => $v:expr),*}) => {{
        $crate::_assert_serialized_snapshot_matches!($name, $value, {$($k => $v),*}, Yaml);
    }}
}

/// Assets a `Serialize` snapshot in RON format.
///
/// This works exactly like `assert_serialized_snapshot_matches` but serializes
/// in [RON](https://github.com/ron-rs/ron/) format instead of YAML which
/// retains some type information for more accurate comparisions.
///
/// Example:
///
/// ```no_run,ignore
/// assert_ron_snapshot_matches!("snapshot_name", vec[1, 2, 3]);
/// ```
///
/// The third argument to the macro can be an object expression for redaction.
/// It's in the form `{ selector => replacement }`.  For more information
/// about redactions see [redactions](index.html#redactions).
#[macro_export]
macro_rules! assert_ron_snapshot_matches {
    ($value:expr, @$snapshot:literal) => {{
        $crate::_assert_serialized_snapshot_matches!($value, Ron, @$snapshot);
    }};
    ($value:expr, {$($k:expr => $v:expr),*}, @$snapshot:literal) => {{
        $crate::_assert_serialized_snapshot_matches!($value, {$($k => $v),*}, Ron, @$snapshot);
    }};
    ($name:expr, $value:expr) => {{
        $crate::_assert_serialized_snapshot_matches!($name, $value, Ron);
    }};
    ($name:expr, $value:expr, {$($k:expr => $v:expr),*}) => {{
        $crate::_assert_serialized_snapshot_matches!($name, $value, {$($k => $v),*}, Ron);
    }}
}

/// Assets a `Serialize` snapshot in JSON format.
///
/// This works exactly like `assert_serialized_snapshot_matches` but serializes
/// in JSON format.  This is normally not recommended because it makes diffs
/// less reliable, but it can be useful for certain specialized situations.
///
/// Example:
///
/// ```no_run,ignore
/// assert_json_snapshot_matches!("snapshot_name", vec[1, 2, 3]);
/// ```
///
/// The third argument to the macro can be an object expression for redaction.
/// It's in the form `{ selector => replacement }`.  For more information
/// about redactions see [redactions](index.html#redactions).
#[macro_export]
macro_rules! assert_json_snapshot_matches {
    ($value:expr, @$snapshot:literal) => {{
        $crate::_assert_serialized_snapshot_matches!($value, Json, @$snapshot);
    }};
    ($value:expr, {$($k:expr => $v:expr),*}, @$snapshot:literal) => {{
        $crate::_assert_serialized_snapshot_matches!($value, {$($k => $v),*}, Json, @$snapshot);
    }};
    ($name:expr, $value:expr) => {{
        $crate::_assert_serialized_snapshot_matches!($name, $value, Json);
    }};
    ($name:expr, $value:expr, {$($k:expr => $v:expr),*}) => {{
        $crate::_assert_serialized_snapshot_matches!($name, $value, {$($k => $v),*}, Json);
    }}
}

#[doc(hidden)]
#[macro_export]
macro_rules! _assert_serialized_snapshot_matches {
    ($value:expr, $format:ident, @$snapshot:literal) => {{
        let value = $crate::_macro_support::serialize_value(
            &$value,
            $crate::_macro_support::SerializationFormat::$format
        );
        $crate::assert_snapshot_matches!(
            value,
            stringify!($value),
            @$snapshot
        );
    }};
    ($value:expr, {$($k:expr => $v:expr),*}, $format:ident, @$snapshot:literal) => {{
        let vec = vec![
            $((
                $crate::_macro_support::Selector::parse($k).unwrap(),
                $crate::_macro_support::Content::from($v)
            ),)*
        ];
        let value = $crate::_macro_support::serialize_value_redacted(
            &$value,
            &vec,
            $crate::_macro_support::SerializationFormat::$format
        );
        $crate::assert_snapshot_matches!(value, stringify!($value), @$snapshot);
    }};
    ($name:expr, $value:expr, $format:ident) => {{
        let value = $crate::_macro_support::serialize_value(
            &$value,
            $crate::_macro_support::SerializationFormat::$format
        );
        $crate::assert_snapshot_matches!(
            $name,
            value,
            stringify!($value)
        );
    }};
    ($name:expr, $value:expr, {$($k:expr => $v:expr),*}, $format:ident) => {{
        let vec = vec![
            $((
                $crate::_macro_support::Selector::parse($k).unwrap(),
                $crate::_macro_support::Content::from($v)
            ),)*
        ];
        let value = $crate::_macro_support::serialize_value_redacted(
            &$value,
            &vec,
            $crate::_macro_support::SerializationFormat::$format
        );
        $crate::assert_snapshot_matches!($name, value, stringify!($value));
    }}
}

/// Assets a `Debug` snapshot.
///
/// The value needs to implement the `fmt::Debug` trait.  This is useful for
/// simple values that do not implement the `Serialize` trait but does not
/// permit redactions.
#[macro_export]
macro_rules! assert_debug_snapshot_matches {
    ($value:expr, @$snapshot:literal) => {{
        let value = format!("{:#?}", $value);
        $crate::assert_snapshot_matches!(value, stringify!($value), @$snapshot);
    }};
    ($name:expr, $value:expr) => {{
        let value = format!("{:#?}", $value);
        $crate::assert_snapshot_matches!($name, value, stringify!($value));
    }};
}

/// Assets a string snapshot.
///
/// This is the most simplistic of all assertion methods.  It just accepts
/// a string to store as snapshot an does not apply any other transformations
/// on it.  This is useful to build ones own primitives.
///
/// ```no_run,ignore
/// assert_snapshot_matches!("snapshot_name", "reference value to snapshot");
/// ```
///
/// Optionally a third argument can be given as expression which will be
/// stringified as debug expression.  For more information on this look at the
/// source of this macro and other assertion macros.
#[macro_export]
macro_rules! assert_snapshot_matches {
    ($value:expr, @$snapshot:literal) => {
        $crate::_assert_snapshot_matches!(
            $crate::_macro_support::ReferenceValue::Inline($snapshot),
            $value,
            stringify!($value)
        )
    };
    ($value:expr, $debug_expr:expr, @$snapshot:literal) => {
        $crate::_assert_snapshot_matches!(
            $crate::_macro_support::ReferenceValue::Inline($snapshot),
            $value,
            $debug_expr
        )
    };
    ($name:expr, $value:expr) => {
        $crate::_assert_snapshot_matches!(
            $crate::_macro_support::ReferenceValue::Named($name),
            $value,
            stringify!($value)
        )
    };
    ($name:expr, $value:expr, $debug_expr:expr) => {
        $crate::_assert_snapshot_matches!(
            $crate::_macro_support::ReferenceValue::Named($name),
            $value,
            $debug_expr
        )
    };
}

#[doc(hidden)]
#[macro_export]
macro_rules! _assert_snapshot_matches {
    ($refval:expr, $value:expr, $debug_expr:expr) => {
        match &$value {
            value => {
                $crate::_macro_support::assert_snapshot(
                    $refval,
                    value,
                    env!("CARGO_MANIFEST_DIR"),
                    module_path!(),
                    file!(),
                    line!(),
                    $debug_expr,
                )
                .unwrap();
            }
        }
    };
}