Skip to content

Commit

Permalink
test: add test cases for token pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
silverhairs committed Jul 29, 2023
1 parent b8d9818 commit a795a53
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
4 changes: 2 additions & 2 deletions glox/token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type Token struct {
}

var keywords = map[string]TokenType{
"and": AND,
"&&": AND,
"class": CLASS,
"else": ELSE,
"false": FALSE,
Expand All @@ -74,7 +74,7 @@ var keywords = map[string]TokenType{
"fn": FUNCTION,
"if": IF,
"nil": NIL,
"or": OR,
"||": OR,
"print": PRINT,
"return": RETURN,
"super": SUPER,
Expand Down
42 changes: 42 additions & 0 deletions glox/token/token_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package token

import "testing"

func TestLookupIdentifier(t *testing.T) {
tests := []struct {
word string
expected TokenType
}{
{word: "nil", expected: NIL},
{word: "class", expected: CLASS},
{word: "if", expected: IF},
{word: "maybe12", expected: IDENTIFIER},
{word: "else", expected: ELSE},
{word: "&&", expected: AND},
{word: "||", expected: OR},
{word: "return", expected: RETURN},
{word: "true", expected: TRUE},
{word: "false", expected: FALSE},
{word: "print", expected: PRINT},
{word: "fn", expected: FUNCTION},
{word: "while", expected: WHILE},
{word: "for", expected: FOR},
{word: "this", expected: THIS},
{word: "super", expected: SUPER},
{word: "let", expected: LET},
{word: "var", expected: LET},
{word: "func", expected: IDENTIFIER},
{word: "struct", expected: IDENTIFIER},
{word: "interface", expected: IDENTIFIER},
{word: "type", expected: IDENTIFIER},
}

for _, test := range tests {
got := LookupIdentifier(test.word)
expected := test.expected

if got != expected {
t.Fatalf("failed to get token type for '%s'. got='%v' expected='%v'", test.word, got, expected)
}
}
}

0 comments on commit a795a53

Please sign in to comment.