1
- //! This crate includes macros for comparing two JSON values. It is designed to give much
2
- //! more helpful error messages than the standard [`assert_eq!`]. It basically does a diff of the
3
- //! two objects and tells you the exact differences. This is useful when asserting that two large
4
- //! JSON objects are the same.
1
+ //! This crate includes macros for comparing two serializable values by diffing their JSON
2
+ //! representations. It is designed to give much more helpful error messages than the standard
3
+ //! [`assert_eq!`]. It basically does a diff of the two objects and tells you the exact
4
+ //! differences. This is useful when asserting that two large JSON objects are the same.
5
5
//!
6
- //! It uses the [`serde_json::Value`] type to represent JSON .
6
+ //! It uses the [serde] and [serde_json] to perform the serialization .
7
7
//!
8
- //! [`serde_json::Value`]: https://docs.serde.rs/serde_json/value/enum.Value.html
8
+ //! [serde]: https://crates.io/crates/serde
9
+ //! [serde_json]: https://crates.io/crates/serde_json
9
10
//! [`assert_eq!`]: https://doc.rust-lang.org/std/macro.assert_eq.html
10
11
//!
11
12
//! ## Partial matching
14
15
//! [`assert_json_include`](macro.assert_json_include.html):
15
16
//!
16
17
//! ```should_panic
17
- //! #[macro_use]
18
- //! extern crate assert_json_diff;
19
- //! #[macro_use]
20
- //! extern crate serde_json;
18
+ //! use assert_json_diff::assert_json_include;
19
+ //! use serde_json::json;
21
20
//!
22
21
//! fn main() {
23
22
//! let a = json!({
82
81
//! of the JSON without having to specify the whole thing. For example this test passes:
83
82
//!
84
83
//! ```
85
- //! #[macro_use]
86
- //! extern crate assert_json_diff;
87
- //! #[macro_use]
88
- //! extern crate serde_json;
84
+ //! use assert_json_diff::assert_json_include;
85
+ //! use serde_json::json;
89
86
//!
90
87
//! fn main() {
91
88
//! assert_json_include!(
102
99
//! However `expected` cannot contain additional data so this test fails:
103
100
//!
104
101
//! ```should_panic
105
- //! #[macro_use]
106
- //! extern crate assert_json_diff;
107
- //! #[macro_use]
108
- //! extern crate serde_json;
102
+ //! use assert_json_diff::assert_json_include;
103
+ //! use serde_json::json;
109
104
//!
110
105
//! fn main() {
111
106
//! assert_json_include!(
130
125
//! If you want to ensure two JSON values are *exactly* the same, use [`assert_json_eq`](macro.assert_json_eq.html).
131
126
//!
132
127
//! ```rust,should_panic
133
- //! #[macro_use]
134
- //! extern crate assert_json_diff;
135
- //! #[macro_use]
136
- //! extern crate serde_json;
128
+ //! use assert_json_diff::assert_json_eq;
129
+ //! use serde_json::json;
137
130
//!
138
131
//! fn main() {
139
132
//! assert_json_eq!(
164
157
) ]
165
158
#![ doc( html_root_url = "https://docs.rs/assert-json-diff/1.0.3" ) ]
166
159
167
- extern crate serde;
168
- #[ allow( unused_imports) ]
169
- #[ macro_use]
170
- extern crate serde_json;
171
-
172
160
use diff:: { diff, Mode } ;
173
- use serde_json :: Value ;
161
+ use serde :: Serialize ;
174
162
175
163
mod core_ext;
176
164
mod diff;
@@ -184,8 +172,8 @@ mod diff;
184
172
#[ macro_export]
185
173
macro_rules! assert_json_include {
186
174
( actual: $actual: expr, expected: $expected: expr) => { {
187
- let actual: serde_json :: Value = $actual;
188
- let expected: serde_json :: Value = $expected;
175
+ let actual = $actual;
176
+ let expected = $expected;
189
177
if let Err ( error) = $crate:: assert_json_include_no_panic( & actual, & expected) {
190
178
panic!( "\n \n {}\n \n " , error) ;
191
179
}
@@ -209,8 +197,8 @@ macro_rules! assert_json_include {
209
197
#[ macro_export]
210
198
macro_rules! assert_json_eq {
211
199
( $lhs: expr, $rhs: expr) => { {
212
- let lhs: serde_json :: Value = $lhs;
213
- let rhs: serde_json :: Value = $rhs;
200
+ let lhs = $lhs;
201
+ let rhs = $rhs;
214
202
if let Err ( error) = $crate:: assert_json_eq_no_panic( & lhs, & rhs) {
215
203
panic!( "\n \n {}\n \n " , error) ;
216
204
}
@@ -225,7 +213,14 @@ macro_rules! assert_json_eq {
225
213
/// Instead it returns a `Result` where the error is the message that would be passed to `panic!`.
226
214
/// This is might be useful if you want to control how failures are reported and don't want to deal
227
215
/// with panics.
228
- pub fn assert_json_include_no_panic ( actual : & Value , expected : & Value ) -> Result < ( ) , String > {
216
+ pub fn assert_json_include_no_panic < Actual , Expected > (
217
+ actual : & Actual ,
218
+ expected : & Expected ,
219
+ ) -> Result < ( ) , String >
220
+ where
221
+ Actual : Serialize ,
222
+ Expected : Serialize ,
223
+ {
229
224
assert_json_no_panic ( actual, expected, Mode :: Lenient )
230
225
}
231
226
@@ -234,12 +229,33 @@ pub fn assert_json_include_no_panic(actual: &Value, expected: &Value) -> Result<
234
229
/// Instead it returns a `Result` where the error is the message that would be passed to `panic!`.
235
230
/// This is might be useful if you want to control how failures are reported and don't want to deal
236
231
/// with panics.
237
- pub fn assert_json_eq_no_panic ( lhs : & Value , rhs : & Value ) -> Result < ( ) , String > {
232
+ pub fn assert_json_eq_no_panic < Lhs , Rhs > ( lhs : & Lhs , rhs : & Rhs ) -> Result < ( ) , String >
233
+ where
234
+ Lhs : Serialize ,
235
+ Rhs : Serialize ,
236
+ {
238
237
assert_json_no_panic ( lhs, rhs, Mode :: Strict )
239
238
}
240
239
241
- fn assert_json_no_panic ( lhs : & Value , rhs : & Value , mode : Mode ) -> Result < ( ) , String > {
242
- let diffs = diff ( lhs, rhs, mode) ;
240
+ fn assert_json_no_panic < Lhs , Rhs > ( lhs : & Lhs , rhs : & Rhs , mode : Mode ) -> Result < ( ) , String >
241
+ where
242
+ Lhs : Serialize ,
243
+ Rhs : Serialize ,
244
+ {
245
+ let lhs = serde_json:: to_value ( lhs) . unwrap_or_else ( |err| {
246
+ panic ! (
247
+ "Couldn't convert left hand side value to JSON. Serde error: {}" ,
248
+ err
249
+ )
250
+ } ) ;
251
+ let rhs = serde_json:: to_value ( rhs) . unwrap_or_else ( |err| {
252
+ panic ! (
253
+ "Couldn't convert right hand side value to JSON. Serde error: {}" ,
254
+ err
255
+ )
256
+ } ) ;
257
+
258
+ let diffs = diff ( & lhs, & rhs, mode) ;
243
259
244
260
if diffs. is_empty ( ) {
245
261
Ok ( ( ) )
@@ -256,6 +272,7 @@ fn assert_json_no_panic(lhs: &Value, rhs: &Value, mode: Mode) -> Result<(), Stri
256
272
#[ cfg( test) ]
257
273
mod tests {
258
274
use super :: * ;
275
+ use serde_json:: { json, Value } ;
259
276
use std:: fmt:: Write ;
260
277
261
278
#[ test]
0 commit comments