mirror of
https://github.com/mikefarah/yq.git
synced 2024-11-14 07:08:06 +00:00
13d1bbb45f
Remove dependency on yaml.Node for internal AST representation. Yaml decoder is now just another decoder.
114 lines
2.8 KiB
Go
114 lines
2.8 KiB
Go
package yqlib
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
var createMapOperatorScenarios = []expressionScenario{
|
|
{
|
|
document: ``,
|
|
expression: `"frog": "jumps"`,
|
|
expected: []string{
|
|
"D0, P[], (!!seq)::- [{frog: jumps}]\n",
|
|
},
|
|
},
|
|
{
|
|
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",
|
|
},
|
|
},
|
|
{
|
|
document: `{name: Mike, age: 32}`,
|
|
expression: `.name: .age`,
|
|
expected: []string{
|
|
"D0, P[], (!!seq)::- [{Mike: 32}]\n",
|
|
},
|
|
},
|
|
{
|
|
document: `{name: Mike, pets: [cat, dog]}`,
|
|
expression: `.name: .pets.[]`,
|
|
expected: []string{
|
|
"D0, P[], (!!seq)::- [{Mike: cat}, {Mike: dog}]\n",
|
|
},
|
|
},
|
|
{
|
|
document: `{name: Mike, pets: [cat, dog], food: [hotdog, burger]}`,
|
|
expression: `.name: .pets.[], "f":.food.[]`,
|
|
expected: []string{
|
|
"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]}",
|
|
expression: `.name: .pets.[], "f":.food.[]`,
|
|
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",
|
|
},
|
|
},
|
|
{
|
|
document: `{name: Mike, pets: {cows: [apl, bba]}}`,
|
|
expression: `"a":.name, "b":.pets`,
|
|
expected: []string{
|
|
"D0, P[], (!!seq)::- [{a: Mike}]\n",
|
|
"D0, P[], (!!seq)::- [{b: {cows: [apl, bba]}}]\n",
|
|
},
|
|
},
|
|
{
|
|
document: `{name: Mike}`,
|
|
expression: `"wrap": .`,
|
|
expected: []string{
|
|
"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",
|
|
},
|
|
},
|
|
}
|
|
|
|
func TestCreateMapOperatorScenarios(t *testing.T) {
|
|
for _, tt := range createMapOperatorScenarios {
|
|
testScenario(t, &tt)
|
|
}
|
|
}
|