2021-11-19 21:34:21 +03:00
|
|
|
import os
|
|
|
|
import toml
|
|
|
|
import toml.to
|
|
|
|
|
2022-05-28 09:17:28 +03:00
|
|
|
fn path_by_extension(ext string) string {
|
2022-11-15 16:53:13 +03:00
|
|
|
return os.join_path(os.dir(@VEXE), 'vlib/toml/tests/testdata/key_test.${ext}')
|
2022-05-28 09:17:28 +03:00
|
|
|
}
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_keys() {
|
2022-10-16 09:28:57 +03:00
|
|
|
toml_doc := toml.parse_file(path_by_extension('toml'))!
|
2021-11-19 21:34:21 +03:00
|
|
|
|
|
|
|
mut value := toml_doc.value('34-11')
|
|
|
|
assert value.int() == 23
|
|
|
|
|
|
|
|
value = toml_doc.value('1.2')
|
|
|
|
assert value.int() == 3
|
|
|
|
|
|
|
|
value = toml_doc.value('34-12.2')
|
|
|
|
assert value.int() == 42
|
|
|
|
|
|
|
|
toml_json := to.json(toml_doc)
|
2022-10-16 09:28:57 +03:00
|
|
|
out_file_json := os.read_file(path_by_extension('out'))!
|
2021-11-19 21:34:21 +03:00
|
|
|
println(toml_json)
|
|
|
|
assert toml_json == out_file_json
|
2022-05-28 09:17:28 +03:00
|
|
|
//
|
|
|
|
if x := toml_doc.value_opt('unknown key') {
|
|
|
|
assert false
|
|
|
|
} else {
|
|
|
|
assert err.msg() == 'no value for key'
|
|
|
|
}
|
|
|
|
if x := toml_doc.value_opt("'a") {
|
|
|
|
assert false
|
|
|
|
} else {
|
|
|
|
assert err.msg() == 'invalid dotted key'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_parse_dotted_key() {
|
2022-10-16 09:28:57 +03:00
|
|
|
assert toml.parse_dotted_key('')! == []
|
|
|
|
assert toml.parse_dotted_key('abc')! == ['abc']
|
|
|
|
assert toml.parse_dotted_key('tube.test."test.test".h."i.j."."k"')! == ['tube', 'test',
|
2022-05-28 09:17:28 +03:00
|
|
|
'test.test', 'h', 'i.j.', 'k']
|
|
|
|
if x := toml.parse_dotted_key("'some unclosed string") {
|
|
|
|
assert false
|
|
|
|
} else {
|
|
|
|
assert err.msg().starts_with('parse_dotted_key: could not parse key, missing closing string delimiter')
|
|
|
|
}
|
2021-11-19 21:34:21 +03:00
|
|
|
}
|