mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
vlib: add toml module + tests (#11964)
This commit is contained in:
185
vlib/toml/tests/burntsushi.toml-test_test.v
Normal file
185
vlib/toml/tests/burntsushi.toml-test_test.v
Normal file
@@ -0,0 +1,185 @@
|
||||
import os
|
||||
import toml
|
||||
|
||||
// Instructions for developers:
|
||||
// The actual tests and data can be obtained by doing:
|
||||
// `cd vlib/toml/tests/testdata`
|
||||
// `git clone --depth 1 https://github.com/BurntSushi/toml-test.git burntsushi/toml-test`
|
||||
// See also the CI toml tests
|
||||
// TODO Goal: make parsing AND value retrieval of all of https://github.com/BurntSushi/toml-test/test/ pass
|
||||
const (
|
||||
valid_exceptions = [
|
||||
'float/inf-and-nan.toml',
|
||||
'table/array-table-array.toml',
|
||||
]
|
||||
invalid_exceptions = [
|
||||
// String
|
||||
'string/basic-multiline-out-of-range-unicode-escape-1.toml',
|
||||
'string/basic-byte-escapes.toml',
|
||||
'string/bad-multiline.toml',
|
||||
'string/multiline-escape-space.toml',
|
||||
'string/bad-codepoint.toml',
|
||||
'string/literal-multiline-quotes-1.toml',
|
||||
'string/literal-multiline-quotes-2.toml',
|
||||
'string/multiline-quotes-1.toml',
|
||||
'string/basic-multiline-out-of-range-unicode-escape-2.toml',
|
||||
'string/bad-slash-escape.toml',
|
||||
'string/basic-out-of-range-unicode-escape-1.toml',
|
||||
'string/basic-out-of-range-unicode-escape-2.toml',
|
||||
'string/multiline-quotes-2.toml',
|
||||
'string/bad-uni-esc.toml',
|
||||
'string/bad-escape.toml',
|
||||
'string/basic-multiline-unknown-escape.toml',
|
||||
'string/missing-quotes.toml',
|
||||
'string/bad-byte-escape.toml',
|
||||
'string/basic-unknown-escape.toml',
|
||||
// Integer
|
||||
'integer/capital-bin.toml',
|
||||
'integer/invalid-bin.toml',
|
||||
'integer/invalid-oct.toml',
|
||||
// Encoding
|
||||
'encoding/bad-utf8-in-comment.toml',
|
||||
'encoding/bad-utf8-in-string.toml',
|
||||
// Float
|
||||
'float/exp-double-us.toml',
|
||||
'float/exp-leading-us.toml',
|
||||
'float/nan_underscore.toml',
|
||||
'float/nan-incomplete-1.toml',
|
||||
'invalid/float/exp-point-1.toml',
|
||||
'float/trailing-us.toml',
|
||||
'float/us-after-point.toml',
|
||||
'float/exp-double-e-1.toml',
|
||||
'float/inf-incomplete-1.toml',
|
||||
'float/inf_underscore.toml',
|
||||
// Table
|
||||
'table/rrbrace.toml',
|
||||
'table/duplicate-table-array2.toml',
|
||||
'table/duplicate.toml',
|
||||
'table/array-implicit.toml',
|
||||
'table/injection-2.toml',
|
||||
'table/llbrace.toml',
|
||||
'table/injection-1.toml',
|
||||
'table/duplicate-table-array.toml',
|
||||
// Array
|
||||
'array/tables-1.toml',
|
||||
'array/no-close-2.toml',
|
||||
'array/missing-separator.toml',
|
||||
'array/text-after-array-entries.toml',
|
||||
'array/no-close.toml',
|
||||
'array/text-before-array-separator.toml',
|
||||
// Date / Time
|
||||
'datetime/impossible-date.toml',
|
||||
'datetime/no-leads-with-milli.toml',
|
||||
'datetime/no-leads.toml',
|
||||
// Control
|
||||
'control/string-us.toml',
|
||||
'control/comment-lf.toml',
|
||||
'control/multi-us.toml',
|
||||
'control/rawstring-del.toml',
|
||||
'control/rawmulti-del.toml',
|
||||
'control/rawstring-us.toml',
|
||||
'control/string-bs.toml',
|
||||
'control/multi-null.toml',
|
||||
'control/rawstring-lf.toml',
|
||||
'control/rawmulti-null.toml',
|
||||
'control/comment-null.toml',
|
||||
'control/multi-lf.toml',
|
||||
'control/comment-del.toml',
|
||||
'control/rawstring-null.toml',
|
||||
'control/rawmulti-lf.toml',
|
||||
'control/multi-del.toml',
|
||||
'control/string-del.toml',
|
||||
'control/rawmulti-us.toml',
|
||||
'control/comment-us.toml',
|
||||
'control/string-lf.toml',
|
||||
'control/string-null.toml',
|
||||
'inline-table/empty.toml',
|
||||
'inline-table/double-comma.toml',
|
||||
'inline-table/trailing-comma.toml',
|
||||
'inline-table/linebreak-4.toml',
|
||||
'inline-table/linebreak-3.toml',
|
||||
'inline-table/linebreak-1.toml',
|
||||
'inline-table/linebreak-2.toml',
|
||||
'inline-table/no-comma.toml',
|
||||
// Key
|
||||
'key/duplicate.toml',
|
||||
'key/after-table.toml',
|
||||
'key/duplicate-keys.toml',
|
||||
'key/after-value.toml',
|
||||
'key/newline.toml',
|
||||
'key/without-value-2.toml',
|
||||
'key/no-eol.toml',
|
||||
'key/after-array.toml',
|
||||
'key/multiline.toml',
|
||||
]
|
||||
)
|
||||
|
||||
// test_burnt_sushi_tomltest run though 'testdata/burntsushi/toml-test/*' if found.
|
||||
fn test_burnt_sushi_tomltest() {
|
||||
this_file := @FILE
|
||||
test_root := os.join_path(os.dir(this_file), 'testdata', 'burntsushi', 'toml-test',
|
||||
'tests')
|
||||
if os.is_dir(test_root) {
|
||||
valid_test_files := os.walk_ext(os.join_path(test_root, 'valid'), '.toml')
|
||||
println('Testing $valid_test_files.len valid TOML files...')
|
||||
mut valid := 0
|
||||
mut e := 0
|
||||
for i, valid_test_file in valid_test_files {
|
||||
relative := valid_test_file.all_after(os.join_path('toml-test', 'tests', 'valid')).trim_left(os.path_separator)
|
||||
if relative !in valid_exceptions {
|
||||
println('OK [$i/$valid_test_files.len] "$valid_test_file"...')
|
||||
toml_doc := toml.parse_file(valid_test_file) or { panic(err) }
|
||||
|
||||
// parsed_json := toml_doc.to_json().replace(' ','')
|
||||
// mut test_suite_json := os.read_file(valid_test_file.all_before_last('.')+'.json') or { panic(err) }
|
||||
// test_suite_json = test_suite_json.replace('\n ','').replace(' ','')
|
||||
// println(test_suite_json.replace('\n ','').replace(' ',''))
|
||||
// assert parsed_json == test_suite_json
|
||||
valid++
|
||||
} else {
|
||||
e++
|
||||
println('SKIP [$i/$valid_test_files.len] "$valid_test_file" EXCEPTION [$e/$valid_exceptions.len]...')
|
||||
}
|
||||
}
|
||||
println('$valid/$valid_test_files.len TOML files was parsed correctly')
|
||||
if valid_exceptions.len > 0 {
|
||||
println('TODO Skipped parsing of $valid_exceptions.len valid TOML files...')
|
||||
}
|
||||
|
||||
// NOTE uncomment to see list of skipped files
|
||||
// assert false
|
||||
|
||||
// TODO test cases where the parser should fail
|
||||
invalid_test_files := os.walk_ext(os.join_path(test_root, 'invalid'), '.toml')
|
||||
println('Testing $invalid_test_files.len invalid TOML files...')
|
||||
mut invalid := 0
|
||||
e = 0
|
||||
for i, invalid_test_file in invalid_test_files {
|
||||
relative := invalid_test_file.all_after(os.join_path('toml-test', 'tests',
|
||||
'invalid')).trim_left(os.path_separator)
|
||||
if relative !in invalid_exceptions {
|
||||
println('OK [$i/$invalid_test_files.len] "$invalid_test_file"...')
|
||||
if toml_doc := toml.parse_file(invalid_test_file) {
|
||||
assert false
|
||||
} else {
|
||||
println(' $err.msg')
|
||||
assert true // err.msg == 'your error'
|
||||
}
|
||||
invalid++
|
||||
} else {
|
||||
e++
|
||||
println('SKIP [$i/$invalid_test_files.len] "$invalid_test_file" EXCEPTION [$e/$invalid_exceptions.len]...')
|
||||
}
|
||||
}
|
||||
println('$invalid/$invalid_test_files.len TOML files was parsed correctly')
|
||||
if invalid_exceptions.len > 0 {
|
||||
println('TODO Skipped parsing of $invalid_exceptions.len invalid TOML files...')
|
||||
}
|
||||
|
||||
// NOTE uncomment to see list of skipped files
|
||||
// assert false
|
||||
} else {
|
||||
println('No test data directory found in "$test_root"')
|
||||
assert true
|
||||
}
|
||||
}
|
||||
83
vlib/toml/tests/compact_test.v
Normal file
83
vlib/toml/tests/compact_test.v
Normal file
@@ -0,0 +1,83 @@
|
||||
import toml
|
||||
|
||||
// Complete text from the example in the README.md:
|
||||
// https://github.com/toml-lang/toml/blob/3b11f6921da7b6f5db37af039aa021fee450c091/README.md#Example
|
||||
const toml_text = '# This is a TOML document.
|
||||
title = "TOML Example"
|
||||
[owner]
|
||||
name = "Tom Preston-Werner"
|
||||
dob = 1979-05-27T07:32:00-08:00 # First class dates
|
||||
[database]
|
||||
server = "192.168.1.1"
|
||||
ports = [ 8000, 8001, 8002 ]
|
||||
connection_max = 5000
|
||||
enabled = true
|
||||
[servers]
|
||||
# Indentation (tabs and/or spaces) is allowed but not required
|
||||
[servers.alpha]
|
||||
ip = "10.0.0.1"
|
||||
dc = "eqdc10"
|
||||
[servers.beta]
|
||||
ip = "10.0.0.2"
|
||||
dc = "eqdc10"
|
||||
[clients]
|
||||
data=[["gamma","delta"],[1,2]]
|
||||
# Line breaks are OK when inside arrays
|
||||
hosts = [
|
||||
"alpha",
|
||||
"omega"
|
||||
]'
|
||||
|
||||
fn test_parse_compact_text() {
|
||||
toml_doc := toml.parse(toml_text) or { panic(err) }
|
||||
|
||||
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
|
||||
any_name := owner.value('name') or { panic(err) }
|
||||
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'
|
||||
|
||||
// TODO BUG depending on WHAT directory the tests is run from, this one assert sometimes fail?!?!
|
||||
// assert toml_doc.value('owner.name') as string == 'Tom Preston-Werner'
|
||||
|
||||
assert toml_doc.value('database.server') as string == '192.168.1.1'
|
||||
|
||||
database_ports := toml_doc.value('database.ports') as []toml.Any
|
||||
assert database_ports[0] as i64 == 8000
|
||||
assert database_ports[1] as i64 == 8001
|
||||
assert database_ports[2] as i64 == 8002
|
||||
assert database_ports[0].int() == 8000
|
||||
assert database_ports[1].int() == 8001
|
||||
assert database_ports[2].int() == 8002
|
||||
|
||||
assert toml_doc.value('database.connection_max') as i64 == 5000
|
||||
assert toml_doc.value('database.enabled') as bool == true
|
||||
|
||||
assert toml_doc.value('servers.alpha.ip').string() == '10.0.0.1'
|
||||
assert toml_doc.value('servers.alpha.dc').string() == 'eqdc10'
|
||||
|
||||
assert toml_doc.value('servers.beta.ip').string() == '10.0.0.2'
|
||||
assert toml_doc.value('servers.beta.dc').string() == 'eqdc10'
|
||||
|
||||
clients_data := (toml_doc.value('clients.data') as []toml.Any)
|
||||
// dump(clients_data)
|
||||
// assert false
|
||||
gamma_delta_array := clients_data[0] as []toml.Any
|
||||
digits_array := clients_data[1] as []toml.Any
|
||||
assert gamma_delta_array[0].string() == 'gamma'
|
||||
assert gamma_delta_array[1].string() == 'delta'
|
||||
assert digits_array[0].int() == 1
|
||||
assert digits_array[1].int() == 2
|
||||
|
||||
clients_hosts := (toml_doc.value('clients.hosts') as []toml.Any).as_strings()
|
||||
assert clients_hosts[0] == 'alpha'
|
||||
assert clients_hosts[1] == 'omega'
|
||||
}
|
||||
73
vlib/toml/tests/datetime_test.v
Normal file
73
vlib/toml/tests/datetime_test.v
Normal file
@@ -0,0 +1,73 @@
|
||||
import toml
|
||||
import time
|
||||
|
||||
fn test_dates() {
|
||||
toml_txt := '
|
||||
# Offset Date-Time
|
||||
odt1 = 1979-05-27T07:32:00Z
|
||||
odt2 = 1979-05-27T00:32:00-07:00
|
||||
odt3 = 1979-05-27T00:32:00.999999-07:00
|
||||
odt4 = 1979-05-27 07:32:00Z
|
||||
# Local Date-Time
|
||||
ldt1 = 1979-05-27T07:32:00
|
||||
ldt2 = 1979-05-27T00:32:00.999999
|
||||
# Local Date
|
||||
ld1 = 1979-05-27
|
||||
# Local Time
|
||||
lt1 = 07:32:00
|
||||
lt2 = 00:32:00.999999
|
||||
'
|
||||
toml_doc := toml.parse(toml_txt) or { panic(err) }
|
||||
|
||||
// Re-use vars
|
||||
mut odt_time := time.parse_rfc3339('1979-05-27T07:32:00Z') or { panic(err) }
|
||||
mut odt_str := toml_doc.value('odt1').string()
|
||||
|
||||
// odt1 test section
|
||||
assert odt_str == '1979-05-26 07:32:00.000000' // W00t?! why 26th? Z=UTC?
|
||||
odt1 := toml_doc.value('odt1')
|
||||
assert odt1.datetime() == odt_time
|
||||
|
||||
// odt2 test section
|
||||
odt_time = time.parse_rfc3339('1979-05-27T00:32:00-07:00') or { panic(err) }
|
||||
odt2 := toml_doc.value('odt2')
|
||||
assert odt2.datetime() == odt_time
|
||||
|
||||
// odt3 test section
|
||||
odt_time = time.parse_rfc3339('1979-05-27T00:32:00.999999-07:00') or { panic(err) }
|
||||
odt3 := toml_doc.value('odt3')
|
||||
assert odt3.datetime() == odt_time
|
||||
|
||||
// odt4 test section
|
||||
odt_time = time.parse_rfc3339('1979-05-27 07:32:00Z') or { panic(err) }
|
||||
odt4 := toml_doc.value('odt4')
|
||||
assert odt4.datetime() == odt_time
|
||||
|
||||
// ldt1 test section
|
||||
odt_time = time.parse_rfc3339('1979-05-27T07:32:00') or { panic(err) }
|
||||
ldt1 := toml_doc.value('ldt1')
|
||||
assert ldt1.datetime() == odt_time
|
||||
|
||||
// ldt2 test section
|
||||
odt_time = time.parse_rfc3339('1979-05-27T00:32:00.999999') or { panic(err) }
|
||||
ldt2 := toml_doc.value('ldt2')
|
||||
assert ldt2.datetime() == odt_time
|
||||
|
||||
// ld1 test section
|
||||
odt_time = time.parse_rfc3339('1979-05-27') or { panic(err) }
|
||||
ld1 := toml_doc.value('ld1')
|
||||
assert ld1.datetime() == odt_time
|
||||
assert ld1.string() == '1979-05-27 00:00:00.000000'
|
||||
|
||||
// lt1 test section
|
||||
odt_time = time.parse_rfc3339('07:32:00') or { panic(err) }
|
||||
lt1 := toml_doc.value('lt1')
|
||||
assert lt1.datetime() == odt_time
|
||||
assert lt1.string() == '0000-00-00 07:32:00.000000'
|
||||
|
||||
// lt2 test section
|
||||
odt_time = time.parse_rfc3339('00:32:00.999999') or { panic(err) }
|
||||
lt2 := toml_doc.value('lt2')
|
||||
assert lt2.datetime() == odt_time
|
||||
assert lt2.string() == '0000-00-00 00:32:00.999999'
|
||||
}
|
||||
19
vlib/toml/tests/json_test.v
Normal file
19
vlib/toml/tests/json_test.v
Normal file
@@ -0,0 +1,19 @@
|
||||
import os
|
||||
import toml
|
||||
|
||||
fn test_parse() {
|
||||
toml_file :=
|
||||
os.real_path(os.join_path(os.dir(@FILE), 'testdata', os.file_name(@FILE).all_before_last('.'))) +
|
||||
'.toml'
|
||||
toml_doc := toml.parse(toml_file) or { panic(err) }
|
||||
|
||||
toml_json := toml_doc.to_json()
|
||||
out_file :=
|
||||
os.real_path(os.join_path(os.dir(@FILE), 'testdata', os.file_name(@FILE).all_before_last('.'))) +
|
||||
'.out'
|
||||
out_file_json := os.read_file(out_file) or { panic(err) }
|
||||
println(toml_json)
|
||||
assert toml_json == out_file_json
|
||||
|
||||
// assert false
|
||||
}
|
||||
43
vlib/toml/tests/nested_test.v
Normal file
43
vlib/toml/tests/nested_test.v
Normal file
@@ -0,0 +1,43 @@
|
||||
import toml
|
||||
|
||||
const toml_text = '
|
||||
[db]
|
||||
enabled = true
|
||||
|
||||
[servers]
|
||||
# Indentation (tabs and/or spaces) is allowed but not required
|
||||
[servers.alpha]
|
||||
ip = "10.0.0.1"
|
||||
dc = "eqdc10"
|
||||
|
||||
[servers.beta]
|
||||
ip = "10.0.0.2"
|
||||
dc = "eqdc10"
|
||||
|
||||
[servers.alpha.tricky]
|
||||
ip = "10.0.0.100"
|
||||
|
||||
[firewall.rules.limit]
|
||||
ip = "10.0.0.101"
|
||||
|
||||
[firewall.rules]
|
||||
block = true
|
||||
'
|
||||
|
||||
fn test_parse() {
|
||||
toml_doc := toml.parse(toml_text) or { panic(err) }
|
||||
// dump(toml_doc.ast)
|
||||
// assert false
|
||||
|
||||
assert toml_doc.value('db.enabled').bool()
|
||||
// TODO make this work
|
||||
assert toml_doc.value('servers.alpha.ip').string() == '10.0.0.1'
|
||||
assert toml_doc.value('servers.alpha.dc').string() == 'eqdc10'
|
||||
|
||||
assert toml_doc.value('servers.beta.ip').string() == '10.0.0.2'
|
||||
assert toml_doc.value('servers.beta.dc').string() == 'eqdc10'
|
||||
|
||||
assert toml_doc.value('servers.alpha.tricky.ip').string() == '10.0.0.100'
|
||||
assert toml_doc.value('firewall.rules.limit.ip').string() == '10.0.0.101'
|
||||
assert toml_doc.value('firewall.rules.block').bool() == true
|
||||
}
|
||||
67
vlib/toml/tests/strings_test.v
Normal file
67
vlib/toml/tests/strings_test.v
Normal file
@@ -0,0 +1,67 @@
|
||||
import os
|
||||
import toml
|
||||
|
||||
const (
|
||||
toml_multiline_text_1 = 'multi1 = """one"""
|
||||
multi2 = """one
|
||||
two"""
|
||||
multi3 = """
|
||||
one
|
||||
two
|
||||
three"""
|
||||
multi4 = """
|
||||
one
|
||||
two
|
||||
three
|
||||
four
|
||||
"""'
|
||||
toml_multiline_text_2 = "multi1 = '''one'''
|
||||
multi2 = '''one
|
||||
two'''
|
||||
multi3 = '''
|
||||
one
|
||||
two
|
||||
three'''
|
||||
multi4 = '''
|
||||
one
|
||||
two
|
||||
three
|
||||
four
|
||||
'''"
|
||||
)
|
||||
|
||||
fn test_multiline_strings() {
|
||||
mut toml_doc := toml.parse(toml_multiline_text_1) or { panic(err) }
|
||||
|
||||
mut value := toml_doc.value('multi1')
|
||||
assert value.string() == 'one'
|
||||
value = toml_doc.value('multi2')
|
||||
assert value.string() == 'one\ntwo'
|
||||
value = toml_doc.value('multi3')
|
||||
assert value.string() == '\none\ntwo\nthree'
|
||||
value = toml_doc.value('multi4')
|
||||
assert value.string() == '\none\ntwo\nthree\nfour\n'
|
||||
|
||||
toml_doc = toml.parse(toml_multiline_text_2) or { panic(err) }
|
||||
value = toml_doc.value('multi1')
|
||||
assert value.string() == 'one'
|
||||
value = toml_doc.value('multi2')
|
||||
assert value.string() == 'one\ntwo'
|
||||
value = toml_doc.value('multi3')
|
||||
assert value.string() == '\none\ntwo\nthree'
|
||||
value = toml_doc.value('multi4')
|
||||
assert value.string() == '\none\ntwo\nthree\nfour\n'
|
||||
|
||||
toml_file :=
|
||||
os.real_path(os.join_path(os.dir(@FILE), 'testdata', os.file_name(@FILE).all_before_last('.'))) +
|
||||
'.toml'
|
||||
toml_doc = toml.parse(toml_file) or { panic(err) }
|
||||
value = toml_doc.value('lit_one')
|
||||
assert value.string() == "'one quote'"
|
||||
value = toml_doc.value('lit_two')
|
||||
assert value.string() == "''two quotes''"
|
||||
value = toml_doc.value('mismatch1')
|
||||
assert value.string() == 'aaa' + "'''" + 'bbb'
|
||||
value = toml_doc.value('mismatch2')
|
||||
assert value.string() == 'aaa' + '"""' + 'bbb'
|
||||
}
|
||||
87
vlib/toml/tests/table_test.v
Normal file
87
vlib/toml/tests/table_test.v
Normal file
@@ -0,0 +1,87 @@
|
||||
import toml
|
||||
|
||||
const (
|
||||
toml_table_text = 'inline = {a.b = 42}
|
||||
|
||||
many.dots.here.dot.dot.dot = {a.b.c = 1, a.b.d = 2}
|
||||
|
||||
a = { a.b = 1 }
|
||||
b = { "a"."b" = 1 }
|
||||
c = { a . b = 1 }
|
||||
d = { \'a\' . "b" = 1 }
|
||||
e = {a.b=1}
|
||||
|
||||
[tbl]
|
||||
a.b.c = {d.e=1}
|
||||
|
||||
[tbl.x]
|
||||
a.b.c = {d.e=1}
|
||||
|
||||
[[arr]]
|
||||
t = {a.b=1}
|
||||
T = {a.b=1}
|
||||
|
||||
[[arr]]
|
||||
t = {a.b=2}
|
||||
T = {a.b=2}'
|
||||
)
|
||||
|
||||
fn test_tables() {
|
||||
mut toml_doc := toml.parse(toml_table_text) or { panic(err) }
|
||||
|
||||
mut value := toml_doc.value('inline.a.b')
|
||||
assert value.int() == 42
|
||||
|
||||
value = toml_doc.value('many.dots.here.dot.dot.dot.a.b.c')
|
||||
assert value.int() == 1
|
||||
|
||||
value = toml_doc.value('many.dots.here.dot.dot.dot.a.b.d')
|
||||
assert value.int() == 2
|
||||
|
||||
value = toml_doc.value('a.a.b')
|
||||
assert value.int() == 1
|
||||
|
||||
value = toml_doc.value('b.a.b')
|
||||
assert value.int() == 1
|
||||
|
||||
value = toml_doc.value('c.a.b')
|
||||
assert value.int() == 1
|
||||
|
||||
value = toml_doc.value('d.a.b')
|
||||
assert value.int() == 1
|
||||
|
||||
value = toml_doc.value('e.a.b')
|
||||
assert value.int() == 1
|
||||
|
||||
value = toml_doc.value('tbl.a.b.c.d.e')
|
||||
assert value.int() == 1
|
||||
|
||||
value = toml_doc.value('tbl.x.a.b.c.d.e')
|
||||
assert value.int() == 1
|
||||
|
||||
mut m := toml_doc.value('tbl') as map[string]toml.Any
|
||||
|
||||
value = m.value('a.b.c.d.e') or { panic(err) }
|
||||
assert value.int() == 1
|
||||
|
||||
value = m.value('x.a.b.c.d.e') or { panic(err) }
|
||||
assert value.int() == 1
|
||||
|
||||
arr := toml_doc.value('arr') as []toml.Any
|
||||
|
||||
arr0 := arr[0] as map[string]toml.Any
|
||||
value = arr0.value('t.a.b') or { panic(err) }
|
||||
assert value.int() == 1
|
||||
|
||||
arr1 := arr[1] as map[string]toml.Any
|
||||
value = arr1.value('T.a.b') or { panic(err) }
|
||||
assert value.int() == 1
|
||||
|
||||
arr2 := arr[2] as map[string]toml.Any
|
||||
value = arr2.value('t.a.b') or { panic(err) }
|
||||
assert value.int() == 2
|
||||
|
||||
arr3 := arr[3] as map[string]toml.Any
|
||||
value = arr3.value('T.a.b') or { panic(err) }
|
||||
assert value.int() == 2
|
||||
}
|
||||
1
vlib/toml/tests/testdata/json_test.out
vendored
Normal file
1
vlib/toml/tests/testdata/json_test.out
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{ "v": true, "animal": { "type": { "name": "pug" } }, "inline": { "a": 4, "b.c": 6, "b": { "c": 7 } }, "db": { "t": true }, "ij": { "a": { "i": 1, "j": 2 }, "b": { "i": "3", "j": "4" } }, "fruit": { "apple": { "color": "red", "taste": { "sweet": true }, "texture": { "smooth": true } } } }
|
||||
25
vlib/toml/tests/testdata/json_test.toml
vendored
Normal file
25
vlib/toml/tests/testdata/json_test.toml
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
|
||||
v = true
|
||||
|
||||
animal = { type.name = "pug" }
|
||||
|
||||
inline = { "a" = 4, "b.c" = 6, b.c = 7 }
|
||||
|
||||
[db]
|
||||
t = true
|
||||
|
||||
[ij]
|
||||
[ij.a]
|
||||
i = 1
|
||||
j = 2
|
||||
|
||||
[ij.b]
|
||||
i = "3"
|
||||
j = "4"
|
||||
|
||||
[fruit]
|
||||
apple.color = "red"
|
||||
apple.taste.sweet = true
|
||||
|
||||
[fruit.apple.texture]
|
||||
smooth = true
|
||||
15
vlib/toml/tests/testdata/strings_test.toml
vendored
Normal file
15
vlib/toml/tests/testdata/strings_test.toml
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
# Make sure that quotes inside multiline strings are allowed, including right
|
||||
# after the opening '''/""" and before the closing '''/"""
|
||||
|
||||
lit_one = ''''one quote''''
|
||||
lit_two = '''''two quotes'''''
|
||||
lit_one_space = ''' 'one quote' '''
|
||||
lit_two_space = ''' ''two quotes'' '''
|
||||
|
||||
one = """"one quote""""
|
||||
two = """""two quotes"""""
|
||||
one_space = """ "one quote" """
|
||||
two_space = """ ""two quotes"" """
|
||||
|
||||
mismatch1 = """aaa'''bbb"""
|
||||
mismatch2 = '''aaa"""bbb'''
|
||||
1
vlib/toml/tests/testdata/toml_test.out
vendored
Normal file
1
vlib/toml/tests/testdata/toml_test.out
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{ "title": "TOML Example", "owner": { "name": "Tom Preston-Werner", "dob": "1979-05-27T07:32:00-08:00" }, "database": { "server": "192.168.1.1", "ports": [ 8000, 8001, 8002 ], "connection_max": 5000, "enabled": true }, "servers": { "alpha": { "ip": "10.0.0.1", "dc": "eqdc10" }, "beta": { "ip": "10.0.0.2", "dc": "eqdc10" } }, "clients": { "data": [ [ "gamma", "delta" ], [ 1, 2 ] ], "hosts": [ "alpha", "omega" ] } }
|
||||
33
vlib/toml/tests/testdata/toml_test.toml
vendored
Normal file
33
vlib/toml/tests/testdata/toml_test.toml
vendored
Normal file
@@ -0,0 +1,33 @@
|
||||
# This is a TOML document.
|
||||
|
||||
title = "TOML Example"
|
||||
|
||||
[owner]
|
||||
name = "Tom Preston-Werner"
|
||||
dob = 1979-05-27T07:32:00-08:00 # First class dates
|
||||
|
||||
[database]
|
||||
server = "192.168.1.1"
|
||||
ports = [ 8000, 8001, 8002 ]
|
||||
connection_max = 5000
|
||||
enabled = true
|
||||
|
||||
[servers]
|
||||
|
||||
# Indentation (tabs and/or spaces) is allowed but not required
|
||||
[servers.alpha]
|
||||
ip = "10.0.0.1"
|
||||
dc = "eqdc10"
|
||||
|
||||
[servers.beta]
|
||||
ip = "10.0.0.2"
|
||||
dc = "eqdc10"
|
||||
|
||||
[clients]
|
||||
data = [ ["gamma", "delta"], [1, 2] ]
|
||||
|
||||
# Line breaks are OK when inside arrays
|
||||
hosts = [
|
||||
"alpha",
|
||||
"omega"
|
||||
]
|
||||
110
vlib/toml/tests/toml_test.v
Normal file
110
vlib/toml/tests/toml_test.v
Normal file
@@ -0,0 +1,110 @@
|
||||
import os
|
||||
import toml
|
||||
|
||||
const toml_text = os.read_file(
|
||||
os.real_path(os.join_path(os.dir(@FILE), 'testdata', os.file_name(@FILE).all_before_last('.'))) +
|
||||
'.toml') or { panic(err) }
|
||||
|
||||
fn test_toml() {
|
||||
// File containing the complete text from the example in the official TOML project README.md:
|
||||
// https://github.com/toml-lang/toml/blob/3b11f6921da7b6f5db37af039aa021fee450c091/README.md#Example
|
||||
toml_doc := toml.parse(toml_text) or { panic(err) }
|
||||
toml_json := toml_doc.to_json()
|
||||
|
||||
// NOTE Kept for easier debugging:
|
||||
// dump(toml_doc.ast)
|
||||
// println(toml_json)
|
||||
// assert false
|
||||
|
||||
assert toml_json == os.read_file(
|
||||
os.real_path(os.join_path(os.dir(@FILE), 'testdata', os.file_name(@FILE).all_before_last('.'))) +
|
||||
'.out') or { panic(err) }
|
||||
|
||||
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
|
||||
any_name := owner.value('name') or { panic(err) }
|
||||
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'
|
||||
|
||||
// TODO BUG depending on WHAT directory the tests is run from, this one assert sometimes fail?!?!
|
||||
// assert toml_doc.value('owner.name') as string == 'Tom Preston-Werner'
|
||||
|
||||
assert toml_doc.value('database.server') as string == '192.168.1.1'
|
||||
|
||||
database_ports := toml_doc.value('database.ports') as []toml.Any
|
||||
assert database_ports[0] as i64 == 8000
|
||||
assert database_ports[1] as i64 == 8001
|
||||
assert database_ports[2] as i64 == 8002
|
||||
assert database_ports[0].int() == 8000
|
||||
assert database_ports[1].int() == 8001
|
||||
assert database_ports[2].int() == 8002
|
||||
|
||||
assert toml_doc.value('database.connection_max') as i64 == 5000
|
||||
assert toml_doc.value('database.enabled') as bool == true
|
||||
|
||||
assert toml_doc.value('servers.alpha.ip').string() == '10.0.0.1'
|
||||
assert toml_doc.value('servers.alpha.dc').string() == 'eqdc10'
|
||||
|
||||
assert toml_doc.value('servers.beta.ip').string() == '10.0.0.2'
|
||||
assert toml_doc.value('servers.beta.dc').string() == 'eqdc10'
|
||||
|
||||
clients_data := (toml_doc.value('clients.data') as []toml.Any)
|
||||
// dump(clients_data)
|
||||
// assert false
|
||||
gamma_delta_array := clients_data[0] as []toml.Any
|
||||
digits_array := clients_data[1] as []toml.Any
|
||||
assert gamma_delta_array[0].string() == 'gamma'
|
||||
assert gamma_delta_array[1].string() == 'delta'
|
||||
assert digits_array[0].int() == 1
|
||||
assert digits_array[1].int() == 2
|
||||
|
||||
clients_hosts := (toml_doc.value('clients.hosts') as []toml.Any).as_strings()
|
||||
assert clients_hosts[0] == 'alpha'
|
||||
assert clients_hosts[1] == 'omega'
|
||||
}
|
||||
|
||||
fn test_toml_file() {
|
||||
out_path := os.join_path(os.temp_dir(), 'v_toml_tests')
|
||||
test_file := os.join_path(out_path, 'toml_example.toml')
|
||||
os.mkdir_all(out_path) or { assert false }
|
||||
defer {
|
||||
os.rmdir_all(out_path) or {}
|
||||
}
|
||||
os.write_file(test_file, toml_text) or { assert false }
|
||||
toml_doc := toml.parse_file(test_file) or { panic(err) }
|
||||
|
||||
toml_json := toml_doc.to_json()
|
||||
|
||||
// NOTE Kept for easier debugging:
|
||||
// dump(toml_doc.ast)
|
||||
// println(toml_json)
|
||||
// assert false
|
||||
|
||||
assert toml_json == os.read_file(
|
||||
os.real_path(os.join_path(os.dir(@FILE), 'testdata', os.file_name(@FILE).all_before_last('.'))) +
|
||||
'.out') or { panic(err) }
|
||||
}
|
||||
|
||||
fn test_toml_parse_text() {
|
||||
toml_doc := toml.parse_text(toml_text) or { panic(err) }
|
||||
toml_json := toml_doc.to_json()
|
||||
assert toml_json == os.read_file(
|
||||
os.real_path(os.join_path(os.dir(@FILE), 'testdata', os.file_name(@FILE).all_before_last('.'))) +
|
||||
'.out') or { panic(err) }
|
||||
}
|
||||
|
||||
fn test_toml_parse() {
|
||||
toml_doc := toml.parse(toml_text) or { panic(err) }
|
||||
toml_json := toml_doc.to_json()
|
||||
assert toml_json == os.read_file(
|
||||
os.real_path(os.join_path(os.dir(@FILE), 'testdata', os.file_name(@FILE).all_before_last('.'))) +
|
||||
'.out') or { panic(err) }
|
||||
}
|
||||
70
vlib/toml/tests/types_test.v
Normal file
70
vlib/toml/tests/types_test.v
Normal file
@@ -0,0 +1,70 @@
|
||||
import toml
|
||||
|
||||
fn test_string() {
|
||||
str_value := 'test string'
|
||||
toml_txt := 'string = "test string"'
|
||||
toml_doc := toml.parse(toml_txt) or { panic(err) }
|
||||
|
||||
value := toml_doc.value('string')
|
||||
assert value == toml.Any(str_value)
|
||||
assert value as string == str_value
|
||||
assert value.string() == str_value
|
||||
}
|
||||
|
||||
fn test_i64() {
|
||||
toml_txt := 'i64 = 120'
|
||||
toml_doc := toml.parse(toml_txt) or { panic(err) }
|
||||
|
||||
value := toml_doc.value('i64')
|
||||
assert value == toml.Any(i64(120))
|
||||
assert value as i64 == 120
|
||||
assert value.i64() == i64(120)
|
||||
}
|
||||
|
||||
fn test_bool() {
|
||||
toml_txt := '
|
||||
bool_true = true
|
||||
bool_false = false'
|
||||
toml_doc := toml.parse(toml_txt) or { panic(err) }
|
||||
|
||||
value_true := toml_doc.value('bool_true')
|
||||
assert value_true == toml.Any(true)
|
||||
assert value_true as bool == true
|
||||
assert value_true != toml.Any(false)
|
||||
assert value_true as bool != false
|
||||
assert value_true.bool() == true
|
||||
|
||||
value_false := toml_doc.value('bool_false')
|
||||
assert value_false == toml.Any(false)
|
||||
assert value_false as bool == false
|
||||
assert value_false != toml.Any(true)
|
||||
assert value_false as bool != true
|
||||
assert value_false.bool() == false
|
||||
}
|
||||
|
||||
fn test_bool_key_is_not_value() {
|
||||
toml_txt := 'true = true
|
||||
false = false'
|
||||
toml_doc := toml.parse(toml_txt) or { panic(err) }
|
||||
|
||||
value_true := toml_doc.value('true')
|
||||
assert value_true == toml.Any(true)
|
||||
assert value_true as bool == true
|
||||
assert value_true != toml.Any(false)
|
||||
assert value_true as bool != false
|
||||
|
||||
value_false := toml_doc.value('false')
|
||||
assert value_false == toml.Any(false)
|
||||
assert value_false as bool == false
|
||||
assert value_false != toml.Any(true)
|
||||
assert value_false as bool != true
|
||||
}
|
||||
|
||||
fn test_single_letter_key() {
|
||||
toml_txt := '[v]
|
||||
open_sourced = "Jun 22 2019 20:20:28"'
|
||||
toml_doc := toml.parse(toml_txt) or { panic(err) }
|
||||
|
||||
value := toml_doc.value('v.open_sourced').string()
|
||||
assert value == 'Jun 22 2019 20:20:28'
|
||||
}
|
||||
Reference in New Issue
Block a user