yq/pkg/yqlib/operator_path_test.go

128 lines
3.0 KiB
Go
Raw Permalink Normal View History

2020-11-22 01:19:57 +00:00
package yqlib
import (
"testing"
)
var pathOperatorScenarios = []expressionScenario{
{
description: "Map path",
document: `{a: {b: cat}}`,
expression: `.a.b | path`,
expected: []string{
"D0, P[a b], (!!seq)::- a\n- b\n",
},
},
2020-12-25 01:46:08 +00:00
{
skipDoc: true,
document: `a:
b:
c:
- 0
- 1
- 2
- 3`,
expression: `.a.b.c.[]`,
expected: []string{
"D0, P[a b c 0], (!!int)::0\n",
"D0, P[a b c 1], (!!int)::1\n",
"D0, P[a b c 2], (!!int)::2\n",
"D0, P[a b c 3], (!!int)::3\n",
},
},
2020-11-26 00:20:53 +00:00
{
description: "Get map key",
document: `{a: {b: cat}}`,
expression: `.a.b | path | .[-1]`,
expected: []string{
"D0, P[a b -1], (!!str)::b\n",
},
},
2020-11-22 01:19:57 +00:00
{
description: "Array path",
document: `{a: [cat, dog]}`,
expression: `.a.[] | select(. == "dog") | path`,
expected: []string{
"D0, P[a 1], (!!seq)::- a\n- 1\n",
},
},
2020-11-26 00:20:53 +00:00
{
description: "Get array index",
document: `{a: [cat, dog]}`,
expression: `.a.[] | select(. == "dog") | path | .[-1]`,
expected: []string{
"D0, P[a 1 -1], (!!int)::1\n",
},
},
2020-11-22 01:19:57 +00:00
{
description: "Print path and value",
document: `{a: [cat, dog, frog]}`,
expression: `.a[] | select(. == "*og") | [{"path":path, "value":.}]`,
expected: []string{
"D0, P[a 1], (!!seq)::- path:\n - a\n - 1\n value: dog\n",
"D0, P[a 2], (!!seq)::- path:\n - a\n - 2\n value: frog\n",
},
2020-11-22 01:19:57 +00:00
},
2022-10-05 03:12:08 +00:00
{
description: "Set path",
document: `{a: {b: cat}}`,
expression: `setpath(["a", "b"]; "things")`,
expected: []string{
"D0, P[], (doc)::{a: {b: things}}\n",
},
},
{
description: "Set on empty document",
expression: `setpath(["a", "b"]; "things")`,
expected: []string{
"D0, P[], ()::a:\n b: things\n",
},
},
{
description: "Set array path",
document: `a: [cat, frog]`,
expression: `setpath(["a", 0]; "things")`,
expected: []string{
"D0, P[], (doc)::a: [things, frog]\n",
},
},
{
description: "Set array path empty",
expression: `setpath(["a", 0]; "things")`,
expected: []string{
"D0, P[], ()::a:\n - things\n",
},
},
2022-10-05 09:09:53 +00:00
{
description: "Delete path",
subdescription: "Notice delpaths takes an _array_ of paths.",
document: `{a: {b: cat, c: dog, d: frog}}`,
expression: `delpaths([["a", "c"], ["a", "d"]])`,
expected: []string{
"D0, P[], (doc)::{a: {b: cat}}\n",
},
},
{
description: "Delete array path",
document: `a: [cat, frog]`,
expression: `delpaths([["a", 0]])`,
expected: []string{
"D0, P[], (doc)::a: [frog]\n",
},
},
{
description: "Delete - wrong parameter",
subdescription: "delpaths does not work with a single path array",
document: `a: [cat, frog]`,
expression: `delpaths(["a", 0])`,
expectedError: "DELPATHS: expected entry [0] to be a sequence, but its a !!str. Note that delpaths takes an array of path arrays, e.g. [[\"a\", \"b\"]]",
},
2020-11-22 01:19:57 +00:00
}
func TestPathOperatorsScenarios(t *testing.T) {
for _, tt := range pathOperatorScenarios {
testScenario(t, &tt)
}
2021-12-21 04:02:07 +00:00
documentOperatorScenarios(t, "path", pathOperatorScenarios)
2020-11-22 01:19:57 +00:00
}