lex coverage

This commit is contained in:
Derek Collison
2016-05-04 12:52:52 -07:00
parent 8a2af1ec21
commit 9b41ee00aa
3 changed files with 93 additions and 9 deletions

View File

@@ -246,7 +246,7 @@ func lexKeyStart(lx *lexer) stateFn {
r := lx.peek()
switch {
case isKeySeparator(r):
return lx.errorf("Unexpected key separator '%v'.", r)
return lx.errorf("Unexpected key separator '%v'", r)
case isWhitespace(r) || isNL(r):
lx.next()
return lexSkip(lx, lexKeyStart)
@@ -349,7 +349,7 @@ func lexValue(lx *lexer) stateFn {
lx.backup() // avoid an extra state and use the same as above
return lexNumberOrDateOrIPStart
case r == '.': // special error case, be kind to users
return lx.errorf("Floats must start with a digit, not '.'.")
return lx.errorf("Floats must start with a digit")
case isNL(r):
return lx.errorf("Expected value but found new line")
}

View File

@@ -1,22 +1,22 @@
package conf
import (
"testing"
)
import "testing"
// Test to make sure we get what we expect.
func expect(t *testing.T, lx *lexer, items []item) {
for i := 0; i < len(items); i++ {
item := lx.nextItem()
_ = item.String()
if item.typ == itemEOF {
break
} else if item.typ == itemError {
t.Fatal(item.val)
}
if item != items[i] {
t.Fatalf("Testing: '%s'\nExpected %q, received %q\n",
lx.input, items[i], item)
}
if item.typ == itemError {
break
}
}
}
@@ -77,6 +77,20 @@ func TestSimpleKeyIntegerValues(t *testing.T) {
expect(t, lx, expectedItems)
}
func TestSimpleKeyNegativeIntegerValues(t *testing.T) {
expectedItems := []item{
{itemKey, "foo", 1},
{itemInteger, "-123", 1},
{itemEOF, "", 1},
}
lx := lex("foo = -123")
expect(t, lx, expectedItems)
lx = lex("foo=-123")
expect(t, lx, expectedItems)
lx = lex("foo=-123\r\n")
expect(t, lx, expectedItems)
}
func TestSimpleKeyFloatValues(t *testing.T) {
expectedItems := []item{
{itemKey, "foo", 1},
@@ -91,6 +105,25 @@ func TestSimpleKeyFloatValues(t *testing.T) {
expect(t, lx, expectedItems)
}
func TestBadFloatValues(t *testing.T) {
expectedItems := []item{
{itemKey, "foo", 1},
{itemError, "Floats must start with a digit", 1},
{itemEOF, "", 1},
}
lx := lex("foo = .2")
expect(t, lx, expectedItems)
}
func TestBadKey(t *testing.T) {
expectedItems := []item{
{itemError, "Unexpected key separator ':'", 1},
{itemEOF, "", 1},
}
lx := lex(" :foo = 22")
expect(t, lx, expectedItems)
}
func TestSimpleKeyBoolValues(t *testing.T) {
expectedItems := []item{
{itemKey, "foo", 1},
@@ -134,6 +167,31 @@ func TestTopValuesWithComments(t *testing.T) {
expect(t, lx, expectedItems)
}
func TestRawString(t *testing.T) {
expectedItems := []item{
{itemKey, "foo", 1},
{itemString, "bar", 1},
{itemEOF, "", 1},
}
lx := lex("foo = bar")
expect(t, lx, expectedItems)
lx = lex(`foo = bar' `)
expect(t, lx, expectedItems)
}
func TestDateValues(t *testing.T) {
expectedItems := []item{
{itemKey, "foo", 1},
{itemDatetime, "2016-05-04T18:53:41Z", 1},
{itemEOF, "", 1},
}
lx := lex("foo = 2016-05-04T18:53:41Z")
expect(t, lx, expectedItems)
}
func TestArrays(t *testing.T) {
expectedItems := []item{
{itemKey, "foo", 1},
@@ -158,7 +216,7 @@ var mlArray = `
foo = [
1, # One
2, // Two
3 , // Three
3 # Three
'bar' ,
"bar"
]
@@ -348,6 +406,32 @@ func TestWhitespaceKeySep(t *testing.T) {
expect(t, lx, expectedItems)
}
var escString = `
foo = \t
bar = \r
baz = \n
q = \"
bs = \\
`
func TestEscapedString(t *testing.T) {
expectedItems := []item{
{itemKey, "foo", 2},
{itemString, `\t`, 2},
{itemKey, "bar", 3},
{itemString, `\r`, 3},
{itemKey, "baz", 4},
{itemString, `\n`, 4},
{itemKey, "q", 5},
{itemString, `\"`, 5},
{itemKey, "bs", 6},
{itemString, `\\`, 6},
{itemEOF, "", 6},
}
lx := lex(escString)
expect(t, lx, expectedItems)
}
var nestedWhitespaceMap = `
foo {
host {

View File

@@ -7,7 +7,7 @@ go test -v -covermode=atomic -coverprofile=./cov/auth.out ./auth
go test -v -covermode=atomic -coverprofile=./cov/conf.out ./conf
go test -v -covermode=atomic -coverprofile=./cov/log.out ./logger
go test -v -covermode=atomic -coverprofile=./cov/server.out ./server
#go test -v -covermode=atomic -coverprofile=./cov/test.out ./test
go test -v -covermode=atomic -coverprofile=./cov/test.out ./test
go test -v -covermode=atomic -coverprofile=./cov/test2.out -coverpkg=./server ./test
gocovmerge ./cov/*.out > acc.out
rm -rf ./cov