char/avril

(wip) why'd you have to go and make things so complicaaaaated?

git clone https://git.t4t.associates/char/avril

Charlotte Somrearrange libs into lib and extensions into extsf4a0f5b

main
4.3 KiB143 linesraw
1use serde_fjson::{parse, stringify};
2use serde_json::{Value, json};
3
4#[test]
5fn parses_every_json_root_and_disambiguates_implicit_keys() {
6    for (source, expected) in [
7        ("null", Value::Null),
8        ("false /* tail */", json!(false)),
9        ("// head\n42", json!(42)),
10        ("-1.25e2", json!(-125.0)),
11        ("'šŸ˜€'", json!("šŸ˜€")),
12        (
13            "[1\ntrue\nnull\n{label: 'ok'}]",
14            json!([1, true, null, {"label": "ok"}]),
15        ),
16        ("{}", json!({})),
17        ("// empty", json!({})),
18        ("true /* : */ : false", json!({"true": false})),
19        ("'quoted'\n/* keep */ : 1", json!({"quoted": 1})),
20        ("'not a key' /* : */", json!("not a key")),
21        (
22            "name: 'fJSON'\nflags: [true, false,]",
23            json!({"name": "fJSON", "flags": [true, false]}),
24        ),
25    ] {
26        assert_eq!(parse(source).unwrap(), expected, "{source}");
27    }
28}
29
30#[test]
31fn numbers_convert_with_json_range_errors_at_the_original_span() {
32    assert_eq!(
33        parse("9007199254740993").unwrap(),
34        json!(9007199254740993_u64)
35    );
36    assert_eq!(parse("18446744073709551615").unwrap(), json!(u64::MAX));
37    let source = "number: 1e9999";
38    let error = parse(source).unwrap_err();
39    assert_eq!(&source[error.span.clone()], "1e9999");
40    assert_eq!(error.kind, serde_fjson::ErrorKind::Number);
41    assert_eq!(
42        error.render("test.fjson", source),
43        "test.fjson:1:9: Invalid or out-of-range JSON number"
44    );
45}
46
47#[test]
48fn rejects_trailing_values_duplicates_and_invalid_roots() {
49    for source in [
50        "1 2",
51        "'a' 'b'",
52        "[1],",
53        "true, false",
54        "bare",
55        "a: 1\na: 2",
56        "{a: 1} extra: 2",
57    ] {
58        assert!(parse(source).is_err(), "{source}");
59    }
60    let source = "a: 'šŸ˜€'\n'\\u0061': false";
61    let error = parse(source).unwrap_err();
62    assert_eq!(
63        error.render("test", source),
64        "test:2:1: Duplicate key \"a\"\ntest:1:1: First defined here"
65    );
66}
67
68#[test]
69fn pretty_output_uses_fjson_keys_and_balanced_nested_indentation() {
70    let value = json!({
71        "name": "fJSON",
72        "options": {"enabled": true, "targets": ["linux", "macos"]},
73        "@scope/pkg": {"$valid_key2": "šŸ˜€\nquoted \"text\""},
74    });
75    let printed = stringify(&value).unwrap();
76    assert_eq!(
77        printed,
78        r#"name: "fJSON"
79options: {
80  enabled: true
81  targets: ["linux", "macos"]
82}
83"@scope/pkg": {
84  $valid_key2: "šŸ˜€\nquoted \"text\""
85}"#
86    );
87    assert_eq!(parse(&printed).unwrap(), value);
88}
89
90#[test]
91fn long_arrays_expand_and_printing_is_stable() {
92    let value = json!({
93        "records": [{"x": [1, 2]}, {"x": [null, false]}],
94        "long": (0..40).collect::<Vec<_>>(),
95    });
96    let text = stringify(&value).unwrap();
97    assert!(text.contains("long: [\n"));
98    assert!(text.lines().all(|line| line.chars().count() <= 80));
99    assert!(text.contains("    x: [1, 2]\n  }"));
100    assert_eq!(parse(&text).unwrap(), value);
101    assert_eq!(stringify(&parse(&text).unwrap()).unwrap(), text);
102}
103
104#[test]
105fn parser_and_printer_accept_the_same_nesting_depths() {
106    for leaf in [json!(null), json!([]), json!({})] {
107        let mut value = leaf;
108        for _ in 0..132 {
109            for value in [&value, &json!({"root": value})] {
110                let encoded = serde_json::to_string(value).unwrap();
111                let parsed = parse(&encoded);
112                let printed = stringify(value);
113                assert_eq!(printed.is_ok(), parsed.is_ok(), "{encoded}");
114                match printed {
115                    Ok(text) => assert_eq!(parse(&text).unwrap(), *value),
116                    Err(error) => {
117                        assert_eq!(error.kind, serde_fjson::ErrorKind::Nesting);
118                        assert_eq!(parsed.unwrap_err().kind, error.kind);
119                    }
120                }
121            }
122            value = json!([value]);
123        }
124    }
125}
126
127#[test]
128fn arbitrary_json_values_round_trip_including_escaped_keys() {
129    for value in [
130        json!(null),
131        json!(true),
132        json!(42),
133        json!(-0.125),
134        json!("\0\tšŸ˜€"),
135        json!([]),
136        json!({}),
137        json!([{}, [], null]),
138        json!({"0": 1, "": 2, "true": 3, "some-key": 4, "ę—„ęœ¬čŖž": "\r\n", "__proto__": {"ok": true}}),
139    ] {
140        let text = stringify(&value).unwrap();
141        assert_eq!(parse(&text).unwrap(), value, "{text}");
142    }
143}