yq/pkg/yqlib/operator_booleans_test.go

186 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 booleanOperatorScenarios = []expressionScenario{
{
2021-05-14 05:01:44 +00:00
description: "`or` example",
2020-11-20 03:35:34 +00:00
expression: `true or false`,
2020-10-17 11:10:47 +00:00
expected: []string{
"D0, P[], (!!bool)::true\n",
},
2020-11-20 03:35:34 +00:00
},
{
skipDoc: true,
document: "b: hi",
expression: `select(.a or .b)`,
expected: []string{
"D0, P[], (doc)::b: hi\n",
},
},
{
skipDoc: true,
document: "b: hi",
expression: `select((.a and .b) | not)`,
expected: []string{
"D0, P[], (doc)::b: hi\n",
},
},
2020-11-20 03:35:34 +00:00
{
2021-05-14 05:01:44 +00:00
description: "`and` example",
2020-11-20 03:35:34 +00:00
expression: `true and false`,
expected: []string{
"D0, P[], (!!bool)::false\n",
},
},
{
document: "[{a: bird, b: dog}, {a: frog, b: bird}, {a: cat, b: fly}]",
description: "Matching nodes with select, equals and or",
2020-11-20 04:33:21 +00:00
expression: `[.[] | select(.a == "cat" or .b == "dog")]`,
2020-11-20 03:35:34 +00:00
expected: []string{
2020-11-20 04:33:21 +00:00
"D0, P[], (!!seq)::- {a: bird, b: dog}\n- {a: cat, b: fly}\n",
2020-11-20 03:35:34 +00:00
},
},
2021-05-14 04:29:55 +00:00
{
2021-05-14 05:01:44 +00:00
description: "`any` returns true if any boolean in a given array is true",
document: `[false, true]`,
expression: "any",
2021-05-14 04:29:55 +00:00
expected: []string{
"D0, P[], (!!bool)::true\n",
},
},
{
2021-05-14 05:01:44 +00:00
description: "`any` returns false for an empty array",
document: `[]`,
expression: "any",
2021-05-14 04:29:55 +00:00
expected: []string{
2021-05-14 05:01:44 +00:00
"D0, P[], (!!bool)::false\n",
2021-05-14 04:29:55 +00:00
},
},
{
2021-05-14 05:01:44 +00:00
description: "`any_c` returns true if any element in the array is true for the given condition.",
document: "a: [rad, awesome]\nb: [meh, whatever]",
expression: `.[] |= any_c(. == "awesome")`,
2021-05-14 04:29:55 +00:00
expected: []string{
2021-05-14 05:01:44 +00:00
"D0, P[], (doc)::a: true\nb: false\n",
2021-05-14 04:29:55 +00:00
},
},
{
2021-05-14 05:01:44 +00:00
skipDoc: true,
document: `[false, false]`,
2021-05-14 04:29:55 +00:00
expression: "any",
expected: []string{
"D0, P[], (!!bool)::false\n",
},
},
{
2021-05-14 05:01:44 +00:00
description: "`all` returns true if all booleans in a given array are true",
document: `[true, true]`,
expression: "all",
2021-05-14 04:29:55 +00:00
expected: []string{
"D0, P[], (!!bool)::true\n",
},
},
{
2021-05-14 05:01:44 +00:00
skipDoc: true,
document: `[false, true]`,
2021-05-14 04:29:55 +00:00
expression: "all",
expected: []string{
"D0, P[], (!!bool)::false\n",
},
},
{
2021-05-14 05:01:44 +00:00
description: "`all` returns true for an empty array",
document: `[]`,
expression: "all",
2021-05-14 04:29:55 +00:00
expected: []string{
"D0, P[], (!!bool)::true\n",
},
},
2021-05-14 05:01:44 +00:00
{
description: "`all_c` returns true if all elements in the array are true for the given condition.",
document: "a: [rad, awesome]\nb: [meh, 12]",
expression: `.[] |= all_c(tag == "!!str")`,
expected: []string{
"D0, P[], (doc)::a: true\nb: false\n",
},
},
2020-11-20 03:35:34 +00:00
{
skipDoc: true,
2020-10-17 11:10:47 +00:00
expression: `false or false`,
expected: []string{
"D0, P[], (!!bool)::false\n",
},
2020-11-20 03:35:34 +00:00
},
{
skipDoc: true,
2020-10-17 11:10:47 +00:00
document: `{a: true, b: false}`,
expression: `.[] or (false, true)`,
expected: []string{
2020-10-21 01:54:58 +00:00
"D0, P[a], (!!bool)::true\n",
2020-10-17 11:10:47 +00:00
"D0, P[a], (!!bool)::true\n",
"D0, P[b], (!!bool)::false\n",
"D0, P[b], (!!bool)::true\n",
},
},
2020-11-22 02:16:54 +00:00
{
description: "Not true is false",
expression: `true | not`,
expected: []string{
"D0, P[], (!!bool)::false\n",
},
},
{
description: "Not false is true",
expression: `false | not`,
expected: []string{
"D0, P[], (!!bool)::true\n",
},
},
{
description: "String values considered to be true",
expression: `"cat" | not`,
expected: []string{
"D0, P[], (!!bool)::false\n",
},
},
{
description: "Empty string value considered to be true",
expression: `"" | not`,
expected: []string{
"D0, P[], (!!bool)::false\n",
},
},
{
description: "Numbers are considered to be true",
expression: `1 | not`,
expected: []string{
"D0, P[], (!!bool)::false\n",
},
},
{
description: "Zero is considered to be true",
expression: `0 | not`,
expected: []string{
"D0, P[], (!!bool)::false\n",
},
},
{
description: "Null is considered to be false",
expression: `~ | not`,
expected: []string{
"D0, P[], (!!bool)::true\n",
},
},
2020-10-17 11:10:47 +00:00
}
func TestBooleanOperatorScenarios(t *testing.T) {
for _, tt := range booleanOperatorScenarios {
testScenario(t, &tt)
}
2020-11-20 04:31:49 +00:00
documentScenarios(t, "Boolean Operators", booleanOperatorScenarios)
2020-10-17 11:10:47 +00:00
}