parser: add scanning ident test

This commit is contained in:
Fatih Arslan 2015-10-03 21:06:30 +03:00
parent ca906622c2
commit 1b6ead19c2
2 changed files with 58 additions and 3 deletions

View File

@ -72,10 +72,12 @@ func (l *Lexer) scanIdentifier() (Token, string) {
// Create a buffer and read the current character into it. // Create a buffer and read the current character into it.
var buf bytes.Buffer var buf bytes.Buffer
// write current character before we move to the next for isLetter(l.ch) || isDigit(l.ch) {
buf.WriteRune(l.ch) buf.WriteRune(l.ch)
l.next()
}
return 0, "" return IDENT, buf.String()
} }
// Pos returns the position of the character immediately after the character or // Pos returns the position of the character immediately after the character or

53
parser/lexer_test.go Normal file
View File

@ -0,0 +1,53 @@
package parser
import (
"bytes"
"fmt"
"testing"
)
type token struct {
tok Token
text string
}
func TestIdent(t *testing.T) {
var identList = []token{
{IDENT, "a"},
{IDENT, "a0"},
{IDENT, "foobar"},
{IDENT, "abc123"},
{IDENT, "LGTM"},
{IDENT, "_"},
{IDENT, "_abc123"},
{IDENT, "abc123_"},
{IDENT, "_abc_123_"},
{IDENT, "_äöü"},
{IDENT, "_本"},
{IDENT, "äöü"},
{IDENT, "本"},
{IDENT, "a۰۱۸"},
{IDENT, "foo६४"},
{IDENT, "bar"},
}
// create artifical source code
buf := new(bytes.Buffer)
for _, ident := range identList {
fmt.Fprintf(buf, " \t%s\n", ident.text)
}
l := NewLexer(buf)
for _, ident := range identList {
tok, lit := l.Scan()
if tok != ident.tok {
t.Errorf("tok = %s want %s for %s\n", tok, ident.tok, ident.text)
}
if lit != ident.text {
t.Errorf("text = %s want %s", lit, ident.text)
}
}
}