yq/pkg/yqlib/operator_multiply_test.go

288 lines
7.1 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"
)
2021-03-19 01:54:03 +00:00
var doc1 = `list:
# Hi this is a comment.
# Hello this is another comment.
- "abc"`
var doc2 = `list2:
# This is yet another comment.
# Indeed this is yet another comment.
- "123"`
var docExpected = `D0, P[], (!!map)::list:
# Hi this is a comment.
# Hello this is another comment.
- "abc"
list2:
# This is yet another comment.
# Indeed this is yet another comment.
- "123"
`
2020-10-19 05:14:29 +00:00
var multiplyOperatorScenarios = []expressionScenario{
{
description: "Multiply integers",
expression: `3 * 4`,
expected: []string{
"D0, P[], (!!int)::12\n",
},
},
2021-03-19 01:54:03 +00:00
{
skipDoc: true,
document: doc1,
document2: doc2,
expression: `select(fi == 0) * select(fi == 1)`,
expected: []string{
docExpected,
},
},
{
skipDoc: true,
expression: `3 * 4.5`,
expected: []string{
"D0, P[], (!!float)::13.5\n",
},
},
{
skipDoc: true,
expression: `4.5 * 3`,
expected: []string{
"D0, P[], (!!float)::13.5\n",
},
},
2020-10-19 05:14:29 +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: me}}`,
expression: `. * {"a" : .b}`,
expected: []string{
"D0, P[], (!!map)::{a: {also: me}, b: {also: me}}\n",
},
2020-10-27 05:45:16 +00:00
},
2021-02-08 02:58:46 +00:00
{
skipDoc: true,
document: "# b\nb:\n # a\n a: cat",
expression: "{} * .",
expected: []string{
"D0, P[], (!!map)::# b\nb:\n # a\n a: cat\n",
},
},
{
skipDoc: true,
document: "# b\nb:\n # a\n a: cat",
expression: ". * {}",
expected: []string{
"D0, P[], (!!map)::# b\nb:\n # a\n a: cat\n",
},
},
2021-02-05 03:40:16 +00:00
{
skipDoc: true,
document: `{a: &a { b: &b { c: &c cat } } }`,
expression: `{} * .`,
expected: []string{
"D0, P[], (!!map)::{a: &a {b: &b {c: &c cat}}}\n",
},
},
2021-01-16 03:56:52 +00:00
{
skipDoc: true,
2021-01-18 02:28:40 +00:00
document: `{a: 2, b: 5}`,
document2: `{a: 3, b: 10}`,
2021-01-17 23:15:31 +00:00
expression: `.a * .b`,
2021-01-16 03:56:52 +00:00
expected: []string{
2021-01-18 02:28:40 +00:00
"D0, P[a], (!!int)::10\n",
"D0, P[a], (!!int)::20\n",
"D0, P[a], (!!int)::15\n",
"D0, P[a], (!!int)::30\n",
},
},
{
skipDoc: true,
document: `{a: 2}`,
document2: `{b: 10}`,
expression: `select(fi ==0) * select(fi==1)`,
expected: []string{
"D0, P[], (!!map)::{a: 2, b: 10}\n",
2021-01-16 03:56:52 +00:00
},
},
{
skipDoc: true,
expression: `{} * {"cat":"dog"}`,
expected: []string{
"D0, P[], (!!map)::cat: dog\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-14 23:50:30 +00:00
description: "Merge objects together, returning merged result only",
document: `{a: {field: me, fieldA: cat}, b: {field: {g: wizz}, fieldB: dog}}`,
expression: `.a * .b`,
expected: []string{
"D0, P[a], (!!map)::{field: {g: wizz}, fieldA: cat, fieldB: dog}\n",
},
},
{
description: "Merge objects together, returning parent object",
document: `{a: {field: me, fieldA: cat}, b: {field: {g: wizz}, fieldB: dog}}`,
2020-11-06 00:45:18 +00:00
expression: `. * {"a":.b}`,
2020-10-21 02:54:51 +00:00
expected: []string{
2020-11-14 23:50:30 +00:00
"D0, P[], (!!map)::{a: {field: {g: wizz}, fieldA: cat, fieldB: dog}, b: {field: {g: wizz}, fieldB: dog}}\n",
2020-10-21 02:54:51 +00:00
},
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}}`,
2021-02-08 02:58:46 +00:00
expression: `. * {"a": .b}`,
2020-10-21 02:54:51 +00:00
expected: []string{
"D0, P[], (!!map)::{a: {things: great, also: me}, b: {also: me}}\n",
},
2020-10-27 05:45:16 +00:00
},
{
2020-11-14 23:50:30 +00:00
description: "Merge keeps style of LHS",
dontFormatInputForDoc: true,
2021-02-08 02:58:46 +00:00
document: "a: {things: great}\nb:\n also: \"me\"",
expression: `. * {"a":.b}`,
2020-10-21 02:54:51 +00:00
expected: []string{
2021-02-08 02:58:46 +00:00
"D0, P[], (!!map)::a: {things: great, also: \"me\"}\nb:\n also: \"me\"\n",
2020-10-21 02:54:51 +00:00
},
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-11-27 23:41:09 +00:00
{
skipDoc: true,
document: `{a: [1], b: [2]}`,
expression: `.a *+ .b`,
expected: []string{
"D0, P[a], (!!seq)::[1, 2]\n",
},
},
2021-01-13 05:54:28 +00:00
{
description: "Merge, only existing fields",
document: `{a: {thing: one, cat: frog}, b: {missing: two, thing: two}}`,
expression: `.a *? .b`,
expected: []string{
"D0, P[a], (!!map)::{thing: two, cat: frog}\n",
},
},
{
skipDoc: true,
document: `{a: [{thing: one}], b: [{missing: two, thing: two}]}`,
expression: `.a *?d .b`,
2021-01-13 05:54:28 +00:00
expected: []string{
"D0, P[a], (!!seq)::[{thing: two}]\n",
},
},
2021-01-18 02:58:46 +00:00
{
skipDoc: true,
document: `{a: {array: [1]}, b: {}}`,
expression: `.b *+ .a`,
expected: []string{
"D0, P[b], (!!map)::{array: [1]}\n",
},
},
2020-11-27 23:41:09 +00:00
{
description: "Merge, appending arrays",
document: `{a: {array: [1, 2, animal: dog], value: coconut}, b: {array: [3, 4, animal: cat], value: banana}}`,
expression: `.a *+ .b`,
expected: []string{
"D0, P[a], (!!map)::{array: [1, 2, {animal: dog}, 3, 4, {animal: cat}], value: banana}\n",
},
},
2021-01-13 05:54:28 +00:00
{
description: "Merge, only existing fields, appending arrays",
document: `{a: {thing: [1,2]}, b: {thing: [3,4], another: [1]}}`,
expression: `.a *?+ .b`,
expected: []string{
"D0, P[a], (!!map)::{thing: [1, 2, 3, 4]}\n",
},
},
{
description: "Merge, deeply merging arrays",
subdescription: "Merging arrays deeply means arrays are merge like objects, with indexes as their key. In this case, we merge the first item in the array, and do nothing with the second.",
document: `{a: [{name: fred, age: 12}, {name: bob, age: 32}], b: [{name: fred, age: 34}]}`,
expression: `.a *d .b`,
expected: []string{
"D0, P[a], (!!seq)::[{name: fred, age: 34}, {name: bob, age: 32}]\n",
},
},
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",
},
},
{
2021-01-12 22:21:16 +00:00
description: "Merge copies anchor names",
2020-11-06 00:45:18 +00:00
document: `{a: {c: &cat frog}, b: {f: *cat}, c: {g: thongs}}`,
expression: `.c * .a`,
2020-10-29 23:56:45 +00:00
expected: []string{
2021-01-12 22:21:16 +00:00
"D0, P[c], (!!map)::{g: thongs, c: &cat frog}\n",
2020-10-29 23:56:45 +00:00
},
},
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)
}
2021-04-15 00:09:41 +00:00
documentScenarios(t, "Multiply (Merge)", multiplyOperatorScenarios)
2020-10-19 05:14:29 +00:00
}