Files
zzg a22c99344e
ci / test (push) Canceled after 0s
initial commit
2026-09-25 22:12:55 -07:00

68 lines
1.6 KiB
Go

package lexer
import "testing"
func TestLex(t *testing.T) {
tokens, err := Lex("1 + 2 * (x - 3) / 4 % 5 ^ 6 = y")
if err != nil {
t.Fatal(err)
}
want := []TokenType{
NUMBER, PLUS, NUMBER, STAR, LPAREN, IDENT, MINUS, NUMBER, RPAREN,
SLASH, NUMBER, PERCENT, NUMBER, CARET, NUMBER, ASSIGN, IDENT, EOF,
}
if len(tokens) != len(want) {
t.Fatalf("got %d tokens, want %d: %v", len(tokens), len(want), tokens)
}
for i, w := range want {
if tokens[i].Type != w {
t.Errorf("token %d = %v, want %v", i, tokens[i].Type, w)
}
}
}
func TestLexIdentifiersLowercased(t *testing.T) {
tokens, _ := Lex("FooBar")
if tokens[0].Value != "foobar" {
t.Errorf("= %q, want foobar (identifiers are case-insensitive)", tokens[0].Value)
}
}
func TestLexColumns(t *testing.T) {
tokens, _ := Lex("1 + 22")
if tokens[0].Column != 0 {
t.Errorf("first token column = %d, want 0", tokens[0].Column)
}
if tokens[2].Column != 4 {
t.Errorf("third token column = %d, want 4", tokens[2].Column)
}
}
func TestLexErrors(t *testing.T) {
for _, in := range []string{"@", "1 + $", "#"} {
if _, err := Lex(in); err == nil {
t.Errorf("Lex(%q) should fail", in)
}
}
}
func TestLexWhitespace(t *testing.T) {
tokens, err := Lex(" \t1\n+\r2 ")
if err != nil {
t.Fatal(err)
}
if len(tokens) != 4 { // 1, +, 2, EOF
t.Errorf("got %d tokens, want 4: %v", len(tokens), tokens)
}
}
func TestEmptyInput(t *testing.T) {
tokens, err := Lex("")
if err != nil {
t.Fatal(err)
}
if len(tokens) != 1 || tokens[0].Type != EOF {
t.Errorf("empty input = %v, want just EOF", tokens)
}
}