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