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
|
|
|
},
|
|
|
|
{
|
|
|
|
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
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|