2020-11-03 23:48:43 +00:00
package yqlib
2020-10-18 00:31:36 +00:00
import (
"testing"
)
var recursiveDescentOperatorScenarios = [ ] expressionScenario {
2020-11-25 04:01:12 +00:00
{
skipDoc : true ,
document : ` { } ` ,
expression : ` .. ` ,
expected : [ ] string {
"D0, P[], (!!map)::{}\n" ,
} ,
} ,
2020-12-28 00:24:42 +00:00
{
skipDoc : true ,
document : ` { } ` ,
expression : ` ... ` ,
expected : [ ] string {
"D0, P[], (!!map)::{}\n" ,
} ,
} ,
2020-11-25 04:01:12 +00:00
{
skipDoc : true ,
document : ` [] ` ,
expression : ` .. ` ,
expected : [ ] string {
"D0, P[], (!!seq)::[]\n" ,
} ,
} ,
2020-12-28 00:24:42 +00:00
{
skipDoc : true ,
document : ` [] ` ,
expression : ` ... ` ,
expected : [ ] string {
"D0, P[], (!!seq)::[]\n" ,
} ,
} ,
2020-10-18 00:31:36 +00:00
{
2020-11-14 23:58:47 +00:00
skipDoc : true ,
2020-10-18 00:31:36 +00:00
document : ` cat ` ,
expression : ` .. ` ,
expected : [ ] string {
"D0, P[], (!!str)::cat\n" ,
} ,
2020-10-27 05:45:16 +00:00
} ,
{
2020-11-14 23:58:47 +00:00
skipDoc : true ,
2020-12-28 00:24:42 +00:00
document : ` cat ` ,
expression : ` ... ` ,
expected : [ ] string {
"D0, P[], (!!str)::cat\n" ,
} ,
} ,
{
description : "Recurse map (values only)" ,
document : ` { a: frog} ` ,
expression : ` .. ` ,
expected : [ ] string {
"D0, P[], (!!map)::{a: frog}\n" ,
"D0, P[a], (!!str)::frog\n" ,
} ,
} ,
2021-01-08 01:11:29 +00:00
{
2021-01-11 03:38:53 +00:00
description : "Recursively find nodes with keys" ,
2021-01-10 00:27:18 +00:00
subdescription : "Note that this example has wrapped the expression in `[]` to show that there are two matches returned. You do not have to wrap in `[]` in your path expression." ,
2021-01-11 03:38:53 +00:00
document : ` { a: { name: frog, b: { name: blog, age: 12}}} ` ,
expression : ` [.. | select(has("name"))] ` ,
2021-01-08 01:11:29 +00:00
expected : [ ] string {
2021-01-12 08:36:28 +00:00
"D0, P[], (!!seq)::- {name: frog, b: {name: blog, age: 12}}\n- {name: blog, age: 12}\n" ,
2021-01-08 01:11:29 +00:00
} ,
} ,
{
description : "Recursively find nodes with values" ,
document : ` { a: { nameA: frog, b: { nameB: frog, age: 12}}} ` ,
expression : ` .. | select(. == "frog") ` ,
expected : [ ] string {
"D0, P[a nameA], (!!str)::frog\n" ,
"D0, P[a b nameB], (!!str)::frog\n" ,
} ,
} ,
2020-12-28 00:24:42 +00:00
{
description : "Recurse map (values and keys)" ,
subdescription : "Note that the map key appears in the results" ,
document : ` { a: frog} ` ,
expression : ` ... ` ,
2020-10-18 00:31:36 +00:00
expected : [ ] string {
"D0, P[], (!!map)::{a: frog}\n" ,
2020-12-28 00:24:42 +00:00
"D0, P[a], (!!str)::a\n" ,
2020-10-18 00:31:36 +00:00
"D0, P[a], (!!str)::frog\n" ,
} ,
2020-10-27 05:45:16 +00:00
} ,
{
2020-11-22 02:16:54 +00:00
skipDoc : true ,
document : ` { a: { b: apple}} ` ,
expression : ` .. ` ,
2020-10-18 00:31:36 +00:00
expected : [ ] string {
"D0, P[], (!!map)::{a: {b: apple}}\n" ,
"D0, P[a], (!!map)::{b: apple}\n" ,
"D0, P[a b], (!!str)::apple\n" ,
} ,
2020-10-27 05:45:16 +00:00
} ,
2020-12-28 00:24:42 +00:00
{
skipDoc : true ,
document : ` { a: { b: apple}} ` ,
expression : ` ... ` ,
expected : [ ] string {
"D0, P[], (!!map)::{a: {b: apple}}\n" ,
"D0, P[a], (!!str)::a\n" ,
"D0, P[a], (!!map)::{b: apple}\n" ,
"D0, P[a b], (!!str)::b\n" ,
"D0, P[a b], (!!str)::apple\n" ,
} ,
} ,
2020-10-27 05:45:16 +00:00
{
2020-11-22 02:16:54 +00:00
skipDoc : true ,
document : ` [1,2,3] ` ,
expression : ` .. ` ,
2020-10-18 00:31:36 +00:00
expected : [ ] string {
"D0, P[], (!!seq)::[1, 2, 3]\n" ,
"D0, P[0], (!!int)::1\n" ,
"D0, P[1], (!!int)::2\n" ,
"D0, P[2], (!!int)::3\n" ,
} ,
2020-10-27 05:45:16 +00:00
} ,
2020-12-28 00:24:42 +00:00
{
skipDoc : true ,
document : ` [1,2,3] ` ,
expression : ` ... ` ,
expected : [ ] string {
"D0, P[], (!!seq)::[1, 2, 3]\n" ,
"D0, P[0], (!!int)::1\n" ,
"D0, P[1], (!!int)::2\n" ,
"D0, P[2], (!!int)::3\n" ,
} ,
} ,
2020-10-27 05:45:16 +00:00
{
2020-11-22 02:16:54 +00:00
skipDoc : true ,
document : ` [ { a: cat},2,true] ` ,
expression : ` .. ` ,
2020-10-18 00:31:36 +00:00
expected : [ ] string {
"D0, P[], (!!seq)::[{a: cat}, 2, true]\n" ,
"D0, P[0], (!!map)::{a: cat}\n" ,
"D0, P[0 a], (!!str)::cat\n" ,
"D0, P[1], (!!int)::2\n" ,
"D0, P[2], (!!bool)::true\n" ,
} ,
} ,
2020-12-28 00:24:42 +00:00
{
skipDoc : true ,
document : ` [ { a: cat},2,true] ` ,
expression : ` ... ` ,
expected : [ ] string {
"D0, P[], (!!seq)::[{a: cat}, 2, true]\n" ,
"D0, P[0], (!!map)::{a: cat}\n" ,
"D0, P[0 a], (!!str)::a\n" ,
"D0, P[0 a], (!!str)::cat\n" ,
"D0, P[1], (!!int)::2\n" ,
"D0, P[2], (!!bool)::true\n" ,
} ,
} ,
2020-10-29 23:56:45 +00:00
{
2020-11-14 23:58:47 +00:00
description : "Aliases are not traversed" ,
document : ` { a: &cat { c: frog}, b: *cat} ` ,
2020-11-22 02:16:54 +00:00
expression : ` [..] ` ,
2020-10-29 23:56:45 +00:00
expected : [ ] string {
2021-01-12 08:36:28 +00:00
"D0, P[], (!!seq)::- {a: &cat {c: frog}, b: *cat}\n- &cat {c: frog}\n- frog\n- *cat\n" ,
2020-10-29 23:56:45 +00:00
} ,
} ,
2020-12-28 00:24:42 +00:00
{
skipDoc : true ,
document : ` { a: &cat { c: frog}, b: *cat} ` ,
expression : ` ... ` ,
expected : [ ] string {
"D0, P[], (!!map)::{a: &cat {c: frog}, b: *cat}\n" ,
"D0, P[a], (!!str)::a\n" ,
"D0, P[a], (!!map)::&cat {c: frog}\n" ,
"D0, P[a c], (!!str)::c\n" ,
"D0, P[a c], (!!str)::frog\n" ,
"D0, P[b], (!!str)::b\n" ,
"D0, P[b], (alias)::*cat\n" ,
} ,
} ,
2020-10-30 01:40:44 +00:00
{
2020-11-14 23:58:47 +00:00
description : "Merge docs are not traversed" ,
document : mergeDocSample ,
2020-11-22 02:16:54 +00:00
expression : ` .foobar | [..] ` ,
2020-10-30 01:40:44 +00:00
expected : [ ] string {
2021-01-12 08:36:28 +00:00
"D0, P[], (!!seq)::- c: foobar_c\n !!merge <<: *foo\n thing: foobar_thing\n- foobar_c\n- *foo\n- foobar_thing\n" ,
2020-10-30 01:40:44 +00:00
} ,
} ,
2020-12-28 00:24:42 +00:00
{
skipDoc : true ,
document : mergeDocSample ,
expression : ` .foobar | [...] ` ,
expected : [ ] string {
2021-01-12 08:36:28 +00:00
"D0, P[], (!!seq)::- c: foobar_c\n !!merge <<: *foo\n thing: foobar_thing\n- c\n- foobar_c\n- !!merge <<\n- *foo\n- thing\n- foobar_thing\n" ,
2020-12-28 00:24:42 +00:00
} ,
} ,
2020-10-30 01:40:44 +00:00
{
2020-11-14 23:58:47 +00:00
skipDoc : true ,
2020-10-30 01:40:44 +00:00
document : mergeDocSample ,
expression : ` .foobarList | .. ` ,
expected : [ ] string {
"D0, P[foobarList], (!!map)::b: foobarList_b\n!!merge <<: [*foo, *bar]\nc: foobarList_c\n" ,
"D0, P[foobarList b], (!!str)::foobarList_b\n" ,
"D0, P[foobarList <<], (!!seq)::[*foo, *bar]\n" ,
"D0, P[foobarList << 0], (alias)::*foo\n" ,
"D0, P[foobarList << 1], (alias)::*bar\n" ,
"D0, P[foobarList c], (!!str)::foobarList_c\n" ,
} ,
} ,
2020-12-28 00:24:42 +00:00
{
skipDoc : true ,
document : mergeDocSample ,
expression : ` .foobarList | ... ` ,
expected : [ ] string {
"D0, P[foobarList], (!!map)::b: foobarList_b\n!!merge <<: [*foo, *bar]\nc: foobarList_c\n" ,
"D0, P[foobarList b], (!!str)::b\n" ,
"D0, P[foobarList b], (!!str)::foobarList_b\n" ,
"D0, P[foobarList <<], (!!merge)::!!merge <<\n" ,
"D0, P[foobarList <<], (!!seq)::[*foo, *bar]\n" ,
"D0, P[foobarList << 0], (alias)::*foo\n" ,
"D0, P[foobarList << 1], (alias)::*bar\n" ,
"D0, P[foobarList c], (!!str)::c\n" ,
"D0, P[foobarList c], (!!str)::foobarList_c\n" ,
} ,
} ,
2020-10-18 00:31:36 +00:00
}
func TestRecursiveDescentOperatorScenarios ( t * testing . T ) {
for _ , tt := range recursiveDescentOperatorScenarios {
testScenario ( t , & tt )
}
2021-11-03 04:00:58 +00:00
documentScenarios ( t , "recursive-descent-glob" , recursiveDescentOperatorScenarios )
2020-10-18 00:31:36 +00:00
}