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

tests: make error handling the same as the main function (#15825)

This commit is contained in:
yuyi
2022-09-22 00:45:43 +08:00
committed by GitHub
parent 391ac12fe2
commit 41dbd12bc4
69 changed files with 193 additions and 192 deletions

View File

@ -1,47 +1,47 @@
module json2
fn test_raw_decode_string() ? {
fn test_raw_decode_string() {
str := raw_decode('"Hello!"')?
assert str.str() == 'Hello!'
}
fn test_raw_decode_string_escape() ? {
fn test_raw_decode_string_escape() {
jstr := raw_decode('"\u001b"')?
str := jstr.str()
assert str.len == 1
assert str[0] == 27
}
fn test_raw_decode_number() ? {
fn test_raw_decode_number() {
num := raw_decode('123')?
assert num.int() == 123
}
fn test_raw_decode_array() ? {
fn test_raw_decode_array() {
raw_arr := raw_decode('["Foo", 1]')?
arr := raw_arr.arr()
assert arr[0] or { 0 }.str() == 'Foo'
assert arr[1] or { 0 }.int() == 1
}
fn test_raw_decode_bool() ? {
fn test_raw_decode_bool() {
bol := raw_decode('false')?
assert bol.bool() == false
}
fn test_raw_decode_map() ? {
fn test_raw_decode_map() {
raw_mp := raw_decode('{"name":"Bob","age":20}')?
mp := raw_mp.as_map()
assert mp['name'] or { 0 }.str() == 'Bob'
assert mp['age'] or { 0 }.int() == 20
}
fn test_raw_decode_null() ? {
fn test_raw_decode_null() {
nul := raw_decode('null')?
assert nul is Null
}
fn test_raw_decode_invalid() ? {
fn test_raw_decode_invalid() {
raw_decode('1z') or {
assert err.msg() == '[x.json2] invalid token `z` (0:17)'
return
@ -49,25 +49,25 @@ fn test_raw_decode_invalid() ? {
assert false
}
fn test_raw_decode_string_with_dollarsign() ? {
fn test_raw_decode_string_with_dollarsign() {
str := raw_decode(r'"Hello $world"')?
assert str.str() == r'Hello $world'
}
fn test_raw_decode_map_with_whitespaces() ? {
fn test_raw_decode_map_with_whitespaces() {
raw_mp := raw_decode(' \n\t{"name":"Bob","age":20}\n\t')?
mp := raw_mp.as_map()
assert mp['name'] or { 0 }.str() == 'Bob'
assert mp['age'] or { 0 }.int() == 20
}
fn test_nested_array_object() ? {
fn test_nested_array_object() {
mut parser := new_parser(r'[[[[[],[],[]]]],{"Test":{}},[[]]]', false)
decoded := parser.decode()?
assert parser.n_level == 0
}
fn test_raw_decode_map_invalid() ? {
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
@ -75,7 +75,7 @@ fn test_raw_decode_map_invalid() ? {
assert false
}
fn test_raw_decode_array_invalid() ? {
fn test_raw_decode_array_invalid() {
raw_decode('["Foo", 1,}') or {
assert err.msg() == '[x.json2] invalid token `rcbr` (0:5)'
return

View File

@ -36,7 +36,7 @@ fn test_json_string_non_ascii() {
assert text.json_str() == r'"\u3072\u3089\u304c\u306a"'
}
fn test_utf8_strings_are_not_modified() ? {
fn test_utf8_strings_are_not_modified() {
original := '{"s":"Schilddrüsenerkrankungen"}'
// dump(original)
deresult := json2.raw_decode(original)?
@ -44,7 +44,7 @@ fn test_utf8_strings_are_not_modified() ? {
assert deresult.str() == original
}
fn test_encoder_unescaped_utf32() ? {
fn test_encoder_unescaped_utf32() {
jap_text := json2.Any('')
enc := json2.Encoder{
escape_unicode: false
@ -61,7 +61,7 @@ fn test_encoder_unescaped_utf32() ? {
assert sb.str() == '"$emoji_text"'
}
fn test_encoder_prettify() ? {
fn test_encoder_prettify() {
obj := {
'hello': json2.Any('world')
'arr': [json2.Any('im a string'), [json2.Any('3rd level')]]