yq/pkg/yqlib/operator_encoder_decoder_test.go

118 lines
3.4 KiB
Go
Raw Normal View History

2021-10-22 01:00:47 +00:00
package yqlib
import (
"testing"
)
2021-10-24 00:35:40 +00:00
var prefix = "D0, P[], (doc)::a:\n cool:\n bob: dylan\n"
2021-10-22 01:37:47 +00:00
var encoderDecoderOperatorScenarios = []expressionScenario{
2021-10-22 01:00:47 +00:00
{
2021-10-24 00:35:40 +00:00
description: "Encode value as yaml string",
subdescription: "Indent defaults to 2",
document: "a:\n cool:\n bob: dylan",
expression: `.b = (.a | to_yaml)`,
2021-10-22 01:00:47 +00:00
expected: []string{
2021-10-24 00:35:40 +00:00
prefix + "b: |\n cool:\n bob: dylan\n",
},
},
{
description: "Encode value as yaml string, with custom indentation",
subdescription: "You can specify the indentation level as the first parameter.",
document: "a:\n cool:\n bob: dylan",
expression: `.b = (.a | to_yaml(8))`,
expected: []string{
prefix + "b: |\n cool:\n bob: dylan\n",
2021-10-22 01:00:47 +00:00
},
},
{
description: "Encode value as yaml string, using toyaml",
subdescription: "Does the same thing as to_yaml, matching jq naming convention.",
document: `{a: {cool: "thing"}}`,
expression: `.b = (.a | to_yaml)`,
expected: []string{
`D0, P[], (doc)::{a: {cool: "thing"}, b: "{cool: \"thing\"}\n"}
`,
},
},
{
description: "Encode value as json string",
document: `{a: {cool: "thing"}}`,
expression: `.b = (.a | to_json)`,
expected: []string{
`D0, P[], (doc)::{a: {cool: "thing"}, b: "{\n \"cool\": \"thing\"\n}\n"}
2021-10-24 00:35:40 +00:00
`,
},
},
{
description: "Encode value as json string, on one line",
subdescription: "Pass in a 0 indent to print json on a single line.",
document: `{a: {cool: "thing"}}`,
expression: `.b = (.a | to_json(0))`,
expected: []string{
`D0, P[], (doc)::{a: {cool: "thing"}, b: '{"cool":"thing"}'}
2021-10-22 01:00:47 +00:00
`,
},
},
{
description: "Encode value as props string",
document: `{a: {cool: "thing"}}`,
expression: `.b = (.a | to_props)`,
expected: []string{
`D0, P[], (doc)::{a: {cool: "thing"}, b: "cool = thing\n"}
`,
},
},
2021-10-22 01:37:47 +00:00
{
description: "Decode a yaml encoded string",
document: `a: "foo: bar"`,
expression: `.b = (.a | from_yaml)`,
expected: []string{
"D0, P[], (doc)::a: \"foo: bar\"\nb:\n foo: bar\n",
},
},
{
description: "Update a multiline encoded yaml string",
2021-10-22 01:37:47 +00:00
dontFormatInputForDoc: true,
document: "a: |\n foo: bar\n baz: dog\n",
2021-10-22 01:37:47 +00:00
expression: `.a |= (from_yaml | .foo = "cat" | to_yaml)`,
expected: []string{
"D0, P[], (doc)::a: |\n foo: cat\n baz: dog\n",
},
},
{
skipDoc: true,
dontFormatInputForDoc: true,
document: "a: |-\n foo: bar\n baz: dog\n",
expression: `.a |= (from_yaml | .foo = "cat" | to_yaml)`,
expected: []string{
"D0, P[], (doc)::a: |-\n foo: cat\n baz: dog\n",
},
},
{
description: "Update a single line encoded yaml string",
dontFormatInputForDoc: true,
document: "a: 'foo: bar'",
expression: `.a |= (from_yaml | .foo = "cat" | to_yaml)`,
expected: []string{
"D0, P[], (doc)::a: 'foo: cat'\n",
},
},
{
skipDoc: true,
dontFormatInputForDoc: true,
document: "a: \"foo: bar\"",
expression: `.a |= (from_yaml | .foo = {"a": "frog"} | to_yaml)`,
expected: []string{
"D0, P[], (doc)::a: \"foo:\\n a: frog\"\n",
2021-10-22 01:37:47 +00:00
},
},
2021-10-22 01:00:47 +00:00
}
2021-10-22 01:37:47 +00:00
func TestEncoderDecoderOperatorScenarios(t *testing.T) {
for _, tt := range encoderDecoderOperatorScenarios {
2021-10-22 01:00:47 +00:00
testScenario(t, &tt)
}
2021-11-03 02:54:09 +00:00
documentScenarios(t, "encode-decode", encoderDecoderOperatorScenarios)
2021-10-22 01:00:47 +00:00
}