config: unit tests for lexer

This commit is contained in:
Mitchell Hashimoto 2014-09-09 14:33:50 -07:00
parent 7cdfc33b3f
commit 21472e98b8
4 changed files with 57 additions and 5 deletions

View File

@ -14,9 +14,9 @@ const lexEOF = 0
// The parser uses the type <prefix>Lex as a lexer. It must provide
// the methods Lex(*<prefix>SymType) int and Error(string).
type exprLex struct {
Err error
Err error
Input string
input string
pos int
width int
}
@ -105,12 +105,12 @@ func (x *exprLex) lexString(yylval *exprSymType) int {
// Return the next rune for the lexer.
func (x *exprLex) next() rune {
if int(x.pos) >= len(x.input) {
if int(x.pos) >= len(x.Input) {
x.width = 0
return lexEOF
}
r, w := utf8.DecodeRuneInString(x.input[x.pos:])
r, w := utf8.DecodeRuneInString(x.Input[x.pos:])
x.width = w
x.pos += x.width
return r

51
config/expr_lex_test.go Normal file
View File

@ -0,0 +1,51 @@
package config
import (
"io/ioutil"
"path/filepath"
"reflect"
"testing"
)
func TestLex(t *testing.T) {
cases := []struct {
Input string
Output []int
}{
{
"concat.hcl",
[]int{IDENTIFIER, LEFTPAREN,
STRING, COMMA, STRING, COMMA, STRING,
RIGHTPAREN, lexEOF},
},
}
for _, tc := range cases {
d, err := ioutil.ReadFile(filepath.Join(
fixtureDir, "interpolations", tc.Input))
if err != nil {
t.Fatalf("err: %s", err)
}
l := &exprLex{Input: string(d)}
var actual []int
for {
token := l.Lex(new(exprSymType))
actual = append(actual, token)
if token == lexEOF {
break
}
if len(actual) > 500 {
t.Fatalf("Input:%s\n\nExausted.", tc.Input)
}
}
if !reflect.DeepEqual(actual, tc.Output) {
t.Fatalf(
"Input: %s\n\nBad: %#v\n\nExpected: %#v",
tc.Input, actual, tc.Output)
}
}
}

View File

@ -21,7 +21,7 @@ func ExprParse(v string) (Interpolation, error) {
exprResult = nil
// Parse
lex := &exprLex{input: v}
lex := &exprLex{Input: v}
exprParse(lex)
// Build up the errors

View File

@ -0,0 +1 @@
concat("foo","-","0.0/16")