mirror of
https://github.com/mikefarah/yq.git
synced 2025-01-12 19:25:37 +00:00
altering encoder and decoder tests somewhat
This commit is contained in:
parent
8543161161
commit
e3655130e2
@ -15,6 +15,8 @@ type formatScenario struct {
|
|||||||
subdescription string
|
subdescription string
|
||||||
skipDoc bool
|
skipDoc bool
|
||||||
scenarioType string
|
scenarioType string
|
||||||
|
decoder Decoder
|
||||||
|
encoder Encoder
|
||||||
}
|
}
|
||||||
|
|
||||||
func processFormatScenario(s formatScenario, decoder Decoder, encoder Encoder) string {
|
func processFormatScenario(s formatScenario, decoder Decoder, encoder Encoder) string {
|
||||||
|
@ -17,7 +17,7 @@ Given a sample.yml file of:
|
|||||||
```yaml
|
```yaml
|
||||||
# block comments don't come through
|
# block comments don't come through
|
||||||
person: # neither do comments on maps
|
person: # neither do comments on maps
|
||||||
name: Mike # comments on values appear
|
name: Mike Wazowski # comments on values appear
|
||||||
pets:
|
pets:
|
||||||
- cat # comments on array values appear
|
- cat # comments on array values appear
|
||||||
food: [pizza] # comments on arrays do not
|
food: [pizza] # comments on arrays do not
|
||||||
@ -32,7 +32,36 @@ yq -o=props sample.yml
|
|||||||
will output
|
will output
|
||||||
```properties
|
```properties
|
||||||
# comments on values appear
|
# comments on values appear
|
||||||
person.name = Mike
|
person.name = Mike Wazowski
|
||||||
|
|
||||||
|
# comments on array values appear
|
||||||
|
person.pets.0 = cat
|
||||||
|
person.food.0 = pizza
|
||||||
|
```
|
||||||
|
|
||||||
|
## Encode properties: scalar encapsulation
|
||||||
|
Note that string values with blank characters in them are encapsulated with double quotes
|
||||||
|
|
||||||
|
Given a sample.yml file of:
|
||||||
|
```yaml
|
||||||
|
# block comments don't come through
|
||||||
|
person: # neither do comments on maps
|
||||||
|
name: Mike Wazowski # comments on values appear
|
||||||
|
pets:
|
||||||
|
- cat # comments on array values appear
|
||||||
|
food: [pizza] # comments on arrays do not
|
||||||
|
emptyArray: []
|
||||||
|
emptyMap: []
|
||||||
|
|
||||||
|
```
|
||||||
|
then
|
||||||
|
```bash
|
||||||
|
yq -o=props sample.yml
|
||||||
|
```
|
||||||
|
will output
|
||||||
|
```properties
|
||||||
|
# comments on values appear
|
||||||
|
person.name = "Mike Wazowski"
|
||||||
|
|
||||||
# comments on array values appear
|
# comments on array values appear
|
||||||
person.pets.0 = cat
|
person.pets.0 = cat
|
||||||
@ -44,7 +73,7 @@ Given a sample.yml file of:
|
|||||||
```yaml
|
```yaml
|
||||||
# block comments don't come through
|
# block comments don't come through
|
||||||
person: # neither do comments on maps
|
person: # neither do comments on maps
|
||||||
name: Mike # comments on values appear
|
name: Mike Wazowski # comments on values appear
|
||||||
pets:
|
pets:
|
||||||
- cat # comments on array values appear
|
- cat # comments on array values appear
|
||||||
food: [pizza] # comments on arrays do not
|
food: [pizza] # comments on arrays do not
|
||||||
@ -58,7 +87,7 @@ yq -o=props '... comments = ""' sample.yml
|
|||||||
```
|
```
|
||||||
will output
|
will output
|
||||||
```properties
|
```properties
|
||||||
person.name = Mike
|
person.name = Mike Wazowski
|
||||||
person.pets.0 = cat
|
person.pets.0 = cat
|
||||||
person.food.0 = pizza
|
person.food.0 = pizza
|
||||||
```
|
```
|
||||||
@ -70,7 +99,7 @@ Given a sample.yml file of:
|
|||||||
```yaml
|
```yaml
|
||||||
# block comments don't come through
|
# block comments don't come through
|
||||||
person: # neither do comments on maps
|
person: # neither do comments on maps
|
||||||
name: Mike # comments on values appear
|
name: Mike Wazowski # comments on values appear
|
||||||
pets:
|
pets:
|
||||||
- cat # comments on array values appear
|
- cat # comments on array values appear
|
||||||
food: [pizza] # comments on arrays do not
|
food: [pizza] # comments on arrays do not
|
||||||
@ -85,7 +114,7 @@ yq -o=props '(.. | select( (tag == "!!map" or tag =="!!seq") and length == 0)) =
|
|||||||
will output
|
will output
|
||||||
```properties
|
```properties
|
||||||
# comments on values appear
|
# comments on values appear
|
||||||
person.name = Mike
|
person.name = Mike Wazowski
|
||||||
|
|
||||||
# comments on array values appear
|
# comments on array values appear
|
||||||
person.pets.0 = cat
|
person.pets.0 = cat
|
||||||
@ -98,7 +127,7 @@ emptyMap =
|
|||||||
Given a sample.properties file of:
|
Given a sample.properties file of:
|
||||||
```properties
|
```properties
|
||||||
# comments on values appear
|
# comments on values appear
|
||||||
person.name = Mike
|
person.name = Mike Wazowski
|
||||||
|
|
||||||
# comments on array values appear
|
# comments on array values appear
|
||||||
person.pets.0 = cat
|
person.pets.0 = cat
|
||||||
@ -112,7 +141,7 @@ yq -p=props sample.properties
|
|||||||
will output
|
will output
|
||||||
```yaml
|
```yaml
|
||||||
person:
|
person:
|
||||||
name: Mike # comments on values appear
|
name: Mike Wazowski # comments on values appear
|
||||||
pets:
|
pets:
|
||||||
- cat # comments on array values appear
|
- cat # comments on array values appear
|
||||||
food:
|
food:
|
||||||
@ -123,7 +152,7 @@ person:
|
|||||||
Given a sample.properties file of:
|
Given a sample.properties file of:
|
||||||
```properties
|
```properties
|
||||||
# comments on values appear
|
# comments on values appear
|
||||||
person.name = Mike
|
person.name = Mike Wazowski
|
||||||
|
|
||||||
# comments on array values appear
|
# comments on array values appear
|
||||||
person.pets.0 = cat
|
person.pets.0 = cat
|
||||||
@ -137,7 +166,7 @@ yq -p=props -o=props '.person.pets.0 = "dog"' sample.properties
|
|||||||
will output
|
will output
|
||||||
```properties
|
```properties
|
||||||
# comments on values appear
|
# comments on values appear
|
||||||
person.name = Mike
|
person.name = Mike Wazowski
|
||||||
|
|
||||||
# comments on array values appear
|
# comments on array values appear
|
||||||
person.pets.0 = dog
|
person.pets.0 = dog
|
||||||
|
@ -8,9 +8,9 @@ import (
|
|||||||
"github.com/mikefarah/yq/v4/test"
|
"github.com/mikefarah/yq/v4/test"
|
||||||
)
|
)
|
||||||
|
|
||||||
var samplePropertiesYaml = `# block comments don't come through
|
const samplePropertiesYaml = `# block comments don't come through
|
||||||
person: # neither do comments on maps
|
person: # neither do comments on maps
|
||||||
name: Mike # comments on values appear
|
name: Mike Wazowski # comments on values appear
|
||||||
pets:
|
pets:
|
||||||
- cat # comments on array values appear
|
- cat # comments on array values appear
|
||||||
food: [pizza] # comments on arrays do not
|
food: [pizza] # comments on arrays do not
|
||||||
@ -18,37 +18,45 @@ emptyArray: []
|
|||||||
emptyMap: []
|
emptyMap: []
|
||||||
`
|
`
|
||||||
|
|
||||||
var expectedProperties = `# comments on values appear
|
const expectedPropertiesUnwrapped = `# comments on values appear
|
||||||
person.name = Mike
|
person.name = Mike Wazowski
|
||||||
|
|
||||||
# comments on array values appear
|
# comments on array values appear
|
||||||
person.pets.0 = cat
|
person.pets.0 = cat
|
||||||
person.food.0 = pizza
|
person.food.0 = pizza
|
||||||
`
|
`
|
||||||
|
|
||||||
var expectedUpdatedProperties = `# comments on values appear
|
const expectedPropertiesWrapped = `# comments on values appear
|
||||||
person.name = Mike
|
person.name = "Mike Wazowski"
|
||||||
|
|
||||||
|
# comments on array values appear
|
||||||
|
person.pets.0 = cat
|
||||||
|
person.food.0 = pizza
|
||||||
|
`
|
||||||
|
|
||||||
|
const expectedUpdatedProperties = `# comments on values appear
|
||||||
|
person.name = Mike Wazowski
|
||||||
|
|
||||||
# comments on array values appear
|
# comments on array values appear
|
||||||
person.pets.0 = dog
|
person.pets.0 = dog
|
||||||
person.food.0 = pizza
|
person.food.0 = pizza
|
||||||
`
|
`
|
||||||
|
|
||||||
var expectedDecodedYaml = `person:
|
const expectedDecodedYaml = `person:
|
||||||
name: Mike # comments on values appear
|
name: Mike Wazowski # comments on values appear
|
||||||
pets:
|
pets:
|
||||||
- cat # comments on array values appear
|
- cat # comments on array values appear
|
||||||
food:
|
food:
|
||||||
- pizza
|
- pizza
|
||||||
`
|
`
|
||||||
|
|
||||||
var expectedPropertiesNoComments = `person.name = Mike
|
const expectedPropertiesNoComments = `person.name = Mike Wazowski
|
||||||
person.pets.0 = cat
|
person.pets.0 = cat
|
||||||
person.food.0 = pizza
|
person.food.0 = pizza
|
||||||
`
|
`
|
||||||
|
|
||||||
var expectedPropertiesWithEmptyMapsAndArrays = `# comments on values appear
|
const expectedPropertiesWithEmptyMapsAndArrays = `# comments on values appear
|
||||||
person.name = Mike
|
person.name = Mike Wazowski
|
||||||
|
|
||||||
# comments on array values appear
|
# comments on array values appear
|
||||||
person.pets.0 = cat
|
person.pets.0 = cat
|
||||||
@ -62,13 +70,25 @@ var propertyScenarios = []formatScenario{
|
|||||||
description: "Encode properties",
|
description: "Encode properties",
|
||||||
subdescription: "Note that empty arrays and maps are not encoded by default.",
|
subdescription: "Note that empty arrays and maps are not encoded by default.",
|
||||||
input: samplePropertiesYaml,
|
input: samplePropertiesYaml,
|
||||||
expected: expectedProperties,
|
expected: expectedPropertiesUnwrapped,
|
||||||
|
encoder: NewPropertiesEncoder(true),
|
||||||
|
decoder: NewYamlDecoder(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "Encode properties: scalar encapsulation",
|
||||||
|
subdescription: "Note that string values with blank characters in them are encapsulated with double quotes",
|
||||||
|
input: samplePropertiesYaml,
|
||||||
|
expected: expectedPropertiesWrapped,
|
||||||
|
decoder: NewYamlDecoder(),
|
||||||
|
encoder: NewPropertiesEncoder(false),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "Encode properties: no comments",
|
description: "Encode properties: no comments",
|
||||||
input: samplePropertiesYaml,
|
input: samplePropertiesYaml,
|
||||||
expected: expectedPropertiesNoComments,
|
expected: expectedPropertiesNoComments,
|
||||||
expression: `... comments = ""`,
|
expression: `... comments = ""`,
|
||||||
|
decoder: NewYamlDecoder(),
|
||||||
|
encoder: NewPropertiesEncoder(true),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "Encode properties: include empty maps and arrays",
|
description: "Encode properties: include empty maps and arrays",
|
||||||
@ -76,12 +96,16 @@ var propertyScenarios = []formatScenario{
|
|||||||
expression: `(.. | select( (tag == "!!map" or tag =="!!seq") and length == 0)) = ""`,
|
expression: `(.. | select( (tag == "!!map" or tag =="!!seq") and length == 0)) = ""`,
|
||||||
input: samplePropertiesYaml,
|
input: samplePropertiesYaml,
|
||||||
expected: expectedPropertiesWithEmptyMapsAndArrays,
|
expected: expectedPropertiesWithEmptyMapsAndArrays,
|
||||||
|
decoder: NewYamlDecoder(),
|
||||||
|
encoder: NewPropertiesEncoder(true),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "Decode properties",
|
description: "Decode properties",
|
||||||
input: expectedProperties,
|
input: expectedPropertiesUnwrapped,
|
||||||
expected: expectedDecodedYaml,
|
expected: expectedDecodedYaml,
|
||||||
scenarioType: "decode",
|
scenarioType: "decode",
|
||||||
|
decoder: NewPropertiesDecoder(),
|
||||||
|
encoder: NewYamlEncoder(2, false, true, true),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "does not expand automatically",
|
description: "does not expand automatically",
|
||||||
@ -89,13 +113,17 @@ var propertyScenarios = []formatScenario{
|
|||||||
input: "mike = ${dontExpand} this",
|
input: "mike = ${dontExpand} this",
|
||||||
expected: "mike: ${dontExpand} this\n",
|
expected: "mike: ${dontExpand} this\n",
|
||||||
scenarioType: "decode",
|
scenarioType: "decode",
|
||||||
|
decoder: NewPropertiesDecoder(),
|
||||||
|
encoder: NewYamlEncoder(2, false, true, true),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "Roundtrip",
|
description: "Roundtrip",
|
||||||
input: expectedProperties,
|
input: expectedPropertiesUnwrapped,
|
||||||
expression: `.person.pets.0 = "dog"`,
|
expression: `.person.pets.0 = "dog"`,
|
||||||
expected: expectedUpdatedProperties,
|
expected: expectedUpdatedProperties,
|
||||||
scenarioType: "roundtrip",
|
scenarioType: "roundtrip",
|
||||||
|
decoder: NewPropertiesDecoder(),
|
||||||
|
encoder: NewPropertiesEncoder(true),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "Empty doc",
|
description: "Empty doc",
|
||||||
@ -103,6 +131,8 @@ var propertyScenarios = []formatScenario{
|
|||||||
input: "",
|
input: "",
|
||||||
expected: "",
|
expected: "",
|
||||||
scenarioType: "decode",
|
scenarioType: "decode",
|
||||||
|
decoder: NewPropertiesDecoder(),
|
||||||
|
encoder: NewYamlEncoder(2, false, true, true),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -128,7 +158,7 @@ func documentEncodePropertyScenario(w *bufio.Writer, s formatScenario) {
|
|||||||
}
|
}
|
||||||
writeOrPanic(w, "will output\n")
|
writeOrPanic(w, "will output\n")
|
||||||
|
|
||||||
writeOrPanic(w, fmt.Sprintf("```properties\n%v```\n\n", processFormatScenario(s, NewYamlDecoder(), NewPropertiesEncoder(true))))
|
writeOrPanic(w, fmt.Sprintf("```properties\n%v```\n\n", processFormatScenario(s, s.decoder, s.encoder)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func documentDecodePropertyScenario(w *bufio.Writer, s formatScenario) {
|
func documentDecodePropertyScenario(w *bufio.Writer, s formatScenario) {
|
||||||
@ -153,7 +183,7 @@ func documentDecodePropertyScenario(w *bufio.Writer, s formatScenario) {
|
|||||||
|
|
||||||
writeOrPanic(w, "will output\n")
|
writeOrPanic(w, "will output\n")
|
||||||
|
|
||||||
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", processFormatScenario(s, NewPropertiesDecoder(), NewYamlEncoder(s.indent, false, true, true))))
|
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", processFormatScenario(s, s.decoder, s.encoder)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func documentRoundTripPropertyScenario(w *bufio.Writer, s formatScenario) {
|
func documentRoundTripPropertyScenario(w *bufio.Writer, s formatScenario) {
|
||||||
@ -178,7 +208,7 @@ func documentRoundTripPropertyScenario(w *bufio.Writer, s formatScenario) {
|
|||||||
|
|
||||||
writeOrPanic(w, "will output\n")
|
writeOrPanic(w, "will output\n")
|
||||||
|
|
||||||
writeOrPanic(w, fmt.Sprintf("```properties\n%v```\n\n", processFormatScenario(s, NewPropertiesDecoder(), NewPropertiesEncoder(true))))
|
writeOrPanic(w, fmt.Sprintf("```properties\n%v```\n\n", processFormatScenario(s, s.decoder, s.encoder)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func documentPropertyScenario(t *testing.T, w *bufio.Writer, i interface{}) {
|
func documentPropertyScenario(t *testing.T, w *bufio.Writer, i interface{}) {
|
||||||
@ -199,11 +229,11 @@ func documentPropertyScenario(t *testing.T, w *bufio.Writer, i interface{}) {
|
|||||||
func TestPropertyScenarios(t *testing.T) {
|
func TestPropertyScenarios(t *testing.T) {
|
||||||
for _, s := range propertyScenarios {
|
for _, s := range propertyScenarios {
|
||||||
if s.scenarioType == "decode" {
|
if s.scenarioType == "decode" {
|
||||||
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewPropertiesDecoder(), NewYamlEncoder(2, false, true, true)), s.description)
|
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, s.decoder, s.encoder), s.description)
|
||||||
} else if s.scenarioType == "roundtrip" {
|
} else if s.scenarioType == "roundtrip" {
|
||||||
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewPropertiesDecoder(), NewPropertiesEncoder(true)), s.description)
|
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, s.decoder, s.encoder), s.description)
|
||||||
} else {
|
} else {
|
||||||
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewYamlDecoder(), NewPropertiesEncoder(true)), s.description)
|
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, s.decoder, s.encoder), s.description)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
genericScenarios := make([]interface{}, len(propertyScenarios))
|
genericScenarios := make([]interface{}, len(propertyScenarios))
|
||||||
|
Loading…
Reference in New Issue
Block a user