2020-11-03 23:48:43 +00:00
|
|
|
package yqlib
|
2020-10-17 11:10:47 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var collectOperatorScenarios = []expressionScenario{
|
|
|
|
{
|
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
|
|
|
},
|
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",
|
|
|
|
},
|
2020-10-19 09:05:38 +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,
|
2020-10-17 11:10:47 +00:00
|
|
|
expression: `1 | collect`,
|
|
|
|
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[a b], (!!seq)::- 1\n- 2\n- 3\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCollectOperatorScenarios(t *testing.T) {
|
|
|
|
for _, tt := range collectOperatorScenarios {
|
|
|
|
testScenario(t, &tt)
|
|
|
|
}
|
2020-11-13 02:19:54 +00:00
|
|
|
documentScenarios(t, "Collect into Array", collectOperatorScenarios)
|
2020-10-17 11:10:47 +00:00
|
|
|
}
|