Check for empty input
All checks were successful
timw4mail/json-parser/pipeline/head This commit looks good

This commit is contained in:
Timothy Warren 2021-04-16 23:04:57 -04:00
parent a6d6d1a36c
commit f229208280
2 changed files with 16 additions and 2 deletions

View File

@ -654,6 +654,12 @@ impl JSON {
/// # assert!(parse_result.is_ok(), "Parse method example failed");
/// ```
pub fn parse(json: &str) -> JSONResult {
if json.len() == 0 {
return Err(ParseError::UnexpectedEndOfInput(format!(
"Empty input"
)))
}
JSON::new(json).parse_value()
}
}

View File

@ -26,12 +26,20 @@ fn bad_object_trailing_comma() {
fn bad_json() {
let res = JSON::parse(r#"5eq"#);
assert!(res.is_err());
println!("{:#?}", res);
}
#[test]
fn empty_json() {
let res: JSONResult = JSON::parse("");
assert_eq!(
res,
Err(ParseError::UnexpectedEndOfInput(String::from("Empty input")))
)
}
#[test]
#[should_panic]
fn invalid_unwrap() {
let wrapped = JSONValue::from(5.2f64);
let invalid: String = wrapped.unwrap();
let _: String = wrapped.unwrap();
}