scanner: reuse tests code

This commit is contained in:
Fatih Arslan 2015-10-04 22:17:59 +03:00
parent 1f011b4e82
commit aa9105226b
2 changed files with 21 additions and 55 deletions

View File

@ -113,7 +113,7 @@ func (s *Scanner) Scan() (tok token.Token) {
} }
if isDigit(ch) { if isDigit(ch) {
// scanDigits() s.scanNumber()
// TODO(arslan) // TODO(arslan)
} }
@ -129,6 +129,10 @@ func (s *Scanner) Scan() (tok token.Token) {
return tok return tok
} }
func (s *Scanner) scanNumber() {
}
// scanString scans a quoted string
func (s *Scanner) scanString() { func (s *Scanner) scanString() {
for { for {
// '"' opening already consumed // '"' opening already consumed

View File

@ -15,36 +15,40 @@ type tokenPair struct {
text string text string
} }
func TestBool(t *testing.T) { func testTokenList(t *testing.T, tokenList []tokenPair) {
var tokenList = []tokenPair{
{token.BOOL, "true"},
{token.BOOL, "false"},
}
// create artifical source code // create artifical source code
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
for _, ident := range tokenList { for _, ident := range tokenList {
fmt.Fprintf(buf, " \t%s\n", ident.text) fmt.Fprintf(buf, " \t%s\n", ident.text)
} }
l, err := NewScanner(buf) s, err := NewScanner(buf)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
for _, ident := range tokenList { for _, ident := range tokenList {
tok := l.Scan() tok := s.Scan()
if tok != ident.tok { if tok != ident.tok {
t.Errorf("tok = %s want %s for %s\n", tok, ident.tok, ident.text) t.Errorf("tok = %s want %s for %s\n", tok, ident.tok, ident.text)
} }
if l.TokenText() != ident.text { if s.TokenText() != ident.text {
t.Errorf("text = %s want %s", l.TokenText(), ident.text) t.Errorf("text = %s want %s", s.TokenText(), ident.text)
} }
} }
} }
func TestBool(t *testing.T) {
var tokenList = []tokenPair{
{token.BOOL, "true"},
{token.BOOL, "false"},
}
testTokenList(t, tokenList)
}
func TestIdent(t *testing.T) { func TestIdent(t *testing.T) {
var tokenList = []tokenPair{ var tokenList = []tokenPair{
{token.IDENT, "a"}, {token.IDENT, "a"},
@ -65,28 +69,7 @@ func TestIdent(t *testing.T) {
{token.IDENT, "bar"}, {token.IDENT, "bar"},
} }
// create artifical source code testTokenList(t, tokenList)
buf := new(bytes.Buffer)
for _, ident := range tokenList {
fmt.Fprintf(buf, " \t%s\n", ident.text)
}
l, err := NewScanner(buf)
if err != nil {
t.Fatal(err)
}
for _, ident := range tokenList {
tok := l.Scan()
if tok != ident.tok {
t.Errorf("tok = %s want %s for %s\n", tok, ident.tok, ident.text)
}
if l.TokenText() != ident.text {
t.Errorf("text = %s want %s", l.TokenText(), ident.text)
}
}
} }
func TestString(t *testing.T) { func TestString(t *testing.T) {
@ -113,26 +96,5 @@ func TestString(t *testing.T) {
{token.STRING, `"` + f100 + `"`}, {token.STRING, `"` + f100 + `"`},
} }
// create artifical source code testTokenList(t, tokenList)
buf := new(bytes.Buffer)
for _, ident := range tokenList {
fmt.Fprintf(buf, " \t%s\n", ident.text)
}
l, err := NewScanner(buf)
if err != nil {
t.Fatal(err)
}
for _, ident := range tokenList {
tok := l.Scan()
if tok != ident.tok {
t.Errorf("tok = %s want %s for %s\n", tok, ident.tok, ident.text)
}
if l.TokenText() != ident.text {
t.Errorf("text = %s want %s", l.TokenText(), ident.text)
}
}
} }