2023-03-10 01:12:46 -08:00
|
|
|
use parser_combinator::primitives::literal;
|
|
|
|
use parser_combinator::testutil::{
|
|
|
|
json_array, json_bool, json_null, json_number, json_object, json_string, JsonValue,
|
|
|
|
};
|
|
|
|
use parser_combinator::{Parser, Representation};
|
2022-10-23 00:31:59 -07:00
|
|
|
|
2022-12-04 00:22:33 -08:00
|
|
|
use proptest::prelude::*;
|
|
|
|
|
2023-02-26 04:29:15 -08:00
|
|
|
use rstest::*;
|
|
|
|
|
2022-12-04 00:22:33 -08:00
|
|
|
proptest! {
|
|
|
|
#[test]
|
|
|
|
fn doesnt_crash(s in "\\PC*") {
|
|
|
|
let _output = json_object().parse(&s);
|
|
|
|
}
|
2022-12-05 00:46:53 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_string(s in r#"[^"]+"#) {
|
|
|
|
let input = format!("\"{}\"", s);
|
|
|
|
let output = json_string().parse(&input).unwrap();
|
|
|
|
match output {
|
|
|
|
(JsonValue::Str(output_s), "") if output_s == s => (),
|
|
|
|
_ => panic!(),
|
|
|
|
}
|
|
|
|
}
|
2022-12-04 00:22:33 -08:00
|
|
|
}
|
|
|
|
|
2022-10-23 00:31:59 -07:00
|
|
|
#[test]
|
|
|
|
fn test_parsing() {
|
2023-01-22 14:25:10 -08:00
|
|
|
let output = literal("a").parse("a yolo");
|
2022-10-23 00:31:59 -07:00
|
|
|
assert_eq!(output.unwrap(), ("a", " yolo"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_json_primitives() {
|
|
|
|
assert_eq!(
|
|
|
|
json_string().parse(r#""yolo swagg""#).unwrap(),
|
|
|
|
(JsonValue::Str("yolo swagg".into()), "")
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
json_number().parse("-383").unwrap().0,
|
|
|
|
JsonValue::Num(-383f64)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
json_number().parse("-.383").unwrap().0,
|
|
|
|
JsonValue::Num(-0.383)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
json_number().parse(".383").unwrap().0,
|
|
|
|
JsonValue::Num(0.383)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
json_number().parse("-1.383").unwrap().0,
|
|
|
|
JsonValue::Num(-1.383)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-02-26 04:41:03 -08:00
|
|
|
#[rstest]
|
|
|
|
#[case(r#"[ 4, 9, "ara",]"#)]
|
|
|
|
fn parse_json_array_err(#[case] input: &str) {
|
|
|
|
assert!(json_array().parse(input).is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[rstest]
|
|
|
|
#[case("[[],[]]", (JsonValue::Array(vec![JsonValue::Array(vec![]), JsonValue::Array(vec![])]), ""))]
|
|
|
|
#[case(r#"[ 4, 9, "foo" ]"#, (
|
2022-10-23 00:31:59 -07:00
|
|
|
JsonValue::Array(vec![
|
|
|
|
JsonValue::Num(4.),
|
|
|
|
JsonValue::Num(9.0),
|
|
|
|
JsonValue::Str("foo".to_string())
|
|
|
|
]),
|
|
|
|
""
|
2023-02-26 04:41:03 -08:00
|
|
|
))]
|
|
|
|
#[case(r#"[8,null,[],5],{}"#,
|
2022-10-23 00:31:59 -07:00
|
|
|
(
|
|
|
|
JsonValue::Array(vec![
|
|
|
|
JsonValue::Num(8.),
|
|
|
|
JsonValue::Null,
|
|
|
|
JsonValue::Array(vec![]),
|
|
|
|
JsonValue::Num(5.),
|
|
|
|
]),
|
|
|
|
",{}"
|
2023-02-26 04:41:03 -08:00
|
|
|
))]
|
|
|
|
fn parse_json_array(#[case] input: &str, #[case] expected: (JsonValue, &str)) {
|
|
|
|
assert_eq!(json_array().parse(input).unwrap(), expected);
|
2022-10-23 00:31:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_json_object() {
|
|
|
|
assert_eq!(
|
|
|
|
json_object().parse(r#"{ "a": 23}"#).unwrap().0,
|
|
|
|
JsonValue::Object(vec![("a".into(), JsonValue::Num(23.))])
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
json_object().parse(r#"{}"#).unwrap().0,
|
|
|
|
JsonValue::Object(vec![])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-01-22 14:26:46 -08:00
|
|
|
fn parse_json_document() {
|
2022-10-23 00:31:59 -07:00
|
|
|
let test_json = include_str!("joplin-cfg.json");
|
|
|
|
let parsed_json = json_object().parse(test_json);
|
|
|
|
assert!(parsed_json.is_ok());
|
|
|
|
}
|
2023-02-25 03:00:29 -08:00
|
|
|
|
2023-02-26 04:29:15 -08:00
|
|
|
#[rstest]
|
|
|
|
#[case(json_null().representation(), Representation::new("null"))]
|
|
|
|
#[case(json_bool().representation(), Representation::new("true | false"))]
|
|
|
|
#[case(json_number().representation(), Representation::new("- | ε (1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0 | )+ . (1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0 | )+ | ε | . (1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0 | )+"))]
|
|
|
|
fn representations_test(
|
|
|
|
#[case] parser_representation: Representation,
|
|
|
|
#[case] expected: Representation,
|
|
|
|
) {
|
|
|
|
assert_eq!(parser_representation, expected);
|
2023-02-25 03:00:29 -08:00
|
|
|
}
|