2021-10-28 16:38:49 +03:00
|
|
|
import os
|
|
|
|
import toml
|
2021-11-18 14:27:59 +03:00
|
|
|
import toml.to
|
2021-10-28 16:38:49 +03:00
|
|
|
import toml.ast
|
|
|
|
|
|
|
|
const empty_toml_document = toml.Doc{
|
2023-04-13 08:38:21 +03:00
|
|
|
ast: &ast.Root(unsafe { nil })
|
2021-10-28 16:38:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
toml_text_with_utf8_bom = os.read_file(os.real_path(os.join_path(os.dir(@FILE), 'testdata',
|
|
|
|
'toml_with_utf8_bom' + '.toml'))) or { panic(err) }
|
|
|
|
toml_text_with_utf16_bom = os.read_file(os.real_path(os.join_path(os.dir(@FILE), 'testdata',
|
|
|
|
'toml_with_utf16_bom' + '.toml'))) or { panic(err) }
|
|
|
|
toml_text_with_utf32_bom = os.read_file(os.real_path(os.join_path(os.dir(@FILE), 'testdata',
|
|
|
|
'toml_with_utf32_bom' + '.toml'))) or { panic(err) }
|
|
|
|
)
|
|
|
|
|
|
|
|
fn test_toml_with_bom() {
|
2022-03-19 00:32:06 +03:00
|
|
|
toml_doc := toml.parse_text(toml_text_with_utf8_bom) or { panic(err) }
|
2021-11-18 14:27:59 +03:00
|
|
|
toml_json := to.json(toml_doc)
|
2021-10-28 16:38:49 +03:00
|
|
|
|
|
|
|
title := toml_doc.value('title')
|
|
|
|
assert title == toml.Any('TOML Example')
|
|
|
|
assert title as string == 'TOML Example'
|
|
|
|
|
|
|
|
owner := toml_doc.value('owner') as map[string]toml.Any
|
2021-11-19 11:26:45 +03:00
|
|
|
any_name := owner.value('name')
|
2021-10-28 16:38:49 +03:00
|
|
|
assert any_name.string() == 'Tom Preston-Werner'
|
|
|
|
|
|
|
|
database := toml_doc.value('database') as map[string]toml.Any
|
|
|
|
db_serv := database['server'] or {
|
|
|
|
panic('could not access "server" index in "database" variable')
|
|
|
|
}
|
|
|
|
assert db_serv as string == '192.168.1.1'
|
|
|
|
|
|
|
|
// Re-cycle bad_toml_doc
|
|
|
|
mut bad_toml_doc := empty_toml_document
|
2022-03-19 00:32:06 +03:00
|
|
|
bad_toml_doc = toml.parse_text(toml_text_with_utf16_bom) or {
|
2022-11-15 16:53:13 +03:00
|
|
|
println(' ${err.msg()}')
|
2021-10-28 16:38:49 +03:00
|
|
|
assert true
|
|
|
|
empty_toml_document
|
|
|
|
}
|
|
|
|
|
2022-03-19 00:32:06 +03:00
|
|
|
bad_toml_doc = toml.parse_text(toml_text_with_utf32_bom) or {
|
2022-11-15 16:53:13 +03:00
|
|
|
println(' ${err.msg()}')
|
2021-10-28 16:38:49 +03:00
|
|
|
assert true
|
|
|
|
empty_toml_document
|
|
|
|
}
|
|
|
|
}
|