Added encoder tests

This commit is contained in:
Mike Farah
2021-12-02 20:06:51 +11:00
parent f62cfe5ec9
commit df5128fa36
7 changed files with 320 additions and 96 deletions
+90 -28
View File
@@ -7,6 +7,70 @@ import (
var prefix = "D0, P[], (doc)::a:\n cool:\n bob: dylan\n"
var encoderDecoderOperatorScenarios = []expressionScenario{
{
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"}
`,
},
},
{
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"}'}
`,
},
},
{
description: "Encode value as json string, on one line shorthand",
subdescription: "Pass in a 0 indent to print json on a single line.",
document: `{a: {cool: "thing"}}`,
expression: `.b = (.a | @json)`,
expected: []string{
`D0, P[], (doc)::{a: {cool: "thing"}, b: '{"cool":"thing"}'}
`,
},
},
{
description: "Decode a json encoded string",
subdescription: "Keep in mind JSON is a subset of YAML. If you want idiomatic yaml, pipe through the style operator to clear out the JSON styling.",
document: `a: '{"cool":"thing"}'`,
expression: `.a | from_json | ... style=""`,
expected: []string{
"D0, P[a], (!!map)::cool: thing\n",
},
},
{
skipDoc: true,
document: `{a: {cool: "thing"}}`,
expression: `.b = (.a | to_props)`,
expected: []string{
`D0, P[], (doc)::{a: {cool: "thing"}, b: "cool = thing\n"}
`,
},
},
{
description: "Encode value as props string",
document: `{a: {cool: "thing"}}`,
expression: `.b = (.a | @props)`,
expected: []string{
`D0, P[], (doc)::{a: {cool: "thing"}, b: "cool = thing\n"}
`,
},
},
{
skipDoc: true,
document: "a:\n cool:\n bob: dylan",
expression: `.b = (.a | @yaml)`,
expected: []string{
prefix + "b: |\n cool:\n bob: dylan\n",
},
},
{
description: "Encode value as yaml string",
subdescription: "Indent defaults to 2",
@@ -32,34 +96,6 @@ var encoderDecoderOperatorScenarios = []expressionScenario{
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"}
`,
},
},
{
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"}'}
`,
},
},
{
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"}
`,
},
},
@@ -98,6 +134,32 @@ var encoderDecoderOperatorScenarios = []expressionScenario{
"D0, P[], (doc)::a: 'foo: cat'\n",
},
},
{
description: "Encode array of scalars as csv string",
subdescription: "Scalars are strings, numbers and booleans.",
document: `[cat, "thing1,thing2", true, 3.40]`,
expression: `@csv`,
expected: []string{
"D0, P[], (!!str)::cat,\"thing1,thing2\",true,3.40\n",
},
},
{
description: "Encode array of arrays as csv string",
document: `[[cat, "thing1,thing2", true, 3.40], [dog, thing3, false, 12]]`,
expression: `@csv`,
expected: []string{
"D0, P[], (!!str)::cat,\"thing1,thing2\",true,3.40\ndog,thing3,false,12\n",
},
},
{
description: "Encode array of array scalars as tsv string",
subdescription: "Scalars are strings, numbers and booleans.",
document: `[[cat, "thing1,thing2", true, 3.40], [dog, thing3, false, 12]]`,
expression: `@tsv`,
expected: []string{
"D0, P[], (!!str)::cat\tthing1,thing2\ttrue\t3.40\ndog\tthing3\tfalse\t12\n",
},
},
{
skipDoc: true,
dontFormatInputForDoc: true,