| 1 |
// SPDX-License-Identifier: MPL-2.0 |
| 2 |
// (c) Hare authors <https://harelang.org> |
| 3 |
|
| 4 |
use io; |
| 5 |
use memio; |
| 6 |
use strings; |
| 7 |
|
| 8 |
@test fn simple() void = { |
| 9 |
const buf = memio::fixed(strings::toutf8( |
| 10 |
"# This is a comment |
| 11 |
[sourcehut.org] |
| 12 |
name=Sourcehut |
| 13 |
description=The hacker's forge |
| 14 |
[harelang.org] |
| 15 |
name=Hare |
| 16 |
description=The Hare programming language |
| 17 |
")); |
| 18 |
const sc = scan(&buf); |
| 19 |
defer finish(&sc); |
| 20 |
|
| 21 |
// [sourcehut.org] |
| 22 |
ini_test(&sc, "sourcehut.org", "name", "Sourcehut"); |
| 23 |
ini_test(&sc, "sourcehut.org", "description", "The hacker's forge"); |
| 24 |
// [harelang.org] |
| 25 |
ini_test(&sc, "harelang.org", "name", "Hare"); |
| 26 |
ini_test(&sc, "harelang.org", "description", |
| 27 |
"The Hare programming language"); |
| 28 |
assert(next(&sc) is io::EOF); |
| 29 |
}; |
| 30 |
|
| 31 |
@test fn extended() void = { |
| 32 |
// TODO: expand? |
| 33 |
const buf = memio::fixed(strings::toutf8( |
| 34 |
"# Equal sign in the value |
| 35 |
exec=env VARIABLE=value binary |
| 36 |
|
| 37 |
# Unicode |
| 38 |
trademark=™ |
| 39 |
")); |
| 40 |
const sc = scan(&buf); |
| 41 |
defer finish(&sc); |
| 42 |
|
| 43 |
ini_test(&sc, "", "exec", "env VARIABLE=value binary"); |
| 44 |
ini_test(&sc, "", "trademark", "™"); |
| 45 |
assert(next(&sc) is io::EOF); |
| 46 |
}; |
| 47 |
|
| 48 |
|
| 49 |
@test fn invalid() void = { |
| 50 |
// Missing equal sign |
| 51 |
const buf = memio::fixed(strings::toutf8("novalue\n")); |
| 52 |
const sc = scan(&buf); |
| 53 |
defer finish(&sc); |
| 54 |
|
| 55 |
assert(next(&sc) as error as syntaxerr == 1); |
| 56 |
|
| 57 |
// Unterminated section header |
| 58 |
const buf = memio::fixed(strings::toutf8("[dangling\n")); |
| 59 |
const sc = scan(&buf); |
| 60 |
defer finish(&sc); |
| 61 |
|
| 62 |
assert(next(&sc) as error as syntaxerr == 1); |
| 63 |
|
| 64 |
// Line numbering and recovery |
| 65 |
const buf = memio::fixed(strings::toutf8( |
| 66 |
"[a] |
| 67 |
b=c |
| 68 |
d=e |
| 69 |
[f] |
| 70 |
g=h |
| 71 |
|
| 72 |
i |
| 73 |
|
| 74 |
j=k |
| 75 |
")); |
| 76 |
const sc = scan(&buf); |
| 77 |
defer finish(&sc); |
| 78 |
|
| 79 |
ini_test(&sc, "a", "b", "c"); |
| 80 |
ini_test(&sc, "a", "d", "e"); |
| 81 |
ini_test(&sc, "f", "g", "h"); |
| 82 |
assert(next(&sc) as error as syntaxerr == 7); |
| 83 |
ini_test(&sc, "f", "j", "k"); |
| 84 |
assert(next(&sc) is io::EOF); |
| 85 |
}; |
| 86 |
|
| 87 |
fn ini_test( |
| 88 |
sc: *scanner, |
| 89 |
section: const str, |
| 90 |
key: const str, |
| 91 |
value: const str, |
| 92 |
) void = { |
| 93 |
const ent = next(sc)! as entry; |
| 94 |
assert(ent.0 == section); |
| 95 |
assert(ent.1 == key); |
| 96 |
assert(ent.2 == value); |
| 97 |
}; |