2023-04-13 04:34:34 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mikefarah/yq/v4/test"
|
|
|
|
)
|
|
|
|
|
2023-04-13 05:40:41 +00:00
|
|
|
var yamlFormatScenarios = []formatScenario{
|
2023-10-10 03:28:53 +00:00
|
|
|
// {
|
|
|
|
// description: "basic - null",
|
|
|
|
// skipDoc: true,
|
|
|
|
// input: "null",
|
|
|
|
// expected: "null\n",
|
|
|
|
// },
|
2023-04-13 04:34:34 +00:00
|
|
|
// {
|
2023-04-13 04:44:39 +00:00
|
|
|
// description: "basic - ~",
|
2023-04-13 04:34:34 +00:00
|
|
|
// skipDoc: true,
|
2023-04-13 04:44:39 +00:00
|
|
|
// input: "~",
|
|
|
|
// expected: "~\n",
|
2023-04-13 04:34:34 +00:00
|
|
|
// },
|
2023-04-13 04:44:39 +00:00
|
|
|
{
|
|
|
|
description: "basic - [null]",
|
|
|
|
skipDoc: true,
|
|
|
|
input: "[null]",
|
|
|
|
expected: "[null]\n",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "basic - [~]",
|
|
|
|
skipDoc: true,
|
|
|
|
input: "[~]",
|
|
|
|
expected: "[~]\n",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "basic - null map value",
|
|
|
|
skipDoc: true,
|
|
|
|
input: "a: null",
|
|
|
|
expected: "a: null\n",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "basic - number",
|
|
|
|
skipDoc: true,
|
|
|
|
input: "3",
|
|
|
|
expected: "3\n",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "basic - float",
|
|
|
|
skipDoc: true,
|
|
|
|
input: "3.1",
|
|
|
|
expected: "3.1\n",
|
|
|
|
},
|
2023-04-15 04:10:12 +00:00
|
|
|
{
|
|
|
|
description: "basic - float",
|
|
|
|
skipDoc: true,
|
|
|
|
input: "[1, 2]",
|
|
|
|
expected: "[1, 2]\n",
|
|
|
|
},
|
2023-04-13 04:34:34 +00:00
|
|
|
}
|
|
|
|
|
2023-04-13 05:40:41 +00:00
|
|
|
var yamlParseScenarios = []expressionScenario{
|
|
|
|
{
|
|
|
|
document: `a: hello # things`,
|
|
|
|
expected: []string{
|
2023-06-05 22:27:59 +00:00
|
|
|
"D0, P[], (!!map)::a: hello # things\n",
|
2023-04-15 04:10:12 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
document: "a: &a apple\nb: *a",
|
2023-04-15 04:24:39 +00:00
|
|
|
expression: ".b | explode(.)",
|
2023-04-15 04:10:12 +00:00
|
|
|
expected: []string{
|
|
|
|
"D0, P[b], (!!str)::apple\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
document: `a: [1,2]`,
|
|
|
|
expected: []string{
|
2023-06-05 22:27:59 +00:00
|
|
|
"D0, P[], (!!map)::a: [1, 2]\n",
|
2023-04-13 05:40:41 +00:00
|
|
|
},
|
|
|
|
},
|
2023-04-15 05:54:44 +00:00
|
|
|
{
|
|
|
|
document: `a: !horse [a]`,
|
|
|
|
expected: []string{
|
2023-06-05 22:27:59 +00:00
|
|
|
"D0, P[], (!!map)::a: !horse [a]\n",
|
2023-04-15 05:54:44 +00:00
|
|
|
},
|
|
|
|
},
|
2023-04-13 05:40:41 +00:00
|
|
|
}
|
|
|
|
|
2023-04-13 04:34:34 +00:00
|
|
|
func testYamlScenario(t *testing.T, s formatScenario) {
|
2023-10-17 03:50:32 +00:00
|
|
|
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(NewDefaultYamlPreferences()), NewYamlEncoder(2, false, ConfiguredYamlPreferences)), s.description)
|
2023-04-13 04:34:34 +00:00
|
|
|
}
|
|
|
|
|
2023-04-13 05:40:41 +00:00
|
|
|
func TestYamlParseScenarios(t *testing.T) {
|
|
|
|
for _, tt := range yamlParseScenarios {
|
|
|
|
testScenario(t, &tt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestYamlFormatScenarios(t *testing.T) {
|
|
|
|
for _, tt := range yamlFormatScenarios {
|
2023-04-13 04:34:34 +00:00
|
|
|
testYamlScenario(t, tt)
|
|
|
|
}
|
|
|
|
}
|