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
2.3 KiB65 linesraw
1use serde_fjson::{Document, Options, parse_with, stringify_with, syntax};
2use serde_json::json;
3
4fn nested_arrays(depth: usize) -> String {
5    format!("{}0{}", "[".repeat(depth), "]".repeat(depth))
6}
7
8#[test]
9fn custom_limits_control_parsing() {
10    let source = nested_arrays(4);
11    parse_with(&source, &Options::new().with_max_depth(3)).unwrap();
12    let error = parse_with(&source, &Options::new().with_max_depth(2)).unwrap_err();
13    assert_eq!(error.kind, serde_fjson::ErrorKind::Nesting);
14    assert!(error.message.contains("2 levels"), "{error}");
15}
16
17#[test]
18fn custom_limits_control_printing_and_agree_with_parsing() {
19    let mut value = json!(null);
20    for _ in 0..6 {
21        value = json!([value]);
22    }
23    let options = Options::new().with_max_depth(6);
24    let text = stringify_with(&value, &options).unwrap();
25    assert_eq!(parse_with(&text, &options).unwrap(), value);
26    let tight = Options::new().with_max_depth(4);
27    assert_eq!(
28        stringify_with(&value, &tight).unwrap_err().kind,
29        serde_fjson::ErrorKind::Nesting
30    );
31    assert!(parse_with(&text, &tight).is_err());
32}
33
34#[test]
35fn syntax_parsers_accept_custom_limits() {
36    let source = format!("value: {}", nested_arrays(4));
37    syntax::parse_object_with(&source, &Options::new().with_max_depth(5)).unwrap();
38    syntax::parse_object_with(&source, &Options::new().with_max_depth(3)).unwrap_err();
39    syntax::parse_with(&source, &Options::new().with_max_depth(5)).unwrap();
40}
41
42#[test]
43fn documents_remember_their_options_for_later_edits() {
44    let options = Options::new().with_max_depth(10);
45    let mut document = Document::parse_with("value: [[[0]]]", &options).unwrap();
46    assert_eq!(document.options(), options);
47    let mut deep = json!(null);
48    for _ in 0..10 {
49        deep = json!([deep]);
50    }
51    document.set(&["value".into()], &deep).unwrap();
52    assert_eq!(document.to_value().unwrap(), json!({"value": deep}));
53}
54
55#[test]
56fn defaults_reject_what_custom_options_allow() {
57    assert_eq!(Options::default().max_depth, Options::DEFAULT_MAX_DEPTH);
58    let source = nested_arrays(Options::DEFAULT_MAX_DEPTH + 2);
59    assert!(syntax::parse(&source).is_err());
60    syntax::parse_with(
61        &source,
62        &Options::new().with_max_depth(Options::DEFAULT_MAX_DEPTH + 1),
63    )
64    .unwrap();
65}