From a795a53191ba1a6d6b32152252c14238f43da712 Mon Sep 17 00:00:00 2001 From: Boris Kayi Date: Sat, 29 Jul 2023 04:03:52 +0200 Subject: [PATCH] test: add test cases for `token` pkg --- glox/token/token.go | 4 ++-- glox/token/token_test.go | 42 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 glox/token/token_test.go diff --git a/glox/token/token.go b/glox/token/token.go index 87269ee..7d77844 100644 --- a/glox/token/token.go +++ b/glox/token/token.go @@ -65,7 +65,7 @@ type Token struct { } var keywords = map[string]TokenType{ - "and": AND, + "&&": AND, "class": CLASS, "else": ELSE, "false": FALSE, @@ -74,7 +74,7 @@ var keywords = map[string]TokenType{ "fn": FUNCTION, "if": IF, "nil": NIL, - "or": OR, + "||": OR, "print": PRINT, "return": RETURN, "super": SUPER, diff --git a/glox/token/token_test.go b/glox/token/token_test.go new file mode 100644 index 0000000..8304c49 --- /dev/null +++ b/glox/token/token_test.go @@ -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) + } + } +}