v / vlib / toml / tests / toml_bom_test.v
51 lines · 40 sloc · 1.49 KB · e2e5cf8db56f3562c7baa735061690be936bdf3e
Raw
1import os
2import toml
3import toml.to
4import toml.ast
5
6const empty_toml_document = toml.Doc{
7 ast: &ast.Root(unsafe { nil })
8}
9
10const toml_text_with_utf8_bom = os.read_file(os.real_path(os.join_path(os.dir(@FILE), 'testdata',
11
12 'toml_with_utf8_bom' + '.toml'))) or { panic(err) }
13const toml_text_with_utf16_bom = os.read_file(os.real_path(os.join_path(os.dir(@FILE), 'testdata',
14
15 'toml_with_utf16_bom' + '.toml'))) or { panic(err) }
16const toml_text_with_utf32_bom = os.read_file(os.real_path(os.join_path(os.dir(@FILE), 'testdata',
17
18 'toml_with_utf32_bom' + '.toml'))) or { panic(err) }
19
20fn test_toml_with_bom() {
21 toml_doc := toml.parse_text(toml_text_with_utf8_bom) or { panic(err) }
22 toml_json := to.json(toml_doc)
23
24 title := toml_doc.value('title')
25 assert title == toml.Any('TOML Example')
26 assert title as string == 'TOML Example'
27
28 owner := toml_doc.value('owner') as map[string]toml.Any
29 any_name := owner.value('name')
30 assert any_name.string() == 'Tom Preston-Werner'
31
32 database := toml_doc.value('database') as map[string]toml.Any
33 db_serv := database['server'] or {
34 panic('could not access "server" index in "database" variable')
35 }
36 assert db_serv as string == '192.168.1.1'
37
38 // Re-cycle bad_toml_doc
39 mut bad_toml_doc := empty_toml_document
40 bad_toml_doc = toml.parse_text(toml_text_with_utf16_bom) or {
41 println(' ${err.msg()}')
42 assert true
43 empty_toml_document
44 }
45
46 bad_toml_doc = toml.parse_text(toml_text_with_utf32_bom) or {
47 println(' ${err.msg()}')
48 assert true
49 empty_toml_document
50 }
51}
52