yq/pkg/yqlib/operator_anchors_aliases_te...

208 lines
5.1 KiB
Go
Raw Normal View History

2020-11-03 23:48:43 +00:00
package yqlib
2020-11-02 00:20:38 +00:00
import (
"testing"
)
2021-05-09 03:26:02 +00:00
var specDocument = `- &CENTER { x: 1, y: 2 }
- &LEFT { x: 0, y: 2 }
- &BIG { r: 10 }
- &SMALL { r: 1 }
`
var expectedSpecResult = "D0, P[4], (!!map)::x: 1\ny: 2\nr: 10\n"
var anchorOperatorScenarios = []expressionScenario{
2021-05-09 03:26:02 +00:00
{
description: "Merge one map",
subdescription: "see https://yaml.org/type/merge.html",
document: specDocument + "- << : *CENTER\n r: 10\n",
expression: ".[4] | explode(.)",
expected: []string{expectedSpecResult},
},
{
description: "Merge multiple maps",
subdescription: "see https://yaml.org/type/merge.html",
document: specDocument + "- << : [ *CENTER, *BIG ]\n",
expression: ".[4] | explode(.)",
expected: []string{"D0, P[4], (!!map)::r: 10\nx: 1\ny: 2\n"},
},
{
description: "Override",
subdescription: "see https://yaml.org/type/merge.html",
document: specDocument + "- << : [ *BIG, *LEFT, *SMALL ]\n x: 1\n",
expression: ".[4] | explode(.)",
expected: []string{"D0, P[4], (!!map)::r: 10\nx: 1\ny: 2\n"},
},
{
description: "Get anchor",
document: `a: &billyBob cat`,
expression: `.a | anchor`,
expected: []string{
"D0, P[a], (!!str)::billyBob\n",
},
},
{
description: "Set anchor",
document: `a: cat`,
expression: `.a anchor = "foobar"`,
expected: []string{
"D0, P[], (doc)::a: &foobar cat\n",
},
},
2021-01-06 09:30:48 +00:00
{
description: "Set anchor relatively using assign-update",
document: `a: {b: cat}`,
expression: `.a anchor |= .b`,
expected: []string{
"D0, P[], (doc)::a: &cat {b: cat}\n",
},
},
{
skipDoc: true,
document: `a: {c: cat}`,
expression: `.a anchor |= .b`,
expected: []string{
"D0, P[], (doc)::a: {c: cat}\n",
},
},
{
skipDoc: true,
document: `a: {c: cat}`,
expression: `.a anchor = .b`,
expected: []string{
"D0, P[], (doc)::a: {c: cat}\n",
},
},
{
description: "Get alias",
document: `{b: &billyBob meow, a: *billyBob}`,
expression: `.a | alias`,
expected: []string{
"D0, P[a], (!!str)::billyBob\n",
},
},
{
description: "Set alias",
document: `{b: &meow purr, a: cat}`,
expression: `.a alias = "meow"`,
expected: []string{
"D0, P[], (doc)::{b: &meow purr, a: *meow}\n",
},
},
2021-05-16 04:18:18 +00:00
{
description: "Set alias to blank does nothing",
document: `{b: &meow purr, a: cat}`,
expression: `.a alias = ""`,
expected: []string{
"D0, P[], (doc)::{b: &meow purr, a: cat}\n",
},
},
{
skipDoc: true,
document: `{b: &meow purr, a: cat}`,
expression: `.a alias = .c`,
expected: []string{
"D0, P[], (doc)::{b: &meow purr, a: cat}\n",
},
},
{
skipDoc: true,
document: `{b: &meow purr, a: cat}`,
expression: `.a alias |= .c`,
expected: []string{
"D0, P[], (doc)::{b: &meow purr, a: cat}\n",
},
},
2021-01-06 09:30:48 +00:00
{
description: "Set alias relatively using assign-update",
document: `{b: &meow purr, a: {f: meow}}`,
expression: `.a alias |= .f`,
expected: []string{
"D0, P[], (doc)::{b: &meow purr, a: *meow}\n",
},
},
2020-11-02 00:20:38 +00:00
{
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",
2020-11-14 23:50:30 +00:00
document: `a: mike`,
2020-11-14 02:38:44 +00:00
expression: `explode(.a)`,
2020-11-02 00:20:38 +00:00
expected: []string{
2020-11-14 23:50:30 +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-14 23:50:30 +00:00
{
description: "Explode with merge anchors",
document: mergeDocSample,
expression: `explode(.)`,
expected: []string{`D0, P[], (doc)::foo:
a: foo_a
thing: foo_thing
c: foo_c
bar:
b: bar_b
thing: bar_thing
c: bar_c
foobarList:
b: bar_b
2021-05-09 03:26:02 +00:00
thing: foo_thing
2020-11-14 23:50:30 +00:00
c: foobarList_c
2021-05-09 03:26:02 +00:00
a: foo_a
2020-11-14 23:50:30 +00:00
foobar:
c: foo_c
a: foo_a
thing: foobar_thing
`},
},
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",
2021-05-09 03:26:02 +00:00
"D0, P[foobarList], (!!map)::{b: bar_b, thing: foo_thing, c: foobarList_c, a: foo_a}\n",
2020-11-02 02:43:45 +00:00
"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",
2021-05-09 03:26:02 +00:00
"D0, P[foobarList], (!!map)::{b: bar_b, thing: foo_thing, c: foobarList_c, a: foo_a}\n",
2020-11-02 02:55:03 +00:00
"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 TestAnchorAliasOperatorScenarios(t *testing.T) {
for _, tt := range anchorOperatorScenarios {
2020-11-02 00:20:38 +00:00
testScenario(t, &tt)
}
2020-12-22 23:30:13 +00:00
documentScenarios(t, "Anchor and Alias Operators", anchorOperatorScenarios)
2020-11-02 00:20:38 +00:00
}