2022-11-20 12:18:14 +03:00
|
|
|
import x.json2 as json
|
2020-11-29 16:54:45 +03:00
|
|
|
|
|
|
|
const (
|
2021-08-04 12:44:41 +03:00
|
|
|
sample_data = {
|
2022-11-20 12:18:14 +03:00
|
|
|
'int': json.Any(int(1))
|
|
|
|
'i64': json.Any(i64(128))
|
|
|
|
'f32': json.Any(f32(2.0))
|
|
|
|
'f64': json.Any(f64(1.283))
|
|
|
|
'bool': json.Any(false)
|
|
|
|
'str': json.Any('test')
|
|
|
|
'null': json.Any(json.null)
|
|
|
|
'arr': json.Any([json.Any('lol')])
|
|
|
|
'obj': json.Any({
|
|
|
|
'foo': json.Any(10)
|
2020-11-29 16:54:45 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-11-20 12:18:14 +03:00
|
|
|
fn is_null(f json.Any) bool {
|
2020-11-29 16:54:45 +03:00
|
|
|
match f {
|
2022-11-20 12:18:14 +03:00
|
|
|
json.Null { return true }
|
2020-11-29 16:54:45 +03:00
|
|
|
else { return false }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_f32() {
|
|
|
|
// valid conversions
|
2021-09-10 16:07:39 +03:00
|
|
|
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
|
2020-11-29 16:54:45 +03:00
|
|
|
// invalid conversions
|
2021-09-10 16:07:39 +03:00
|
|
|
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
|
2020-11-29 16:54:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_f64() {
|
|
|
|
// valid conversions
|
2021-09-10 16:07:39 +03:00
|
|
|
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
|
2020-11-29 16:54:45 +03:00
|
|
|
// invalid conversions
|
2021-09-10 16:07:39 +03:00
|
|
|
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
|
2020-11-29 16:54:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_int() {
|
|
|
|
// valid conversions
|
2021-09-10 16:07:39 +03:00
|
|
|
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
|
2022-11-20 12:18:14 +03:00
|
|
|
assert json.Any(true).int() == 1
|
2020-11-29 16:54:45 +03:00
|
|
|
// invalid conversions
|
2022-11-20 12:18:14 +03:00
|
|
|
assert json.Any('123').int() == 0
|
2021-09-10 16:07:39 +03:00
|
|
|
assert sample_data['null'] or { 0 }.int() == 0
|
|
|
|
assert sample_data['arr'] or { 0 }.int() == 0
|
|
|
|
assert sample_data['obj'] or { 0 }.int() == 0
|
2020-11-29 16:54:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_i64() {
|
|
|
|
// valid conversions
|
2021-09-10 16:07:39 +03:00
|
|
|
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
|
2022-11-20 12:18:14 +03:00
|
|
|
assert json.Any(true).i64() == 1
|
2020-11-29 16:54:45 +03:00
|
|
|
// invalid conversions
|
2022-11-20 12:18:14 +03:00
|
|
|
assert json.Any('123').i64() == 0
|
2021-09-10 16:07:39 +03:00
|
|
|
assert sample_data['null'] or { 0 }.i64() == 0
|
|
|
|
assert sample_data['arr'] or { 0 }.i64() == 0
|
|
|
|
assert sample_data['obj'] or { 0 }.i64() == 0
|
2020-11-29 16:54:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_as_map() {
|
2021-09-10 16:07:39 +03:00
|
|
|
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
|
2020-11-29 16:54:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_arr() {
|
2021-09-10 16:07:39 +03:00
|
|
|
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
|
2020-11-29 16:54:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_bool() {
|
|
|
|
// valid conversions
|
2021-09-10 16:07:39 +03:00
|
|
|
assert sample_data['bool'] or { 0 }.bool() == false
|
2022-11-20 12:18:14 +03:00
|
|
|
assert json.Any('true').bool() == true
|
2020-11-29 16:54:45 +03:00
|
|
|
// invalid conversions
|
2022-11-19 12:52:17 +03:00
|
|
|
assert sample_data['int'] or { 0 }.bool() == true
|
|
|
|
assert sample_data['i64'] or { 0 }.bool() == true
|
|
|
|
assert sample_data['f32'] or { 0 }.bool() == true
|
|
|
|
assert sample_data['f64'] or { 0 }.bool() == true
|
2021-09-10 16:07:39 +03:00
|
|
|
assert sample_data['null'] or { 0 }.bool() == false
|
|
|
|
assert sample_data['arr'] or { 0 }.bool() == false
|
|
|
|
assert sample_data['obj'] or { 0 }.bool() == false
|
2020-11-29 16:54:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_str() {
|
2021-09-10 16:07:39 +03:00
|
|
|
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"]'
|
2021-02-26 09:36:02 +03:00
|
|
|
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}}'
|
2020-11-29 16:54:45 +03:00
|
|
|
}
|