yq/pkg/yqlib/operator_parent_test.go

63 lines
1.6 KiB
Go
Raw Permalink Normal View History

2021-11-23 23:16:48 +00:00
package yqlib
import (
"testing"
)
var parentOperatorScenarios = []expressionScenario{
{
description: "Simple example",
document: `a: {nested: cat}`,
expression: `.a.nested | parent`,
expected: []string{
"D0, P[a], (!!map)::{nested: cat}\n",
},
},
{
2021-11-23 23:59:19 +00:00
description: "Parent of nested matches",
document: `{a: {fruit: apple, name: bob}, b: {fruit: banana, name: sam}}`,
2021-11-23 23:16:48 +00:00
expression: `.. | select(. == "banana") | parent`,
expected: []string{
2021-11-23 23:59:19 +00:00
"D0, P[b], (!!map)::{fruit: banana, name: sam}\n",
2021-11-23 23:16:48 +00:00
},
},
{
description: "Get parent attribute",
document: `{a: {fruit: apple, name: bob}, b: {fruit: banana, name: sam}}`,
expression: `.. | select(. == "banana") | parent.name`,
expected: []string{
"D0, P[b name], (!!str)::sam\n",
},
},
2024-03-11 23:48:42 +00:00
{
description: "N-th parent",
subdescription: "You can optionally supply the number of levels to go up for the parent, the default being 1.",
document: "a:\n b:\n c: cat\n",
expression: `.a.b.c | parent(2)`,
expected: []string{
"D0, P[a], (!!map)::b:\n c: cat\n",
},
},
{
description: "N-th parent - another level",
document: "a:\n b:\n c: cat\n",
expression: `.a.b.c | parent(3)`,
expected: []string{
"D0, P[], (!!map)::a:\n b:\n c: cat\n",
},
},
2021-11-23 23:16:48 +00:00
{
description: "No parent",
document: `{}`,
expression: `parent`,
expected: []string{},
},
}
func TestParentOperatorScenarios(t *testing.T) {
for _, tt := range parentOperatorScenarios {
testScenario(t, &tt)
}
2021-12-21 04:02:07 +00:00
documentOperatorScenarios(t, "parent", parentOperatorScenarios)
2021-11-23 23:16:48 +00:00
}