mirror of
https://github.com/mikefarah/yq.git
synced 2024-11-12 13:48:06 +00:00
105 lines
2.8 KiB
Go
105 lines
2.8 KiB
Go
package yqlib
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/mikefarah/yq/v4/test"
|
|
)
|
|
|
|
var samplePropertiesYaml = `# block comments don't come through
|
|
person: # neither do comments on maps
|
|
name: Mike # comments on values appear
|
|
pets:
|
|
- cat # comments on array values appear
|
|
food: [pizza] # comments on arrays do not
|
|
emptyArray: []
|
|
emptyMap: []
|
|
`
|
|
|
|
var expectedProperties = `# comments on values appear
|
|
person.name = Mike
|
|
|
|
# comments on array values appear
|
|
person.pets.0 = cat
|
|
person.food.0 = pizza
|
|
`
|
|
|
|
var expectedPropertiesNoComments = `person.name = Mike
|
|
person.pets.0 = cat
|
|
person.food.0 = pizza
|
|
`
|
|
|
|
var expectedPropertiesWithEmptyMapsAndArrays = `# comments on values appear
|
|
person.name = Mike
|
|
|
|
# comments on array values appear
|
|
person.pets.0 = cat
|
|
person.food.0 = pizza
|
|
emptyArray =
|
|
emptyMap =
|
|
`
|
|
|
|
var propertyScenarios = []formatScenario{
|
|
{
|
|
description: "Encode properties",
|
|
subdescription: "Note that empty arrays and maps are not encoded by default.",
|
|
input: samplePropertiesYaml,
|
|
expected: expectedProperties,
|
|
},
|
|
{
|
|
description: "Encode properties: no comments",
|
|
input: samplePropertiesYaml,
|
|
expected: expectedPropertiesNoComments,
|
|
expression: `... comments = ""`,
|
|
},
|
|
{
|
|
description: "Encode properties: include empty maps and arrays",
|
|
subdescription: "Use a yq expression to set the empty maps and sequences to your desired value.",
|
|
expression: `(.. | select( (tag == "!!map" or tag =="!!seq") and length == 0)) = ""`,
|
|
input: samplePropertiesYaml,
|
|
expected: expectedPropertiesWithEmptyMapsAndArrays,
|
|
},
|
|
}
|
|
|
|
func documentPropertyScenario(t *testing.T, w *bufio.Writer, i interface{}) {
|
|
s := i.(formatScenario)
|
|
writeOrPanic(w, fmt.Sprintf("## %v\n", s.description))
|
|
|
|
if s.subdescription != "" {
|
|
writeOrPanic(w, s.subdescription)
|
|
writeOrPanic(w, "\n\n")
|
|
}
|
|
|
|
writeOrPanic(w, "Given a sample.yml file of:\n")
|
|
writeOrPanic(w, fmt.Sprintf("```yaml\n%v\n```\n", s.input))
|
|
|
|
writeOrPanic(w, "then\n")
|
|
|
|
expression := s.expression
|
|
if expression == "" {
|
|
expression = "."
|
|
}
|
|
|
|
if s.indent == 2 {
|
|
writeOrPanic(w, fmt.Sprintf("```bash\nyq -o=props '%v' sample.yml\n```\n", expression))
|
|
} else {
|
|
writeOrPanic(w, fmt.Sprintf("```bash\nyq -o=props -I=%v '%v' sample.yml\n```\n", s.indent, expression))
|
|
}
|
|
writeOrPanic(w, "will output\n")
|
|
|
|
writeOrPanic(w, fmt.Sprintf("```properties\n%v```\n\n", processFormatScenario(s, NewPropertiesEncoder())))
|
|
}
|
|
|
|
func TestPropertyScenarios(t *testing.T) {
|
|
for _, s := range propertyScenarios {
|
|
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewPropertiesEncoder()), s.description)
|
|
}
|
|
genericScenarios := make([]interface{}, len(propertyScenarios))
|
|
for i, s := range propertyScenarios {
|
|
genericScenarios[i] = s
|
|
}
|
|
documentScenarios(t, "usage", "properties", genericScenarios, documentPropertyScenario)
|
|
}
|