
ApiLexerTest.pudu
Pudu46 lines2.7 KB
1/** @Docgen.Api.LexerTests — source boundaries, literals, and comments lex without loss */2module PuduLangDocgen.ApiLexerTest34import Std.Io as Io5import Std.Test as Test6import PuduLangDocgen.Api.Lexer as Lexer789fn texts(source: Str) -> Array[Str] { Lexer.tokens(source).map(|token: Lexer.Token| token.text) }101112fn placed(source: Str) -> Array[(Str, Int)] { Lexer.tokens(source).map(|token: Lexer.Token| (token.text, token.line)) }131415fn main() -> Int {16 let checks = Test.suite("Api lexer", &[17 Test.equals("lines after comments and strings", &placed("a // c\n/* b\n c */ \"s\nt\" x"), &[("a", 1), ("\"s\nt\"", 3), ("x", 4)]),18 Test.equals("kinds", &Lexer.tokens("w 7 \"s\" + /// d\n/** b */").map(|token: Lexer.Token| token.kind), &[Lexer.Word, Lexer.Literal, Lexer.Literal, Lexer.Symbol, Lexer.LineDoc, Lexer.BlockDoc]),19 Test.equals("line comment at the end", &texts("x // c"), &["x"]),20 Test.equals("bare comment marker at the end", &texts("x //"), &["x"]),21 Test.equals("four slashes are no doc", &texts("//// not doc\nx"), &["x"]),22 Test.equals("empty block is no doc", &texts("/**/ x"), &["x"]),23 Test.equals("unclosed block comment", &texts("/* open"), &[]),24 Test.equals("unclosed doc block", &texts("/** doc"), &["doc"]),25 Test.equals("doc block lines", &texts("/**\n * one\n *\n * two\n */"), &["one\n\ntwo"]),26 Test.equals("unclosed string", &texts("\"abc"), &["\"abc"]),27 Test.equals("closing brace outside interpolation", &texts("\"a\}b\" x"), &["\"a\}b\"", "x"]),28 Test.equals("escaped quote in string", &texts("\"a\\\"b\" x"), &["\"a\\\"b\"", "x"]),29 Test.equals("character after a backslash", &texts("\\'y'"), &["\\", "'y'"]),30 Test.equals("quoted word is not a character", &texts("'ab'"), &["'", "ab", "'"]),31 Test.equals("quote after a word", &texts("ab'"), &["ab", "'"]),32 Test.equals("lone quote", &texts("x ' y"), &["x", "'", "y"]),33 Test.equals("quote at the end", &texts("x '"), &["x", "'"]),34 Test.equals("escape at the end", &texts("'\\n"), &["'", "\\", "n"]),35 Test.equals("escaped character", &texts("'\\n' x"), &["'\\n'", "x"]),36 Test.equals("overlong escape", &texts("'\\u\{12345678\}'"), &["'", "\\", "u", "\{", "12345678", "\}", "'"]),37 Test.equals("longest escape", &texts("'\\u\{1234567\}'"), &["'\\u\{1234567\}'"]),38 Test.equals("operator pairs", &texts("a->b<=c"), &["a", "->", "b", "<=", "c"]),39 Test.equals("symbol at the end", &texts("a-"), &["a", "-"]),40 Test.equals("underscored words", &texts("_a1 b_2"), &["_a1", "b_2"])41 ])42 let ran = Test.run(&checks)43 for failure in Test.failuresOf(&ran) { let _printed = Io.writeErrorLine(failure) }44 Test.report(&ran)45}46