yq/pkg/yqlib/operator_style_test.go

178 lines
4.8 KiB
Go
Raw Normal View History

2020-11-18 09:42:37 +00:00
package yqlib
import (
"testing"
)
var styleOperatorScenarios = []expressionScenario{
2021-04-26 04:17:52 +00:00
{
description: "Update and set style of a particular node (simple)",
document: `a: {b: thing, c: something}`,
expression: `.a.b = "new" | .a.b style="double"`,
expected: []string{
"D0, P[], (!!map)::a: {b: \"new\", c: something}\n",
2021-04-26 04:17:52 +00:00
},
},
{
2021-09-12 11:52:02 +00:00
description: "Update and set style of a particular node using path variables",
document: `a: {b: thing, c: something}`,
expression: `with(.a.b ; . = "new" | . style="double")`,
2021-04-26 04:17:52 +00:00
expected: []string{
"D0, P[], (!!map)::a: {b: \"new\", c: something}\n",
2021-04-26 04:17:52 +00:00
},
},
2020-11-18 09:42:37 +00:00
{
description: "Set tagged style",
2024-02-08 23:45:49 +00:00
document: `{a: cat, b: 5, c: 3.2, e: true, f: [1,2,3], g: { something: cool}}`,
2020-11-18 09:42:37 +00:00
expression: `.. style="tagged"`,
expected: []string{
2024-02-08 23:45:49 +00:00
"D0, P[], (!!map)::!!map\na: !!str cat\nb: !!int 5\nc: !!float 3.2\ne: !!bool true\nf: !!seq\n - !!int 1\n - !!int 2\n - !!int 3\ng: !!map\n something: !!str cool\n",
2020-11-18 09:42:37 +00:00
},
},
{
description: "Set double quote style",
2024-02-08 23:45:49 +00:00
document: `{a: cat, b: 5, c: 3.2, e: true, f: [1,2,3], g: { something: cool}}`,
2020-11-18 09:42:37 +00:00
expression: `.. style="double"`,
expected: []string{
2024-02-08 23:45:49 +00:00
"D0, P[], (!!map)::a: \"cat\"\nb: \"5\"\nc: \"3.2\"\ne: \"true\"\nf:\n - \"1\"\n - \"2\"\n - \"3\"\ng:\n something: \"cool\"\n",
2020-11-18 09:42:37 +00:00
},
},
2020-12-28 00:24:42 +00:00
{
description: "Set double quote style on map keys too",
2024-02-08 23:45:49 +00:00
document: `{a: cat, b: 5, c: 3.2, e: true, f: [1,2,3], g: { something: cool}}`,
2020-12-28 00:24:42 +00:00
expression: `... style="double"`,
expected: []string{
2024-02-08 23:45:49 +00:00
"D0, P[], (!!map)::\"a\": \"cat\"\n\"b\": \"5\"\n\"c\": \"3.2\"\n\"e\": \"true\"\n\"f\":\n - \"1\"\n - \"2\"\n - \"3\"\n\"g\":\n \"something\": \"cool\"\n",
2020-12-28 00:24:42 +00:00
},
},
{
skipDoc: true,
document: "bing: &foo frog\na:\n c: cat\n <<: [*foo]",
expression: `(... | select(tag=="!!str")) style="single"`,
expected: []string{
"D0, P[], (!!map)::'bing': &foo 'frog'\n'a':\n 'c': 'cat'\n !!merge <<: [*foo]\n",
},
},
2020-11-18 09:42:37 +00:00
{
description: "Set single quote style",
2024-02-08 23:45:49 +00:00
document: `{a: cat, b: 5, c: 3.2, e: true, f: [1,2,3], g: { something: cool}}`,
2020-11-18 09:42:37 +00:00
expression: `.. style="single"`,
expected: []string{
2024-02-08 23:45:49 +00:00
"D0, P[], (!!map)::a: 'cat'\nb: '5'\nc: '3.2'\ne: 'true'\nf:\n - '1'\n - '2'\n - '3'\ng:\n something: 'cool'\n",
2020-11-18 09:42:37 +00:00
},
},
{
description: "Set literal quote style",
2024-02-08 23:45:49 +00:00
document: `{a: cat, b: 5, c: 3.2, e: true, f: [1,2,3], g: { something: cool}}`,
2020-11-18 09:42:37 +00:00
expression: `.. style="literal"`,
expected: []string{
2020-11-19 05:45:05 +00:00
`D0, P[], (!!map)::a: |-
cat
b: |-
5
c: |-
3.2
e: |-
true
2024-02-08 23:45:49 +00:00
f:
- |-
1
- |-
2
- |-
3
g:
something: |-
cool
2020-11-19 05:45:05 +00:00
`,
2020-11-18 09:42:37 +00:00
},
},
{
description: "Set folded quote style",
2024-02-08 23:45:49 +00:00
document: `{a: cat, b: 5, c: 3.2, e: true, f: [1,2,3], g: { something: cool}}`,
2020-11-18 09:42:37 +00:00
expression: `.. style="folded"`,
expected: []string{
2020-11-19 05:45:05 +00:00
`D0, P[], (!!map)::a: >-
cat
b: >-
5
c: >-
3.2
e: >-
true
2024-02-08 23:45:49 +00:00
f:
- >-
1
- >-
2
- >-
3
g:
something: >-
cool
2020-11-19 05:45:05 +00:00
`,
2020-11-18 09:42:37 +00:00
},
},
{
description: "Set flow quote style",
2024-02-08 23:45:49 +00:00
document: `{a: cat, b: 5, c: 3.2, e: true, f: [1,2,3], g: { something: cool}}`,
2020-11-18 09:42:37 +00:00
expression: `.. style="flow"`,
expected: []string{
2024-02-08 23:45:49 +00:00
"D0, P[], (!!map)::{a: cat, b: 5, c: 3.2, e: true, f: [1, 2, 3], g: {something: cool}}\n",
2020-11-18 09:42:37 +00:00
},
},
{
2024-02-08 23:45:49 +00:00
description: "Reset style - or pretty print",
subdescription: "Set empty (default) quote style, note the usage of `...` to match keys too. Note that there is a `--prettyPrint/-P` short flag for this.",
dontFormatInputForDoc: true,
document: `{a: cat, "b": 5, 'c': 3.2, "e": true, f: [1,2,3], "g": { something: "cool"} }`,
expression: `... style=""`,
2020-11-18 09:42:37 +00:00
expected: []string{
2024-02-08 23:45:49 +00:00
"D0, P[], (!!map)::a: cat\nb: 5\nc: 3.2\ne: true\nf:\n - 1\n - 2\n - 3\ng:\n something: cool\n",
2020-11-18 09:42:37 +00:00
},
},
2021-01-06 09:37:53 +00:00
{
description: "Set style relatively with assign-update",
document: `{a: single, b: double}`,
expression: `.[] style |= .`,
expected: []string{
"D0, P[], (!!map)::{a: 'single', b: \"double\"}\n",
2021-01-06 09:37:53 +00:00
},
},
2020-11-18 09:42:37 +00:00
{
skipDoc: true,
document: `{a: cat, b: double}`,
expression: `.a style=.b`,
expected: []string{
"D0, P[], (!!map)::{a: \"cat\", b: double}\n",
2020-11-18 09:42:37 +00:00
},
},
{
2020-11-22 02:16:54 +00:00
description: "Read style",
document: `{a: "cat", b: 'thing'}`,
dontFormatInputForDoc: true,
expression: `.. | style`,
2020-11-18 09:42:37 +00:00
expected: []string{
"D0, P[], (!!str)::flow\n",
"D0, P[a], (!!str)::double\n",
"D0, P[b], (!!str)::single\n",
},
},
{
skipDoc: true,
document: `a: cat`,
expression: `.. | style`,
expected: []string{
2021-11-13 23:51:18 +00:00
"D0, P[], (!!str)::\n",
"D0, P[a], (!!str)::\n",
2020-11-18 09:42:37 +00:00
},
},
}
func TestStyleOperatorScenarios(t *testing.T) {
for _, tt := range styleOperatorScenarios {
testScenario(t, &tt)
}
2021-12-21 04:02:07 +00:00
documentOperatorScenarios(t, "style", styleOperatorScenarios)
2020-11-18 09:42:37 +00:00
}