2020-11-24 00:38:39 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var hasOperatorScenarios = []expressionScenario{
|
2021-01-11 22:30:24 +00:00
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: `a: hello`,
|
|
|
|
expression: `has("a")`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!bool)::true\n",
|
|
|
|
},
|
|
|
|
},
|
2021-05-16 04:00:30 +00:00
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: `a: hello`,
|
2023-02-28 05:40:38 +00:00
|
|
|
expression: `has(.b) as $c | .`,
|
2021-05-16 04:00:30 +00:00
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (doc)::a: hello\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: `a: hello`,
|
|
|
|
expression: `has(.b)`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!bool)::false\n",
|
|
|
|
},
|
|
|
|
},
|
2020-11-24 00:38:39 +00:00
|
|
|
{
|
|
|
|
description: "Has map key",
|
|
|
|
document: `- a: "yes"
|
|
|
|
- a: ~
|
|
|
|
- a:
|
|
|
|
- b: nope
|
|
|
|
`,
|
|
|
|
expression: `.[] | has("a")`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[0], (!!bool)::true\n",
|
|
|
|
"D0, P[1], (!!bool)::true\n",
|
|
|
|
"D0, P[2], (!!bool)::true\n",
|
|
|
|
"D0, P[3], (!!bool)::false\n",
|
|
|
|
},
|
|
|
|
},
|
2021-02-26 00:31:43 +00:00
|
|
|
{
|
|
|
|
description: "Select, checking for existence of deep paths",
|
|
|
|
subdescription: "Simply pipe in parent expressions into `has`",
|
|
|
|
document: "- {a: {b: {c: cat}}}\n- {a: {b: {d: dog}}}",
|
|
|
|
expression: `.[] | select(.a.b | has("c"))`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[0], (!!map)::{a: {b: {c: cat}}}\n",
|
|
|
|
},
|
|
|
|
},
|
2020-11-24 00:38:39 +00:00
|
|
|
{
|
|
|
|
dontFormatInputForDoc: true,
|
|
|
|
description: "Has array index",
|
|
|
|
document: `- []
|
|
|
|
- [1]
|
|
|
|
- [1, 2]
|
|
|
|
- [1, null]
|
|
|
|
- [1, 2, 3]
|
|
|
|
`,
|
|
|
|
expression: `.[] | has(1)`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[0], (!!bool)::false\n",
|
|
|
|
"D0, P[1], (!!bool)::false\n",
|
|
|
|
"D0, P[2], (!!bool)::true\n",
|
|
|
|
"D0, P[3], (!!bool)::true\n",
|
|
|
|
"D0, P[4], (!!bool)::true\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHasOperatorScenarios(t *testing.T) {
|
|
|
|
for _, tt := range hasOperatorScenarios {
|
|
|
|
testScenario(t, &tt)
|
|
|
|
}
|
2021-12-21 04:02:07 +00:00
|
|
|
documentOperatorScenarios(t, "has", hasOperatorScenarios)
|
2020-11-24 00:38:39 +00:00
|
|
|
}
|