2021-11-30 19:02:01 +00:00
|
|
|
package lexer
|
2021-11-30 19:45:39 +00:00
|
|
|
|
2021-11-30 19:02:01 +00:00
|
|
|
import (
|
2021-11-30 19:45:39 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"gitlab.com/Tyrel/monkey/token"
|
2021-11-30 19:02:01 +00:00
|
|
|
)
|
|
|
|
|
2021-11-30 19:45:39 +00:00
|
|
|
func TestNextToken(t *testing.T) {
|
|
|
|
input := `=+(){},;`
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
expectedType token.TokenType
|
|
|
|
expectedLiteral string
|
|
|
|
}{
|
|
|
|
{token.ASSIGN, "="},
|
|
|
|
{token.PLUS, "+"},
|
|
|
|
{token.LPAREN, "("},
|
|
|
|
{token.RPAREN, ")"},
|
|
|
|
{token.LBRACE, "{"},
|
|
|
|
{token.RBRACE, "}"},
|
|
|
|
{token.COMMA, ","},
|
|
|
|
{token.SEMICOLON, ";"},
|
|
|
|
{token.EOF, ""},
|
|
|
|
}
|
2021-11-30 19:02:01 +00:00
|
|
|
|
2021-11-30 19:45:39 +00:00
|
|
|
l := New(input)
|
|
|
|
for i, tt := range tests {
|
|
|
|
tok := l.NextToken()
|
2021-11-30 19:02:01 +00:00
|
|
|
|
2021-11-30 19:45:39 +00:00
|
|
|
if tok.Type != tt.expectedType {
|
|
|
|
t.Fatalf("tests[%d] - tokentype wrong. expected=%q, got %q",
|
|
|
|
i, tt.expectedType, tok.Type)
|
|
|
|
}
|
|
|
|
if tok.Literal != tt.expectedLiteral {
|
|
|
|
t.Fatalf("tests[%d] - literal wrong. expected=%q, got %q",
|
|
|
|
i, tt.expectedLiteral, tok.Literal)
|
|
|
|
}
|
|
|
|
}
|
2021-11-30 19:02:01 +00:00
|
|
|
}
|