2021-11-20 20:45:17 +03:00
|
|
|
import toml
|
2021-11-25 13:33:54 +03:00
|
|
|
import strconv
|
2021-11-20 20:45:17 +03:00
|
|
|
|
|
|
|
const toml_text = '
|
|
|
|
modules = [ "ui", "toml" ]
|
|
|
|
errors = []
|
|
|
|
|
|
|
|
[[themes]]
|
|
|
|
name = "Dracula"
|
|
|
|
colors = [ "red", "black", "white" ]
|
|
|
|
|
|
|
|
[[themes]]
|
|
|
|
name = "Lemon"
|
|
|
|
colors = [
|
|
|
|
"green",
|
|
|
|
"yellow",
|
|
|
|
[ "transparent" ]
|
|
|
|
]
|
2021-11-24 21:39:22 +03:00
|
|
|
|
|
|
|
[[tests]]
|
|
|
|
id = 1
|
|
|
|
|
|
|
|
[[tests]]
|
|
|
|
id = 2
|
|
|
|
|
|
|
|
[values]
|
2021-11-25 13:33:54 +03:00
|
|
|
nan = nan
|
|
|
|
inf = inf
|
2021-11-24 21:39:22 +03:00
|
|
|
test = 2
|
2021-11-25 13:33:54 +03:00
|
|
|
minus-inf = -inf
|
2021-11-24 21:39:22 +03:00
|
|
|
|
|
|
|
[[themes]]
|
|
|
|
name = "Ice"
|
|
|
|
colors = [
|
|
|
|
"blue",
|
|
|
|
"white"
|
|
|
|
]
|
2021-11-20 20:45:17 +03:00
|
|
|
'
|
|
|
|
|
|
|
|
fn test_value_query_in_array() {
|
|
|
|
toml_doc := toml.parse(toml_text) or { panic(err) }
|
|
|
|
mut value := toml_doc.value('themes[0].colors[1]').string()
|
|
|
|
assert value == 'black'
|
|
|
|
value = toml_doc.value('themes[1].colors[0]').string()
|
|
|
|
assert value == 'green'
|
|
|
|
value = toml_doc.value('themes[1].colors[2].[0]').string()
|
|
|
|
assert value == 'transparent'
|
|
|
|
value = toml_doc.value('modules[1]').string()
|
|
|
|
assert value == 'toml'
|
|
|
|
value = toml_doc.value('errors[11]').default_to('<none>').string()
|
|
|
|
assert value == '<none>'
|
2021-11-25 13:33:54 +03:00
|
|
|
|
2021-11-24 21:39:22 +03:00
|
|
|
value = toml_doc.value('themes[2].colors[0]').string()
|
|
|
|
assert value == 'blue'
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_any_value_query() {
|
|
|
|
toml_doc := toml.parse(toml_text) or { panic(err) }
|
|
|
|
themes := toml_doc.value('themes')
|
|
|
|
assert themes.value('[0].colors[0]').string() == 'red'
|
|
|
|
|
|
|
|
themes_arr := toml_doc.value('themes') as []toml.Any
|
|
|
|
assert themes_arr[0].value('colors[0]').string() == 'red'
|
|
|
|
|
|
|
|
mut any := themes
|
|
|
|
assert any.value('[1].name').string() == 'Lemon'
|
|
|
|
any = any.value('[1]')
|
|
|
|
assert any.value('name').string() == 'Lemon'
|
|
|
|
|
|
|
|
any = toml_doc.value('themes').value('[1].colors').value('[1]')
|
|
|
|
assert any.string() == 'yellow'
|
|
|
|
|
|
|
|
any = toml_doc.value('themes[1]').value('colors[1]')
|
|
|
|
assert any.string() == 'yellow'
|
|
|
|
|
|
|
|
any = toml_doc.value('themes[1].colors[0]')
|
|
|
|
assert any.string() == 'green'
|
|
|
|
|
|
|
|
any = toml_doc.value('values')
|
|
|
|
any = any.value('test')
|
|
|
|
assert any.int() == 2
|
2021-11-20 20:45:17 +03:00
|
|
|
}
|
2021-11-25 13:33:54 +03:00
|
|
|
|
|
|
|
fn test_inf_and_nan_query() {
|
|
|
|
toml_doc := toml.parse(toml_text) or { panic(err) }
|
|
|
|
|
|
|
|
value := toml_doc.value('values.nan').string()
|
|
|
|
assert value == 'nan'
|
|
|
|
|
|
|
|
mut value_u64 := toml_doc.value('values.inf').u64()
|
|
|
|
assert value_u64 == strconv.double_plus_infinity
|
|
|
|
value_u64 = toml_doc.value('values.minus-inf').u64()
|
|
|
|
assert value_u64 == strconv.double_minus_infinity
|
|
|
|
}
|