2021-06-28 14:05:27 +03:00
|
|
|
module json2
|
2020-11-29 16:54:45 +03:00
|
|
|
|
2021-06-28 14:05:27 +03:00
|
|
|
fn test_raw_decode_string() ? {
|
2022-05-13 06:56:21 +03:00
|
|
|
str := raw_decode('"Hello!"')?
|
2020-11-29 16:54:45 +03:00
|
|
|
assert str.str() == 'Hello!'
|
|
|
|
}
|
|
|
|
|
2021-11-18 08:34:00 +03:00
|
|
|
fn test_raw_decode_string_escape() ? {
|
2022-05-13 06:56:21 +03:00
|
|
|
jstr := raw_decode('"\u001b"')?
|
2021-11-18 08:34:00 +03:00
|
|
|
str := jstr.str()
|
|
|
|
assert str.len == 1
|
|
|
|
assert str[0] == 27
|
|
|
|
}
|
|
|
|
|
2021-06-28 14:05:27 +03:00
|
|
|
fn test_raw_decode_number() ? {
|
2022-05-13 06:56:21 +03:00
|
|
|
num := raw_decode('123')?
|
2020-11-29 16:54:45 +03:00
|
|
|
assert num.int() == 123
|
|
|
|
}
|
|
|
|
|
2021-06-28 14:05:27 +03:00
|
|
|
fn test_raw_decode_array() ? {
|
2022-05-13 06:56:21 +03:00
|
|
|
raw_arr := raw_decode('["Foo", 1]')?
|
2020-11-29 16:54:45 +03:00
|
|
|
arr := raw_arr.arr()
|
2021-09-10 16:07:39 +03:00
|
|
|
assert arr[0] or { 0 }.str() == 'Foo'
|
|
|
|
assert arr[1] or { 0 }.int() == 1
|
2020-11-29 16:54:45 +03:00
|
|
|
}
|
|
|
|
|
2021-06-28 14:05:27 +03:00
|
|
|
fn test_raw_decode_bool() ? {
|
2022-05-13 06:56:21 +03:00
|
|
|
bol := raw_decode('false')?
|
2020-11-29 16:54:45 +03:00
|
|
|
assert bol.bool() == false
|
|
|
|
}
|
|
|
|
|
2021-06-28 14:05:27 +03:00
|
|
|
fn test_raw_decode_map() ? {
|
2022-05-13 06:56:21 +03:00
|
|
|
raw_mp := raw_decode('{"name":"Bob","age":20}')?
|
2020-11-29 16:54:45 +03:00
|
|
|
mp := raw_mp.as_map()
|
2021-09-10 16:07:39 +03:00
|
|
|
assert mp['name'] or { 0 }.str() == 'Bob'
|
|
|
|
assert mp['age'] or { 0 }.int() == 20
|
2020-11-29 16:54:45 +03:00
|
|
|
}
|
|
|
|
|
2021-06-28 14:05:27 +03:00
|
|
|
fn test_raw_decode_null() ? {
|
2022-05-13 06:56:21 +03:00
|
|
|
nul := raw_decode('null')?
|
2021-06-28 14:05:27 +03:00
|
|
|
assert nul is Null
|
2020-11-29 16:54:45 +03:00
|
|
|
}
|
|
|
|
|
2021-06-28 14:05:27 +03:00
|
|
|
fn test_raw_decode_invalid() ? {
|
|
|
|
raw_decode('1z') or {
|
2022-02-11 16:52:33 +03:00
|
|
|
assert err.msg() == '[x.json2] invalid token `z` (0:17)'
|
2020-11-29 16:54:45 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
assert false
|
|
|
|
}
|
2021-02-26 09:36:02 +03:00
|
|
|
|
2021-06-28 14:05:27 +03:00
|
|
|
fn test_raw_decode_string_with_dollarsign() ? {
|
2022-05-13 06:56:21 +03:00
|
|
|
str := raw_decode(r'"Hello $world"')?
|
2021-02-26 09:36:02 +03:00
|
|
|
assert str.str() == r'Hello $world'
|
|
|
|
}
|
2021-03-30 10:40:20 +03:00
|
|
|
|
2021-06-28 14:05:27 +03:00
|
|
|
fn test_raw_decode_map_with_whitespaces() ? {
|
2022-05-13 06:56:21 +03:00
|
|
|
raw_mp := raw_decode(' \n\t{"name":"Bob","age":20}\n\t')?
|
2021-03-30 10:40:20 +03:00
|
|
|
mp := raw_mp.as_map()
|
2021-09-10 16:07:39 +03:00
|
|
|
assert mp['name'] or { 0 }.str() == 'Bob'
|
|
|
|
assert mp['age'] or { 0 }.int() == 20
|
2021-03-30 10:40:20 +03:00
|
|
|
}
|
2021-06-28 14:05:27 +03:00
|
|
|
|
|
|
|
fn test_nested_array_object() ? {
|
|
|
|
mut parser := new_parser(r'[[[[[],[],[]]]],{"Test":{}},[[]]]', false)
|
2022-05-13 06:56:21 +03:00
|
|
|
decoded := parser.decode()?
|
2021-06-28 14:05:27 +03:00
|
|
|
assert parser.n_level == 0
|
|
|
|
}
|
2022-03-05 14:02:43 +03:00
|
|
|
|
|
|
|
fn test_raw_decode_map_invalid() ? {
|
|
|
|
raw_decode('{"name","Bob","age":20}') or {
|
|
|
|
assert err.msg() == '[x.json2] invalid token `comma`, expecting `colon` (0:5)'
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert false
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_raw_decode_array_invalid() ? {
|
|
|
|
raw_decode('["Foo", 1,}') or {
|
|
|
|
assert err.msg() == '[x.json2] invalid token `rcbr` (0:5)'
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert false
|
|
|
|
}
|