char/avril
(wip) why'd you have to go and make things so complicaaaaated?
git clone https://git.t4t.associates/char/avril
f4a0f5b
main
1use serde_fjson::{ parse, stringify}; 2use serde_json::{ Value , json}; 3 4# [ test ] 5fn parses_every_json_root_and_disambiguates_implicit_keys () { 6for ( 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'}]" , 14json! ([ 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,]" , 23json! ({ "name" : "fJSON" , "flags" : [ true , false ]}), 24), 25] { 26assert_eq! ( parse ( source). unwrap (), expected, "{source}" ); 27} 28} 29 30# [ test ] 31fn numbers_convert_with_json_range_errors_at_the_original_span () { 32assert_eq! ( 33parse ( "9007199254740993" ). unwrap (), 34json! ( 9007199254740993_u64 ) 35); 36assert_eq! ( parse ( "18446744073709551615" ). unwrap (), json! ( u64 :: MAX )); 37let source ="number: 1e9999" ; 38let error =parse ( source). unwrap_err (); 39assert_eq! ( & source[ error. span . clone ()], "1e9999" ); 40assert_eq! ( error. kind , serde_fjson:: ErrorKind :: Number ); 41assert_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 () { 49for sourcein [ 50"1 2" , 51"'a' 'b'" , 52"[1]," , 53"true, false" , 54"bare" , 55"a: 1\na: 2" , 56"{a: 1} extra: 2" , 57] { 58assert! ( parse ( source). is_err (), "{source}" ); 59} 60let source ="a: 'š'\n'\\u0061': false" ; 61let error =parse ( source). unwrap_err (); 62assert_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 () { 70let value =json! ({ 71"name" : "fJSON" , 72"options" : { "enabled" : true , "targets" : [ "linux" , "macos" ]}, 73"@scope/pkg" : { "$valid_key2" : "š\nquoted \"text\"" }, 74}); 75let printed =stringify ( & value). unwrap (); 76assert_eq! ( 77 printed, 78r#"name: "fJSON" 79options: { 80enabled: true 81targets: ["linux", "macos"] 82} 83"@scope/pkg": { 84$valid_key2: "š\nquoted \"text\"" 85}"# 86); 87assert_eq! ( parse ( & printed). unwrap (), value); 88} 89 90# [ test ] 91fn long_arrays_expand_and_printing_is_stable () { 92let value =json! ({ 93"records" : [{ "x" : [ 1 , 2 ]}, { "x" : [ null, false ]}], 94"long" : ( 0 ..40 ). collect ::< Vec < _ >>(), 95}); 96let text =stringify ( & value). unwrap (); 97assert! ( text. contains ( "long: [\n" )); 98assert! ( text. lines (). all ( |line| line. chars (). count () <=80 )); 99assert! ( text. contains ( " x: [1, 2]\n }" )); 100assert_eq! ( parse ( & text). unwrap (), value); 101assert_eq! ( stringify ( & parse ( & text). unwrap ()). unwrap (), text); 102} 103 104# [ test ] 105fn parser_and_printer_accept_the_same_nesting_depths () { 106for leafin [ json! ( null), json! ([]), json! ({})] { 107let mut value = leaf; 108for _in 0 ..132 { 109for valuein [ & value, & json! ({ "root" : value})] { 110let encoded = serde_json:: to_string ( value). unwrap (); 111let parsed =parse ( & encoded); 112let printed =stringify ( value); 113assert_eq! ( printed. is_ok (), parsed. is_ok (), "{encoded}" ); 114match printed{ 115Ok ( text) =>assert_eq! ( parse ( & text). unwrap (), * value), 116Err ( error) =>{ 117assert_eq! ( error. kind , serde_fjson:: ErrorKind :: Nesting ); 118assert_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 () { 129for valuein [ 130json! ( null), 131json! ( true ), 132json! ( 42 ), 133json! ( -0.125 ), 134json! ( "\0\tš" ), 135json! ([]), 136json! ({}), 137json! ([{}, [], null]), 138json! ({ "0" : 1 , "" : 2 , "true" : 3 , "some-key" : 4 , "ę„ę¬čŖ" : "\r\n" , "__proto__" : { "ok" : true }}), 139] { 140let text =stringify ( & value). unwrap (); 141assert_eq! ( parse ( & text). unwrap (), value, "{text}" ); 142} 143}