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) {
// scanDigits()
s.scanNumber()
// TODO(arslan)
}
@ -129,6 +129,10 @@ func (s *Scanner) Scan() (tok token.Token) {
return tok
}
func (s *Scanner) scanNumber() {
}
// scanString scans a quoted string
func (s *Scanner) scanString() {
for {
// '"' opening already consumed

View File

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