yq/pkg/yqlib/operator_flatten_test.go

71 lines
1.5 KiB
Go
Raw Normal View History

2021-10-26 04:42:25 +00:00
package yqlib
import (
"testing"
)
var flattenOperatorScenarios = []expressionScenario{
{
description: "Flatten",
subdescription: "Recursively flattens all arrays",
document: `[1, [2], [[3]]]`,
expression: `flatten`,
expected: []string{
"D0, P[], (!!seq)::[1, 2, 3]\n",
2021-10-26 04:42:25 +00:00
},
},
{
description: "Flatten splat",
skipDoc: true,
document: `[1, [2], [[3]]]`,
expression: `flatten[]`,
expected: []string{
"D0, P[0], (!!int)::1\n",
"D0, P[0], (!!int)::2\n",
"D0, P[0], (!!int)::3\n",
},
},
2021-10-26 04:42:25 +00:00
{
description: "Flatten with depth of one",
document: `[1, [2], [[3]]]`,
expression: `flatten(1)`,
expected: []string{
"D0, P[], (!!seq)::[1, 2, [3]]\n",
2021-10-26 04:42:25 +00:00
},
},
{
description: "Flatten with depth and splat",
skipDoc: true,
document: `[1, [2], [[3]]]`,
expression: `flatten(1)[]`,
expected: []string{
"D0, P[0], (!!int)::1\n",
"D0, P[0], (!!int)::2\n",
"D0, P[0], (!!seq)::[3]\n",
},
},
2021-10-26 04:42:25 +00:00
{
description: "Flatten empty array",
document: `[[]]`,
expression: `flatten`,
expected: []string{
"D0, P[], (!!seq)::[]\n",
2021-10-26 04:42:25 +00:00
},
},
{
description: "Flatten array of objects",
document: `[{foo: bar}, [{foo: baz}]]`,
expression: `flatten`,
expected: []string{
"D0, P[], (!!seq)::[{foo: bar}, {foo: baz}]\n",
2021-10-26 04:42:25 +00:00
},
},
}
func TestFlattenOperatorScenarios(t *testing.T) {
for _, tt := range flattenOperatorScenarios {
testScenario(t, &tt)
}
2021-12-21 04:02:07 +00:00
documentOperatorScenarios(t, "flatten", flattenOperatorScenarios)
2021-10-26 04:42:25 +00:00
}