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
//! Matchers for `serde_json::Value`.
use crate::error::MatchError;
use crate::expectation::Expectation;
/// Recursively checks if `actual` is a superset of `expected`.
///
/// For objects, every key in `expected` must exist in `actual` with a matching
/// value (extra keys in `actual` are allowed). Arrays and primitives require
/// exact equality.
fn json_is_superset(actual: &serde_json::Value, expected: &serde_json::Value) -> bool {
match (actual, expected) {
(serde_json::Value::Object(a), serde_json::Value::Object(e)) => e
.iter()
.all(|(k, ev)| a.get(k).is_some_and(|av| json_is_superset(av, ev))),
(a, e) => a == e,
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "json")))]
impl Expectation<serde_json::Value> {
/// Asserts the JSON value is an object containing the given field.
///
/// # Errors
///
/// Returns [`MatchError`] if the value is not an object or lacks the field.
///
/// # Examples
///
/// ```
/// use behave::Expectation;
/// use serde_json::json;
///
/// let v = json!({"name": "Alice"});
/// let result = Expectation::new(v, "v").to_have_field("name");
/// assert!(result.is_ok());
/// ```
pub fn to_have_field(&self, field: &str) -> Result<(), MatchError> {
let has = self
.value()
.as_object()
.is_some_and(|obj| obj.contains_key(field));
self.check(has, format!("to have field {field:?}"))
}
/// Asserts the JSON object has a field with the given value.
///
/// # Errors
///
/// Returns [`MatchError`] if the field is missing or has a different value.
///
/// # Examples
///
/// ```
/// use behave::Expectation;
/// use serde_json::json;
///
/// let v = json!({"age": 30});
/// let result = Expectation::new(v, "v")
/// .to_have_field_value("age", &json!(30));
/// assert!(result.is_ok());
/// ```
pub fn to_have_field_value(
&self,
field: &str,
expected: &serde_json::Value,
) -> Result<(), MatchError> {
let has = self
.value()
.as_object()
.and_then(|obj| obj.get(field))
.is_some_and(|v| v == expected);
self.check(
has,
format!("to have field {field:?} with value {expected}"),
)
}
/// Asserts the JSON value is a superset of the expected value.
///
/// For objects, every key in `expected` must exist in the actual value
/// with a matching value (extra keys in `actual` are allowed). This is
/// similar to Jest's `toMatchObject`.
///
/// # Errors
///
/// Returns [`MatchError`] if the actual value does not contain all
/// expected fields and values.
///
/// # Examples
///
/// ```
/// use behave::Expectation;
/// use serde_json::json;
///
/// let actual = json!({"name": "Alice", "age": 30, "city": "NYC"});
/// let expected = json!({"name": "Alice", "age": 30});
/// let result = Expectation::new(actual, "v")
/// .to_be_json_superset_of(&expected);
/// assert!(result.is_ok());
/// ```
pub fn to_be_json_superset_of(&self, expected: &serde_json::Value) -> Result<(), MatchError> {
let is_match = json_is_superset(self.value(), expected);
self.check(is_match, format!("to be a JSON superset of {expected}"))
}
}
#[cfg(test)]
mod tests {
use crate::Expectation;
use serde_json::json;
// --- to_have_field ---
#[test]
fn to_have_field_pass() {
let v = json!({"name": "Alice"});
assert!(Expectation::new(v, "v").to_have_field("name").is_ok());
}
#[test]
fn to_have_field_fail() {
let v = json!({"name": "Alice"});
assert!(Expectation::new(v, "v").to_have_field("age").is_err());
}
#[test]
fn to_have_field_not_object() {
let v = json!(42);
assert!(Expectation::new(v, "v").to_have_field("x").is_err());
}
#[test]
fn to_have_field_negated() {
let v = json!({"name": "Alice"});
assert!(Expectation::new(v, "v")
.negate()
.to_have_field("age")
.is_ok());
}
// --- to_have_field_value ---
#[test]
fn to_have_field_value_pass() {
let v = json!({"age": 30});
assert!(Expectation::new(v, "v")
.to_have_field_value("age", &json!(30))
.is_ok());
}
#[test]
fn to_have_field_value_fail_wrong_value() {
let v = json!({"age": 30});
assert!(Expectation::new(v, "v")
.to_have_field_value("age", &json!(25))
.is_err());
}
#[test]
fn to_have_field_value_fail_missing() {
let v = json!({"age": 30});
assert!(Expectation::new(v, "v")
.to_have_field_value("name", &json!("Alice"))
.is_err());
}
#[test]
fn to_have_field_value_negated() {
let v = json!({"age": 30});
assert!(Expectation::new(v, "v")
.negate()
.to_have_field_value("age", &json!(25))
.is_ok());
}
// --- to_be_json_superset_of ---
#[test]
fn superset_pass() {
let actual = json!({"name": "Alice", "age": 30, "city": "NYC"});
let expected = json!({"name": "Alice", "age": 30});
assert!(Expectation::new(actual, "v")
.to_be_json_superset_of(&expected)
.is_ok());
}
#[test]
fn superset_fail_missing_key() {
let actual = json!({"name": "Alice"});
let expected = json!({"name": "Alice", "age": 30});
assert!(Expectation::new(actual, "v")
.to_be_json_superset_of(&expected)
.is_err());
}
#[test]
fn superset_fail_wrong_value() {
let actual = json!({"name": "Alice", "age": 25});
let expected = json!({"name": "Alice", "age": 30});
assert!(Expectation::new(actual, "v")
.to_be_json_superset_of(&expected)
.is_err());
}
#[test]
fn superset_nested() {
let actual = json!({"user": {"name": "Alice", "age": 30}});
let expected = json!({"user": {"name": "Alice"}});
assert!(Expectation::new(actual, "v")
.to_be_json_superset_of(&expected)
.is_ok());
}
#[test]
fn superset_exact_primitives() {
let actual = json!(42);
let expected = json!(42);
assert!(Expectation::new(actual, "v")
.to_be_json_superset_of(&expected)
.is_ok());
}
#[test]
fn superset_array_exact() {
let actual = json!([1, 2, 3]);
let expected = json!([1, 2, 3]);
assert!(Expectation::new(actual, "v")
.to_be_json_superset_of(&expected)
.is_ok());
}
#[test]
fn superset_array_not_partial() {
let actual = json!([1, 2, 3]);
let expected = json!([1, 2]);
assert!(Expectation::new(actual, "v")
.to_be_json_superset_of(&expected)
.is_err());
}
#[test]
fn superset_negated() {
let actual = json!({"name": "Alice"});
let expected = json!({"name": "Alice", "age": 30});
assert!(Expectation::new(actual, "v")
.negate()
.to_be_json_superset_of(&expected)
.is_ok());
}
}