yq/pkg/yqlib/operator_with_test.go

58 lines
1.9 KiB
Go
Raw Permalink Normal View History

2021-09-12 11:52:02 +00:00
package yqlib
import "testing"
var withOperatorScenarios = []expressionScenario{
{
description: "Update and style",
document: `a: {deeply: {nested: value}}`,
2021-09-15 12:24:03 +00:00
expression: `with(.a.deeply.nested; . = "newValue" | . style="single")`,
2021-09-12 11:52:02 +00:00
expected: []string{
"D0, P[], (!!map)::a: {deeply: {nested: 'newValue'}}\n",
2021-09-12 11:52:02 +00:00
},
},
{
description: "with splat",
skipDoc: true,
document: `a: {deeply: {nested: value}}`,
expression: `with(.a.deeply.nested; . = "newValue" | . style="single")[]`,
expected: []string{
"D0, P[a], (!!map)::{deeply: {nested: 'newValue'}}\n",
},
},
2021-09-15 05:18:10 +00:00
{
description: "Update multiple deeply nested properties",
document: `a: {deeply: {nested: value, other: thing}}`,
2021-09-15 12:24:03 +00:00
expression: `with(.a.deeply; .nested = "newValue" | .other= "newThing")`,
2021-09-15 05:18:10 +00:00
expected: []string{
"D0, P[], (!!map)::a: {deeply: {nested: newValue, other: newThing}}\n",
2021-09-15 05:18:10 +00:00
},
},
{
2021-09-16 00:55:26 +00:00
description: "Update array elements relatively",
subdescription: "The second expression runs with each element of the array as it's contextual root. This allows you to make updates relative to the element.",
document: `myArray: [{a: apple},{a: banana}]`,
expression: `with(.myArray[]; .b = .a + " yum")`,
2021-09-15 05:18:10 +00:00
expected: []string{
"D0, P[], (!!map)::myArray: [{a: apple, b: apple yum}, {a: banana, b: banana yum}]\n",
2021-09-15 05:18:10 +00:00
},
},
2022-04-14 02:03:18 +00:00
{
description: "Update array elements relatively +=",
skipDoc: true,
subdescription: "The second expression runs with each element of the array as it's contextual root. This allows you to make updates relative to the element.",
document: `myArray: [{a: apple},{a: banana}]`,
expression: `with(.myArray[]; .a += .a)`,
expected: []string{
"D0, P[], (!!map)::myArray: [{a: appleapple}, {a: bananabanana}]\n",
2022-04-14 02:03:18 +00:00
},
},
2021-09-12 11:52:02 +00:00
}
func TestWithOperatorScenarios(t *testing.T) {
for _, tt := range withOperatorScenarios {
testScenario(t, &tt)
}
2021-12-21 04:02:07 +00:00
documentOperatorScenarios(t, "with", withOperatorScenarios)
2021-09-12 11:52:02 +00:00
}