2021-01-14 04:45:07 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2022-09-30 00:27:35 +00:00
|
|
|
var expectedIsKey = `D0, P[], (!!seq)::- p: ""
|
|
|
|
isKey: false
|
|
|
|
tag: '!!map'
|
|
|
|
- p: a
|
|
|
|
isKey: true
|
|
|
|
tag: '!!str'
|
|
|
|
- p: a
|
|
|
|
isKey: false
|
|
|
|
tag: '!!map'
|
|
|
|
- p: a.b
|
|
|
|
isKey: true
|
|
|
|
tag: '!!str'
|
|
|
|
- p: a.b
|
|
|
|
isKey: false
|
|
|
|
tag: '!!seq'
|
|
|
|
- p: a.b.0
|
|
|
|
isKey: false
|
|
|
|
tag: '!!str'
|
|
|
|
- p: a.c
|
|
|
|
isKey: true
|
|
|
|
tag: '!!str'
|
|
|
|
- p: a.c
|
|
|
|
isKey: false
|
|
|
|
tag: '!!str'
|
|
|
|
`
|
|
|
|
|
2021-01-14 04:45:07 +00:00
|
|
|
var keysOperatorScenarios = []expressionScenario{
|
2023-05-09 03:51:21 +00:00
|
|
|
{
|
|
|
|
description: "Map keys",
|
|
|
|
document: `{dog: woof, cat: meow}`,
|
|
|
|
expression: `keys`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!seq)::- dog\n- cat\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: `{}`,
|
|
|
|
expression: `keys`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!seq)::[]\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Array keys",
|
|
|
|
document: `[apple, banana]`,
|
|
|
|
expression: `keys`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!seq)::- 0\n- 1\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: `[]`,
|
|
|
|
expression: `keys`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!seq)::[]\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Retrieve array key",
|
|
|
|
document: "[1,2,3]",
|
|
|
|
expression: `.[1] | key`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[1], (!!int)::1\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Retrieve map key",
|
|
|
|
document: "a: thing",
|
|
|
|
expression: `.a | key`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[a], (!!str)::a\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "No key",
|
|
|
|
document: "{}",
|
|
|
|
expression: `key`,
|
|
|
|
expected: []string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Update map key",
|
|
|
|
document: "a:\n x: 3\n y: 4",
|
|
|
|
expression: `(.a.x | key) = "meow"`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (doc)::a:\n meow: 3\n y: 4\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Get comment from map key",
|
|
|
|
document: "a: \n # comment on key\n x: 3\n y: 4",
|
|
|
|
expression: `.a.x | key | headComment`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[a x], (!!str)::comment on key\n",
|
|
|
|
},
|
|
|
|
},
|
2022-09-30 00:27:35 +00:00
|
|
|
{
|
|
|
|
description: "Check node is a key",
|
2023-05-09 03:51:21 +00:00
|
|
|
document: "a: \n b: [cat]\n c: frog\n",
|
2022-09-30 00:27:35 +00:00
|
|
|
expression: `[... | { "p": path | join("."), "isKey": is_key, "tag": tag }]`,
|
|
|
|
expected: []string{
|
2023-05-09 03:51:21 +00:00
|
|
|
expectedIsKey,
|
2022-09-30 00:27:35 +00:00
|
|
|
},
|
|
|
|
},
|
2021-01-14 04:45:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestKeysOperatorScenarios(t *testing.T) {
|
|
|
|
for _, tt := range keysOperatorScenarios {
|
|
|
|
testScenario(t, &tt)
|
|
|
|
}
|
2021-12-21 04:02:07 +00:00
|
|
|
documentOperatorScenarios(t, "keys", keysOperatorScenarios)
|
2021-01-14 04:45:07 +00:00
|
|
|
}
|