yq/pkg/yqlib/operator_path_test.go
Abel Sen fa6fac1a76
Minor typos (#1595)
* Remove extra backtick

* Reword explanation of update

* Reword explanation of relative update

* Change "remaple" to "remain"

* Change "clovver" to "clobber"

* Reword explanation of update for comment operators

* Reword explanation of relative update for comment operators

* Change "array" to "expression"

* Change "the golangs" to "Golang's"

* Change "golangs" to "Golang's"

* Change "can durations" to "can add durations"

* Change "array scalars" to "arrays"

* Change "beit" to "be it"

* Fix typo in `eval` tip

* Fix typo in header for `has` operation

* Add space before pipe in `line` operator example

* Fix typos in explanation of deep array merges

* Change "is now used" to "is now used."

* Change "object," to "object."

* Changes "indexes" to "indices"

* Remove extraneous copied text from `..` article

* Reword explanation of `...` operator

* Change "your are" to "you are"

* Add link to `string` operator docs in `select` article

* Change "is a" to "parameter specifies" in `string` operators article

* Change "new line" to "newline"

* Change "golang regex" to "Golang's regex"

* Change "golang" to "Golang"

* Add period

* Remove comma in `subtract` article

* Remove duplicate number subtraction example

* Remove comma in `traverse` operator article

* Clarify use of brackets when `read`ing with special characters
2023-03-16 13:39:36 +11:00

147 lines
3.6 KiB
Go

package yqlib
import (
"testing"
)
var documentToPrune = `
parentA: bob
parentB:
child1: i am child1
child2: i am child2
parentC:
child1: me child1
child2: me child2
`
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",
},
},
{
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",
},
},
{
description: "Get map key",
document: `{a: {b: cat}}`,
expression: `.a.b | path | .[-1]`,
expected: []string{
"D0, P[a b -1], (!!str)::b\n",
},
},
{
description: "Array path",
document: `{a: [cat, dog]}`,
expression: `.a.[] | select(. == "dog") | path`,
expected: []string{
"D0, P[a 1], (!!seq)::- a\n- 1\n",
},
},
{
description: "Get array index",
document: `{a: [cat, dog]}`,
expression: `.a.[] | select(. == "dog") | path | .[-1]`,
expected: []string{
"D0, P[a 1 -1], (!!int)::1\n",
},
},
{
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",
},
},
{
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 path to prune deep paths",
subdescription: "Like pick but recursive. This uses `ireduce` to deeply set the selected paths into an empty object.",
document: documentToPrune,
expression: "(.parentB.child2, .parentC.child1) as $i\n ireduce({}; setpath($i | path; $i))",
expected: []string{
"D0, P[], (!!map)::parentB:\n child2: i am child2\nparentC:\n child1: me child1\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",
},
},
{
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\"]]",
},
}
func TestPathOperatorsScenarios(t *testing.T) {
for _, tt := range pathOperatorScenarios {
testScenario(t, &tt)
}
documentOperatorScenarios(t, "path", pathOperatorScenarios)
}