yq/pkg/yqlib/treeops/data_tree_navigator_test.go

983 lines
18 KiB
Go
Raw Normal View History

2020-10-08 23:59:03 +00:00
package treeops
import (
"strings"
"testing"
"github.com/mikefarah/yq/v3/test"
yaml "gopkg.in/yaml.v3"
)
var treeNavigator = NewDataTreeNavigator(NavigationPrefs{})
var treeCreator = NewPathTreeCreator()
func readDoc(t *testing.T, content string) []*CandidateNode {
2020-10-17 11:10:47 +00:00
if content == "" {
return []*CandidateNode{}
}
2020-10-08 23:59:03 +00:00
decoder := yaml.NewDecoder(strings.NewReader(content))
var dataBucket yaml.Node
err := decoder.Decode(&dataBucket)
if err != nil {
t.Error(err)
}
2020-10-11 23:09:13 +00:00
return []*CandidateNode{&CandidateNode{Node: dataBucket.Content[0], Document: 0}}
2020-10-08 23:59:03 +00:00
}
2020-10-17 11:10:47 +00:00
func resultsToString(results []*CandidateNode) []string {
var pretty []string = make([]string, 0)
2020-10-08 23:59:03 +00:00
for _, n := range results {
2020-10-17 11:10:47 +00:00
pretty = append(pretty, NodeToString(n))
2020-10-08 23:59:03 +00:00
}
return pretty
}
func TestDataTreeNavigatorSimple(t *testing.T) {
nodes := readDoc(t, `a:
b: apple`)
path, errPath := treeCreator.ParsePath("a")
if errPath != nil {
t.Error(errPath)
}
results, errNav := treeNavigator.GetMatchingNodes(nodes, path)
if errNav != nil {
t.Error(errNav)
}
expected := `
-- Node --
Document 0, path: [a]
Tag: !!map, Kind: MappingNode, Anchor:
b: apple
`
test.AssertResult(t, expected, resultsToString(results))
}
2020-10-11 23:09:13 +00:00
func TestDataTreeNavigatorDeleteSimple(t *testing.T) {
2020-10-10 11:42:09 +00:00
nodes := readDoc(t, `a:
b: apple
c: camel`)
path, errPath := treeCreator.ParsePath("a .- b")
if errPath != nil {
t.Error(errPath)
}
results, errNav := treeNavigator.GetMatchingNodes(nodes, path)
if errNav != nil {
t.Error(errNav)
}
expected := `
-- Node --
Document 0, path: [a]
Tag: !!map, Kind: MappingNode, Anchor:
c: camel
`
test.AssertResult(t, expected, resultsToString(results))
}
2020-10-11 23:09:13 +00:00
func TestDataTreeNavigatorDeleteTwice(t *testing.T) {
2020-10-10 11:42:09 +00:00
nodes := readDoc(t, `a:
b: apple
c: camel
d: dingo`)
path, errPath := treeCreator.ParsePath("a .- b OR a .- c")
if errPath != nil {
t.Error(errPath)
}
results, errNav := treeNavigator.GetMatchingNodes(nodes, path)
if errNav != nil {
t.Error(errNav)
}
expected := `
-- Node --
Document 0, path: [a]
Tag: !!map, Kind: MappingNode, Anchor:
d: dingo
`
test.AssertResult(t, expected, resultsToString(results))
}
2020-10-11 23:09:13 +00:00
func TestDataTreeNavigatorDeleteWithUnion(t *testing.T) {
2020-10-10 11:42:09 +00:00
nodes := readDoc(t, `a:
b: apple
c: camel
d: dingo`)
path, errPath := treeCreator.ParsePath("a .- (b OR c)")
if errPath != nil {
t.Error(errPath)
}
results, errNav := treeNavigator.GetMatchingNodes(nodes, path)
if errNav != nil {
t.Error(errNav)
}
expected := `
-- Node --
Document 0, path: [a]
Tag: !!map, Kind: MappingNode, Anchor:
d: dingo
`
test.AssertResult(t, expected, resultsToString(results))
}
2020-10-11 23:09:13 +00:00
func TestDataTreeNavigatorDeleteByIndex(t *testing.T) {
2020-10-10 12:04:10 +00:00
nodes := readDoc(t, `a:
- b: apple
- b: sdfsd
- b: apple`)
path, errPath := treeCreator.ParsePath("(a .- (0 or 1))")
if errPath != nil {
t.Error(errPath)
}
results, errNav := treeNavigator.GetMatchingNodes(nodes, path)
if errNav != nil {
t.Error(errNav)
}
expected := `
-- Node --
Document 0, path: [a]
Tag: !!seq, Kind: SequenceNode, Anchor:
- b: apple
`
test.AssertResult(t, expected, resultsToString(results))
}
2020-10-11 23:09:13 +00:00
func TestDataTreeNavigatorDeleteByFind(t *testing.T) {
nodes := readDoc(t, `a:
- b: apple
- b: sdfsd
- b: apple`)
path, errPath := treeCreator.ParsePath("(a .- (* == apple))")
if errPath != nil {
t.Error(errPath)
}
results, errNav := treeNavigator.GetMatchingNodes(nodes, path)
if errNav != nil {
t.Error(errNav)
}
expected := `
-- Node --
Document 0, path: [a]
Tag: !!seq, Kind: SequenceNode, Anchor:
- b: sdfsd
`
test.AssertResult(t, expected, resultsToString(results))
}
func TestDataTreeNavigatorDeleteArrayByFind(t *testing.T) {
nodes := readDoc(t, `a:
- apple
- sdfsd
- apple`)
path, errPath := treeCreator.ParsePath("(a .- (. == apple))")
if errPath != nil {
t.Error(errPath)
}
results, errNav := treeNavigator.GetMatchingNodes(nodes, path)
if errNav != nil {
t.Error(errNav)
}
expected := `
-- Node --
Document 0, path: [a]
Tag: !!seq, Kind: SequenceNode, Anchor:
- sdfsd
`
test.AssertResult(t, expected, resultsToString(results))
}
func TestDataTreeNavigatorDeleteViaSelf(t *testing.T) {
nodes := readDoc(t, `- apple
- sdfsd
- apple`)
2020-10-11 23:44:33 +00:00
path, errPath := treeCreator.ParsePath(". .- (. == apple)")
2020-10-11 23:09:13 +00:00
if errPath != nil {
t.Error(errPath)
}
results, errNav := treeNavigator.GetMatchingNodes(nodes, path)
if errNav != nil {
t.Error(errNav)
}
expected := `
-- Node --
Document 0, path: []
Tag: !!seq, Kind: SequenceNode, Anchor:
- sdfsd
`
test.AssertResult(t, expected, resultsToString(results))
}
2020-10-16 01:29:26 +00:00
func TestDataTreeNavigatorFilterWithSplat(t *testing.T) {
2020-10-12 01:24:59 +00:00
nodes := readDoc(t, `f:
a: frog
b: dally
c: log`)
2020-10-16 01:29:26 +00:00
path, errPath := treeCreator.ParsePath(".f | .[] == \"frog\"")
if errPath != nil {
t.Error(errPath)
}
results, errNav := treeNavigator.GetMatchingNodes(nodes, path)
if errNav != nil {
t.Error(errNav)
}
expected := `
-- Node --
Document 0, path: [f]
Tag: !!int, Kind: ScalarNode, Anchor:
2
`
test.AssertResult(t, expected, resultsToString(results))
}
func TestDataTreeNavigatorCountAndCollectWithFilterCmd(t *testing.T) {
nodes := readDoc(t, `f:
a: frog
b: dally
c: log`)
path, errPath := treeCreator.ParsePath(".f | .[] == *og ")
2020-10-12 01:24:59 +00:00
if errPath != nil {
t.Error(errPath)
}
results, errNav := treeNavigator.GetMatchingNodes(nodes, path)
if errNav != nil {
t.Error(errNav)
}
expected := `
-- Node --
Document 0, path: [f]
Tag: !!int, Kind: ScalarNode, Anchor:
2
`
test.AssertResult(t, expected, resultsToString(results))
}
2020-10-13 03:04:21 +00:00
func TestDataTreeNavigatorCollectWithFilter(t *testing.T) {
nodes := readDoc(t, `f:
a: frog
b: dally
c: log`)
path, errPath := treeCreator.ParsePath("f(collect(. == *og))")
if errPath != nil {
t.Error(errPath)
}
results, errNav := treeNavigator.GetMatchingNodes(nodes, path)
if errNav != nil {
t.Error(errNav)
}
expected := `
-- Node --
Document 0, path: [f]
Tag: , Kind: SequenceNode, Anchor:
- frog
- log
`
test.AssertResult(t, expected, resultsToString(results))
}
2020-10-12 01:24:59 +00:00
func TestDataTreeNavigatorCountWithFilter2(t *testing.T) {
nodes := readDoc(t, `f:
a: frog
b: dally
c: log`)
path, errPath := treeCreator.ParsePath("count(f(. == *og))")
if errPath != nil {
t.Error(errPath)
}
results, errNav := treeNavigator.GetMatchingNodes(nodes, path)
if errNav != nil {
t.Error(errNav)
}
expected := `
-- Node --
Document 0, path: []
Tag: !!int, Kind: ScalarNode, Anchor:
2
`
test.AssertResult(t, expected, resultsToString(results))
}
2020-10-13 03:04:21 +00:00
func TestDataTreeNavigatorCollectWithFilter2(t *testing.T) {
nodes := readDoc(t, `f:
a: frog
b: dally
c: log`)
path, errPath := treeCreator.ParsePath("collect(f(. == *og))")
if errPath != nil {
t.Error(errPath)
}
results, errNav := treeNavigator.GetMatchingNodes(nodes, path)
if errNav != nil {
t.Error(errNav)
}
expected := `
-- Node --
Document 0, path: []
Tag: , Kind: SequenceNode, Anchor:
- frog
- log
`
test.AssertResult(t, expected, resultsToString(results))
}
2020-10-12 01:24:59 +00:00
func TestDataTreeNavigatorCountMultipleMatchesInside(t *testing.T) {
nodes := readDoc(t, `f:
a: [1,2]
b: dally
c: [3,4,5]`)
2020-10-13 03:37:01 +00:00
path, errPath := treeCreator.ParsePath("f | count(a or c)")
2020-10-12 01:24:59 +00:00
if errPath != nil {
t.Error(errPath)
}
results, errNav := treeNavigator.GetMatchingNodes(nodes, path)
if errNav != nil {
t.Error(errNav)
}
expected := `
-- Node --
Document 0, path: [f]
Tag: !!int, Kind: ScalarNode, Anchor:
2
`
test.AssertResult(t, expected, resultsToString(results))
}
2020-10-13 03:04:21 +00:00
func TestDataTreeNavigatorCollectMultipleMatchesInside(t *testing.T) {
nodes := readDoc(t, `f:
a: [1,2]
b: dally
c: [3,4,5]`)
2020-10-13 03:37:01 +00:00
path, errPath := treeCreator.ParsePath("f | collect(a or c)")
2020-10-13 03:04:21 +00:00
if errPath != nil {
t.Error(errPath)
}
results, errNav := treeNavigator.GetMatchingNodes(nodes, path)
if errNav != nil {
t.Error(errNav)
}
expected := `
-- Node --
Document 0, path: [f]
Tag: , Kind: SequenceNode, Anchor:
- [1, 2]
- [3, 4, 5]
`
test.AssertResult(t, expected, resultsToString(results))
}
2020-10-12 01:24:59 +00:00
func TestDataTreeNavigatorCountMultipleMatchesInsideSplat(t *testing.T) {
nodes := readDoc(t, `f:
a: [1,2,3]
b: [1,2,3,4]
c: [1,2,3,4,5]`)
path, errPath := treeCreator.ParsePath("f(count( (a or c)*))")
if errPath != nil {
t.Error(errPath)
}
results, errNav := treeNavigator.GetMatchingNodes(nodes, path)
if errNav != nil {
t.Error(errNav)
}
expected := `
-- Node --
Document 0, path: [f]
Tag: !!int, Kind: ScalarNode, Anchor:
8
`
test.AssertResult(t, expected, resultsToString(results))
}
func TestDataTreeNavigatorCountMultipleMatchesOutside(t *testing.T) {
nodes := readDoc(t, `f:
a: [1,2,3]
b: [1,2,3,4]
c: [1,2,3,4,5]`)
path, errPath := treeCreator.ParsePath("f(a or c)(count(*))")
if errPath != nil {
t.Error(errPath)
}
results, errNav := treeNavigator.GetMatchingNodes(nodes, path)
if errNav != nil {
t.Error(errNav)
}
expected := `
-- Node --
Document 0, path: [f a]
Tag: !!int, Kind: ScalarNode, Anchor:
3
-- Node --
Document 0, path: [f c]
Tag: !!int, Kind: ScalarNode, Anchor:
5
`
test.AssertResult(t, expected, resultsToString(results))
}
func TestDataTreeNavigatorCountOfResults(t *testing.T) {
nodes := readDoc(t, `- apple
- sdfsd
- apple`)
path, errPath := treeCreator.ParsePath("count(*)")
if errPath != nil {
t.Error(errPath)
}
results, errNav := treeNavigator.GetMatchingNodes(nodes, path)
if errNav != nil {
t.Error(errNav)
}
expected := `
-- Node --
Document 0, path: []
Tag: !!int, Kind: ScalarNode, Anchor:
3
`
test.AssertResult(t, expected, resultsToString(results))
}
func TestDataTreeNavigatorCountNoMatches(t *testing.T) {
nodes := readDoc(t, `- apple
- sdfsd
- apple`)
path, errPath := treeCreator.ParsePath("count(5)")
if errPath != nil {
t.Error(errPath)
}
results, errNav := treeNavigator.GetMatchingNodes(nodes, path)
if errNav != nil {
t.Error(errNav)
}
expected := `
-- Node --
Document 0, path: []
Tag: !!int, Kind: ScalarNode, Anchor:
0
`
test.AssertResult(t, expected, resultsToString(results))
}
2020-10-10 12:04:10 +00:00
func TestDataTreeNavigatorDeleteAndWrite(t *testing.T) {
nodes := readDoc(t, `a:
- b: apple
- b: sdfsd
- { b: apple, c: cat }`)
path, errPath := treeCreator.ParsePath("(a .- (0 or 1)) or (a[0].b := frog)")
if errPath != nil {
t.Error(errPath)
}
results, errNav := treeNavigator.GetMatchingNodes(nodes, path)
if errNav != nil {
t.Error(errNav)
}
expected := `
-- Node --
Document 0, path: [a]
Tag: !!seq, Kind: SequenceNode, Anchor:
- {b: frog, c: cat}
-- Node --
Document 0, path: [a 0 b]
Tag: !!str, Kind: ScalarNode, Anchor:
frog
`
test.AssertResult(t, expected, resultsToString(results))
}
2020-10-11 23:09:13 +00:00
func TestDataTreeNavigatorDeleteArray(t *testing.T) {
2020-10-10 11:42:09 +00:00
nodes := readDoc(t, `a:
- b: apple
- b: sdfsd
- b: apple`)
path, errPath := treeCreator.ParsePath("a .- (b == a*)")
if errPath != nil {
t.Error(errPath)
}
results, errNav := treeNavigator.GetMatchingNodes(nodes, path)
if errNav != nil {
t.Error(errNav)
}
expected := `
-- Node --
Document 0, path: [a]
Tag: !!seq, Kind: SequenceNode, Anchor:
- b: sdfsd
`
test.AssertResult(t, expected, resultsToString(results))
}
2020-10-09 05:38:07 +00:00
func TestDataTreeNavigatorArraySimple(t *testing.T) {
nodes := readDoc(t, `- b: apple`)
path, errPath := treeCreator.ParsePath("[0]")
if errPath != nil {
t.Error(errPath)
}
results, errNav := treeNavigator.GetMatchingNodes(nodes, path)
if errNav != nil {
t.Error(errNav)
}
expected := `
-- Node --
Document 0, path: [0]
Tag: !!map, Kind: MappingNode, Anchor:
b: apple
`
test.AssertResult(t, expected, resultsToString(results))
}
2020-10-16 01:47:31 +00:00
func TestDataTreeNavigatorSimpleAssignBooleanCmd(t *testing.T) {
nodes := readDoc(t, `a:
b: apple`)
path, errPath := treeCreator.ParsePath(`.a.b |= true`)
if errPath != nil {
t.Error(errPath)
}
results, errNav := treeNavigator.GetMatchingNodes(nodes, path)
if errNav != nil {
t.Error(errNav)
}
expected := `
-- Node --
Document 0, path: [a b]
Tag: !!bool, Kind: ScalarNode, Anchor:
true
`
test.AssertResult(t, expected, resultsToString(results))
}
2020-10-16 01:49:15 +00:00
func TestDataTreeNavigatorSimpleAssignChildCmd(t *testing.T) {
nodes := readDoc(t, `a:
b: {g: 3}`)
path, errPath := treeCreator.ParsePath(`.a |= .b`)
if errPath != nil {
t.Error(errPath)
}
results, errNav := treeNavigator.GetMatchingNodes(nodes, path)
if errNav != nil {
t.Error(errNav)
}
expected := `
-- Node --
Document 0, path: [a]
Tag: !!map, Kind: MappingNode, Anchor:
{g: 3}
`
test.AssertResult(t, expected, resultsToString(results))
}
2020-10-11 23:09:13 +00:00
func TestDataTreeNavigatorSimpleAssignSelf(t *testing.T) {
nodes := readDoc(t, `a:
b: apple`)
path, errPath := treeCreator.ParsePath("a(. == apple)(. := frog)")
if errPath != nil {
t.Error(errPath)
}
results, errNav := treeNavigator.GetMatchingNodes(nodes, path)
if errNav != nil {
t.Error(errNav)
}
expected := `
-- Node --
Document 0, path: [a b]
Tag: !!str, Kind: ScalarNode, Anchor:
frog
`
test.AssertResult(t, expected, resultsToString(results))
}
func TestDataTreeNavigatorSimpleAssignByFind(t *testing.T) {
nodes := readDoc(t, `a:
b: apple`)
2020-10-11 23:44:33 +00:00
path, errPath := treeCreator.ParsePath("a(. == apple) := frog")
2020-10-11 23:09:13 +00:00
if errPath != nil {
t.Error(errPath)
}
results, errNav := treeNavigator.GetMatchingNodes(nodes, path)
if errNav != nil {
t.Error(errNav)
}
expected := `
-- Node --
Document 0, path: [a b]
Tag: !!str, Kind: ScalarNode, Anchor:
frog
`
test.AssertResult(t, expected, resultsToString(results))
}
2020-10-09 05:38:07 +00:00
func TestDataTreeNavigatorArraySplat(t *testing.T) {
nodes := readDoc(t, `- b: apple
- c: banana`)
path, errPath := treeCreator.ParsePath("[*]")
if errPath != nil {
t.Error(errPath)
}
results, errNav := treeNavigator.GetMatchingNodes(nodes, path)
if errNav != nil {
t.Error(errNav)
}
expected := `
-- Node --
Document 0, path: [0]
Tag: !!map, Kind: MappingNode, Anchor:
b: apple
-- Node --
Document 0, path: [1]
Tag: !!map, Kind: MappingNode, Anchor:
c: banana
`
test.AssertResult(t, expected, resultsToString(results))
}
2020-10-08 23:59:03 +00:00
func TestDataTreeNavigatorSimpleDeep(t *testing.T) {
nodes := readDoc(t, `a:
b: apple`)
path, errPath := treeCreator.ParsePath("a.b")
if errPath != nil {
t.Error(errPath)
}
results, errNav := treeNavigator.GetMatchingNodes(nodes, path)
if errNav != nil {
t.Error(errNav)
}
expected := `
-- Node --
Document 0, path: [a b]
Tag: !!str, Kind: ScalarNode, Anchor:
apple
`
test.AssertResult(t, expected, resultsToString(results))
}
func TestDataTreeNavigatorSimpleMismatch(t *testing.T) {
nodes := readDoc(t, `a:
c: apple`)
path, errPath := treeCreator.ParsePath("a.b")
if errPath != nil {
t.Error(errPath)
}
results, errNav := treeNavigator.GetMatchingNodes(nodes, path)
if errNav != nil {
t.Error(errNav)
}
expected := ``
test.AssertResult(t, expected, resultsToString(results))
}
func TestDataTreeNavigatorWild(t *testing.T) {
nodes := readDoc(t, `a:
2020-10-09 00:10:37 +00:00
cat: apple
mad: things`)
2020-10-08 23:59:03 +00:00
path, errPath := treeCreator.ParsePath("a.*a*")
if errPath != nil {
t.Error(errPath)
}
results, errNav := treeNavigator.GetMatchingNodes(nodes, path)
if errNav != nil {
t.Error(errNav)
}
expected := `
-- Node --
Document 0, path: [a cat]
Tag: !!str, Kind: ScalarNode, Anchor:
apple
2020-10-09 00:10:37 +00:00
-- Node --
Document 0, path: [a mad]
Tag: !!str, Kind: ScalarNode, Anchor:
things
`
test.AssertResult(t, expected, resultsToString(results))
}
func TestDataTreeNavigatorWildDeepish(t *testing.T) {
nodes := readDoc(t, `a:
cat: {b: 3}
mad: {b: 4}
fad: {c: t}`)
path, errPath := treeCreator.ParsePath("a.*a*.b")
if errPath != nil {
t.Error(errPath)
}
results, errNav := treeNavigator.GetMatchingNodes(nodes, path)
if errNav != nil {
t.Error(errNav)
}
expected := `
-- Node --
Document 0, path: [a cat b]
Tag: !!int, Kind: ScalarNode, Anchor:
3
-- Node --
Document 0, path: [a mad b]
Tag: !!int, Kind: ScalarNode, Anchor:
4
`
test.AssertResult(t, expected, resultsToString(results))
}
func TestDataTreeNavigatorOrSimple(t *testing.T) {
nodes := readDoc(t, `a:
cat: apple
mad: things`)
path, errPath := treeCreator.ParsePath("a.(cat or mad)")
if errPath != nil {
t.Error(errPath)
}
results, errNav := treeNavigator.GetMatchingNodes(nodes, path)
if errNav != nil {
t.Error(errNav)
}
expected := `
-- Node --
Document 0, path: [a cat]
Tag: !!str, Kind: ScalarNode, Anchor:
apple
-- Node --
Document 0, path: [a mad]
Tag: !!str, Kind: ScalarNode, Anchor:
things
2020-10-08 23:59:03 +00:00
`
test.AssertResult(t, expected, resultsToString(results))
}
2020-10-09 00:10:37 +00:00
func TestDataTreeNavigatorOrSimpleWithDepth(t *testing.T) {
nodes := readDoc(t, `a:
cat: {b: 3}
mad: {b: 4}
fad: {c: t}`)
path, errPath := treeCreator.ParsePath("a.(cat.* or fad.*)")
if errPath != nil {
t.Error(errPath)
}
results, errNav := treeNavigator.GetMatchingNodes(nodes, path)
if errNav != nil {
t.Error(errNav)
}
expected := `
-- Node --
Document 0, path: [a cat b]
Tag: !!int, Kind: ScalarNode, Anchor:
3
-- Node --
Document 0, path: [a fad c]
Tag: !!str, Kind: ScalarNode, Anchor:
t
`
test.AssertResult(t, expected, resultsToString(results))
}
2020-10-09 00:37:47 +00:00
func TestDataTreeNavigatorOrDeDupes(t *testing.T) {
nodes := readDoc(t, `a:
cat: apple
mad: things`)
path, errPath := treeCreator.ParsePath("a.(cat or cat)")
if errPath != nil {
t.Error(errPath)
}
results, errNav := treeNavigator.GetMatchingNodes(nodes, path)
if errNav != nil {
t.Error(errNav)
}
expected := `
-- Node --
Document 0, path: [a cat]
Tag: !!str, Kind: ScalarNode, Anchor:
apple
`
test.AssertResult(t, expected, resultsToString(results))
}
2020-10-09 01:10:46 +00:00
func TestDataTreeNavigatorAnd(t *testing.T) {
nodes := readDoc(t, `a:
cat: apple
pat: apple
cow: apple
mad: things`)
path, errPath := treeCreator.ParsePath("a.(*t and c*)")
if errPath != nil {
t.Error(errPath)
}
results, errNav := treeNavigator.GetMatchingNodes(nodes, path)
if errNav != nil {
t.Error(errNav)
}
expected := `
-- Node --
Document 0, path: [a cat]
Tag: !!str, Kind: ScalarNode, Anchor:
apple
`
test.AssertResult(t, expected, resultsToString(results))
}