yq/pkg/yqlib/operator_assign_test.go

258 lines
6.3 KiB
Go
Raw Permalink Normal View History

2020-11-03 23:48:43 +00:00
package yqlib
2020-10-17 11:39:01 +00:00
import (
"testing"
)
var mergeAnchorAssign = `a: &a
x: OriginalValue
b:
<<: *a`
2020-10-17 11:39:01 +00:00
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",
},
},
{
description: "Create yaml file",
document: "a: {b: 3}",
expression: `.a |= .`,
skipDoc: true,
expected: []string{
"D0, P[], (!!map)::a: {b: 3}\n",
},
},
{
skipDoc: true,
document: "{}",
expression: `.a |= .b`,
expected: []string{
"D0, P[], (!!map)::a: null\n",
},
},
{
skipDoc: true,
document: mergeAnchorAssign,
expression: `.c = .b | .a.x = "ModifiedValue" | explode(.)`,
expected: []string{
"D0, P[], (!!map)::a:\n x: ModifiedValue\nb:\n x: ModifiedValue\nc:\n x: ModifiedValue\n",
},
},
{
skipDoc: true,
document: "{}",
expression: `.a = .b`,
expected: []string{
"D0, P[], (!!map)::a: null\n",
},
},
2022-02-20 03:29:52 +00:00
{
skipDoc: true,
description: "self reference",
document: "a: cat",
expression: `.a = [.a]`,
expected: []string{
"D0, P[], (!!map)::a:\n - cat\n",
2022-02-20 03:29:52 +00:00
},
},
2022-02-24 22:14:41 +00:00
{
skipDoc: true,
description: "change to number when old value is valid number",
document: `a: "3"`,
expression: `.a = 3`,
expected: []string{
"D0, P[], (!!map)::a: 3\n",
2022-02-24 22:14:41 +00:00
},
},
{
skipDoc: true,
description: "change to bool when old value is valid bool",
document: `a: "true"`,
expression: `.a = true`,
expected: []string{
"D0, P[], (!!map)::a: true\n",
2022-02-24 22:14:41 +00:00
},
},
{
skipDoc: true,
description: "update custom tag string, dont clobber style",
document: `a: !cat "meow"`,
expression: `.a = "woof"`,
expected: []string{
"D0, P[], (!!map)::a: !cat \"woof\"\n",
2022-02-24 22:14:41 +00:00
},
},
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[], (!!map)::{a: {g: foof}}\n",
2020-11-14 02:38:44 +00:00
},
},
2021-07-07 09:53:33 +00:00
{
description: "Double elements in an array",
document: `[1,2,3]`,
expression: `.[] |= . * 2`,
expected: []string{
"D0, P[], (!!seq)::[2, 4, 6]\n",
2021-07-07 09:53:33 +00:00
},
},
2021-01-01 23:27:32 +00:00
{
description: "Update node from another file",
subdescription: "Note this will also work when the second file is a scalar (string/number)",
document: `{a: apples}`,
document2: "{b: bob}",
expression: `select(fileIndex==0).a = select(fileIndex==1) | select(fileIndex==0)`,
expected: []string{
"D0, P[], (!!map)::{a: {b: bob}}\n",
2021-01-01 23:27:32 +00:00
},
},
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[], (!!map)::{a: sibling, b: sibling}\n",
2020-11-19 06:08:13 +00:00
},
},
2020-11-17 23:32:30 +00:00
{
description: "Updated multiple paths",
document: `{a: fieldA, b: fieldB, c: fieldC}`,
2022-05-24 08:18:27 +00:00
expression: `(.a, .c) = "potato"`,
2020-11-17 23:32:30 +00:00
expected: []string{
"D0, P[], (!!map)::{a: potato, b: fieldB, c: potato}\n",
2020-11-17 23:32:30 +00:00
},
},
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[], (!!map)::{a: {b: frog}}\n",
2020-11-19 06:08:13 +00:00
},
},
{
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{
"D0, P[], (!!map)::{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{
"D0, P[], (!!map)::{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{
"D0, P[], (!!map)::{a: {b: 3.142}}\n",
2020-10-17 11:39:01 +00:00
},
2020-10-27 05:45:16 +00:00
},
{
2021-10-27 22:15:28 +00:00
description: "Update deeply selected results",
2022-05-24 08:18:27 +00:00
subdescription: "Note that the LHS is wrapped in brackets! This is to ensure we don't first filter out the yaml and then update the snippet.",
2021-10-27 22:15:28 +00:00
document: `{a: {b: apple, c: cactus}}`,
expression: `(.a[] | select(. == "apple")) = "frog"`,
2020-12-26 10:37:08 +00:00
expected: []string{
"D0, P[], (!!map)::{a: {b: frog, c: cactus}}\n",
2020-12-26 10:37:08 +00:00
},
},
{
skipDoc: true,
document: `{a: {b: apple, c: cactus}}`,
expression: `(.a.[] | select(. == "apple")) = "frog"`,
2020-10-17 11:58:18 +00:00
expected: []string{
"D0, P[], (!!map)::{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{
"D0, P[], (!!seq)::[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{
"D0, P[], (!!map)::a:\n b: bogs\n",
2020-10-19 05:14:29 +00:00
},
2020-10-27 05:45:16 +00:00
},
{
description: "Update node value that has an anchor",
subdescription: "Anchor will remain",
dontFormatInputForDoc: true,
document: `a: &cool cat`,
expression: `.a = "dog"`,
expected: []string{
"D0, P[], (!!map)::a: &cool dog\n",
},
},
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{
"D0, P[], (!!map)::a:\n b:\n - 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{
"D0, P[], (!!map)::a:\n b:\n - null\n - c: bogs\n",
2020-10-19 05:14:29 +00:00
},
2020-10-17 11:39:01 +00:00
},
{
description: "Custom types are maintained by default",
document: "a: !cat meow\nb: !dog woof",
expression: `.a = .b`,
expected: []string{
"D0, P[], (!!map)::a: !cat woof\nb: !dog woof\n",
},
},
{
description: "Custom types: clobber",
subdescription: "Use the `c` option to clobber custom tags",
document: "a: !cat meow\nb: !dog woof",
expression: `.a =c .b`,
expected: []string{
"D0, P[], (!!map)::a: !dog woof\nb: !dog woof\n",
},
},
2020-10-17 11:39:01 +00:00
}
func TestAssignOperatorScenarios(t *testing.T) {
for _, tt := range assignOperatorScenarios {
testScenario(t, &tt)
}
2021-12-21 04:02:07 +00:00
documentOperatorScenarios(t, "assign-update", assignOperatorScenarios)
2020-10-17 11:39:01 +00:00
}