yq/pkg/yqlib/operator_collect_test.go

137 lines
2.5 KiB
Go
Raw Normal View History

2020-11-03 23:48:43 +00:00
package yqlib
2020-10-17 11:10:47 +00:00
import (
"testing"
)
var collectOperatorScenarios = []expressionScenario{
2023-05-05 04:13:18 +00:00
{
skipDoc: true,
expression: `["x", "y"] | .[1]`,
expected: []string{
"D0, P[1], (!!str)::y\n",
},
},
{
skipDoc: true,
document: ``,
expression: `.a += [0]`,
expected: []string{
"D0, P[], ()::a:\n - 0\n",
},
},
{
skipDoc: true,
expression: `[1,2,3]`,
expected: []string{
"D0, P[], (!!seq)::- 1\n- 2\n- 3\n",
},
},
{
skipDoc: true,
description: "update in collect",
expression: `[.a = "cat"]`,
expected: []string{
"D0, P[], (!!seq)::- a: cat\n",
},
},
2020-10-17 11:10:47 +00:00
{
2020-11-13 10:22:05 +00:00
description: "Collect empty",
document: ``,
expression: `[]`,
2020-11-22 02:50:32 +00:00
expected: []string{
"D0, P[], (!!seq)::[]\n",
},
2020-11-13 02:19:54 +00:00
},
2021-01-11 03:44:53 +00:00
{
skipDoc: true,
document: "{a: apple}\n---\n{b: frog}",
2021-01-11 04:43:50 +00:00
expression: `[.]`,
2021-01-11 03:44:53 +00:00
expected: []string{
"D0, P[], (!!seq)::- {a: apple}\n- {b: frog}\n",
},
},
{
description: "with comments",
skipDoc: true,
document: "# abc\n[{a: apple}]\n\n# xyz\n",
expression: `[.[]]`,
expected: []string{
"D0, P[], (!!seq)::- {a: apple}\n",
},
},
{
skipDoc: true,
document: ``,
expression: `[3]`,
expected: []string{
"D0, P[], (!!seq)::- 3\n",
},
},
2020-11-13 02:19:54 +00:00
{
2020-11-13 10:22:05 +00:00
description: "Collect single",
document: ``,
expression: `["cat"]`,
2020-10-17 11:10:47 +00:00
expected: []string{
"D0, P[], (!!seq)::- cat\n",
},
},
{
2020-11-13 02:19:54 +00:00
document: ``,
2020-11-13 10:22:05 +00:00
skipDoc: true,
2020-10-19 09:05:38 +00:00
expression: `[true]`,
expected: []string{
"D0, P[], (!!seq)::- true\n",
},
2020-11-13 10:22:05 +00:00
},
{
description: "Collect many",
document: `{a: cat, b: dog}`,
expression: `[.a, .b]`,
2020-10-17 11:10:47 +00:00
expected: []string{
"D0, P[], (!!seq)::- cat\n- dog\n",
},
2020-11-13 10:22:05 +00:00
},
{
2020-11-13 02:19:54 +00:00
document: ``,
2020-11-13 10:22:05 +00:00
skipDoc: true,
expression: `collect(1)`,
2020-10-17 11:10:47 +00:00
expected: []string{
"D0, P[], (!!seq)::- 1\n",
},
2020-11-13 10:22:05 +00:00
},
{
2020-10-17 11:10:47 +00:00
document: `[1,2,3]`,
2020-11-13 10:22:05 +00:00
skipDoc: true,
2020-10-17 11:10:47 +00:00
expression: `[.[]]`,
expected: []string{
"D0, P[], (!!seq)::- 1\n- 2\n- 3\n",
},
2020-11-13 10:22:05 +00:00
},
{
2020-10-17 11:10:47 +00:00
document: `a: {b: [1,2,3]}`,
2020-11-22 02:50:32 +00:00
expression: `[.a.b.[]]`,
2020-11-13 10:22:05 +00:00
skipDoc: true,
2020-10-17 11:10:47 +00:00
expected: []string{
"D0, P[], (!!seq)::- 1\n- 2\n- 3\n",
},
},
{
skipDoc: true,
document: `[{name: cat, thing: bor}, {name: dog}]`,
expression: `.[] | [.name]`,
expected: []string{
"D0, P[0], (!!seq)::- cat\n",
"D0, P[1], (!!seq)::- dog\n",
2020-10-17 11:10:47 +00:00
},
},
}
func TestCollectOperatorScenarios(t *testing.T) {
for _, tt := range collectOperatorScenarios {
testScenario(t, &tt)
}
2021-12-21 04:02:07 +00:00
documentOperatorScenarios(t, "collect-into-array", collectOperatorScenarios)
2020-10-17 11:10:47 +00:00
}