Increased test coverage, remove dead code

This commit is contained in:
Derek Collison
2015-01-10 18:32:30 -08:00
parent 42ea437f21
commit 99f1ea29bd
2 changed files with 49 additions and 51 deletions

View File

@@ -1,4 +1,4 @@
// Copyright 2013 Apcera Inc. All rights reserved. // Copyright 2013-2015 Apcera Inc. All rights reserved.
// Customized heavily from // Customized heavily from
// https://github.com/BurntSushi/toml/blob/master/lex.go, which is based on // https://github.com/BurntSushi/toml/blob/master/lex.go, which is based on
@@ -154,15 +154,6 @@ func (lx *lexer) backup() {
} }
} }
// accept consumes the next rune if it's equal to `valid`.
func (lx *lexer) accept(valid rune) bool {
if lx.next() == valid {
return true
}
lx.backup()
return false
}
// peek returns but does not consume the next rune in the input. // peek returns but does not consume the next rune in the input.
func (lx *lexer) peek() rune { func (lx *lexer) peek() rune {
r := lx.next() r := lx.next()
@@ -362,8 +353,8 @@ func lexValue(lx *lexer) stateFn {
case isNL(r): case isNL(r):
return lx.errorf("Expected value but found new line") return lx.errorf("Expected value but found new line")
} }
lx.backup()
return lexString return lexString
//return lx.errorf("Expected value but found '%s' instead.", r)
} }
// lexArrayValue consumes one value in an array. It assumes that '[' or ',' // lexArrayValue consumes one value in an array. It assumes that '[' or ','
@@ -529,17 +520,6 @@ func lexMapValue(lx *lexer) stateFn {
switch { switch {
case isWhitespace(r) || isNL(r): case isWhitespace(r) || isNL(r):
return lexSkip(lx, lexMapValue) return lexSkip(lx, lexMapValue)
case r == commentHashStart:
lx.push(lexMapValue)
return lexCommentStart
case r == commentSlashStart:
rn := lx.next()
if rn == commentSlashStart {
lx.push(lexMapValue)
return lexCommentStart
}
lx.backup()
fallthrough
case r == mapValTerm: case r == mapValTerm:
return lx.errorf("Unexpected map value terminator %q.", mapValTerm) return lx.errorf("Unexpected map value terminator %q.", mapValTerm)
case r == mapEnd: case r == mapEnd:
@@ -649,32 +629,6 @@ func lexString(lx *lexer) stateFn {
return lexString return lexString
} }
// lexDubString consumes the inner contents of a string. It assumes that the
// beginning '"' has already been consumed and ignored.
func lexDubString(lx *lexer) stateFn {
r := lx.next()
switch {
case r == '\\':
return lexStringEscape
// Termination of non-quoted strings
case isNL(r) || r == eof || r == optValTerm || isWhitespace(r):
lx.backup()
if lx.isBool() {
lx.emit(itemBool)
} else {
lx.emit(itemString)
}
return lx.pop()
case r == dqStringEnd:
lx.backup()
lx.emit(itemString)
lx.next()
lx.ignore()
return lx.pop()
}
return lexDubString
}
// lexBlock consumes the inner contents as a string. It assumes that the // lexBlock consumes the inner contents as a string. It assumes that the
// beginning '(' has already been consumed and ignored. It will continue // beginning '(' has already been consumed and ignored. It will continue
// processing until it finds a ')' on a new line by itself. // processing until it finds a ')' on a new line by itself.

View File

@@ -38,6 +38,29 @@ func TestSimpleKeyStringValues(t *testing.T) {
// NL // NL
lx = lex("foo='bar'\r\n") lx = lex("foo='bar'\r\n")
expect(t, lx, expectedItems) expect(t, lx, expectedItems)
lx = lex("foo=\t'bar'\t")
expect(t, lx, expectedItems)
}
func TestComplexStringValues(t *testing.T) {
expectedItems := []item{
{itemKey, "foo", 1},
{itemString, "bar\\r\\n \\t", 1},
{itemEOF, "", 2},
}
lx := lex("foo = 'bar\\r\\n \\t'")
expect(t, lx, expectedItems)
}
func TestBinaryString(t *testing.T) {
expectedItems := []item{
{itemKey, "foo", 1},
{itemString, "\\x22", 1},
{itemEOF, "", 1},
}
lx := lex("foo = \\x22")
expect(t, lx, expectedItems)
} }
func TestSimpleKeyIntegerValues(t *testing.T) { func TestSimpleKeyIntegerValues(t *testing.T) {
@@ -96,6 +119,21 @@ func TestComments(t *testing.T) {
expect(t, lx, expectedItems) expect(t, lx, expectedItems)
} }
func TestTopValuesWithComments(t *testing.T) {
expectedItems := []item{
{itemKey, "foo", 1},
{itemInteger, "123", 1},
{itemCommentStart, "", 1},
{itemText, " This is a comment", 1},
{itemEOF, "", 1},
}
lx := lex("foo = 123 // This is a comment")
expect(t, lx, expectedItems)
lx = lex("foo=123 # This is a comment")
expect(t, lx, expectedItems)
}
func TestArrays(t *testing.T) { func TestArrays(t *testing.T) {
expectedItems := []item{ expectedItems := []item{
{itemKey, "foo", 1}, {itemKey, "foo", 1},
@@ -153,7 +191,7 @@ func TestMultilineArrays(t *testing.T) {
var mlArrayNoSep = ` var mlArrayNoSep = `
# top level comment # top level comment
foo = [ foo = [
1 1 // foo
2 2
3 3
'bar' 'bar'
@@ -168,6 +206,8 @@ func TestMultilineArraysNoSep(t *testing.T) {
{itemKey, "foo", 3}, {itemKey, "foo", 3},
{itemArrayStart, "", 3}, {itemArrayStart, "", 3},
{itemInteger, "1", 4}, {itemInteger, "1", 4},
{itemCommentStart, "", 4},
{itemText, " foo", 4},
{itemInteger, "2", 5}, {itemInteger, "2", 5},
{itemInteger, "3", 6}, {itemInteger, "3", 6},
{itemString, "bar", 7}, {itemString, "bar", 7},
@@ -197,8 +237,8 @@ func TestSimpleMap(t *testing.T) {
var mlMap = ` var mlMap = `
foo = { foo = {
ip = '127.0.0.1' ip = '127.0.0.1' # the IP
port= 4242 port= 4242 // the port
} }
` `
@@ -208,8 +248,12 @@ func TestMultilineMap(t *testing.T) {
{itemMapStart, "", 2}, {itemMapStart, "", 2},
{itemKey, "ip", 3}, {itemKey, "ip", 3},
{itemString, "127.0.0.1", 3}, {itemString, "127.0.0.1", 3},
{itemCommentStart, "", 3},
{itemText, " the IP", 3},
{itemKey, "port", 4}, {itemKey, "port", 4},
{itemInteger, "4242", 4}, {itemInteger, "4242", 4},
{itemCommentStart, "", 4},
{itemText, " the port", 4},
{itemMapEnd, "", 5}, {itemMapEnd, "", 5},
{itemEOF, "", 5}, {itemEOF, "", 5},
} }