1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

toml: use toml.parse_text and toml.parse_file in the tests

This commit is contained in:
Delyan Angelov 2022-03-18 23:32:06 +02:00
parent 5237d1d446
commit ee6b23c2a7
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
26 changed files with 70 additions and 75 deletions

View File

@ -38,7 +38,7 @@ hosts = [
]'
fn main() {
doc := toml.parse(toml_text) or { panic(err) }
doc := toml.parse_text(toml_text) or { panic(err) }
title := doc.value('title').string()
println('title: "$title"')
ip := doc.value('servers.alpha.ip').string()

View File

@ -8,7 +8,8 @@ Parsing files or `string`s containing TOML is easy.
Simply import the `toml` module and do:
```v ignore
doc := toml.parse(<file path or string>) or { panic(err) }
doc1 := toml.parse_text(<string content>) or { panic(err) }
doc2 := toml.parse_file(<file path>) or { panic(err) }
```
## Example
@ -54,7 +55,7 @@ hosts = [
]'
fn main() {
doc := toml.parse(toml_text) or { panic(err) }
doc := toml.parse_text(toml_text) or { panic(err) }
title := doc.value('title').string()
println('title: "$title"')
ip := doc.value('servers.alpha.ip').string()
@ -91,7 +92,7 @@ array = [
]
'
doc := toml.parse(toml_text) or { panic(err) }
doc := toml.parse_text(toml_text) or { panic(err) }
assert doc.value('val').bool() == true
assert doc.value('table.array[0].a').string() == 'A'
@ -142,6 +143,6 @@ array = [
]
'
doc := toml.parse(toml_text) or { panic(err) }
doc := toml.parse_text(toml_text) or { panic(err) }
assert to.json(doc) == '{ "val": true, "table": { "array": [ { "a": "A" }, { "b": "B" } ] } }'
```

View File

@ -18,7 +18,7 @@ color = "gray"'
)
fn test_tables() {
mut toml_doc := toml.parse(toml_table_text) or { panic(err) }
mut toml_doc := toml.parse_text(toml_table_text) or { panic(err) }
toml_json := to.json(toml_doc)

View File

@ -22,13 +22,13 @@ name = "Born in the USA"
name = "Dancing in the Dark"'
)
fn test_nested_array_of_tables() {
mut toml_doc := toml.parse(toml_text) or { panic(err) }
const fprefix = os.join_path(os.dir(@FILE), 'testdata', os.file_name(@FILE).all_before_last('.'))
fn test_nested_array_of_tables() ? {
mut toml_doc := toml.parse_text(toml_text) ?
toml_json := to.json(toml_doc)
eprintln(toml_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) }
assert toml_json == os.read_file(fprefix + '.out') ?
}

View File

@ -14,7 +14,7 @@ const (
)
fn test_nested_array_of_tables() {
mut toml_doc := toml.parse(toml_text) or { panic(err) }
mut toml_doc := toml.parse_text(toml_text) or { panic(err) }
toml_json := to.json(toml_doc)

View File

@ -6,7 +6,7 @@ fn test_array_of_tables_edge_case_file() {
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_doc := toml.parse_file(toml_file) or { panic(err) }
toml_json := to.json(toml_doc)
out_file :=

View File

@ -2,17 +2,14 @@ import os
import toml
import toml.to
fn test_array_of_tables_edge_case_file() {
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) }
const fprefix = os.join_path(os.dir(@FILE), 'testdata', os.file_name(@FILE).all_before_last('.'))
fn test_array_of_tables_edge_case_file() ? {
toml_doc := toml.parse_file(os.real_path(fprefix + '.toml')) ?
toml_json := to.json(toml_doc)
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) }
out_file_json := os.read_file(os.real_path(fprefix + '.out')) ?
println(toml_json)
assert toml_json == out_file_json
}

View File

@ -29,7 +29,7 @@ hosts = [
]'
fn test_parse_compact_text() {
toml_doc := toml.parse(toml_text) or { panic(err) }
toml_doc := toml.parse_text(toml_text) or { panic(err) }
title := toml_doc.value('title')
assert title == toml.Any('TOML Example')

View File

@ -4,7 +4,7 @@ fn test_crlf() {
str_value := 'test string'
mut toml_txt := 'crlf_string = "test string"\r\n
# Comment with CRLF is not allowed'
toml_doc := toml.parse(toml_txt) or { panic(err) }
toml_doc := toml.parse_text(toml_txt) or { panic(err) }
value := toml_doc.value('crlf_string')
assert value == toml.Any(str_value)
@ -16,7 +16,7 @@ fn test_crlf_is_parsable_just_like_lf() ? {
crlf_content := '# a comment\r\ntitle = "TOML Example"\r\n[database]\r\nserver = "192.168.1.1"\r\nports = [ 8000, 8001, 8002 ]\r\n'
all := [crlf_content, crlf_content.replace('\r\n', '\n')]
for content in all {
res := toml.parse(content) ?
res := toml.parse_text(content) ?
assert res.value('title') == toml.Any('TOML Example')
assert (res.value('database') as map[string]toml.Any)['server'] ? == toml.Any('192.168.1.1')
}

View File

@ -16,7 +16,7 @@ fn test_dates() {
lt1 = 07:32:00
lt2 = 00:32:00.999999
'
toml_doc := toml.parse(toml_txt) or { panic(err) }
toml_doc := toml.parse_text(toml_txt) or { panic(err) }
// Re-use vars
mut odt_time := toml.DateTime{'1979-05-27T07:32:00Z'}

View File

@ -3,7 +3,7 @@ import toml
fn test_default_to() {
default_value := 4321
mut toml_txt := 'var = 1234'
toml_doc := toml.parse(toml_txt) or { panic(err) }
toml_doc := toml.parse_text(toml_txt) or { panic(err) }
value := toml_doc.value('tar').default_to(default_value).int()
assert value == default_value
}

View File

@ -2,17 +2,14 @@ import os
import toml
import toml.to
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) }
const fprefix = os.join_path(os.dir(@FILE), 'testdata', os.file_name(@FILE).all_before_last('.'))
fn test_parse() ? {
toml_doc := toml.parse_file(os.real_path(fprefix + '.toml')) ?
toml_json := to.json(toml_doc)
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)
out_file_json := os.read_file(os.real_path(fprefix + '.out')) ?
assert toml_json == out_file_json
}

View File

@ -6,7 +6,7 @@ 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_doc := toml.parse_file(toml_file) or { panic(err) }
toml_json := to.json(toml_doc)
out_file :=

View File

@ -6,7 +6,7 @@ fn test_keys() {
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_doc := toml.parse_file(toml_file) or { panic(err) }
mut value := toml_doc.value('34-11')
assert value.int() == 23

View File

@ -15,7 +15,7 @@ fn test_large_file() {
'.toml'
if os.exists(toml_file) {
println('Testing parsing of large (${os.file_size(toml_file)} bytes) "$toml_file"...')
toml_doc := toml.parse(toml_file) or { panic(err) }
toml_doc := toml.parse_file(toml_file) or { panic(err) }
println('OK [1/1] "$toml_file"...') // So it can be checked with `v -stats test ...`
}
}

View File

@ -25,7 +25,7 @@ enabled = true
'
fn test_parse() {
toml_doc := toml.parse(toml_text) or { panic(err) }
toml_doc := toml.parse_text(toml_text) or { panic(err) }
// dump(toml_doc.ast)
// assert false

View File

@ -3,7 +3,7 @@ import toml
fn test_quoted_keys() {
str_value := 'V rocks!'
toml_txt := 'a."b.c" = "V rocks!"'
toml_doc := toml.parse(toml_txt) or { panic(err) }
toml_doc := toml.parse_text(toml_txt) or { panic(err) }
value := toml_doc.value('a."b.c"')
assert value == toml.Any(str_value)

View File

@ -50,7 +50,7 @@ mut:
}
fn test_reflect() {
toml_doc := toml.parse(toml_text) or { panic(err) }
toml_doc := toml.parse_text(toml_text) or { panic(err) }
mut user := toml_doc.reflect<User>()
user.bio = toml_doc.value('bio').reflect<Bio>()

View File

@ -12,7 +12,7 @@ fn test_spaced_keys() {
[ tube . test . "test.test" ]
h . "i.j." . "k" = "Cryptic"
'
toml_doc := toml.parse(toml_txt) or { panic(err) }
toml_doc := toml.parse_text(toml_txt) or { panic(err) }
mut value := toml_doc.value('a."b.c"[0].d.e')
assert value == toml.Any(str_value)
assert value as string == str_value

View File

@ -33,7 +33,7 @@ long = "\U000003B4"'
)
fn test_multiline_strings() {
mut toml_doc := toml.parse(toml_multiline_text_1) or { panic(err) }
mut toml_doc := toml.parse_text(toml_multiline_text_1) or { panic(err) }
mut value := toml_doc.value('multi1')
assert value.string() == 'one'
@ -44,7 +44,7 @@ fn test_multiline_strings() {
value = toml_doc.value('multi4')
assert value.string() == 'one\ntwo\nthree\nfour\n'
toml_doc = toml.parse(toml_multiline_text_2) or { panic(err) }
toml_doc = toml.parse_text(toml_multiline_text_2) or { panic(err) }
value = toml_doc.value('multi1')
assert value.string() == 'one'
value = toml_doc.value('multi2')
@ -57,7 +57,7 @@ fn test_multiline_strings() {
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_doc = toml.parse_file(toml_file) or { panic(err) }
value = toml_doc.value('lit_one')
assert value.string() == "'one quote'"
value = toml_doc.value('lit_two')
@ -69,7 +69,7 @@ fn test_multiline_strings() {
}
fn test_unicode_escapes() {
mut toml_doc := toml.parse(toml_unicode_escapes) or { panic(err) }
mut toml_doc := toml.parse_text(toml_unicode_escapes) or { panic(err) }
mut value := toml_doc.value('short')
assert value.string() == '\u03B4' // <- This escape is handled by V
@ -81,7 +81,7 @@ fn test_literal_strings() {
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_doc := toml.parse_file(toml_file) or { panic(err) }
assert toml_doc.value('lit1').string() == r'\' // '\'
assert toml_doc.value('lit2').string() == r'\\' // '\\'

View File

@ -27,7 +27,7 @@ T = {a.b=2}'
)
fn test_tables() {
mut toml_doc := toml.parse(toml_table_text) or { panic(err) }
mut toml_doc := toml.parse_text(toml_table_text) or { panic(err) }
mut value := toml_doc.value('inline.a.b')
assert value.int() == 42

View File

@ -17,7 +17,7 @@ const (
)
fn test_toml_with_bom() {
toml_doc := toml.parse(toml_text_with_utf8_bom) or { panic(err) }
toml_doc := toml.parse_text(toml_text_with_utf8_bom) or { panic(err) }
toml_json := to.json(toml_doc)
title := toml_doc.value('title')
@ -36,13 +36,13 @@ fn test_toml_with_bom() {
// Re-cycle bad_toml_doc
mut bad_toml_doc := empty_toml_document
bad_toml_doc = toml.parse(toml_text_with_utf16_bom) or {
bad_toml_doc = toml.parse_text(toml_text_with_utf16_bom) or {
println(' $err.msg()')
assert true
empty_toml_document
}
bad_toml_doc = toml.parse(toml_text_with_utf32_bom) or {
bad_toml_doc = toml.parse_text(toml_text_with_utf32_bom) or {
println(' $err.msg()')
assert true
empty_toml_document

View File

@ -7,7 +7,7 @@ const toml_text = os.read_file(os.real_path(os.join_path(os.dir(@FILE), 'testdat
'.toml') or { panic(err) }
fn test_toml_known_memory_corruption() {
toml_doc := toml.parse(toml_text) or { panic(err) }
toml_doc := toml.parse_text(toml_text) or { panic(err) }
owner := toml_doc.value('owner') as map[string]toml.Any
any_name := owner.value('name')
@ -34,7 +34,7 @@ fn test_toml_known_memory_corruption_2() {
lt1 = 07:32:00
lt2 = 00:32:00.999999
'
toml_doc := toml.parse(toml_txt) or { panic(err) }
toml_doc := toml.parse_text(toml_txt) or { panic(err) }
// ldt1 test section
odt_time := toml.DateTime{'1979-05-27T07:32:00'}

View File

@ -9,7 +9,7 @@ const toml_text = os.read_file(
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_doc := toml.parse_text(toml_text) or { panic(err) }
toml_json := to.json(toml_doc)
// NOTE Kept for easier debugging:
@ -98,7 +98,7 @@ fn test_toml_parse_text() {
}
fn test_toml_parse() {
toml_doc := toml.parse(toml_text) or { panic(err) }
toml_doc := toml.parse_text(toml_text) or { panic(err) }
toml_json := to.json(toml_doc)
assert toml_json == os.read_file(
os.real_path(os.join_path(os.dir(@FILE), 'testdata', os.file_name(@FILE).all_before_last('.'))) +

View File

@ -4,7 +4,7 @@ import strconv
fn test_string() {
str_value := 'test string'
toml_txt := 'string = "test string"'
toml_doc := toml.parse(toml_txt) or { panic(err) }
toml_doc := toml.parse_text(toml_txt) or { panic(err) }
value := toml_doc.value('string')
assert value == toml.Any(str_value)
@ -14,7 +14,7 @@ fn test_string() {
fn test_i64() {
toml_txt := 'i64 = 120'
toml_doc := toml.parse(toml_txt) or { panic(err) }
toml_doc := toml.parse_text(toml_txt) or { panic(err) }
value := toml_doc.value('i64')
assert value == toml.Any(i64(120))
@ -26,7 +26,7 @@ fn test_bool() {
toml_txt := '
bool_true = true
bool_false = false'
toml_doc := toml.parse(toml_txt) or { panic(err) }
toml_doc := toml.parse_text(toml_txt) or { panic(err) }
value_true := toml_doc.value('bool_true')
assert value_true == toml.Any(true)
@ -46,7 +46,7 @@ bool_false = false'
fn test_bool_key_is_not_value() {
toml_txt := 'true = true
false = false'
toml_doc := toml.parse(toml_txt) or { panic(err) }
toml_doc := toml.parse_text(toml_txt) or { panic(err) }
value_true := toml_doc.value('true')
assert value_true == toml.Any(true)
@ -64,7 +64,7 @@ false = false'
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) }
toml_doc := toml.parse_text(toml_txt) or { panic(err) }
value := toml_doc.value('v.open_sourced').string()
assert value == 'Jun 22 2019 20:20:28'
@ -74,7 +74,7 @@ fn test_hex_values() {
// Regression test
// '0xb' is carefully chosen to include the 'b' character that also denotes binary via 0b prefix.
toml_txt := 'hex = 0xb'
toml_doc := toml.parse(toml_txt) or { panic(err) }
toml_doc := toml.parse_text(toml_txt) or { panic(err) }
value := toml_doc.value('hex')
assert value as i64 == 11
@ -85,7 +85,7 @@ fn test_comment_as_last_value() {
toml_txt := '
test = 42
# this line has comment as last thing'
toml_doc := toml.parse(toml_txt) or { panic(err) }
toml_doc := toml.parse_text(toml_txt) or { panic(err) }
value := toml_doc.value('test')
assert value as i64 == 42
@ -93,31 +93,31 @@ test = 42
}
fn test_nan_and_inf_values() {
mut toml_doc := toml.parse('nan = nan') or { panic(err) }
mut toml_doc := toml.parse_text('nan = nan') or { panic(err) }
mut value := toml_doc.value('nan')
assert value.string() == 'nan'
toml_doc = toml.parse('nan = nan#comment') or { panic(err) }
toml_doc = toml.parse_text('nan = nan#comment') or { panic(err) }
value = toml_doc.value('nan')
assert value.string() == 'nan'
toml_doc = toml.parse('nan = -nan') or { panic(err) }
toml_doc = toml.parse_text('nan = -nan') or { panic(err) }
value = toml_doc.value('nan')
assert value.string() == 'nan'
toml_doc = toml.parse('nan = +nan') or { panic(err) }
toml_doc = toml.parse_text('nan = +nan') or { panic(err) }
value = toml_doc.value('nan')
assert value.string() == 'nan'
toml_doc = toml.parse('inf = inf') or { panic(err) }
toml_doc = toml.parse_text('inf = inf') or { panic(err) }
value = toml_doc.value('inf')
assert value.u64() == strconv.double_plus_infinity
toml_doc = toml.parse('inf = +inf') or { panic(err) }
toml_doc = toml.parse_text('inf = +inf') or { panic(err) }
value = toml_doc.value('inf')
assert value.u64() == strconv.double_plus_infinity
toml_doc = toml.parse('inf = -inf') or { panic(err) }
toml_doc = toml.parse_text('inf = -inf') or { panic(err) }
value = toml_doc.value('inf')
assert value.u64() == strconv.double_minus_infinity
}

View File

@ -50,7 +50,7 @@ colors = [
)
fn test_value_query_in_array() {
toml_doc := toml.parse(toml_text) or { panic(err) }
toml_doc := toml.parse_text(toml_text) or { panic(err) }
mut value := toml_doc.value('themes[0].colors[1]').string()
assert value == 'black'
value = toml_doc.value('themes[1].colors[0]').string()
@ -67,7 +67,7 @@ fn test_value_query_in_array() {
}
fn test_any_value_query() {
toml_doc := toml.parse(toml_text) or { panic(err) }
toml_doc := toml.parse_text(toml_text) or { panic(err) }
themes := toml_doc.value('themes')
assert themes.value('[0].colors[0]').string() == 'red'
@ -94,7 +94,7 @@ fn test_any_value_query() {
}
fn test_inf_and_nan_query() {
toml_doc := toml.parse(toml_text) or { panic(err) }
toml_doc := toml.parse_text(toml_text) or { panic(err) }
value := toml_doc.value('values.nan').string()
assert value == 'nan'
@ -106,7 +106,7 @@ fn test_inf_and_nan_query() {
}
fn test_any_value_query_2() {
toml_doc := toml.parse(toml_text_2) or { panic(err) }
toml_doc := toml.parse_text(toml_text_2) or { panic(err) }
defaults := toml_doc.value('defaults')
assert defaults.value('run.flags[0]').string() == '-f 1'
assert defaults.value('env[0].RUN_TIME').int() == 5