yq/pkg/yqlib/operator_select_test.go

151 lines
4.0 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{},
},
2022-01-15 07:18:52 +00:00
{
description: "Select elements from array using wildcard prefix",
2020-11-17 22:41:29 +00:00
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
},
2022-01-15 07:18:52 +00:00
{
description: "Select elements from array using wildcard suffix",
document: `[go-kart,goat,dog]`,
expression: `.[] | select(. == "go*")`,
expected: []string{
"D0, P[0], (!!str)::go-kart\n",
"D0, P[1], (!!str)::goat\n",
},
},
{
description: "Select elements from array using wildcard prefix and suffix",
document: `[ago, go, meow, going]`,
expression: `.[] | select(. == "*go*")`,
expected: []string{
"D0, P[0], (!!str)::ago\n",
"D0, P[1], (!!str)::go\n",
"D0, P[3], (!!str)::going\n",
},
},
2022-01-15 07:22:26 +00:00
{
description: "Select elements from array with regular expression",
subdescription: "See more regular expression examples under the `string` operator docs.",
document: `[this_0, not_this, nor_0_this, thisTo_4]`,
expression: `.[] | select(test("[a-zA-Z]+_[0-9]$"))`,
expected: []string{
"D0, P[0], (!!str)::this_0\n",
"D0, P[3], (!!str)::thisTo_4\n",
},
},
{
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
},
{
2022-01-15 07:18:52 +00:00
description: "Select items from a map",
document: `{ things: cat, bob: goat, horse: dog }`,
expression: `.[] | select(. == "cat" or test("og$"))`,
expected: []string{
"D0, P[things], (!!str)::cat\n",
"D0, P[horse], (!!str)::dog\n",
},
},
{
2022-01-15 07:22:26 +00:00
description: "Use select and with_entries to filter map keys",
2022-01-15 07:18:52 +00:00
document: `{name: bob, legs: 2, game: poker}`,
expression: `with_entries(select(.key | test("ame$")))`,
expected: []string{
"D0, P[], (!!map)::name: bob\ngame: poker\n",
},
},
{
description: "Select multiple items in a map and update",
subdescription: "Note the brackets around the entire LHS.",
document: `a: { things: cat, bob: goat, horse: dog }`,
expression: `(.a.[] | select(. == "cat" or . == "goat")) |= "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-12-21 04:02:07 +00:00
documentOperatorScenarios(t, "select", selectOperatorScenarios)
2020-10-17 11:10:47 +00:00
}