yq/pkg/yqlib/operator_assign_test.go

130 lines
3.0 KiB
Go
Raw Normal View History

2020-11-03 23:48:43 +00:00
package yqlib
2020-10-17 11:39:01 +00:00
import (
"testing"
)
var assignOperatorScenarios = []expressionScenario{
2020-12-01 03:06:49 +00:00
{
description: "Create yaml file",
expression: `.a.b = "cat" | .x = "frog"`,
2020-12-01 03:06:49 +00:00
expected: []string{
"D0, P[], ()::a:\n b: cat\nx: frog\n",
},
},
2020-10-17 11:39:01 +00:00
{
2020-11-19 11:53:05 +00:00
description: "Update node to be the child value",
2020-11-14 02:38:44 +00:00
document: `{a: {b: {g: foof}}}`,
expression: `.a |= .b`,
expected: []string{
"D0, P[], (doc)::{a: {g: foof}}\n",
},
},
2020-11-19 06:08:13 +00:00
{
2020-11-19 11:53:05 +00:00
description: "Update node to be the sibling value",
2020-11-19 06:08:13 +00:00
document: `{a: {b: child}, b: sibling}`,
expression: `.a = .b`,
expected: []string{
"D0, P[], (doc)::{a: sibling, b: sibling}\n",
},
},
2020-11-17 23:32:30 +00:00
{
description: "Updated multiple paths",
document: `{a: fieldA, b: fieldB, c: fieldC}`,
expression: `(.a, .c) |= "potatoe"`,
expected: []string{
"D0, P[], (doc)::{a: potatoe, b: fieldB, c: potatoe}\n",
},
},
2020-11-14 02:38:44 +00:00
{
description: "Update string value",
document: `{a: {b: apple}}`,
2020-11-19 06:08:13 +00:00
expression: `.a.b = "frog"`,
expected: []string{
"D0, P[], (doc)::{a: {b: frog}}\n",
},
},
{
description: "Update string value via |=",
subdescription: "Note there is no difference between `=` and `|=` when the RHS is a scalar",
document: `{a: {b: apple}}`,
expression: `.a.b |= "frog"`,
2020-10-17 11:39:01 +00:00
expected: []string{
2020-10-27 05:45:16 +00:00
"D0, P[], (doc)::{a: {b: frog}}\n",
2020-10-17 11:39:01 +00:00
},
2020-10-27 05:45:16 +00:00
},
{
2020-11-14 02:38:44 +00:00
skipDoc: true,
2020-10-17 11:39:01 +00:00
document: `{a: {b: apple}}`,
expression: `.a.b | (. |= "frog")`,
expected: []string{
"D0, P[a b], (!!str)::frog\n",
},
2020-10-27 05:45:16 +00:00
},
{
2020-11-14 02:38:44 +00:00
skipDoc: true,
2020-10-17 11:39:01 +00:00
document: `{a: {b: apple}}`,
expression: `.a.b |= 5`,
expected: []string{
2020-10-27 05:45:16 +00:00
"D0, P[], (doc)::{a: {b: 5}}\n",
2020-10-17 11:39:01 +00:00
},
2020-10-27 05:45:16 +00:00
},
{
2020-11-14 02:38:44 +00:00
skipDoc: true,
2020-10-17 11:39:01 +00:00
document: `{a: {b: apple}}`,
expression: `.a.b |= 3.142`,
expected: []string{
2020-10-27 05:45:16 +00:00
"D0, P[], (doc)::{a: {b: 3.142}}\n",
2020-10-17 11:39:01 +00:00
},
2020-10-27 05:45:16 +00:00
},
{
2020-11-14 02:38:44 +00:00
description: "Update selected results",
document: `{a: {b: apple, c: cactus}}`,
expression: `(.a.[] | select(. == "apple")) = "frog"`,
2020-10-17 11:58:18 +00:00
expected: []string{
2020-10-27 05:45:16 +00:00
"D0, P[], (doc)::{a: {b: frog, c: cactus}}\n",
2020-10-17 11:58:18 +00:00
},
2020-10-27 05:45:16 +00:00
},
{
2020-11-14 02:38:44 +00:00
description: "Update array values",
document: `[candy, apple, sandy]`,
expression: `(.[] | select(. == "*andy")) = "bogs"`,
2020-10-17 11:58:18 +00:00
expected: []string{
2020-10-27 05:45:16 +00:00
"D0, P[], (doc)::[bogs, apple, bogs]\n",
2020-10-17 11:58:18 +00:00
},
2020-10-27 05:45:16 +00:00
},
{
2020-11-22 02:16:54 +00:00
description: "Update empty object",
dontFormatInputForDoc: true,
document: `{}`,
expression: `.a.b |= "bogs"`,
2020-10-19 05:14:29 +00:00
expected: []string{
2020-10-27 05:45:16 +00:00
"D0, P[], (doc)::{a: {b: bogs}}\n",
2020-10-19 05:14:29 +00:00
},
2020-10-27 05:45:16 +00:00
},
{
2020-11-22 02:16:54 +00:00
description: "Update empty object and array",
dontFormatInputForDoc: true,
document: `{}`,
expression: `.a.b.[0] |= "bogs"`,
2020-10-19 05:14:29 +00:00
expected: []string{
2020-10-27 05:45:16 +00:00
"D0, P[], (doc)::{a: {b: [bogs]}}\n",
2020-10-19 05:14:29 +00:00
},
2020-10-27 05:45:16 +00:00
},
{
2020-11-14 02:38:44 +00:00
skipDoc: true,
2020-10-19 05:14:29 +00:00
document: `{}`,
expression: `.a.b.[1].c |= "bogs"`,
2020-10-19 05:14:29 +00:00
expected: []string{
2020-10-27 05:45:16 +00:00
"D0, P[], (doc)::{a: {b: [null, {c: bogs}]}}\n",
2020-10-19 05:14:29 +00:00
},
2020-10-17 11:39:01 +00:00
},
}
func TestAssignOperatorScenarios(t *testing.T) {
for _, tt := range assignOperatorScenarios {
testScenario(t, &tt)
}
2020-11-22 02:16:54 +00:00
documentScenarios(t, "Assign", assignOperatorScenarios)
2020-10-17 11:39:01 +00:00
}