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",
|
|
|
|
},
|
|
|
|
},
|
2021-05-21 22:22:45 +00:00
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: ``,
|
|
|
|
expression: `.a += [0]`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], ()::a:\n - 0\n",
|
|
|
|
},
|
|
|
|
},
|
2021-11-30 02:19:30 +00:00
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
expression: `[1,2,3]`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!seq)::- 1\n- 2\n- 3\n",
|
|
|
|
},
|
|
|
|
},
|
2022-10-24 06:55:19 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2022-05-31 06:28: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",
|
|
|
|
},
|
|
|
|
},
|
2022-05-31 06:28:53 +00:00
|
|
|
{
|
|
|
|
description: "with comments",
|
|
|
|
skipDoc: true,
|
|
|
|
document: "# abc\n[{a: apple}]\n\n# xyz\n",
|
|
|
|
|
|
|
|
expression: `[.[]]`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!seq)::- {a: apple}\n",
|
|
|
|
},
|
|
|
|
},
|
2020-11-28 00:24:16 +00:00
|
|
|
{
|
|
|
|
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",
|
|
|
|
},
|
2021-11-30 02:19:30 +00:00
|
|
|
},
|
|
|
|
{
|
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,
|
2021-11-30 02:19:30 +00:00
|
|
|
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{
|
2021-11-30 02:19:30 +00:00
|
|
|
"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
|
|
|
}
|