Skip to content

Commit 2d0bb0d

Browse files
committed
jsondocck: Improved error messages for invalid json value and failed @count check
1 parent 4e8fb74 commit 2d0bb0d

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/tools/jsondocck/src/main.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,21 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
231231

232232
let val = cache.get_value(&command.args[0])?;
233233
let results = select(&val, &command.args[1]).unwrap();
234-
results.len() == expected
234+
let eq = results.len() == expected;
235+
if !command.negated && !eq {
236+
return Err(CkError::FailedCheck(
237+
format!(
238+
"`{}` matched to `{:?}` with length {}, but expected length {}",
239+
&command.args[1],
240+
results,
241+
results.len(),
242+
expected
243+
),
244+
command,
245+
));
246+
} else {
247+
eq
248+
}
235249
}
236250
CommandKind::Is => {
237251
// @has <path> <jsonpath> <value> = check *exactly one* item matched by path, and it equals value
@@ -317,6 +331,6 @@ fn string_to_value<'a>(s: &str, cache: &'a Cache) -> Cow<'a, Value> {
317331
panic!("No variable: `{}`. Current state: `{:?}`", &s[1..], cache.variables)
318332
}))
319333
} else {
320-
Cow::Owned(serde_json::from_str(s).unwrap())
334+
Cow::Owned(serde_json::from_str(s).expect(&format!("Cannot convert `{}` to json", s)))
321335
}
322336
}

0 commit comments

Comments
 (0)