v / vlib / toml / tests / strings_test.v
97 lines · 85 sloc · 2.77 KB · f09826e928f9612bab9299faefff7cf34a503362
Raw
1import os
2import toml
3
4const toml_multiline_text_1 = 'multi1 = """one"""
5multi2 = """one
6two"""
7multi3 = """
8one
9two
10three"""
11multi4 = """
12one
13two
14three
15four
16"""'
17
18const toml_multiline_text_2 = "multi1 = '''one'''
19multi2 = '''one
20two'''
21multi3 = '''
22one
23two
24three'''
25multi4 = '''
26one
27two
28three
29four
30'''"
31
32const toml_unicode_escapes = r'short = "\u03B4"
33long = "\U000003B4"'
34
35fn test_multiline_strings() {
36 mut toml_doc := toml.parse_text(toml_multiline_text_1) or { panic(err) }
37
38 mut value := toml_doc.value('multi1')
39 assert value.string() == 'one'
40 value = toml_doc.value('multi2')
41 assert value.string() == 'one\ntwo'
42 value = toml_doc.value('multi3')
43 assert value.string() == 'one\ntwo\nthree'
44 value = toml_doc.value('multi4')
45 assert value.string() == 'one\ntwo\nthree\nfour\n'
46
47 toml_doc = toml.parse_text(toml_multiline_text_2) or { panic(err) }
48 value = toml_doc.value('multi1')
49 assert value.string() == 'one'
50 value = toml_doc.value('multi2')
51 assert value.string() == 'one\ntwo'
52 value = toml_doc.value('multi3')
53 assert value.string() == 'one\ntwo\nthree'
54 value = toml_doc.value('multi4')
55 assert value.string() == 'one\ntwo\nthree\nfour\n'
56
57 toml_file :=
58 os.real_path(os.join_path(os.dir(@FILE), 'testdata', os.file_name(@FILE).all_before_last('.'))) +
59 '.toml'
60 toml_doc = toml.parse_file(toml_file) or { panic(err) }
61 value = toml_doc.value('lit_one')
62 assert value.string() == "'one quote'"
63 value = toml_doc.value('lit_two')
64 assert value.string() == "''two quotes''"
65 value = toml_doc.value('mismatch1')
66 assert value.string() == 'aaa' + "'''" + 'bbb'
67 value = toml_doc.value('mismatch2')
68 assert value.string() == 'aaa' + '"""' + 'bbb'
69}
70
71fn test_unicode_escapes() {
72 mut toml_doc := toml.parse_text(toml_unicode_escapes) or { panic(err) }
73
74 mut value := toml_doc.value('short')
75 assert value.string() == '\u03B4' // <- This escape is handled by V
76 value = toml_doc.value('long')
77 assert value.string() == 'δ' // <- for the long escape we compare with the unicode point
78}
79
80fn test_literal_strings() {
81 toml_file :=
82 os.real_path(os.join_path(os.dir(@FILE), 'testdata', os.file_name(@FILE).all_before_last('.'))) +
83 '.toml'
84 toml_doc := toml.parse_file(toml_file) or { panic(err) }
85
86 assert toml_doc.value('lit1').string() == r'\' // '\'
87 assert toml_doc.value('lit2').string() == r'\\' // '\\'
88 assert toml_doc.value('lit3').string() == r'\tricky\' // '\tricky\'
89
90 // NOTE to Windows users: git is set to use Unix EOLs for all TOML files (*.toml) in the repo.
91 // See `.gitattributes` in the project root for the rule in action.
92 // These lines would look like this on Windows:
93 // assert toml_doc.value('ml_lit1').string() == '\r\n\\'
94 assert toml_doc.value('ml_lit1').string() == '\\'
95 assert toml_doc.value('ml_lit2').string() == '\\\n\\'
96 assert toml_doc.value('ml_lit3').string() == '\\\ntricky\\\n'
97}
98