1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/x/json2/decoder_test.v

62 lines
1.0 KiB
V
Raw Normal View History

import x.json2
fn test_raw_decode_string() {
str := json2.raw_decode('"Hello!"') or {
assert false
json2.Any{}
}
assert str.str() == 'Hello!'
}
fn test_raw_decode_number() {
num := json2.raw_decode('123') or {
assert false
json2.Any{}
}
assert num.int() == 123
}
fn test_raw_decode_array() {
raw_arr := json2.raw_decode('["Foo", 1]') or {
assert false
json2.Any{}
}
arr := raw_arr.arr()
assert arr[0].str() == 'Foo'
assert arr[1].int() == 1
}
fn test_raw_decode_bool() {
bol := json2.raw_decode('false') or {
assert false
json2.Any{}
}
assert bol.bool() == false
}
fn test_raw_decode_map() {
raw_mp := json2.raw_decode('{"name":"Bob","age":20}') or {
assert false
json2.Any{}
}
mp := raw_mp.as_map()
assert mp['name'].str() == 'Bob'
assert mp['age'].int() == 20
}
fn test_raw_decode_null() {
nul := json2.raw_decode('null') or {
assert false
json2.Any{}
}
assert nul is json2.Null
}
fn test_raw_decode_invalid() {
json2.raw_decode('1z') or {
assert err == '[json] invalid JSON. (0:0)'
return
}
assert false
}