yq/pkg/yqlib/operator_anchors_aliases_test.go

273 lines
6.7 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 simpleArrayRef = `item_value: &item_value
2021-07-07 12:40:46 +00:00
value: true
thingOne:
name: item_1
<<: *item_value
thingTwo:
name: item_2
<<: *item_value
`
2023-06-05 22:27:59 +00:00
var expectedUpdatedArrayRef = `D0, P[], (!!map)::item_value: &item_value
2021-07-07 12:40:46 +00:00
value: true
thingOne:
name: item_1
value: false
thingTwo:
name: item_2
!!merge <<: *item_value
`
2023-06-05 22:27:59 +00:00
var explodeMergeAnchorsExpected = `D0, P[], (!!map)::foo:
2023-05-05 04:13:18 +00:00
a: foo_a
thing: foo_thing
c: foo_c
bar:
b: bar_b
thing: bar_thing
c: bar_c
foobarList:
b: bar_b
thing: foo_thing
c: foobarList_c
a: foo_a
foobar:
c: foo_c
a: foo_a
thing: foobar_thing
`
var anchorOperatorScenarios = []expressionScenario{
2023-05-05 04:13:18 +00:00
{
skipDoc: true,
description: "merge anchor not map",
document: "a: &a\n - 0\nc:\n <<: [*a]\n",
expectedError: "merge anchor only supports maps, got !!seq instead",
expression: "explode(.)",
},
{
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{
2023-06-05 22:27:59 +00:00
"D0, P[], (!!map)::a: &foobar cat\n",
2023-05-05 04:13:18 +00:00
},
},
{
description: "Set anchor relatively using assign-update",
document: `a: {b: cat}`,
expression: `.a anchor |= .b`,
expected: []string{
2023-06-05 22:27:59 +00:00
"D0, P[], (!!map)::a: &cat {b: cat}\n",
2023-05-05 04:13:18 +00:00
},
},
{
skipDoc: true,
document: `a: {c: cat}`,
expression: `.a anchor |= .b`,
expected: []string{
2023-06-05 22:27:59 +00:00
"D0, P[], (!!map)::a: {c: cat}\n",
2023-05-05 04:13:18 +00:00
},
},
{
skipDoc: true,
document: `a: {c: cat}`,
expression: `.a anchor = .b`,
expected: []string{
2023-06-05 22:27:59 +00:00
"D0, P[], (!!map)::a: {c: cat}\n",
2023-05-05 04:13:18 +00:00
},
},
{
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{
2023-06-05 22:27:59 +00:00
"D0, P[], (!!map)::{b: &meow purr, a: *meow}\n",
2023-05-05 04:13:18 +00:00
},
},
{
description: "Set alias to blank does nothing",
document: `{b: &meow purr, a: cat}`,
expression: `.a alias = ""`,
expected: []string{
2023-06-05 22:27:59 +00:00
"D0, P[], (!!map)::{b: &meow purr, a: cat}\n",
2023-05-05 04:13:18 +00:00
},
},
{
skipDoc: true,
document: `{b: &meow purr, a: cat}`,
expression: `.a alias = .c`,
expected: []string{
2023-06-05 22:27:59 +00:00
"D0, P[], (!!map)::{b: &meow purr, a: cat}\n",
2023-05-05 04:13:18 +00:00
},
},
{
skipDoc: true,
document: `{b: &meow purr, a: cat}`,
expression: `.a alias |= .c`,
expected: []string{
2023-06-05 22:27:59 +00:00
"D0, P[], (!!map)::{b: &meow purr, a: cat}\n",
2023-05-05 04:13:18 +00:00
},
},
{
description: "Set alias relatively using assign-update",
document: `{b: &meow purr, a: {f: meow}}`,
expression: `.a alias |= .f`,
expected: []string{
2023-06-05 22:27:59 +00:00
"D0, P[], (!!map)::{b: &meow purr, a: *meow}\n",
2023-05-05 04:13:18 +00:00
},
},
2023-04-15 04:24:39 +00:00
// {
2023-05-05 04:13:18 +00:00
// description: "Dont explode alias and anchor - check alias parent",
// skipDoc: true,
// document: `{a: &a [1], b: *a}`,
// expression: `.b[]`,
2023-04-15 04:24:39 +00:00
// expected: []string{
2023-05-05 04:13:18 +00:00
// "D0, P[b], (!!str)::cat\n",
2023-04-15 04:24:39 +00:00
// },
// },
2023-05-05 04:13:18 +00:00
{
description: "Explode alias and anchor - check alias parent",
skipDoc: true,
document: `{a: &a cat, b: *a}`,
expression: `explode(.) | .b`,
expected: []string{
"D0, P[b], (!!str)::cat\n",
},
},
{
description: "Explode alias and anchor - check original parent",
skipDoc: true,
document: `{a: &a cat, b: *a}`,
expression: `explode(.) | .a`,
expected: []string{
"D0, P[a], (!!str)::cat\n",
},
},
{
description: "Explode alias and anchor",
document: `{f : {a: &a cat, b: *a}}`,
expression: `explode(.f)`,
expected: []string{
2023-06-05 22:27:59 +00:00
"D0, P[], (!!map)::{f: {a: cat, b: cat}}\n",
2023-05-05 04:13:18 +00:00
},
},
{
description: "Explode with no aliases or anchors",
document: `a: mike`,
expression: `explode(.a)`,
expected: []string{
2023-06-05 22:27:59 +00:00
"D0, P[], (!!map)::a: mike\n",
2023-05-05 04:13:18 +00:00
},
},
{
description: "Explode with alias keys",
document: `{f : {a: &a cat, *a: b}}`,
expression: `explode(.f)`,
expected: []string{
2023-06-05 22:27:59 +00:00
"D0, P[], (!!map)::{f: {a: cat, cat: b}}\n",
2023-05-05 04:13:18 +00:00
},
},
{
description: "Explode with merge anchors",
document: mergeDocSample,
expression: `explode(.)`,
expected: []string{explodeMergeAnchorsExpected},
},
{
skipDoc: true,
document: mergeDocSample,
expression: `.foo* | 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, thing: foo_thing, c: foobarList_c, a: foo_a}\n",
"D0, P[foobar], (!!map)::{c: foo_c, a: foo_a, thing: foobar_thing}\n",
},
},
{
skipDoc: true,
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, thing: foo_thing, c: foobarList_c, a: foo_a}\n",
"D0, P[foobar], (!!map)::{c: foo_c, a: foo_a, thing: foobar_thing}\n",
},
},
{
skipDoc: true,
document: `{f : {a: &a cat, b: &b {foo: *a}, *a: *b}}`,
expression: `explode(.f)`,
expected: []string{
2023-06-05 22:27:59 +00:00
"D0, P[], (!!map)::{f: {a: cat, b: {foo: cat}, cat: {foo: cat}}}\n",
2023-05-05 04:13:18 +00:00
},
},
2023-04-15 05:07:23 +00:00
{
description: "Dereference and update a field",
subdescription: "Use explode with multiply to dereference an object",
document: simpleArrayRef,
expression: `.thingOne |= explode(.) * {"value": false}`,
expected: []string{expectedUpdatedArrayRef},
},
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)
}
2021-12-21 04:02:07 +00:00
documentOperatorScenarios(t, "anchor-and-alias-operators", anchorOperatorScenarios)
2020-11-02 00:20:38 +00:00
}