2020-12-21 00:32:34 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var alternativeOperatorScenarios = []expressionScenario{
|
2021-05-28 06:59:02 +00:00
|
|
|
{
|
2021-09-02 05:36:23 +00:00
|
|
|
// to match jq - we do not use a readonly clone context on the LHS.
|
2021-05-28 06:59:02 +00:00
|
|
|
skipDoc: true,
|
|
|
|
expression: `.b // .c`,
|
|
|
|
document: `a: bridge`,
|
2021-09-02 05:36:23 +00:00
|
|
|
expected: []string{
|
2023-05-05 04:13:18 +00:00
|
|
|
"D0, P[c], (!!null)::null\n",
|
2021-09-02 05:36:23 +00:00
|
|
|
},
|
2021-05-28 06:59:02 +00:00
|
|
|
},
|
2021-05-16 04:36:13 +00:00
|
|
|
{
|
|
|
|
skipDoc: true,
|
2023-02-28 05:40:38 +00:00
|
|
|
expression: `(.b // "hello") as $x | .`,
|
2021-05-16 04:36:13 +00:00
|
|
|
document: `a: bridge`,
|
|
|
|
expected: []string{
|
2023-06-05 22:27:59 +00:00
|
|
|
"D0, P[], (!!map)::a: bridge\n",
|
2021-05-16 04:36:13 +00:00
|
|
|
},
|
|
|
|
},
|
2021-06-09 23:35:07 +00:00
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
expression: `.a // .b`,
|
|
|
|
document: `a: 2`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[a], (!!int)::2\n",
|
|
|
|
},
|
|
|
|
},
|
2020-12-21 00:32:34 +00:00
|
|
|
{
|
|
|
|
description: "LHS is defined",
|
|
|
|
expression: `.a // "hello"`,
|
|
|
|
document: `{a: bridge}`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[a], (!!str)::bridge\n",
|
|
|
|
},
|
|
|
|
},
|
2021-04-13 00:42:20 +00:00
|
|
|
{
|
|
|
|
expression: `select(tag == "seq") // "cat"`,
|
|
|
|
skipDoc: true,
|
|
|
|
document: `a: frog`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!str)::cat\n",
|
|
|
|
},
|
|
|
|
},
|
2020-12-21 00:32:34 +00:00
|
|
|
{
|
|
|
|
description: "LHS is not defined",
|
|
|
|
expression: `.a // "hello"`,
|
|
|
|
document: `{}`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!str)::hello\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "LHS is null",
|
|
|
|
expression: `.a // "hello"`,
|
|
|
|
document: `{a: ~}`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!str)::hello\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "LHS is false",
|
|
|
|
expression: `.a // "hello"`,
|
|
|
|
document: `{a: false}`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!str)::hello\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "RHS is an expression",
|
|
|
|
expression: `.a // .b`,
|
|
|
|
document: `{a: false, b: cat}`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[b], (!!str)::cat\n",
|
|
|
|
},
|
|
|
|
},
|
2020-12-21 00:42:35 +00:00
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
expression: `false // true`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!bool)::true\n",
|
|
|
|
},
|
|
|
|
},
|
2022-09-09 00:18:49 +00:00
|
|
|
{
|
|
|
|
description: "Update or create - entity exists",
|
|
|
|
subdescription: "This initialises `a` if it's not present",
|
|
|
|
expression: "(.a // (.a = 0)) += 1",
|
|
|
|
document: `a: 1`,
|
|
|
|
expected: []string{
|
2023-06-05 22:27:59 +00:00
|
|
|
"D0, P[], (!!map)::a: 2\n",
|
2022-09-09 00:18:49 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Update or create - entity does not exist",
|
|
|
|
subdescription: "This initialises `a` if it's not present",
|
|
|
|
expression: "(.a // (.a = 0)) += 1",
|
|
|
|
document: `b: camel`,
|
|
|
|
expected: []string{
|
2023-06-05 22:27:59 +00:00
|
|
|
"D0, P[], (!!map)::b: camel\na: 1\n",
|
2022-09-09 00:18:49 +00:00
|
|
|
},
|
|
|
|
},
|
2020-12-21 00:32:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAlternativeOperatorScenarios(t *testing.T) {
|
|
|
|
for _, tt := range alternativeOperatorScenarios {
|
|
|
|
testScenario(t, &tt)
|
|
|
|
}
|
2021-12-21 04:02:07 +00:00
|
|
|
documentOperatorScenarios(t, "alternative-default-value", alternativeOperatorScenarios)
|
2020-12-21 00:32:34 +00:00
|
|
|
}
|