2020-11-03 23:48:43 +00:00
|
|
|
package yqlib
|
2020-10-21 01:54:58 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var createMapOperatorScenarios = []expressionScenario{
|
2020-11-13 02:19:54 +00:00
|
|
|
{
|
|
|
|
document: ``,
|
|
|
|
expression: `"frog": "jumps"`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!seq)::- [{frog: jumps}]\n",
|
|
|
|
},
|
|
|
|
},
|
2023-10-18 01:11:53 +00:00
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
description: "sets key properly",
|
|
|
|
expression: `("frog": "jumps") | .[0][0] | .frog`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[0 0 frog], (!!str)::jumps\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
description: "sets key properly on map",
|
|
|
|
expression: `{"frog": "jumps"} | .frog`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[frog], (!!str)::jumps\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
document: `{name: Mike, pets: [cat, dog]}`,
|
|
|
|
expression: `(.name: .pets.[]) | .[0][0] | ..`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[0 0], (!!map)::Mike: cat\n",
|
|
|
|
"D0, P[0 0 Mike], (!!str)::cat\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "check path of nested child",
|
|
|
|
document: "pets:\n cows: value",
|
|
|
|
expression: `("b":.pets) | .[0][0] | .b.cows`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[0 0 b cows], (!!str)::value\n",
|
|
|
|
},
|
|
|
|
},
|
2020-10-21 01:54:58 +00:00
|
|
|
{
|
|
|
|
document: `{name: Mike, age: 32}`,
|
|
|
|
expression: `.name: .age`,
|
|
|
|
expected: []string{
|
2020-11-13 02:19:54 +00:00
|
|
|
"D0, P[], (!!seq)::- [{Mike: 32}]\n",
|
2020-10-21 01:54:58 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
document: `{name: Mike, pets: [cat, dog]}`,
|
2020-11-22 02:50:32 +00:00
|
|
|
expression: `.name: .pets.[]`,
|
2020-10-21 01:54:58 +00:00
|
|
|
expected: []string{
|
2020-11-13 02:19:54 +00:00
|
|
|
"D0, P[], (!!seq)::- [{Mike: cat}, {Mike: dog}]\n",
|
2020-10-21 01:54:58 +00:00
|
|
|
},
|
|
|
|
},
|
2020-10-21 02:54:51 +00:00
|
|
|
{
|
|
|
|
document: `{name: Mike, pets: [cat, dog], food: [hotdog, burger]}`,
|
2020-11-22 02:50:32 +00:00
|
|
|
expression: `.name: .pets.[], "f":.food.[]`,
|
2020-10-21 02:54:51 +00:00
|
|
|
expected: []string{
|
2020-11-13 02:19:54 +00:00
|
|
|
"D0, P[], (!!seq)::- [{Mike: cat}, {Mike: dog}]\n",
|
|
|
|
"D0, P[], (!!seq)::- [{f: hotdog}, {f: burger}]\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
document: "{name: Mike, pets: [cat, dog], food: [hotdog, burger]}\n---\n{name: Fred, pets: [mouse], food: [pizza, onion, apple]}",
|
2020-11-22 02:50:32 +00:00
|
|
|
expression: `.name: .pets.[], "f":.food.[]`,
|
2020-11-13 02:19:54 +00:00
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!seq)::- [{Mike: cat}, {Mike: dog}]\n- [{Fred: mouse}]\n",
|
|
|
|
"D0, P[], (!!seq)::- [{f: hotdog}, {f: burger}]\n- [{f: pizza}, {f: onion}, {f: apple}]\n",
|
2020-10-21 02:54:51 +00:00
|
|
|
},
|
|
|
|
},
|
2020-10-28 00:34:01 +00:00
|
|
|
{
|
|
|
|
document: `{name: Mike, pets: {cows: [apl, bba]}}`,
|
|
|
|
expression: `"a":.name, "b":.pets`,
|
|
|
|
expected: []string{
|
2020-11-13 02:19:54 +00:00
|
|
|
"D0, P[], (!!seq)::- [{a: Mike}]\n",
|
|
|
|
"D0, P[], (!!seq)::- [{b: {cows: [apl, bba]}}]\n",
|
2020-10-28 00:34:01 +00:00
|
|
|
},
|
|
|
|
},
|
2020-10-28 02:00:26 +00:00
|
|
|
{
|
|
|
|
document: `{name: Mike}`,
|
|
|
|
expression: `"wrap": .`,
|
|
|
|
expected: []string{
|
2020-11-13 02:19:54 +00:00
|
|
|
"D0, P[], (!!seq)::- [{wrap: {name: Mike}}]\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
document: "{name: Mike}\n---\n{name: Bob}",
|
|
|
|
expression: `"wrap": .`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!seq)::- [{wrap: {name: Mike}}]\n- [{wrap: {name: Bob}}]\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
document: "{name: Mike}\n---\n{name: Bob}",
|
|
|
|
expression: `"wrap": ., .name: "great"`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!seq)::- [{wrap: {name: Mike}}]\n- [{wrap: {name: Bob}}]\n",
|
|
|
|
"D0, P[], (!!seq)::- [{Mike: great}]\n- [{Bob: great}]\n",
|
2020-10-28 02:00:26 +00:00
|
|
|
},
|
|
|
|
},
|
2020-10-21 01:54:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateMapOperatorScenarios(t *testing.T) {
|
|
|
|
for _, tt := range createMapOperatorScenarios {
|
|
|
|
testScenario(t, &tt)
|
|
|
|
}
|
|
|
|
}
|