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