yq/pkg/yqlib/operator_select_test.go

64 lines
1.5 KiB
Go
Raw Normal View History

2020-11-03 23:48:43 +00:00
package yqlib
2020-10-17 11:10:47 +00:00
import (
"testing"
)
var selectOperatorScenarios = []expressionScenario{
{
2020-11-17 22:41:29 +00:00
description: "Select elements from array",
document: `[cat,goat,dog]`,
expression: `.[] | select(. == "*at")`,
2020-10-17 11:10:47 +00:00
expected: []string{
"D0, P[0], (!!str)::cat\n",
"D0, P[1], (!!str)::goat\n",
},
2020-11-17 23:32:30 +00:00
},
{
2020-11-17 22:41:29 +00:00
skipDoc: true,
2020-10-17 11:10:47 +00:00
document: `[hot, fot, dog]`,
expression: `.[] | select(. == "*at")`,
expected: []string{},
2020-11-17 23:32:30 +00:00
},
{
2020-11-17 22:41:29 +00:00
skipDoc: true,
2020-10-17 11:10:47 +00:00
document: `a: [cat,goat,dog]`,
expression: `.a[] | select(. == "*at")`,
expected: []string{
"D0, P[a 0], (!!str)::cat\n",
"D0, P[a 1], (!!str)::goat\n"},
2020-11-17 23:32:30 +00:00
},
{
description: "Select and update matching values in map",
2020-11-17 22:41:29 +00:00
document: `a: { things: cat, bob: goat, horse: dog }`,
2020-11-17 23:32:30 +00:00
expression: `(.a[] | select(. == "*at")) |= "rabbit"`,
2020-10-17 11:10:47 +00:00
expected: []string{
2020-11-17 23:32:30 +00:00
"D0, P[], (doc)::a: {things: rabbit, bob: rabbit, horse: dog}\n",
},
},
{
2020-11-17 22:41:29 +00:00
skipDoc: true,
2020-10-17 11:10:47 +00:00
document: `a: { things: {include: true}, notMe: {include: false}, andMe: {include: fold} }`,
expression: `.a[] | select(.include)`,
expected: []string{
"D0, P[a things], (!!map)::{include: true}\n",
"D0, P[a andMe], (!!map)::{include: fold}\n",
},
2020-11-17 23:32:30 +00:00
},
{
2020-11-17 22:41:29 +00:00
skipDoc: true,
2020-10-20 04:40:11 +00:00
document: `[cat,~,dog]`,
expression: `.[] | select(. == ~)`,
expected: []string{
"D0, P[1], (!!null)::~\n",
},
2020-10-17 11:10:47 +00:00
},
}
func TestSelectOperatorScenarios(t *testing.T) {
for _, tt := range selectOperatorScenarios {
testScenario(t, &tt)
}
2020-11-17 22:41:29 +00:00
documentScenarios(t, "Select Operator", selectOperatorScenarios)
2020-10-17 11:10:47 +00:00
}