yq/pkg/yqlib/treeops/operator_assign_test.go

85 lines
1.6 KiB
Go
Raw Normal View History

2020-10-17 11:39:01 +00:00
package treeops
import (
"testing"
)
var assignOperatorScenarios = []expressionScenario{
{
document: `{a: {b: apple}}`,
expression: `.a.b |= "frog"`,
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-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-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-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-10-17 11:58:18 +00:00
document: `{a: {b: {g: foof}}}`,
expression: `.a |= .b`,
expected: []string{
2020-10-27 05:45:16 +00:00
"D0, P[], (doc)::{a: {g: foof}}\n",
2020-10-17 11:58:18 +00:00
},
2020-10-27 05:45:16 +00:00
},
{
2020-10-17 11:58:18 +00:00
document: `{a: {b: apple, c: cactus}}`,
expression: `.a[] | select(. == "apple") |= "frog"`,
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-10-17 11:58:18 +00:00
document: `[candy, apple, sandy]`,
expression: `.[] | select(. == "*andy") |= "bogs"`,
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-10-19 05:14:29 +00:00
document: `{}`,
expression: `.a.b |= "bogs"`,
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-10-19 05:14:29 +00:00
document: `{}`,
expression: `.a.b[0] |= "bogs"`,
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-10-19 05:14:29 +00:00
document: `{}`,
expression: `.a.b[1].c |= "bogs"`,
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)
}
}