yq/pkg/yqlib/operator_multiply_test.go

126 lines
3.0 KiB
Go
Raw Normal View History

2020-11-03 23:48:43 +00:00
package yqlib
2020-10-19 05:14:29 +00:00
import (
"testing"
)
var multiplyOperatorScenarios = []expressionScenario{
{
2020-11-06 00:45:18 +00:00
skipDoc: true,
2020-10-21 02:54:51 +00:00
document: `{a: {also: [1]}, b: {also: me}}`,
expression: `. * {"a" : .b}`,
expected: []string{
"D0, P[], (!!map)::{a: {also: me}, b: {also: me}}\n",
},
2020-10-27 05:45:16 +00:00
},
{
2020-11-06 00:45:18 +00:00
skipDoc: true,
2020-10-21 02:54:51 +00:00
document: `{a: {also: me}, b: {also: [1]}}`,
expression: `. * {"a":.b}`,
expected: []string{
"D0, P[], (!!map)::{a: {also: [1]}, b: {also: [1]}}\n",
},
2020-10-27 05:45:16 +00:00
},
{
2020-11-06 00:45:18 +00:00
description: "Merge objects together",
document: `{a: {also: me}, b: {also: {g: wizz}}}`,
expression: `. * {"a":.b}`,
2020-10-21 02:54:51 +00:00
expected: []string{
"D0, P[], (!!map)::{a: {also: {g: wizz}}, b: {also: {g: wizz}}}\n",
},
2020-10-27 05:45:16 +00:00
},
{
2020-11-06 00:45:18 +00:00
skipDoc: true,
2020-10-21 02:54:51 +00:00
document: `{a: {also: {g: wizz}}, b: {also: me}}`,
expression: `. * {"a":.b}`,
expected: []string{
"D0, P[], (!!map)::{a: {also: me}, b: {also: me}}\n",
},
2020-10-27 05:45:16 +00:00
},
{
2020-11-06 00:45:18 +00:00
skipDoc: true,
2020-10-21 02:54:51 +00:00
document: `{a: {also: {g: wizz}}, b: {also: [1]}}`,
expression: `. * {"a":.b}`,
expected: []string{
"D0, P[], (!!map)::{a: {also: [1]}, b: {also: [1]}}\n",
},
2020-10-27 05:45:16 +00:00
},
{
2020-11-06 00:45:18 +00:00
skipDoc: true,
2020-10-21 02:54:51 +00:00
document: `{a: {also: [1]}, b: {also: {g: wizz}}}`,
expression: `. * {"a":.b}`,
expected: []string{
"D0, P[], (!!map)::{a: {also: {g: wizz}}, b: {also: {g: wizz}}}\n",
},
2020-10-27 05:45:16 +00:00
},
{
2020-11-06 00:45:18 +00:00
skipDoc: true,
2020-10-21 02:54:51 +00:00
document: `{a: {things: great}, b: {also: me}}`,
expression: `. * {"a":.b}`,
expected: []string{
"D0, P[], (!!map)::{a: {things: great, also: me}, b: {also: me}}\n",
},
2020-10-27 05:45:16 +00:00
},
{
2020-11-06 00:45:18 +00:00
description: "Merge keeps style of LHS",
2020-10-21 02:54:51 +00:00
document: `a: {things: great}
b:
also: "me"
`,
expression: `. * {"a":.b}`,
expected: []string{
2020-10-28 00:34:01 +00:00
`D0, P[], (!!map)::a: {things: great, also: "me"}
2020-10-21 02:54:51 +00:00
b:
also: "me"
`,
},
2020-10-27 05:45:16 +00:00
},
{
2020-11-06 00:45:18 +00:00
description: "Merge arrays",
document: `{a: [1,2,3], b: [3,4,5]}`,
expression: `. * {"a":.b}`,
2020-10-21 02:54:51 +00:00
expected: []string{
"D0, P[], (!!map)::{a: [3, 4, 5], b: [3, 4, 5]}\n",
},
2020-10-19 05:14:29 +00:00
},
2020-10-28 02:00:26 +00:00
{
2020-11-06 00:45:18 +00:00
description: "Merge to prefix an element",
document: `{a: cat, b: dog}`,
expression: `. * {"a": {"c": .a}}`,
2020-10-28 02:00:26 +00:00
expected: []string{
2020-11-06 00:45:18 +00:00
"D0, P[], (!!map)::{a: {c: cat}, b: dog}\n",
2020-10-28 02:00:26 +00:00
},
},
2020-10-29 23:56:45 +00:00
{
2020-11-06 00:45:18 +00:00
description: "Merge with simple aliases",
document: `{a: &cat {c: frog}, b: {f: *cat}, c: {g: thongs}}`,
expression: `.c * .b`,
2020-10-29 23:56:45 +00:00
expected: []string{
"D0, P[c], (!!map)::{g: thongs, f: *cat}\n",
},
},
{
2020-11-06 00:45:18 +00:00
description: "Merge does not copy anchor names",
document: `{a: {c: &cat frog}, b: {f: *cat}, c: {g: thongs}}`,
expression: `.c * .a`,
2020-10-29 23:56:45 +00:00
expected: []string{
"D0, P[c], (!!map)::{g: thongs, c: frog}\n",
},
},
2020-10-30 01:40:44 +00:00
{
2020-11-06 00:45:18 +00:00
description: "Merge with merge anchors",
document: mergeDocSample,
expression: `.foobar * .foobarList`,
2020-10-30 01:40:44 +00:00
expected: []string{
"D0, P[foobar], (!!map)::c: foobarList_c\n<<: [*foo, *bar]\nthing: foobar_thing\nb: foobarList_b\n",
},
},
2020-10-19 05:14:29 +00:00
}
func TestMultiplyOperatorScenarios(t *testing.T) {
for _, tt := range multiplyOperatorScenarios {
testScenario(t, &tt)
}
2020-11-06 00:45:18 +00:00
documentScenarios(t, "Mulitply Operator", multiplyOperatorScenarios)
2020-10-19 05:14:29 +00:00
}