Improve test coverage (#66)

This commit is contained in:
Thomas Pelletier
2016-04-22 14:26:15 +02:00
parent 288bc57940
commit 8d9c606c69
4 changed files with 96 additions and 2 deletions
+36 -1
View File
@@ -480,6 +480,12 @@ func TestKeyEqualNumber(t *testing.T) {
token{Position{1, 8}, tokenFloat, "9_224_617.445_991_228_313"}, token{Position{1, 8}, tokenFloat, "9_224_617.445_991_228_313"},
token{Position{1, 33}, tokenEOF, ""}, 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) { func TestMultiline(t *testing.T) {
@@ -507,6 +513,16 @@ func TestKeyEqualStringUnicodeEscape(t *testing.T) {
token{Position{1, 8}, tokenString, "hello δ"}, token{Position{1, 8}, tokenString, "hello δ"},
token{Position{1, 25}, tokenEOF, ""}, 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) { 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, 8}, tokenString, `<\i\c*\s*>`},
token{Position{1, 19}, tokenEOF, ""}, 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) { 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{2, 1}, tokenString, "hello\n'literal'\nworld"},
token{Position{4, 9}, tokenEOF, ""}, 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) { func TestMultilineString(t *testing.T) {
@@ -573,7 +600,7 @@ func TestMultilineString(t *testing.T) {
token{Position{1, 34}, tokenEOF, ""}, 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, 1}, tokenKey, "foo"},
token{Position{1, 5}, tokenEqual, "="}, token{Position{1, 5}, tokenEqual, "="},
token{Position{2, 1}, tokenString, "hello\"literal\"world"}, token{Position{2, 1}, tokenString, "hello\"literal\"world"},
@@ -624,6 +651,14 @@ func TestUnicodeString(t *testing.T) {
token{Position{1, 22}, tokenEOF, ""}, 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) { func TestKeyGroupArray(t *testing.T) {
testFlow(t, "[[foo]]", []token{ testFlow(t, "[[foo]]", []token{
+17
View File
@@ -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) { func TestParseKeyGroupArrayQueryExample(t *testing.T) {
tree, err := Load(` tree, err := Load(`
[[book]] [[book]]
@@ -690,6 +702,11 @@ func TestInvalidGroupArray(t *testing.T) {
if err == nil { if err == nil {
t.Error("Should error") 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) { func TestDoubleEqual(t *testing.T) {
+1 -1
View File
@@ -94,7 +94,7 @@ func (t *TomlTree) GetPath(keys []string) interface{} {
} }
subtree = node[len(node)-1] subtree = node[len(node)-1]
default: default:
return nil // cannot naigate through other node types return nil // cannot navigate through other node types
} }
} }
// branch based on final node type // branch based on final node type
+42
View File
@@ -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) { func TestTomlHasPath(t *testing.T) {
tree, _ := Load(` tree, _ := Load(`
[test] [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) 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) { func TestTomlQuery(t *testing.T) {