Improve test coverage (#66)
diff --git a/lexer_test.go b/lexer_test.go
index 9a00690..382e05a 100644
--- a/lexer_test.go
+++ b/lexer_test.go
@@ -480,6 +480,12 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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{
diff --git a/parser_test.go b/parser_test.go
index bbcbcee..1ea5924 100644
--- a/parser_test.go
+++ b/parser_test.go
@@ -556,6 +556,18 @@
})
}
+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 @@
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) {
diff --git a/toml.go b/toml.go
index 3975d01..c0e1ad2 100644
--- a/toml.go
+++ b/toml.go
@@ -94,7 +94,7 @@
}
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
diff --git a/toml_test.go b/toml_test.go
index c36a69c..9d85ccb 100644
--- a/toml_test.go
+++ b/toml_test.go
@@ -21,6 +21,43 @@
}
}
+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 @@
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) {