mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: require or block for sumtype map (#11089)
This commit is contained in:
@@ -25,106 +25,106 @@ fn is_null(f json2.Any) bool {
|
||||
|
||||
fn test_f32() {
|
||||
// valid conversions
|
||||
assert sample_data['int'].f32() == 1.0
|
||||
assert sample_data['i64'].f32() == 128.0
|
||||
assert sample_data['f32'].f32() == 2.0
|
||||
assert sample_data['f64'].f32() == 1.2829999923706055
|
||||
assert sample_data['int'] or { 0 }.f32() == 1.0
|
||||
assert sample_data['i64'] or { 0 }.f32() == 128.0
|
||||
assert sample_data['f32'] or { 0 }.f32() == 2.0
|
||||
assert sample_data['f64'] or { 0 }.f32() == 1.2829999923706055
|
||||
// invalid conversions
|
||||
assert sample_data['bool'].f32() == 0.0
|
||||
assert sample_data['str'].f32() == 0.0
|
||||
assert sample_data['null'].f32() == 0.0
|
||||
assert sample_data['arr'].f32() == 0.0
|
||||
assert sample_data['obj'].f32() == 0.0
|
||||
assert sample_data['bool'] or { 0 }.f32() == 0.0
|
||||
assert sample_data['str'] or { 0 }.f32() == 0.0
|
||||
assert sample_data['null'] or { 0 }.f32() == 0.0
|
||||
assert sample_data['arr'] or { 0 }.f32() == 0.0
|
||||
assert sample_data['obj'] or { 0 }.f32() == 0.0
|
||||
}
|
||||
|
||||
fn test_f64() {
|
||||
// valid conversions
|
||||
assert sample_data['int'].f64() == 1.0
|
||||
assert sample_data['i64'].f64() == 128.0
|
||||
assert sample_data['f32'].f64() == 2.0
|
||||
assert sample_data['f64'].f64() == 1.283
|
||||
assert sample_data['int'] or { 0 }.f64() == 1.0
|
||||
assert sample_data['i64'] or { 0 }.f64() == 128.0
|
||||
assert sample_data['f32'] or { 0 }.f64() == 2.0
|
||||
assert sample_data['f64'] or { 0 }.f64() == 1.283
|
||||
// invalid conversions
|
||||
assert sample_data['bool'].f64() == 0.0
|
||||
assert sample_data['str'].f64() == 0.0
|
||||
assert sample_data['null'].f64() == 0.0
|
||||
assert sample_data['arr'].f64() == 0.0
|
||||
assert sample_data['obj'].f64() == 0.0
|
||||
assert sample_data['bool'] or { 0 }.f64() == 0.0
|
||||
assert sample_data['str'] or { 0 }.f64() == 0.0
|
||||
assert sample_data['null'] or { 0 }.f64() == 0.0
|
||||
assert sample_data['arr'] or { 0 }.f64() == 0.0
|
||||
assert sample_data['obj'] or { 0 }.f64() == 0.0
|
||||
}
|
||||
|
||||
fn test_int() {
|
||||
// valid conversions
|
||||
assert sample_data['int'].int() == 1
|
||||
assert sample_data['i64'].int() == 128
|
||||
assert sample_data['f32'].int() == 2
|
||||
assert sample_data['f64'].int() == 1
|
||||
assert sample_data['int'] or { 0 }.int() == 1
|
||||
assert sample_data['i64'] or { 0 }.int() == 128
|
||||
assert sample_data['f32'] or { 0 }.int() == 2
|
||||
assert sample_data['f64'] or { 0 }.int() == 1
|
||||
assert json2.Any(true).int() == 1
|
||||
// invalid conversions
|
||||
assert json2.Any('123').int() == 0
|
||||
assert sample_data['null'].int() == 0
|
||||
assert sample_data['arr'].int() == 0
|
||||
assert sample_data['obj'].int() == 0
|
||||
assert sample_data['null'] or { 0 }.int() == 0
|
||||
assert sample_data['arr'] or { 0 }.int() == 0
|
||||
assert sample_data['obj'] or { 0 }.int() == 0
|
||||
}
|
||||
|
||||
fn test_i64() {
|
||||
// valid conversions
|
||||
assert sample_data['int'].i64() == 1
|
||||
assert sample_data['i64'].i64() == 128
|
||||
assert sample_data['f32'].i64() == 2
|
||||
assert sample_data['f64'].i64() == 1
|
||||
assert sample_data['int'] or { 0 }.i64() == 1
|
||||
assert sample_data['i64'] or { 0 }.i64() == 128
|
||||
assert sample_data['f32'] or { 0 }.i64() == 2
|
||||
assert sample_data['f64'] or { 0 }.i64() == 1
|
||||
assert json2.Any(true).i64() == 1
|
||||
// invalid conversions
|
||||
assert json2.Any('123').i64() == 0
|
||||
assert sample_data['null'].i64() == 0
|
||||
assert sample_data['arr'].i64() == 0
|
||||
assert sample_data['obj'].i64() == 0
|
||||
assert sample_data['null'] or { 0 }.i64() == 0
|
||||
assert sample_data['arr'] or { 0 }.i64() == 0
|
||||
assert sample_data['obj'] or { 0 }.i64() == 0
|
||||
}
|
||||
|
||||
fn test_as_map() {
|
||||
assert sample_data['int'].as_map()['0'].int() == 1
|
||||
assert sample_data['i64'].as_map()['0'].i64() == 128.0
|
||||
assert sample_data['f32'].as_map()['0'].f32() == 2.0
|
||||
assert sample_data['f64'].as_map()['0'].f64() == 1.283
|
||||
assert sample_data['bool'].as_map()['0'].bool() == false
|
||||
assert sample_data['str'].as_map()['0'].str() == 'test'
|
||||
assert is_null(sample_data['null'].as_map()['0']) == true
|
||||
assert sample_data['arr'].as_map()['0'].str() == 'lol'
|
||||
assert sample_data['obj'].as_map()['foo'].int() == 10
|
||||
assert sample_data['int'] or { 0 }.as_map()['0'] or { 0 }.int() == 1
|
||||
assert sample_data['i64'] or { 0 }.as_map()['0'] or { 0 }.i64() == 128.0
|
||||
assert sample_data['f32'] or { 0 }.as_map()['0'] or { 0 }.f32() == 2.0
|
||||
assert sample_data['f64'] or { 0 }.as_map()['0'] or { 0 }.f64() == 1.283
|
||||
assert sample_data['bool'] or { 0 }.as_map()['0'] or { 0 }.bool() == false
|
||||
assert sample_data['str'] or { 0 }.as_map()['0'] or { 0 }.str() == 'test'
|
||||
assert is_null(sample_data['null'] or { 0 }.as_map()['0'] or { 0 }) == true
|
||||
assert sample_data['arr'] or { 0 }.as_map()['0'] or { 0 }.str() == 'lol'
|
||||
assert sample_data['obj'] or { 0 }.as_map()['foo'] or { 0 }.int() == 10
|
||||
}
|
||||
|
||||
fn test_arr() {
|
||||
assert sample_data['int'].arr()[0].int() == 1
|
||||
assert sample_data['i64'].arr()[0].i64() == 128.0
|
||||
assert sample_data['f32'].arr()[0].f32() == 2.0
|
||||
assert sample_data['f64'].arr()[0].f64() == 1.283
|
||||
assert sample_data['bool'].arr()[0].bool() == false
|
||||
assert sample_data['str'].arr()[0].str() == 'test'
|
||||
assert is_null(sample_data['null'].arr()[0]) == true
|
||||
assert sample_data['arr'].arr()[0].str() == 'lol'
|
||||
assert sample_data['obj'].arr()[0].int() == 10
|
||||
assert sample_data['int'] or { 0 }.arr()[0].int() == 1
|
||||
assert sample_data['i64'] or { 0 }.arr()[0].i64() == 128.0
|
||||
assert sample_data['f32'] or { 0 }.arr()[0].f32() == 2.0
|
||||
assert sample_data['f64'] or { 0 }.arr()[0].f64() == 1.283
|
||||
assert sample_data['bool'] or { 0 }.arr()[0].bool() == false
|
||||
assert sample_data['str'] or { 0 }.arr()[0].str() == 'test'
|
||||
assert is_null(sample_data['null'] or { 0 }.arr()[0]) == true
|
||||
assert sample_data['arr'] or { 0 }.arr()[0].str() == 'lol'
|
||||
assert sample_data['obj'] or { 0 }.arr()[0].int() == 10
|
||||
}
|
||||
|
||||
fn test_bool() {
|
||||
// valid conversions
|
||||
assert sample_data['bool'].bool() == false
|
||||
assert sample_data['bool'] or { 0 }.bool() == false
|
||||
assert json2.Any('true').bool() == true
|
||||
// invalid conversions
|
||||
assert sample_data['int'].bool() == false
|
||||
assert sample_data['i64'].bool() == false
|
||||
assert sample_data['f32'].bool() == false
|
||||
assert sample_data['f64'].bool() == false
|
||||
assert sample_data['null'].bool() == false
|
||||
assert sample_data['arr'].bool() == false
|
||||
assert sample_data['obj'].bool() == false
|
||||
assert sample_data['int'] or { 0 }.bool() == false
|
||||
assert sample_data['i64'] or { 0 }.bool() == false
|
||||
assert sample_data['f32'] or { 0 }.bool() == false
|
||||
assert sample_data['f64'] or { 0 }.bool() == false
|
||||
assert sample_data['null'] or { 0 }.bool() == false
|
||||
assert sample_data['arr'] or { 0 }.bool() == false
|
||||
assert sample_data['obj'] or { 0 }.bool() == false
|
||||
}
|
||||
|
||||
fn test_str() {
|
||||
assert sample_data['int'].str() == '1'
|
||||
assert sample_data['i64'].str() == '128'
|
||||
assert sample_data['f32'].str() == '2.0'
|
||||
assert sample_data['f64'].str() == '1.283'
|
||||
assert sample_data['bool'].str() == 'false'
|
||||
assert sample_data['str'].str() == 'test'
|
||||
assert sample_data['null'].str() == 'null'
|
||||
assert sample_data['arr'].str() == '["lol"]'
|
||||
assert sample_data['int'] or { 0 }.str() == '1'
|
||||
assert sample_data['i64'] or { 0 }.str() == '128'
|
||||
assert sample_data['f32'] or { 0 }.str() == '2.0'
|
||||
assert sample_data['f64'] or { 0 }.str() == '1.283'
|
||||
assert sample_data['bool'] or { 0 }.str() == 'false'
|
||||
assert sample_data['str'] or { 0 }.str() == 'test'
|
||||
assert sample_data['null'] or { 0 }.str() == 'null'
|
||||
assert sample_data['arr'] or { 0 }.str() == '["lol"]'
|
||||
assert sample_data.str() == '{"int":1,"i64":128,"f32":2.0,"f64":1.283,"bool":false,"str":"test","null":null,"arr":["lol"],"obj":{"foo":10}}'
|
||||
}
|
||||
|
@@ -13,8 +13,8 @@ fn test_raw_decode_number() ? {
|
||||
fn test_raw_decode_array() ? {
|
||||
raw_arr := raw_decode('["Foo", 1]') ?
|
||||
arr := raw_arr.arr()
|
||||
assert arr[0].str() == 'Foo'
|
||||
assert arr[1].int() == 1
|
||||
assert arr[0] or { 0 }.str() == 'Foo'
|
||||
assert arr[1] or { 0 }.int() == 1
|
||||
}
|
||||
|
||||
fn test_raw_decode_bool() ? {
|
||||
@@ -25,8 +25,8 @@ fn test_raw_decode_bool() ? {
|
||||
fn test_raw_decode_map() ? {
|
||||
raw_mp := raw_decode('{"name":"Bob","age":20}') ?
|
||||
mp := raw_mp.as_map()
|
||||
assert mp['name'].str() == 'Bob'
|
||||
assert mp['age'].int() == 20
|
||||
assert mp['name'] or { 0 }.str() == 'Bob'
|
||||
assert mp['age'] or { 0 }.int() == 20
|
||||
}
|
||||
|
||||
fn test_raw_decode_null() ? {
|
||||
@@ -50,8 +50,8 @@ fn test_raw_decode_string_with_dollarsign() ? {
|
||||
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'].str() == 'Bob'
|
||||
assert mp['age'].int() == 20
|
||||
assert mp['name'] or { 0 }.str() == 'Bob'
|
||||
assert mp['age'] or { 0 }.int() == 20
|
||||
}
|
||||
|
||||
fn test_nested_array_object() ? {
|
||||
|
@@ -36,10 +36,10 @@ fn (e Employee) to_json() string {
|
||||
|
||||
fn (mut e Employee) from_json(any json2.Any) {
|
||||
mp := any.as_map()
|
||||
e.name = mp['name'].str()
|
||||
e.age = mp['age'].int()
|
||||
e.salary = mp['salary'].f32()
|
||||
e.title = JobTitle(mp['title'].int())
|
||||
e.name = mp['name'] or { 0 }.str()
|
||||
e.age = mp['age'] or { 0 }.int()
|
||||
e.salary = mp['salary'] or { 0 }.f32()
|
||||
e.title = JobTitle(mp['title'] or { 0 }.int())
|
||||
}
|
||||
|
||||
fn test_simple() {
|
||||
@@ -84,11 +84,11 @@ fn test_character_unescape() {
|
||||
}
|
||||
lines := obj.as_map()
|
||||
eprintln('$lines')
|
||||
assert lines['newline'].str() == 'new\nline'
|
||||
assert lines['tab'].str() == '\ttab'
|
||||
assert lines['backslash'].str() == 'back\\slash'
|
||||
assert lines['quotes'].str() == '"quotes"'
|
||||
assert lines['slash'].str() == '/dev/null'
|
||||
assert lines['newline'] or { 0 }.str() == 'new\nline'
|
||||
assert lines['tab'] or { 0 }.str() == '\ttab'
|
||||
assert lines['backslash'] or { 0 }.str() == 'back\\slash'
|
||||
assert lines['quotes'] or { 0 }.str() == '"quotes"'
|
||||
assert lines['slash'] or { 0 }.str() == '/dev/null'
|
||||
}
|
||||
|
||||
struct User2 {
|
||||
@@ -109,8 +109,8 @@ fn (mut u User2) from_json(an json2.Any) {
|
||||
}
|
||||
}
|
||||
match field.name {
|
||||
'age' { u.age = mp[js_field_name].int() }
|
||||
'nums' { u.nums = mp[js_field_name].arr().map(it.int()) }
|
||||
'age' { u.age = mp[js_field_name] or { 0 }.int() }
|
||||
'nums' { u.nums = mp[js_field_name] or { 0 }.arr().map(it.int()) }
|
||||
else {}
|
||||
}
|
||||
}
|
||||
@@ -140,12 +140,12 @@ fn (mut u User) from_json(an json2.Any) {
|
||||
}
|
||||
}
|
||||
match field.name {
|
||||
'age' { u.age = mp[js_field_name].int() }
|
||||
'nums' { u.nums = mp[js_field_name].arr().map(it.int()) }
|
||||
'last_name' { u.last_name = mp[js_field_name].str() }
|
||||
'is_registered' { u.is_registered = mp[js_field_name].bool() }
|
||||
'typ' { u.typ = mp[js_field_name].int() }
|
||||
'pets' { u.pets = mp[js_field_name].str() }
|
||||
'age' { u.age = mp[js_field_name] or { 0 }.int() }
|
||||
'nums' { u.nums = mp[js_field_name] or { 0 }.arr().map(it.int()) }
|
||||
'last_name' { u.last_name = mp[js_field_name] or { 0 }.str() }
|
||||
'is_registered' { u.is_registered = mp[js_field_name] or { 0 }.bool() }
|
||||
'typ' { u.typ = mp[js_field_name] or { 0 }.int() }
|
||||
'pets' { u.pets = mp[js_field_name] or { 0 }.str() }
|
||||
else {}
|
||||
}
|
||||
}
|
||||
@@ -212,8 +212,8 @@ fn (mut c Color) from_json(an json2.Any) {
|
||||
mp := an.as_map()
|
||||
$for field in Color.fields {
|
||||
match field.name {
|
||||
'space' { c.space = mp[field.name].str() }
|
||||
'point' { c.point = mp[field.name].str() }
|
||||
'space' { c.space = mp[field.name] or { 0 }.str() }
|
||||
'point' { c.point = mp[field.name] or { 0 }.str() }
|
||||
else {}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user