2020-11-03 23:48:43 +00:00
|
|
|
package yqlib
|
2020-11-02 00:20:38 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var explodeTest = []expressionScenario{
|
|
|
|
{
|
2020-11-14 02:38:44 +00:00
|
|
|
description: "Explode alias and anchor",
|
|
|
|
document: `{f : {a: &a cat, b: *a}}`,
|
|
|
|
expression: `explode(.f)`,
|
2020-11-02 00:20:38 +00:00
|
|
|
expected: []string{
|
2020-11-14 02:38:44 +00:00
|
|
|
"D0, P[], (doc)::{f: {a: cat, b: cat}}\n",
|
2020-11-02 00:20:38 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-11-14 02:38:44 +00:00
|
|
|
description: "Explode with no aliases or anchors",
|
|
|
|
document: `{a: mike}`,
|
|
|
|
expression: `explode(.a)`,
|
2020-11-02 00:20:38 +00:00
|
|
|
expected: []string{
|
2020-11-14 02:38:44 +00:00
|
|
|
"D0, P[], (doc)::{a: mike}\n",
|
2020-11-02 00:20:38 +00:00
|
|
|
},
|
|
|
|
},
|
2020-11-02 02:43:45 +00:00
|
|
|
{
|
2020-11-14 02:38:44 +00:00
|
|
|
description: "Explode with alias keys",
|
|
|
|
document: `{f : {a: &a cat, *a: b}}`,
|
|
|
|
expression: `explode(.f)`,
|
2020-11-02 02:43:45 +00:00
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (doc)::{f: {a: cat, cat: b}}\n",
|
|
|
|
},
|
|
|
|
},
|
2020-11-02 00:20:38 +00:00
|
|
|
{
|
2020-11-14 02:38:44 +00:00
|
|
|
skipDoc: true,
|
2020-11-02 00:20:38 +00:00
|
|
|
document: mergeDocSample,
|
2020-11-02 02:43:45 +00:00
|
|
|
expression: `.foo* | explode(.) | (. style="flow")`,
|
2020-11-02 00:20:38 +00:00
|
|
|
expected: []string{
|
2020-11-02 02:43:45 +00:00
|
|
|
"D0, P[foo], (!!map)::{a: foo_a, thing: foo_thing, c: foo_c}\n",
|
|
|
|
"D0, P[foobarList], (!!map)::{b: bar_b, a: foo_a, thing: bar_thing, c: foobarList_c}\n",
|
|
|
|
"D0, P[foobar], (!!map)::{c: foo_c, a: foo_a, thing: foobar_thing}\n",
|
|
|
|
},
|
|
|
|
},
|
2020-11-02 02:55:03 +00:00
|
|
|
{
|
2020-11-14 02:38:44 +00:00
|
|
|
skipDoc: true,
|
2020-11-02 02:55:03 +00:00
|
|
|
document: mergeDocSample,
|
|
|
|
expression: `.foo* | explode(explode(.)) | (. style="flow")`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[foo], (!!map)::{a: foo_a, thing: foo_thing, c: foo_c}\n",
|
|
|
|
"D0, P[foobarList], (!!map)::{b: bar_b, a: foo_a, thing: bar_thing, c: foobarList_c}\n",
|
|
|
|
"D0, P[foobar], (!!map)::{c: foo_c, a: foo_a, thing: foobar_thing}\n",
|
|
|
|
},
|
|
|
|
},
|
2020-11-02 02:43:45 +00:00
|
|
|
{
|
2020-11-14 02:38:44 +00:00
|
|
|
skipDoc: true,
|
2020-11-02 02:43:45 +00:00
|
|
|
document: `{f : {a: &a cat, b: &b {f: *a}, *a: *b}}`,
|
|
|
|
expression: `explode(.f)`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (doc)::{f: {a: cat, b: {f: cat}, cat: {f: cat}}}\n",
|
2020-11-02 00:20:38 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExplodeOperatorScenarios(t *testing.T) {
|
|
|
|
for _, tt := range explodeTest {
|
|
|
|
testScenario(t, &tt)
|
|
|
|
}
|
2020-11-14 02:38:44 +00:00
|
|
|
documentScenarios(t, "Explode Operator", explodeTest)
|
2020-11-02 00:20:38 +00:00
|
|
|
}
|