| 1 | import os |
| 2 | import toml |
| 3 | |
| 4 | const toml_multiline_text_1 = 'multi1 = """one""" |
| 5 | multi2 = """one |
| 6 | two""" |
| 7 | multi3 = """ |
| 8 | one |
| 9 | two |
| 10 | three""" |
| 11 | multi4 = """ |
| 12 | one |
| 13 | two |
| 14 | three |
| 15 | four |
| 16 | """' |
| 17 | |
| 18 | const toml_multiline_text_2 = "multi1 = '''one''' |
| 19 | multi2 = '''one |
| 20 | two''' |
| 21 | multi3 = ''' |
| 22 | one |
| 23 | two |
| 24 | three''' |
| 25 | multi4 = ''' |
| 26 | one |
| 27 | two |
| 28 | three |
| 29 | four |
| 30 | '''" |
| 31 | |
| 32 | const toml_unicode_escapes = r'short = "\u03B4" |
| 33 | long = "\U000003B4"' |
| 34 | |
| 35 | fn 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 | |
| 71 | fn 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 | |
| 80 | fn 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 | |