2020-11-03 23:48:43 +00:00
|
|
|
package yqlib
|
2020-10-17 11:10:47 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var equalsOperatorScenarios = []expressionScenario{
|
2021-05-28 06:59:02 +00:00
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
expression: ".a == .b",
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!bool)::true\n",
|
|
|
|
},
|
|
|
|
},
|
2021-01-11 22:40:37 +00:00
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: "cat",
|
|
|
|
document2: "dog",
|
|
|
|
expression: "select(fi==0) == select(fi==1)",
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!bool)::false\n",
|
|
|
|
},
|
|
|
|
},
|
2021-05-16 04:36:13 +00:00
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: "{}",
|
|
|
|
expression: "(.a == .b) as $x",
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (doc)::{}\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: "{}",
|
|
|
|
expression: ".a == .b",
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!bool)::true\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: "{}",
|
|
|
|
expression: "(.a != .b) as $x",
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (doc)::{}\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: "{}",
|
|
|
|
expression: ".a != .b",
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!bool)::false\n",
|
|
|
|
},
|
|
|
|
},
|
2021-05-16 02:16:10 +00:00
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: "{a: {b: 10}}",
|
|
|
|
expression: "select(.c != null)",
|
|
|
|
expected: []string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: "{a: {b: 10}}",
|
|
|
|
expression: "select(.d == .c)",
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (doc)::{a: {b: 10}}\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: "{a: {b: 10}}",
|
|
|
|
expression: "select(null == .c)",
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (doc)::{a: {b: 10}}\n",
|
|
|
|
},
|
|
|
|
},
|
2021-02-04 22:49:40 +00:00
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: "{a: { b: {things: \"\"}, f: [1], g: [] }}",
|
|
|
|
expression: ".. | select(. == \"\")",
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[a b things], (!!str)::\"\"\n",
|
|
|
|
},
|
|
|
|
},
|
2020-10-21 01:54:58 +00:00
|
|
|
{
|
2020-11-13 09:58:01 +00:00
|
|
|
description: "Match string",
|
|
|
|
document: `[cat,goat,dog]`,
|
|
|
|
expression: `.[] | (. == "*at")`,
|
2020-10-21 01:54:58 +00:00
|
|
|
expected: []string{
|
|
|
|
"D0, P[0], (!!bool)::true\n",
|
|
|
|
"D0, P[1], (!!bool)::true\n",
|
|
|
|
"D0, P[2], (!!bool)::false\n",
|
|
|
|
},
|
2021-02-04 22:54:03 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Don't match string",
|
|
|
|
document: `[cat,goat,dog]`,
|
|
|
|
expression: `.[] | (. != "*at")`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[0], (!!bool)::false\n",
|
|
|
|
"D0, P[1], (!!bool)::false\n",
|
|
|
|
"D0, P[2], (!!bool)::true\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-11-13 09:58:01 +00:00
|
|
|
description: "Match number",
|
|
|
|
document: `[3, 4, 5]`,
|
|
|
|
expression: `.[] | (. == 4)`,
|
2020-10-21 01:54:58 +00:00
|
|
|
expected: []string{
|
|
|
|
"D0, P[0], (!!bool)::false\n",
|
|
|
|
"D0, P[1], (!!bool)::true\n",
|
|
|
|
"D0, P[2], (!!bool)::false\n",
|
|
|
|
},
|
2021-02-04 22:54:03 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Dont match number",
|
|
|
|
document: `[3, 4, 5]`,
|
|
|
|
expression: `.[] | (. != 4)`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[0], (!!bool)::true\n",
|
|
|
|
"D0, P[1], (!!bool)::false\n",
|
|
|
|
"D0, P[2], (!!bool)::true\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-11-13 09:58:01 +00:00
|
|
|
skipDoc: true,
|
2020-10-21 01:54:58 +00:00
|
|
|
document: `a: { cat: {b: apple, c: whatever}, pat: {b: banana} }`,
|
|
|
|
expression: `.a | (.[].b == "apple")`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[a cat b], (!!bool)::true\n",
|
|
|
|
"D0, P[a pat b], (!!bool)::false\n",
|
|
|
|
},
|
|
|
|
},
|
2020-10-17 11:10:47 +00:00
|
|
|
{
|
2020-11-13 09:58:01 +00:00
|
|
|
skipDoc: true,
|
2020-10-20 05:27:30 +00:00
|
|
|
document: ``,
|
|
|
|
expression: `null == null`,
|
2020-10-17 11:10:47 +00:00
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!bool)::true\n",
|
|
|
|
},
|
|
|
|
},
|
2020-10-21 01:54:58 +00:00
|
|
|
{
|
2020-11-13 09:58:01 +00:00
|
|
|
description: "Match nulls",
|
|
|
|
document: ``,
|
|
|
|
expression: `null == ~`,
|
2020-10-21 01:54:58 +00:00
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!bool)::true\n",
|
|
|
|
},
|
|
|
|
},
|
2021-05-09 02:44:05 +00:00
|
|
|
{
|
|
|
|
description: "Non exisitant key doesn't equal a value",
|
|
|
|
document: "a: frog",
|
|
|
|
expression: `select(.b != "thing")`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (doc)::a: frog\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Two non existant keys are equal",
|
|
|
|
document: "a: frog",
|
|
|
|
expression: `select(.b == .c)`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (doc)::a: frog\n",
|
|
|
|
},
|
|
|
|
},
|
2020-10-17 11:10:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEqualOperatorScenarios(t *testing.T) {
|
|
|
|
for _, tt := range equalsOperatorScenarios {
|
|
|
|
testScenario(t, &tt)
|
|
|
|
}
|
2020-11-22 02:16:54 +00:00
|
|
|
documentScenarios(t, "Equals", equalsOperatorScenarios)
|
2020-10-17 11:10:47 +00:00
|
|
|
}
|