2020-11-03 23:48:43 +00:00
|
|
|
package yqlib
|
2020-10-21 01:54:58 +00:00
|
|
|
|
2020-10-21 02:54:51 +00:00
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var collectObjectOperatorScenarios = []expressionScenario{
|
2020-11-13 02:19:54 +00:00
|
|
|
{
|
|
|
|
document: ``,
|
|
|
|
expression: `{}`,
|
|
|
|
expected: []string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
document: "{name: Mike}\n",
|
|
|
|
expression: `{"wrap": .}`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!map)::wrap: {name: Mike}\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
document: "{name: Mike}\n---\n{name: Bob}",
|
|
|
|
expression: `{"wrap": .}`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!map)::wrap: {name: Mike}\n",
|
|
|
|
"D0, P[], (!!map)::wrap: {name: Bob}\n",
|
|
|
|
},
|
|
|
|
},
|
2020-10-21 02:54:51 +00:00
|
|
|
{
|
|
|
|
document: `{name: Mike, age: 32}`,
|
|
|
|
expression: `{.name: .age}`,
|
|
|
|
expected: []string{
|
2020-10-28 02:00:26 +00:00
|
|
|
"D0, P[], (!!map)::Mike: 32\n",
|
2020-10-21 02:54:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
document: `{name: Mike, pets: [cat, dog]}`,
|
|
|
|
expression: `{.name: .pets[]}`,
|
|
|
|
expected: []string{
|
2020-10-28 02:00:26 +00:00
|
|
|
"D0, P[], (!!map)::Mike: cat\n",
|
|
|
|
"D0, P[], (!!map)::Mike: dog\n",
|
2020-10-21 02:54:51 +00:00
|
|
|
},
|
|
|
|
},
|
2020-11-13 02:19:54 +00:00
|
|
|
{
|
|
|
|
document: "{name: Mike, pets: [cat, dog]}\n---\n{name: Rosey, pets: [monkey, sheep]}",
|
|
|
|
expression: `{.name: .pets[]}`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!map)::Mike: cat\n",
|
|
|
|
"D0, P[], (!!map)::Mike: dog\n",
|
|
|
|
"D0, P[], (!!map)::Rosey: monkey\n",
|
|
|
|
"D0, P[], (!!map)::Rosey: sheep\n",
|
|
|
|
},
|
|
|
|
},
|
2020-10-21 02:54:51 +00:00
|
|
|
{
|
|
|
|
document: `{name: Mike, pets: [cat, dog], food: [hotdog, burger]}`,
|
|
|
|
expression: `{.name: .pets[], "f":.food[]}`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!map)::Mike: cat\nf: hotdog\n",
|
|
|
|
"D0, P[], (!!map)::Mike: cat\nf: burger\n",
|
|
|
|
"D0, P[], (!!map)::Mike: dog\nf: hotdog\n",
|
|
|
|
"D0, P[], (!!map)::Mike: dog\nf: burger\n",
|
|
|
|
},
|
|
|
|
},
|
2020-10-28 00:34:01 +00:00
|
|
|
{
|
|
|
|
document: `{name: Mike, pets: {cows: [apl, bba]}}`,
|
|
|
|
expression: `{"a":.name, "b":.pets}`,
|
|
|
|
expected: []string{
|
|
|
|
`D0, P[], (!!map)::a: Mike
|
|
|
|
b: {cows: [apl, bba]}
|
2020-10-28 02:00:26 +00:00
|
|
|
`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
document: ``,
|
|
|
|
expression: `{"wrap": "frog"}`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!map)::wrap: frog\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
document: `{name: Mike}`,
|
|
|
|
expression: `{"wrap": .}`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!map)::wrap: {name: Mike}\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
document: `{name: Mike}`,
|
2020-11-13 02:19:54 +00:00
|
|
|
expression: `{"wrap": {"further": .}} | (.. style= "flow")`,
|
2020-10-28 02:00:26 +00:00
|
|
|
expected: []string{
|
2020-11-13 02:19:54 +00:00
|
|
|
"D0, P[], (!!map)::{wrap: {further: {name: Mike}}}\n",
|
2020-10-28 00:34:01 +00:00
|
|
|
},
|
|
|
|
},
|
2020-10-21 02:54:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCollectObjectOperatorScenarios(t *testing.T) {
|
|
|
|
for _, tt := range collectObjectOperatorScenarios {
|
|
|
|
testScenario(t, &tt)
|
|
|
|
}
|
2020-11-13 02:19:54 +00:00
|
|
|
documentScenarios(t, "Collect into Object", collectObjectOperatorScenarios)
|
2020-10-21 02:54:51 +00:00
|
|
|
}
|