v
Зеркало из https://github.com/vlang/v
1import x.json2 as json
2
3const sample_data = {
4'int': json.Any(int(1))
5'i64': json.Any(i64(128))
6'f32': json.Any(f32(2.0))
7'f64': json.Any(f64(1.283))
8'bool': json.Any(false)
9'str': json.Any('test')
10'null': json.Any(json.null)
11'arr': json.Any([json.Any('lol')])
12'obj': json.Any({
13'foo': json.Any(10)
14})
15}
16
17fn is_null(f json.Any) bool {
18match f {
19json.Null { return true }
20else { return false }
21}
22}
23
24fn test_f32() {
25// valid conversions
26assert sample_data['int'] or { 0 }.f32() == 1.0
27assert sample_data['i64'] or { 0 }.f32() == 128.0
28assert sample_data['f32'] or { 0 }.f32() == 2.0
29assert sample_data['f64'] or { 0 }.f32() == 1.2829999923706055
30// invalid conversions
31assert sample_data['bool'] or { 0 }.f32() == 0.0
32assert sample_data['str'] or { 0 }.f32() == 0.0
33assert sample_data['null'] or { 0 }.f32() == 0.0
34assert sample_data['arr'] or { 0 }.f32() == 0.0
35assert sample_data['obj'] or { 0 }.f32() == 0.0
36}
37
38fn test_f64() {
39// valid conversions
40assert sample_data['int'] or { 0 }.f64() == 1.0
41assert sample_data['i64'] or { 0 }.f64() == 128.0
42assert sample_data['f32'] or { 0 }.f64() == 2.0
43assert sample_data['f64'] or { 0 }.f64() == 1.283
44// invalid conversions
45assert sample_data['bool'] or { 0 }.f64() == 0.0
46assert sample_data['str'] or { 0 }.f64() == 0.0
47assert sample_data['null'] or { 0 }.f64() == 0.0
48assert sample_data['arr'] or { 0 }.f64() == 0.0
49assert sample_data['obj'] or { 0 }.f64() == 0.0
50}
51
52fn test_int() {
53// valid conversions
54assert sample_data['int'] or { 0 }.int() == 1
55assert sample_data['i64'] or { 0 }.int() == 128
56assert sample_data['f32'] or { 0 }.int() == 2
57assert sample_data['f64'] or { 0 }.int() == 1
58assert json.Any(true).int() == 1
59assert json.Any('123').int() == 123
60// invalid conversions
61assert sample_data['null'] or { 0 }.int() == 0
62assert sample_data['arr'] or { 0 }.int() == 0
63assert sample_data['obj'] or { 0 }.int() == 0
64}
65
66fn test_i64() {
67// valid conversions
68assert sample_data['int'] or { 0 }.i64() == 1
69assert sample_data['i64'] or { 0 }.i64() == 128
70assert sample_data['f32'] or { 0 }.i64() == 2
71assert sample_data['f64'] or { 0 }.i64() == 1
72assert json.Any(true).i64() == 1
73assert json.Any('123').i64() == 123
74// invalid conversions
75assert sample_data['null'] or { 0 }.i64() == 0
76assert sample_data['arr'] or { 0 }.i64() == 0
77assert sample_data['obj'] or { 0 }.i64() == 0
78}
79
80fn test_as_map() {
81assert sample_data['int'] or { 0 }.as_map()['0'] or { 0 }.int() == 1
82assert sample_data['i64'] or { 0 }.as_map()['0'] or { 0 }.i64() == 128.0
83assert sample_data['f32'] or { 0 }.as_map()['0'] or { 0 }.f32() == 2.0
84assert sample_data['f64'] or { 0 }.as_map()['0'] or { 0 }.f64() == 1.283
85assert sample_data['bool'] or { 0 }.as_map()['0'] or { 0 }.bool() == false
86assert sample_data['str'] or { 0 }.as_map()['0'] or { 0 }.str() == 'test'
87assert is_null(sample_data['null'] or { 0 }.as_map()['0'] or { 0 }) == true
88assert sample_data['arr'] or { 0 }.as_map()['0'] or { 0 }.str() == 'lol'
89assert sample_data['obj'] or { 0 }.as_map()['foo'] or { 0 }.int() == 10
90}
91
92fn test_arr() {
93assert sample_data['int'] or { 0 }.arr()[0].int() == 1
94assert sample_data['i64'] or { 0 }.arr()[0].i64() == 128.0
95assert sample_data['f32'] or { 0 }.arr()[0].f32() == 2.0
96assert sample_data['f64'] or { 0 }.arr()[0].f64() == 1.283
97assert sample_data['bool'] or { 0 }.arr()[0].bool() == false
98assert sample_data['str'] or { 0 }.arr()[0].str() == 'test'
99assert is_null(sample_data['null'] or { 0 }.arr()[0]) == true
100assert sample_data['arr'] or { 0 }.arr()[0].str() == 'lol'
101assert sample_data['obj'] or { 0 }.arr()[0].int() == 10
102}
103
104fn test_bool() {
105// valid conversions
106assert sample_data['bool'] or { 0 }.bool() == false
107assert json.Any('true').bool() == true
108assert sample_data['int'] or { 0 }.bool() == true
109assert sample_data['i64'] or { 0 }.bool() == true
110assert sample_data['f32'] or { 0 }.bool() == true
111assert sample_data['f64'] or { 0 }.bool() == true
112// invalid conversions
113assert sample_data['null'] or { 0 }.bool() == false
114assert sample_data['arr'] or { 0 }.bool() == false
115assert sample_data['obj'] or { 0 }.bool() == false
116}
117
118fn test_str() {
119assert sample_data['int'] or { 0 }.str() == '1'
120assert sample_data['i64'] or { 0 }.str() == '128'
121assert sample_data['f32'] or { 0 }.str() == '2.0'
122assert sample_data['f64'] or { 0 }.str() == '1.283'
123assert sample_data['bool'] or { 0 }.str() == 'false'
124assert sample_data['str'] or { 0 }.str() == 'test'
125assert sample_data['null'] or { 0 }.str() == 'null'
126assert sample_data['arr'] or { 'not lol' }.str() == '["lol"]'
127assert sample_data.str() == '{"int":1,"i64":128,"f32":2.0,"f64":1.283,"bool":false,"str":"test","null":null,"arr":["lol"],"obj":{"foo":10}}'
128}
129