2020-12-21 00:32:34 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var alternativeOperatorScenarios = []expressionScenario{
|
2021-05-16 04:36:13 +00:00
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
expression: `(.b // "hello") as $x`,
|
|
|
|
document: `a: bridge`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (doc)::a: bridge\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",
|
|
|
|
},
|
|
|
|
},
|
2020-12-21 00:32:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAlternativeOperatorScenarios(t *testing.T) {
|
|
|
|
for _, tt := range alternativeOperatorScenarios {
|
|
|
|
testScenario(t, &tt)
|
|
|
|
}
|
|
|
|
documentScenarios(t, "Alternative (Default value)", alternativeOperatorScenarios)
|
|
|
|
}
|