v / vlib / toml / tests / value_query_test.v
131 lines · 106 sloc · 2.89 KB · acf88b7fc93582685f71f90f50e257e18fcdc39b
Raw
1import toml
2import strconv
3
4const toml_text = '
5modules = [ "ui", "toml" ]
6errors = []
7
8[[themes]]
9name = "Dracula"
10colors = [ "red", "black", "white" ]
11
12[[themes]]
13name = "Lemon"
14colors = [
15 "green",
16 "yellow",
17 [ "transparent" ]
18]
19
20[[tests]]
21id = 1
22
23[[tests]]
24id = 2
25
26[values]
27nan = nan
28inf = inf
29test = 2
30minus-inf = -inf
31
32[[themes]]
33name = "Ice"
34colors = [
35 "blue",
36 "white"
37]
38'
39
40const toml_text_2 = "
41[defaults]
42 run.flags = ['-f 1']
43
44 [[defaults.env]]
45 'RUN_PATH' = '\$OUT_PATH'
46 'RUN_TIME' = 5
47 'TEST_PATH' = '/tmp/test'
48"
49
50const toml_text_3 = '
51[[foo.bar]]
52baz = 1
53bzz = 1
54
55[[foo.bar]]
56baz = 2
57bzz = 2
58'
59
60fn test_value_query_in_array() {
61 toml_doc := toml.parse_text(toml_text) or { panic(err) }
62 mut value := toml_doc.value('themes[0].colors[1]').string()
63 assert value == 'black'
64 value = toml_doc.value('themes[1].colors[0]').string()
65 assert value == 'green'
66 value = toml_doc.value('themes[1].colors[2].[0]').string()
67 assert value == 'transparent'
68 value = toml_doc.value('modules[1]').string()
69 assert value == 'toml'
70 value = toml_doc.value('errors[11]').default_to('<none>').string()
71 assert value == '<none>'
72
73 value = toml_doc.value('themes[2].colors[0]').string()
74 assert value == 'blue'
75}
76
77fn test_any_value_query() {
78 toml_doc := toml.parse_text(toml_text) or { panic(err) }
79 themes := toml_doc.value('themes')
80 assert themes.value('[0].colors[0]').string() == 'red'
81
82 themes_arr := toml_doc.value('themes') as []toml.Any
83 assert themes_arr[0].value('colors[0]').string() == 'red'
84
85 mut any := themes
86 assert any.value('[1].name').string() == 'Lemon'
87 any = any.value('[1]')
88 assert any.value('name').string() == 'Lemon'
89
90 any = toml_doc.value('themes').value('[1].colors').value('[1]')
91 assert any.string() == 'yellow'
92
93 any = toml_doc.value('themes[1]').value('colors[1]')
94 assert any.string() == 'yellow'
95
96 any = toml_doc.value('themes[1].colors[0]')
97 assert any.string() == 'green'
98
99 any = toml_doc.value('values')
100 any = any.value('test')
101 assert any.int() == 2
102}
103
104fn test_inf_and_nan_query() {
105 toml_doc := toml.parse_text(toml_text) or { panic(err) }
106
107 value := toml_doc.value('values.nan').string()
108 assert value == 'nan'
109
110 mut value_u64 := toml_doc.value('values.inf').u64()
111 assert value_u64 == strconv.double_plus_infinity
112 value_u64 = toml_doc.value('values.minus-inf').u64()
113 assert value_u64 == strconv.double_minus_infinity
114}
115
116fn test_any_value_query_2() {
117 toml_doc := toml.parse_text(toml_text_2) or { panic(err) }
118 defaults := toml_doc.value('defaults')
119 assert defaults.value('run.flags[0]').string() == '-f 1'
120 assert defaults.value('env[0].RUN_TIME').int() == 5
121}
122
123fn test_any_value_query_for_nested_tables_array() {
124 toml_doc := toml.parse_text(toml_text_3) or { panic(err) }
125 items := toml_doc.value('foo.bar').array()
126 assert items.len == 2
127 assert items[0].value('baz').int() == 1
128 assert items[0].value('bzz').int() == 1
129 assert items[1].value('baz').int() == 2
130 assert items[1].value('bzz').int() == 2
131}
132