1 use assert_json_diff::{
2     assert_json_eq, assert_json_include, assert_json_matches, assert_json_matches_no_panic,
3     CompareMode, Config, NumericMode,
4 };
5 use serde::Serialize;
6 use serde_json::json;
7 
8 #[test]
can_pass()9 fn can_pass() {
10     assert_json_include!(
11         actual: json!({ "a": { "b": true }, "c": [true, null, 1] }),
12         expected: json!({ "a": { "b": true }, "c": [true, null, 1] })
13     );
14 
15     assert_json_include!(
16         actual: json!({ "a": { "b": true } }),
17         expected: json!({ "a": {} })
18     );
19 
20     assert_json_include!(
21         actual: json!({ "a": { "b": true } }),
22         expected: json!({ "a": {} }),
23     );
24 
25     assert_json_include!(
26         expected: json!({ "a": {} }),
27         actual: json!({ "a": { "b": true } }),
28     );
29 }
30 
31 #[test]
32 #[should_panic]
can_fail()33 fn can_fail() {
34     assert_json_include!(
35         actual: json!({ "a": { "b": true }, "c": [true, null, 1] }),
36         expected: json!({ "a": { "b": false }, "c": [false, null, {}] })
37     );
38 }
39 
40 #[test]
41 #[should_panic]
different_numeric_types_include_should_fail()42 fn different_numeric_types_include_should_fail() {
43     assert_json_include!(
44         actual: json!({ "a": { "b": true }, "c": 1 }),
45         expected: json!({ "a": { "b": true }, "c": 1.0 })
46     );
47 }
48 
49 #[test]
50 #[should_panic]
different_numeric_types_eq_should_fail()51 fn different_numeric_types_eq_should_fail() {
52     assert_json_eq!(
53         json!({ "a": { "b": true }, "c": 1 }),
54         json!({ "a": { "b": true }, "c": 1.0 })
55     );
56 }
57 
58 #[test]
different_numeric_types_assume_float()59 fn different_numeric_types_assume_float() {
60     let actual = json!({ "a": { "b": true }, "c": [true, null, 1] });
61     let expected = json!({ "a": { "b": true }, "c": [true, null, 1.0] });
62     let config = Config::new(CompareMode::Inclusive).numeric_mode(NumericMode::AssumeFloat);
63     assert_json_matches!(&actual, &expected, config.clone());
64 
65     assert_json_matches!(actual, expected, config.compare_mode(CompareMode::Strict))
66 }
67 
68 #[test]
can_pass_with_exact_match()69 fn can_pass_with_exact_match() {
70     assert_json_eq!(json!({ "a": { "b": true } }), json!({ "a": { "b": true } }));
71     assert_json_eq!(json!({ "a": { "b": true } }), json!({ "a": { "b": true } }),);
72 }
73 
74 #[test]
75 #[should_panic]
can_fail_with_exact_match()76 fn can_fail_with_exact_match() {
77     assert_json_eq!(json!({ "a": { "b": true } }), json!({ "a": {} }));
78 }
79 
80 #[test]
inclusive_match_without_panicking()81 fn inclusive_match_without_panicking() {
82     assert!(assert_json_matches_no_panic(
83         &json!({ "a": 1, "b": 2 }),
84         &json!({ "b": 2}),
85         Config::new(CompareMode::Inclusive,).numeric_mode(NumericMode::Strict),
86     )
87     .is_ok());
88 
89     assert!(assert_json_matches_no_panic(
90         &json!({ "a": 1, "b": 2 }),
91         &json!("foo"),
92         Config::new(CompareMode::Inclusive,).numeric_mode(NumericMode::Strict),
93     )
94     .is_err());
95 }
96 
97 #[test]
exact_match_without_panicking()98 fn exact_match_without_panicking() {
99     assert!(assert_json_matches_no_panic(
100         &json!([1, 2, 3]),
101         &json!([1, 2, 3]),
102         Config::new(CompareMode::Strict).numeric_mode(NumericMode::Strict)
103     )
104     .is_ok());
105 
106     assert!(assert_json_matches_no_panic(
107         &json!([1, 2, 3]),
108         &json!("foo"),
109         Config::new(CompareMode::Strict).numeric_mode(NumericMode::Strict)
110     )
111     .is_err());
112 }
113 
114 #[derive(Serialize)]
115 struct User {
116     id: i32,
117     username: String,
118 }
119 
120 #[test]
include_with_serializable()121 fn include_with_serializable() {
122     let user = User {
123         id: 1,
124         username: "bob".to_string(),
125     };
126 
127     assert_json_include!(
128         actual: json!({
129             "id": 1,
130             "username": "bob",
131             "email": "bob@example.com"
132         }),
133         expected: user,
134     );
135 }
136 
137 #[test]
include_with_serializable_ref()138 fn include_with_serializable_ref() {
139     let user = User {
140         id: 1,
141         username: "bob".to_string(),
142     };
143 
144     assert_json_include!(
145         actual: &json!({
146              "id": 1,
147              "username": "bob",
148              "email": "bob@example.com"
149          }),
150         expected: &user,
151     );
152 }
153 
154 #[test]
eq_with_serializable()155 fn eq_with_serializable() {
156     let user = User {
157         id: 1,
158         username: "bob".to_string(),
159     };
160 
161     assert_json_eq!(
162         json!({
163             "id": 1,
164             "username": "bob"
165         }),
166         user,
167     );
168 }
169 
170 #[test]
eq_with_serializable_ref()171 fn eq_with_serializable_ref() {
172     let user = User {
173         id: 1,
174         username: "bob".to_string(),
175     };
176 
177     assert_json_eq!(
178         &json!({
179             "id": 1,
180             "username": "bob"
181         }),
182         &user,
183     );
184 }
185