v

Зеркало из https://github.com/vlang/v
Форк
0
/x
/
any_test.v 
128 строк · 4.6 Кб
1
import x.json2 as json
2

3
const 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

17
fn is_null(f json.Any) bool {
18
	match f {
19
		json.Null { return true }
20
		else { return false }
21
	}
22
}
23

24
fn test_f32() {
25
	// valid conversions
26
	assert sample_data['int'] or { 0 }.f32() == 1.0
27
	assert sample_data['i64'] or { 0 }.f32() == 128.0
28
	assert sample_data['f32'] or { 0 }.f32() == 2.0
29
	assert sample_data['f64'] or { 0 }.f32() == 1.2829999923706055
30
	// invalid conversions
31
	assert sample_data['bool'] or { 0 }.f32() == 0.0
32
	assert sample_data['str'] or { 0 }.f32() == 0.0
33
	assert sample_data['null'] or { 0 }.f32() == 0.0
34
	assert sample_data['arr'] or { 0 }.f32() == 0.0
35
	assert sample_data['obj'] or { 0 }.f32() == 0.0
36
}
37

38
fn test_f64() {
39
	// valid conversions
40
	assert sample_data['int'] or { 0 }.f64() == 1.0
41
	assert sample_data['i64'] or { 0 }.f64() == 128.0
42
	assert sample_data['f32'] or { 0 }.f64() == 2.0
43
	assert sample_data['f64'] or { 0 }.f64() == 1.283
44
	// invalid conversions
45
	assert sample_data['bool'] or { 0 }.f64() == 0.0
46
	assert sample_data['str'] or { 0 }.f64() == 0.0
47
	assert sample_data['null'] or { 0 }.f64() == 0.0
48
	assert sample_data['arr'] or { 0 }.f64() == 0.0
49
	assert sample_data['obj'] or { 0 }.f64() == 0.0
50
}
51

52
fn test_int() {
53
	// valid conversions
54
	assert sample_data['int'] or { 0 }.int() == 1
55
	assert sample_data['i64'] or { 0 }.int() == 128
56
	assert sample_data['f32'] or { 0 }.int() == 2
57
	assert sample_data['f64'] or { 0 }.int() == 1
58
	assert json.Any(true).int() == 1
59
	assert json.Any('123').int() == 123
60
	// invalid conversions
61
	assert sample_data['null'] or { 0 }.int() == 0
62
	assert sample_data['arr'] or { 0 }.int() == 0
63
	assert sample_data['obj'] or { 0 }.int() == 0
64
}
65

66
fn test_i64() {
67
	// valid conversions
68
	assert sample_data['int'] or { 0 }.i64() == 1
69
	assert sample_data['i64'] or { 0 }.i64() == 128
70
	assert sample_data['f32'] or { 0 }.i64() == 2
71
	assert sample_data['f64'] or { 0 }.i64() == 1
72
	assert json.Any(true).i64() == 1
73
	assert json.Any('123').i64() == 123
74
	// invalid conversions
75
	assert sample_data['null'] or { 0 }.i64() == 0
76
	assert sample_data['arr'] or { 0 }.i64() == 0
77
	assert sample_data['obj'] or { 0 }.i64() == 0
78
}
79

80
fn test_as_map() {
81
	assert sample_data['int'] or { 0 }.as_map()['0'] or { 0 }.int() == 1
82
	assert sample_data['i64'] or { 0 }.as_map()['0'] or { 0 }.i64() == 128.0
83
	assert sample_data['f32'] or { 0 }.as_map()['0'] or { 0 }.f32() == 2.0
84
	assert sample_data['f64'] or { 0 }.as_map()['0'] or { 0 }.f64() == 1.283
85
	assert sample_data['bool'] or { 0 }.as_map()['0'] or { 0 }.bool() == false
86
	assert sample_data['str'] or { 0 }.as_map()['0'] or { 0 }.str() == 'test'
87
	assert is_null(sample_data['null'] or { 0 }.as_map()['0'] or { 0 }) == true
88
	assert sample_data['arr'] or { 0 }.as_map()['0'] or { 0 }.str() == 'lol'
89
	assert sample_data['obj'] or { 0 }.as_map()['foo'] or { 0 }.int() == 10
90
}
91

92
fn test_arr() {
93
	assert sample_data['int'] or { 0 }.arr()[0].int() == 1
94
	assert sample_data['i64'] or { 0 }.arr()[0].i64() == 128.0
95
	assert sample_data['f32'] or { 0 }.arr()[0].f32() == 2.0
96
	assert sample_data['f64'] or { 0 }.arr()[0].f64() == 1.283
97
	assert sample_data['bool'] or { 0 }.arr()[0].bool() == false
98
	assert sample_data['str'] or { 0 }.arr()[0].str() == 'test'
99
	assert is_null(sample_data['null'] or { 0 }.arr()[0]) == true
100
	assert sample_data['arr'] or { 0 }.arr()[0].str() == 'lol'
101
	assert sample_data['obj'] or { 0 }.arr()[0].int() == 10
102
}
103

104
fn test_bool() {
105
	// valid conversions
106
	assert sample_data['bool'] or { 0 }.bool() == false
107
	assert json.Any('true').bool() == true
108
	assert sample_data['int'] or { 0 }.bool() == true
109
	assert sample_data['i64'] or { 0 }.bool() == true
110
	assert sample_data['f32'] or { 0 }.bool() == true
111
	assert sample_data['f64'] or { 0 }.bool() == true
112
	// invalid conversions
113
	assert sample_data['null'] or { 0 }.bool() == false
114
	assert sample_data['arr'] or { 0 }.bool() == false
115
	assert sample_data['obj'] or { 0 }.bool() == false
116
}
117

118
fn test_str() {
119
	assert sample_data['int'] or { 0 }.str() == '1'
120
	assert sample_data['i64'] or { 0 }.str() == '128'
121
	assert sample_data['f32'] or { 0 }.str() == '2.0'
122
	assert sample_data['f64'] or { 0 }.str() == '1.283'
123
	assert sample_data['bool'] or { 0 }.str() == 'false'
124
	assert sample_data['str'] or { 0 }.str() == 'test'
125
	assert sample_data['null'] or { 0 }.str() == 'null'
126
	assert sample_data['arr'] or { 'not lol' }.str() == '["lol"]'
127
	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}}'
128
}
129

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.