scanner: add string test for token type

This commit is contained in:
Fatih Arslan 2015-10-07 15:31:27 +03:00
parent f507aa7d78
commit c62cc48b92
2 changed files with 36 additions and 4 deletions

View File

@ -67,10 +67,6 @@ var tokens = [...]string{
} }
// String returns the string corresponding to the token tok. // String returns the string corresponding to the token tok.
// For operators, delimiters, and keywords the string is the actual
// token character sequence (e.g., for the token ADD, the string is
// "+"). For all other tokens the string corresponds to the token
// constant name (e.g. for the token IDENT, the string is "IDENT").
func (t TokenType) String() string { func (t TokenType) String() string {
s := "" s := ""
if 0 <= t && t < TokenType(len(tokens)) { if 0 <= t && t < TokenType(len(tokens)) {

36
scanner/token_test.go Normal file
View File

@ -0,0 +1,36 @@
package scanner
import "testing"
func TestTokenTypeString(t *testing.T) {
var tokens = []struct {
tt TokenType
str string
}{
{ILLEGAL, "ILLEGAL"},
{EOF, "EOF"},
{COMMENT, "COMMENT"},
{IDENT, "IDENT"},
{NUMBER, "NUMBER"},
{FLOAT, "FLOAT"},
{BOOL, "BOOL"},
{STRING, "STRING"},
{LBRACK, "LBRACK"},
{LBRACE, "LBRACE"},
{COMMA, "COMMA"},
{PERIOD, "PERIOD"},
{RBRACK, "RBRACK"},
{RBRACE, "RBRACE"},
{ASSIGN, "ASSIGN"},
{ADD, "ADD"},
{SUB, "SUB"},
}
for _, token := range tokens {
if token.tt.String() != token.str {
t.Errorf("want: %q got:%q\n", token.str, token.tt)
}
}
}