You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
1.6 KiB
Go
77 lines
1.6 KiB
Go
package token
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestTokenCreation(t *testing.T) {
|
|
tests := []struct {
|
|
tokenType TokenType
|
|
lexeme string
|
|
literal interface{}
|
|
line int
|
|
}{
|
|
{LEFT_PAREN, "(", nil, 1},
|
|
{RIGHT_PAREN, ")", nil, 1},
|
|
{IDENTIFIER, "foo", nil, 1},
|
|
{STRING, "\"bar\"", "bar", 1},
|
|
{NUMBER, "123", 123, 1},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
token := New(tt.tokenType, tt.lexeme, tt.literal, tt.line)
|
|
if token.Type != tt.tokenType {
|
|
t.Errorf("expected token type %v, got %v", tt.tokenType, token.Type)
|
|
}
|
|
if token.Lexeme != tt.lexeme {
|
|
t.Errorf("expected lexeme %v, got %v", tt.lexeme, token.Lexeme)
|
|
}
|
|
if token.Literal != tt.literal {
|
|
t.Errorf("expected literal %v, got %v", tt.literal, token.Literal)
|
|
}
|
|
if token.Line != tt.line {
|
|
t.Errorf("expected line %v, got %v", tt.line, token.Line)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestTokenString(t *testing.T) {
|
|
token := New(IDENTIFIER, "foo", nil, 1)
|
|
expected := "foo"
|
|
if token.String() != expected {
|
|
t.Errorf("expected %v, got %v", expected, token.String())
|
|
}
|
|
}
|
|
|
|
func TestLookupKeyword(t *testing.T) {
|
|
tests := []struct {
|
|
identifier string
|
|
expected TokenType
|
|
}{
|
|
{"and", AND},
|
|
{"class", CLASS},
|
|
{"else", ELSE},
|
|
{"false", FALSE},
|
|
{"for", FOR},
|
|
{"fun", FUN},
|
|
{"if", IF},
|
|
{"nil", NIL},
|
|
{"or", OR},
|
|
{"print", PRINT},
|
|
{"return", RETURN},
|
|
{"super", SUPER},
|
|
{"this", THIS},
|
|
{"true", TRUE},
|
|
{"var", VAR},
|
|
{"while", WHILE},
|
|
{"foobar", IDENTIFIER},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tokenType := LookupKeyword(tt.identifier)
|
|
if tokenType != tt.expected {
|
|
t.Errorf("expected %v, got %v", tt.expected, tokenType)
|
|
}
|
|
}
|
|
}
|