yq/pkg/yqlib/operator_select_test.go

104 lines
2.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{
2021-10-07 04:04:36 +00:00
{
skipDoc: true,
document: `cat`,
expression: `select(false, true)`,
expected: []string{
"D0, P[], (doc)::cat\n",
},
},
{
skipDoc: true,
document: `cat`,
expression: `select(true, false)`,
expected: []string{
"D0, P[], (doc)::cat\n",
},
},
{
skipDoc: true,
document: `cat`,
expression: `select(false)`,
expected: []string{},
},
2020-10-17 11:10:47 +00:00
{
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
},
{
skipDoc: true,
document: "a: hello",
document2: "b: world",
expression: `select(.a == "hello" or .b == "world")`,
expected: []string{
"D0, P[], (doc)::a: hello\n",
"D0, P[], (doc)::b: world\n",
},
},
2021-02-03 00:54:10 +00:00
{
skipDoc: true,
document: `[{animal: cat, legs: {cool: true}}, {animal: fish}]`,
expression: `(.[] | select(.legs.cool == true).canWalk) = true | (.[] | .alive.things) = "yes"`,
expected: []string{
"D0, P[], (doc)::[{animal: cat, legs: {cool: true}, canWalk: true, alive: {things: yes}}, {animal: fish, alive: {things: yes}}]\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]`,
2020-11-22 02:50:32 +00:00
expression: `.a.[] | select(. == "*at")`,
2020-10-17 11:10:47 +00:00
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-22 02:50:32 +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} }`,
2020-11-22 02:50:32 +00:00
expression: `.a.[] | select(.include)`,
2020-10-17 11:10:47 +00:00
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)
}
2021-11-03 04:00:58 +00:00
documentScenarios(t, "select", selectOperatorScenarios)
2020-10-17 11:10:47 +00:00
}