Final Toml-Path Solution

* Refactored type names and file names to mesh with existing TOML library more closely
* Added QueryResult structure that provides values and position data
* Added Query() method to TomlTree type
* Tests, tests, and more tests
* Fixed bug where positions returned from some tables were invalid
* Added test case for bug patch

The bugfix was an interesting case. Position information wasn't being
set in cases where createPath was called.  So table names like [foo.bar]
would result in table 'foo' having no position.
This commit is contained in:
eanderton
2014-09-10 21:32:04 -04:00
parent 2811a1a3c9
commit 081f3db916
15 changed files with 713 additions and 779 deletions
+14 -2
View File
@@ -31,7 +31,7 @@ func assertTree(t *testing.T, tree *TomlTree, err error, ref map[string]interfac
func TestCreateSubTree(t *testing.T) {
tree := newTomlTree()
tree.createSubTree([]string{"a", "b", "c"})
tree.createSubTree([]string{"a", "b", "c"}, Position{})
tree.Set("a.b.c", 42)
if tree.Get("a.b.c") != 42 {
t.Fail()
@@ -385,7 +385,7 @@ func assertPosition(t *testing.T, text string, ref map[string]Position) {
for path, pos := range ref {
testPos := tree.GetPosition(path)
if testPos.Invalid() {
t.Errorf("Failed to query tree path: %s", path)
t.Errorf("Failed to query tree path or path has invalid position: %s", path)
} else if pos != testPos {
t.Errorf("Expected position %v, got %v instead", pos, testPos)
}
@@ -424,3 +424,15 @@ func TestDocumentPositionsWithGroupArray(t *testing.T) {
"foo.baz": Position{3, 1},
})
}
func TestNestedTreePosition(t *testing.T) {
assertPosition(t,
"[foo.bar]\na=42\nb=69",
map[string]Position{
"": Position{1, 1},
"foo": Position{1, 1},
"foo.bar": Position{1, 1},
"foo.bar.a": Position{2, 1},
"foo.bar.b": Position{3, 1},
})
}