yq/pkg/yqlib/encoder_properties_test.go

79 lines
1.7 KiB
Go
Raw Normal View History

2021-07-27 11:51:27 +00:00
package yqlib
import (
"bufio"
"bytes"
"strings"
"testing"
"github.com/mikefarah/yq/v4/test"
)
func yamlToProps(sampleYaml string) string {
var output bytes.Buffer
writer := bufio.NewWriter(&output)
var propsEncoder = NewPropertiesEncoder()
2021-12-21 04:02:07 +00:00
inputs, err := readDocuments(strings.NewReader(sampleYaml), "sample.yml", 0, NewYamlDecoder())
2021-07-27 11:51:27 +00:00
if err != nil {
panic(err)
}
node := inputs.Front().Value.(*CandidateNode).Node
err = propsEncoder.Encode(writer, node)
2021-07-27 11:51:27 +00:00
if err != nil {
panic(err)
}
writer.Flush()
return strings.TrimSuffix(output.String(), "\n")
}
func TestPropertiesEncoderSimple(t *testing.T) {
var sampleYaml = `a: 'bob cool'`
var expectedProps = `a = bob cool`
2021-07-27 11:51:27 +00:00
var actualProps = yamlToProps(sampleYaml)
test.AssertResult(t, expectedProps, actualProps)
2021-07-27 11:51:27 +00:00
}
func TestPropertiesEncoderSimpleWithComments(t *testing.T) {
var sampleYaml = `a: 'bob cool' # line`
var expectedProps = `# line
2021-07-27 11:51:27 +00:00
a = bob cool`
var actualProps = yamlToProps(sampleYaml)
test.AssertResult(t, expectedProps, actualProps)
2021-07-27 11:51:27 +00:00
}
func TestPropertiesEncoderDeep(t *testing.T) {
var sampleYaml = `a:
b: "bob cool"
`
var expectedProps = `a.b = bob cool`
2021-07-27 11:51:27 +00:00
var actualProps = yamlToProps(sampleYaml)
test.AssertResult(t, expectedProps, actualProps)
2021-07-27 11:51:27 +00:00
}
func TestPropertiesEncoderDeepWithComments(t *testing.T) {
var sampleYaml = `a: # a thing
b: "bob cool" # b thing
`
var expectedProps = `# b thing
2021-07-27 11:51:27 +00:00
a.b = bob cool`
var actualProps = yamlToProps(sampleYaml)
test.AssertResult(t, expectedProps, actualProps)
2021-07-27 11:51:27 +00:00
}
func TestPropertiesEncoderArray(t *testing.T) {
var sampleYaml = `a:
b: [{c: dog}, {c: cat}]
`
var expectedProps = `a.b.0.c = dog
2021-07-27 11:51:27 +00:00
a.b.1.c = cat`
var actualProps = yamlToProps(sampleYaml)
test.AssertResult(t, expectedProps, actualProps)
2021-07-27 11:51:27 +00:00
}