ident_test.ha (1534B)
1 // SPDX-License-Identifier: MPL-2.0 2 // (c) Hare authors <https://harelang.org> 3 4 use bufio; 5 use hare::ast; 6 use hare::lex; 7 use hare::lex::{ltok}; 8 use io; 9 use io::{mode}; 10 use memio; 11 use strings; 12 13 fn ident_test(in: str, expected: ast::ident, extra: ltok...) void = { 14 let buf = memio::fixed(strings::toutf8(in)); 15 let sc = bufio::newscanner(&buf); 16 defer bufio::finish(&sc); 17 let lexer = lex::init(&sc, "<test>"); 18 19 match (ident(&lexer)) { 20 case let id: ast::ident => 21 defer ast::ident_free(id); 22 assert(ast::ident_eq(id, expected)); 23 case lex::syntax => 24 assert(len(expected) == 0); 25 case error => abort(); 26 }; 27 28 for (let i = 0z; i < len(extra); i += 1) { 29 let tok = lex::lex(&lexer)!; 30 defer if (tok.1 is str) free(tok.1 as str); 31 assert(tok.0 == extra[i]); 32 }; 33 let tok = lex::lex(&lexer)!; 34 defer if (tok.1 is str) free(tok.1 as str); 35 assert(tok.0 == ltok::EOF); 36 }; 37 38 @test fn ident() void = { 39 ident_test(";", [], ltok::SEMICOLON); 40 ident_test("foo", ["foo"]); 41 ident_test("foo::bar", ["foo", "bar"]); 42 ident_test("foo::bar::baz", ["foo", "bar", "baz"]); 43 ident_test("foo::bar;", ["foo", "bar"], ltok::SEMICOLON); 44 45 // identifier exceeds maximum length 46 let buf: [ast::IDENT_MAX / 2 + ast::IDENT_MAX + 3]u8 = [0...]; 47 let buf = buf[..0]; 48 static append(buf, 'a'); 49 for (let i = 0z; i < ast::IDENT_MAX / 2; i += 1) { 50 static append(buf, [':', ':', 'a']...); 51 }; 52 ident_test(strings::fromutf8(buf)!, 53 ["a"...]: [ast::IDENT_MAX / 2 + 1]str); 54 static append(buf, [':', ':', 'a']...); 55 ident_test(strings::fromutf8(buf)!, []); 56 };