Improve test coverage (#66)
This commit is contained in:
+36
-1
@@ -480,6 +480,12 @@ func TestKeyEqualNumber(t *testing.T) {
|
||||
token{Position{1, 8}, tokenFloat, "9_224_617.445_991_228_313"},
|
||||
token{Position{1, 33}, tokenEOF, ""},
|
||||
})
|
||||
|
||||
testFlow(t, "foo = +", []token{
|
||||
token{Position{1, 1}, tokenKey, "foo"},
|
||||
token{Position{1, 5}, tokenEqual, "="},
|
||||
token{Position{1, 7}, tokenError, "no digit in that number"},
|
||||
})
|
||||
}
|
||||
|
||||
func TestMultiline(t *testing.T) {
|
||||
@@ -507,6 +513,16 @@ func TestKeyEqualStringUnicodeEscape(t *testing.T) {
|
||||
token{Position{1, 8}, tokenString, "hello δ"},
|
||||
token{Position{1, 25}, tokenEOF, ""},
|
||||
})
|
||||
testFlow(t, `foo = "\u2"`, []token{
|
||||
token{Position{1, 1}, tokenKey, "foo"},
|
||||
token{Position{1, 5}, tokenEqual, "="},
|
||||
token{Position{1, 8}, tokenError, "unfinished unicode escape"},
|
||||
})
|
||||
testFlow(t, `foo = "\U2"`, []token{
|
||||
token{Position{1, 1}, tokenKey, "foo"},
|
||||
token{Position{1, 5}, tokenEqual, "="},
|
||||
token{Position{1, 8}, tokenError, "unfinished unicode escape"},
|
||||
})
|
||||
}
|
||||
|
||||
func TestKeyEqualStringNoEscape(t *testing.T) {
|
||||
@@ -547,6 +563,11 @@ func TestLiteralString(t *testing.T) {
|
||||
token{Position{1, 8}, tokenString, `<\i\c*\s*>`},
|
||||
token{Position{1, 19}, tokenEOF, ""},
|
||||
})
|
||||
testFlow(t, `foo = 'C:\Users\nodejs\unfinis`, []token{
|
||||
token{Position{1, 1}, tokenKey, "foo"},
|
||||
token{Position{1, 5}, tokenEqual, "="},
|
||||
token{Position{1, 8}, tokenError, "unclosed string"},
|
||||
})
|
||||
}
|
||||
|
||||
func TestMultilineLiteralString(t *testing.T) {
|
||||
@@ -563,6 +584,12 @@ func TestMultilineLiteralString(t *testing.T) {
|
||||
token{Position{2, 1}, tokenString, "hello\n'literal'\nworld"},
|
||||
token{Position{4, 9}, tokenEOF, ""},
|
||||
})
|
||||
testFlow(t, "foo = '''\r\nhello\r\n'literal'\r\nworld'''", []token{
|
||||
token{Position{1, 1}, tokenKey, "foo"},
|
||||
token{Position{1, 5}, tokenEqual, "="},
|
||||
token{Position{2, 1}, tokenString, "hello\r\n'literal'\r\nworld"},
|
||||
token{Position{4, 9}, tokenEOF, ""},
|
||||
})
|
||||
}
|
||||
|
||||
func TestMultilineString(t *testing.T) {
|
||||
@@ -573,7 +600,7 @@ func TestMultilineString(t *testing.T) {
|
||||
token{Position{1, 34}, tokenEOF, ""},
|
||||
})
|
||||
|
||||
testFlow(t, "foo = \"\"\"\nhello\\\n\"literal\"\\\nworld\"\"\"", []token{
|
||||
testFlow(t, "foo = \"\"\"\r\nhello\\\r\n\"literal\"\\\nworld\"\"\"", []token{
|
||||
token{Position{1, 1}, tokenKey, "foo"},
|
||||
token{Position{1, 5}, tokenEqual, "="},
|
||||
token{Position{2, 1}, tokenString, "hello\"literal\"world"},
|
||||
@@ -624,6 +651,14 @@ func TestUnicodeString(t *testing.T) {
|
||||
token{Position{1, 22}, tokenEOF, ""},
|
||||
})
|
||||
}
|
||||
func TestEscapeInString(t *testing.T) {
|
||||
testFlow(t, `foo = "\b\f\/"`, []token{
|
||||
token{Position{1, 1}, tokenKey, "foo"},
|
||||
token{Position{1, 5}, tokenEqual, "="},
|
||||
token{Position{1, 8}, tokenString, "\b\f/"},
|
||||
token{Position{1, 15}, tokenEOF, ""},
|
||||
})
|
||||
}
|
||||
|
||||
func TestKeyGroupArray(t *testing.T) {
|
||||
testFlow(t, "[[foo]]", []token{
|
||||
|
||||
@@ -556,6 +556,18 @@ func TestParseKeyGroupArray(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestParseKeyGroupArrayUnfinished(t *testing.T) {
|
||||
_, err := Load("[[foo.bar]\na = 42")
|
||||
if err.Error() != "(1, 10): was expecting token [[, but got unclosed key group array instead" {
|
||||
t.Error("Bad error message:", err.Error())
|
||||
}
|
||||
|
||||
_, err = Load("[[foo.[bar]\na = 42")
|
||||
if err.Error() != "(1, 3): unexpected token group name cannot contain ']', was expecting a key group array" {
|
||||
t.Error("Bad error message:", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseKeyGroupArrayQueryExample(t *testing.T) {
|
||||
tree, err := Load(`
|
||||
[[book]]
|
||||
@@ -690,6 +702,11 @@ func TestInvalidGroupArray(t *testing.T) {
|
||||
if err == nil {
|
||||
t.Error("Should error")
|
||||
}
|
||||
|
||||
_, err = Load("[foo.[bar]\na = 42")
|
||||
if err.Error() != "(1, 2): unexpected token group name cannot contain ']', was expecting a key group" {
|
||||
t.Error("Bad error message:", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestDoubleEqual(t *testing.T) {
|
||||
|
||||
@@ -94,7 +94,7 @@ func (t *TomlTree) GetPath(keys []string) interface{} {
|
||||
}
|
||||
subtree = node[len(node)-1]
|
||||
default:
|
||||
return nil // cannot naigate through other node types
|
||||
return nil // cannot navigate through other node types
|
||||
}
|
||||
}
|
||||
// branch based on final node type
|
||||
|
||||
@@ -21,6 +21,43 @@ func TestTomlHas(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestTomlGet(t *testing.T) {
|
||||
tree, _ := Load(`
|
||||
[test]
|
||||
key = "value"
|
||||
`)
|
||||
|
||||
if tree.Get("") != tree {
|
||||
t.Errorf("Get should return the tree itself when given an empty path")
|
||||
}
|
||||
|
||||
if tree.Get("test.key") != "value" {
|
||||
t.Errorf("Get should return the value")
|
||||
}
|
||||
if tree.Get(`\`) != nil {
|
||||
t.Errorf("should return nil when the key is malformed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTomlGetDefault(t *testing.T) {
|
||||
tree, _ := Load(`
|
||||
[test]
|
||||
key = "value"
|
||||
`)
|
||||
|
||||
if tree.GetDefault("", "hello") != tree {
|
||||
t.Error("GetDefault should return the tree itself when given an empty path")
|
||||
}
|
||||
|
||||
if tree.GetDefault("test.key", "hello") != "value" {
|
||||
t.Error("Get should return the value")
|
||||
}
|
||||
|
||||
if tree.GetDefault("whatever", "hello") != "hello" {
|
||||
t.Error("GetDefault should return the default value if the key does not exist")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTomlHasPath(t *testing.T) {
|
||||
tree, _ := Load(`
|
||||
[test]
|
||||
@@ -50,6 +87,11 @@ func TestTomlGetPath(t *testing.T) {
|
||||
t.Errorf("GetPath[%d] %v - expected %v, got %v instead.", idx, item.Path, item.Expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
tree, _ := Load("[foo.bar]\na=1\nb=2\n[baz.foo]\na=3\nb=4\n[gorf.foo]\na=5\nb=6")
|
||||
if tree.GetPath([]string{"whatever"}) != nil {
|
||||
t.Error("GetPath should return nil when the key does not exist")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTomlQuery(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user