v2 / vlib / toml / tests / toml_test.v
106 lines · 86 sloc · 3.57 KB · e465f7490c13b59a4c275c5f47967cbc8e2d66a5
Raw
1import os
2import toml
3import toml.to
4
5const toml_text = os.read_file(
6 os.real_path(os.join_path(os.dir(@FILE), 'testdata', os.file_name(@FILE).all_before_last('.'))) +
7 '.toml') or { panic(err) }
8
9fn test_toml() {
10 // File containing the complete text from the example in the official TOML project README.md:
11 // https://github.com/toml-lang/toml/blob/3b11f6921da7b6f5db37af039aa021fee450c091/README.md#Example
12 toml_doc := toml.parse_text(toml_text) or { panic(err) }
13 toml_json := to.json(toml_doc)
14
15 // NOTE Kept for easier debugging:
16 // dump(toml_doc.ast)
17 // println(toml_json)
18 // assert false
19
20 assert toml_json == os.read_file(
21 os.real_path(os.join_path(os.dir(@FILE), 'testdata', os.file_name(@FILE).all_before_last('.'))) +
22 '.out') or { panic(err) }
23
24 title := toml_doc.value('title')
25 assert title == toml.Any('TOML Example')
26 assert title as string == 'TOML Example'
27
28 database := toml_doc.value('database') as map[string]toml.Any
29 db_serv := database['server'] or {
30 panic('could not access "server" index in "database" variable')
31 }
32 assert db_serv as string == '192.168.1.1'
33
34 assert toml_doc.value('owner.name') as string == 'Tom Preston-Werner'
35
36 assert toml_doc.value('database.server') as string == '192.168.1.1'
37
38 database_ports := toml_doc.value('database.ports') as []toml.Any
39 assert database_ports[0] as i64 == 8000
40 assert database_ports[1] as i64 == 8001
41 assert database_ports[2] as i64 == 8002
42 assert database_ports[0].int() == 8000
43 assert database_ports[1].int() == 8001
44 assert database_ports[2].int() == 8002
45
46 assert toml_doc.value('database.connection_max') as i64 == 5000
47 assert toml_doc.value('database.enabled') as bool == true
48
49 assert toml_doc.value('servers.alpha.ip').string() == '10.0.0.1'
50 assert toml_doc.value('servers.alpha.dc').string() == 'eqdc10'
51
52 assert toml_doc.value('servers.beta.ip').string() == '10.0.0.2'
53 assert toml_doc.value('servers.beta.dc').string() == 'eqdc10'
54
55 clients_data := (toml_doc.value('clients.data') as []toml.Any)
56 // dump(clients_data)
57 // assert false
58 gamma_delta_array := clients_data[0] as []toml.Any
59 digits_array := clients_data[1] as []toml.Any
60 assert gamma_delta_array[0].string() == 'gamma'
61 assert gamma_delta_array[1].string() == 'delta'
62 assert digits_array[0].int() == 1
63 assert digits_array[1].int() == 2
64
65 clients_hosts := (toml_doc.value('clients.hosts') as []toml.Any).as_strings()
66 assert clients_hosts[0] == 'alpha'
67 assert clients_hosts[1] == 'omega'
68}
69
70fn test_toml_file() {
71 out_path := os.join_path(os.vtmp_dir(), 'toml_tests')
72 test_file := os.join_path(out_path, 'toml_example.toml')
73 os.mkdir_all(out_path) or { assert false }
74 defer {
75 os.rmdir_all(out_path) or {}
76 }
77 os.write_file(test_file, toml_text) or { assert false }
78 toml_doc := toml.parse_file(test_file) or { panic(err) }
79
80 toml_json := to.json(toml_doc)
81
82 // NOTE Kept for easier debugging:
83 // dump(toml_doc.ast)
84 // println(toml_json)
85 // assert false
86
87 assert toml_json == os.read_file(
88 os.real_path(os.join_path(os.dir(@FILE), 'testdata', os.file_name(@FILE).all_before_last('.'))) +
89 '.out') or { panic(err) }
90}
91
92fn test_toml_parse_text() {
93 toml_doc := toml.parse_text(toml_text) or { panic(err) }
94 toml_json := to.json(toml_doc)
95 assert toml_json == os.read_file(
96 os.real_path(os.join_path(os.dir(@FILE), 'testdata', os.file_name(@FILE).all_before_last('.'))) +
97 '.out') or { panic(err) }
98}
99
100fn test_toml_parse() {
101 toml_doc := toml.parse_text(toml_text) or { panic(err) }
102 toml_json := to.json(toml_doc)
103 assert toml_json == os.read_file(
104 os.real_path(os.join_path(os.dir(@FILE), 'testdata', os.file_name(@FILE).all_before_last('.'))) +
105 '.out') or { panic(err) }
106}
107