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
} ,
2023-05-23 05:35:39 +00:00
{
description : "\"yes\" and \"no\" are strings" ,
subdescription : "In the yaml 1.2 standard, support for yes/no as booleans was dropped - they are now considered strings. See '10.2.1.2. Boolean' in https://yaml.org/spec/1.2.2/" ,
document : ` [yes, no] ` ,
expression : ` .[] | tag ` ,
expected : [ ] string {
"D0, P[0], (!!str)::!!str\n" ,
"D0, P[1], (!!str)::!!str\n" ,
} ,
} ,
2021-05-28 06:59:02 +00:00
{
skipDoc : true ,
document : "b: hi" ,
expression : ` .a or .c ` ,
expected : [ ] string {
"D0, P[], (!!bool)::false\n" ,
} ,
} ,
2021-06-09 23:53:50 +00:00
{
skipDoc : true ,
document : "b: false" ,
expression : ` .b or .c ` ,
expected : [ ] string {
"D0, P[b], (!!bool)::false\n" ,
} ,
} ,
2021-05-09 02:44:05 +00:00
{
skipDoc : true ,
document : "b: hi" ,
expression : ` select(.a or .b) ` ,
expected : [ ] string {
2023-10-18 01:11:53 +00:00
"D0, P[], (!!map)::b: hi\n" ,
2021-05-09 02:44:05 +00:00
} ,
} ,
{
skipDoc : true ,
document : "b: hi" ,
expression : ` select((.a and .b) | not) ` ,
expected : [ ] string {
2023-10-18 01:11:53 +00:00
"D0, P[], (!!map)::b: hi\n" ,
2021-05-09 02:44:05 +00:00
} ,
} ,
2022-03-20 01:55:58 +00:00
{
skipDoc : true ,
description : "And should not run 2nd arg if first is false" ,
expression : ` false and test(3) ` ,
expected : [ ] string {
"D0, P[], (!!bool)::false\n" ,
} ,
} ,
{
skipDoc : true ,
description : "Or should not run 2nd arg if first is true" ,
expression : ` true or test(3) ` ,
expected : [ ] string {
"D0, P[], (!!bool)::true\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 {
2023-10-18 01:11:53 +00:00
"D0, P[], (!!map)::a: true\nb: false\n" ,
2021-05-14 04:29:55 +00:00
} ,
} ,
2021-05-16 04:00:30 +00:00
{
skipDoc : true ,
document : ` [ { pet: cat}] ` ,
2023-02-28 05:40:38 +00:00
expression : ` any_c(.name == "harry") as $c | . ` ,
2021-05-16 04:00:30 +00:00
expected : [ ] string {
2023-10-18 01:11:53 +00:00
"D0, P[], (!!seq)::[{pet: cat}]\n" ,
2021-05-16 04:00:30 +00:00
} ,
} ,
{
skipDoc : true ,
document : ` [ { pet: cat}] ` ,
2023-02-28 05:40:38 +00:00
expression : ` any_c(.name == "harry") as $c | $c ` ,
2021-05-16 04:00:30 +00:00
expected : [ ] string {
2023-02-28 05:40:38 +00:00
"D0, P[], (!!bool)::false\n" ,
} ,
} ,
{
skipDoc : true ,
document : ` [ { pet: cat}] ` ,
expression : ` all_c(.name == "harry") as $c | $c ` ,
expected : [ ] string {
"D0, P[], (!!bool)::false\n" ,
2021-05-16 04:00:30 +00:00
} ,
} ,
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 {
2023-10-18 01:11:53 +00:00
"D0, P[], (!!map)::a: true\nb: false\n" ,
2021-05-14 05:01:44 +00:00
} ,
} ,
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 {
"D0, P[a], (!!bool)::true\n" ,
"D0, P[b], (!!bool)::false\n" ,
"D0, P[b], (!!bool)::true\n" ,
} ,
} ,
2022-03-20 01:55:58 +00:00
{
skipDoc : true ,
document : ` { a: true, b: false} ` ,
expression : ` .[] and (false, true) ` ,
expected : [ ] string {
"D0, P[a], (!!bool)::false\n" ,
"D0, P[a], (!!bool)::true\n" ,
"D0, P[b], (!!bool)::false\n" ,
} ,
} ,
2021-05-16 04:17:13 +00:00
{
skipDoc : true ,
document : ` { } ` ,
2023-02-28 05:40:38 +00:00
expression : ` (.a.b or .c) as $x | . ` ,
2021-05-16 04:17:13 +00:00
expected : [ ] string {
2023-10-18 01:11:53 +00:00
"D0, P[], (!!map)::{}\n" ,
2021-05-16 04:17:13 +00:00
} ,
} ,
{
skipDoc : true ,
document : ` { } ` ,
2023-02-28 05:40:38 +00:00
expression : ` (.a.b and .c) as $x | . ` ,
2021-05-16 04:17:13 +00:00
expected : [ ] string {
2023-10-18 01:11:53 +00:00
"D0, P[], (!!map)::{}\n" ,
2021-05-16 04:17:13 +00:00
} ,
} ,
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 )
}
2021-12-21 04:02:07 +00:00
documentOperatorScenarios ( t , "boolean-operators" , booleanOperatorScenarios )
2020-10-17 11:10:47 +00:00
}