x/json2: add necessary tests

pull/13654/head
Ned Palacios 2022-03-04 13:53:02 +08:00
parent 80b4f6be94
commit 10a003cbfc
1 changed files with 46 additions and 0 deletions

View File

@ -1,4 +1,5 @@
import x.json2
import strings
fn test_json_string_characters() {
text := json2.raw_decode(r'"\n\r\b\f\t\\\"\/"') or { '' }
@ -42,3 +43,48 @@ fn test_utf8_strings_are_not_modified() ? {
// dump(deresult)
assert deresult.str() == original
}
fn test_encoder_unescaped_utf32() ? {
jap_text := json2.Any('')
enc := json2.Encoder{
escape_utf32: false
}
mut sb := strings.new_builder(20)
enc.encode_value(jap_text, mut sb) ?
assert sb.str() == '"$jap_text"'
sb.go_back_to(0)
emoji_text := json2.Any('🐈')
enc.encode_value(emoji_text, mut sb) ?
assert sb.str() == '"$emoji_text"'
}
fn test_encoder_prettify() ? {
obj := {
'hello': json2.Any('world')
'arr': [json2.Any('im a string'), [json2.Any('3rd level')]]
'obj': {
'map': json2.Any('map inside a map')
}
}
enc := json2.Encoder{
newline: `\n`
newline_spaces_count: 2
}
mut sb := strings.new_builder(20)
enc.encode_value(obj, mut sb) ?
assert sb.str() == '{
"hello": "world",
"arr": [
"im a string",
[
"3rd level"
]
],
"obj": {
"map": "map inside a map"
}
}'
}