yq/pkg/yqlib/operator_booleans_test.go

178 lines
3.6 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{
{
2020-11-20 03:35:34 +00:00
description: "OR example",
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
{
description: "AND example",
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
{
description: "ANY returns true if any boolean in a given array is true",
document: `[false, true]`,
expression: "any",
expected: []string{
"D0, P[], (!!bool)::true\n",
},
},
{
description: "ANY returns true if any boolean in a given array is true",
document: `[false, true]`,
expression: "any",
expected: []string{
"D0, P[], (!!bool)::true\n",
},
},
{
description: "ANY returns false for an empty array",
document: `[]`,
expression: "any",
expected: []string{
"D0, P[], (!!bool)::false\n",
},
},
{
skipDoc: true,
document: `[false, false]`,
expression: "any",
expected: []string{
"D0, P[], (!!bool)::false\n",
},
},
{
description: "ALL returns true if all booleans in a given array are true",
document: `[true, true]`,
expression: "all",
expected: []string{
"D0, P[], (!!bool)::true\n",
},
},
{
skipDoc: true,
document: `[false, true]`,
expression: "all",
expected: []string{
"D0, P[], (!!bool)::false\n",
},
},
{
description: "ANY returns true for an empty array",
document: `[]`,
expression: "all",
expected: []string{
"D0, P[], (!!bool)::true\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
}