2021-07-27 11:51:27 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mikefarah/yq/v4/test"
|
|
|
|
)
|
|
|
|
|
2022-06-25 02:22:03 +00:00
|
|
|
func yamlToProps(sampleYaml string, unwrapScalar bool) string {
|
2021-07-27 11:51:27 +00:00
|
|
|
var output bytes.Buffer
|
|
|
|
writer := bufio.NewWriter(&output)
|
|
|
|
|
2022-06-25 02:22:03 +00:00
|
|
|
var propsEncoder = NewPropertiesEncoder(unwrapScalar)
|
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
|
2022-01-15 00:57:59 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2022-06-25 02:22:03 +00:00
|
|
|
func TestPropertiesEncoderSimple_Unwrapped(t *testing.T) {
|
2021-07-27 11:51:27 +00:00
|
|
|
var sampleYaml = `a: 'bob cool'`
|
|
|
|
|
2022-02-07 00:55:55 +00:00
|
|
|
var expectedProps = `a = bob cool`
|
2022-06-25 02:22:03 +00:00
|
|
|
var actualProps = yamlToProps(sampleYaml, true)
|
2022-02-07 00:55:55 +00:00
|
|
|
test.AssertResult(t, expectedProps, actualProps)
|
2021-07-27 11:51:27 +00:00
|
|
|
}
|
|
|
|
|
2022-06-25 02:22:03 +00:00
|
|
|
func TestPropertiesEncoderSimple_Wrapped(t *testing.T) {
|
|
|
|
var sampleYaml = `a: 'bob cool'`
|
|
|
|
|
|
|
|
var expectedProps = `a = "bob cool"`
|
|
|
|
var actualProps = yamlToProps(sampleYaml, false)
|
|
|
|
test.AssertResult(t, expectedProps, actualProps)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPropertiesEncoderSimpleWithComments_Unwrapped(t *testing.T) {
|
2021-07-27 11:51:27 +00:00
|
|
|
var sampleYaml = `a: 'bob cool' # line`
|
|
|
|
|
2022-02-07 00:55:55 +00:00
|
|
|
var expectedProps = `# line
|
2021-07-27 11:51:27 +00:00
|
|
|
a = bob cool`
|
2022-06-25 02:22:03 +00:00
|
|
|
var actualProps = yamlToProps(sampleYaml, true)
|
|
|
|
test.AssertResult(t, expectedProps, actualProps)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPropertiesEncoderSimpleWithComments_Wrapped(t *testing.T) {
|
|
|
|
var sampleYaml = `a: 'bob cool' # line`
|
|
|
|
|
|
|
|
var expectedProps = `# line
|
|
|
|
a = "bob cool"`
|
|
|
|
var actualProps = yamlToProps(sampleYaml, false)
|
2022-02-07 00:55:55 +00:00
|
|
|
test.AssertResult(t, expectedProps, actualProps)
|
2021-07-27 11:51:27 +00:00
|
|
|
}
|
|
|
|
|
2022-06-25 02:22:03 +00:00
|
|
|
func TestPropertiesEncoderDeep_Unwrapped(t *testing.T) {
|
2021-07-27 11:51:27 +00:00
|
|
|
var sampleYaml = `a:
|
|
|
|
b: "bob cool"
|
|
|
|
`
|
|
|
|
|
2022-02-07 00:55:55 +00:00
|
|
|
var expectedProps = `a.b = bob cool`
|
2022-06-25 02:22:03 +00:00
|
|
|
var actualProps = yamlToProps(sampleYaml, true)
|
|
|
|
test.AssertResult(t, expectedProps, actualProps)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPropertiesEncoderDeep_Wrapped(t *testing.T) {
|
|
|
|
var sampleYaml = `a:
|
|
|
|
b: "bob cool"
|
|
|
|
`
|
|
|
|
|
|
|
|
var expectedProps = `a.b = "bob cool"`
|
|
|
|
var actualProps = yamlToProps(sampleYaml, false)
|
2022-02-07 00:55:55 +00:00
|
|
|
test.AssertResult(t, expectedProps, actualProps)
|
2021-07-27 11:51:27 +00:00
|
|
|
}
|
|
|
|
|
2022-06-25 02:22:03 +00:00
|
|
|
func TestPropertiesEncoderDeepWithComments_Unwrapped(t *testing.T) {
|
2021-07-27 11:51:27 +00:00
|
|
|
var sampleYaml = `a: # a thing
|
|
|
|
b: "bob cool" # b thing
|
|
|
|
`
|
|
|
|
|
2022-02-07 00:55:55 +00:00
|
|
|
var expectedProps = `# b thing
|
2021-07-27 11:51:27 +00:00
|
|
|
a.b = bob cool`
|
2022-06-25 02:22:03 +00:00
|
|
|
var actualProps = yamlToProps(sampleYaml, true)
|
|
|
|
test.AssertResult(t, expectedProps, actualProps)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPropertiesEncoderDeepWithComments_Wrapped(t *testing.T) {
|
|
|
|
var sampleYaml = `a: # a thing
|
|
|
|
b: "bob cool" # b thing
|
|
|
|
`
|
|
|
|
|
|
|
|
var expectedProps = `# b thing
|
|
|
|
a.b = "bob cool"`
|
|
|
|
var actualProps = yamlToProps(sampleYaml, false)
|
2022-02-07 00:55:55 +00:00
|
|
|
test.AssertResult(t, expectedProps, actualProps)
|
2021-07-27 11:51:27 +00:00
|
|
|
}
|
|
|
|
|
2022-06-25 02:22:03 +00:00
|
|
|
func TestPropertiesEncoderArray_Unwrapped(t *testing.T) {
|
2021-07-27 11:51:27 +00:00
|
|
|
var sampleYaml = `a:
|
|
|
|
b: [{c: dog}, {c: cat}]
|
|
|
|
`
|
|
|
|
|
2022-02-07 00:55:55 +00:00
|
|
|
var expectedProps = `a.b.0.c = dog
|
2021-07-27 11:51:27 +00:00
|
|
|
a.b.1.c = cat`
|
2022-06-25 02:22:03 +00:00
|
|
|
var actualProps = yamlToProps(sampleYaml, true)
|
|
|
|
test.AssertResult(t, expectedProps, actualProps)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPropertiesEncoderArray_Wrapped(t *testing.T) {
|
|
|
|
var sampleYaml = `a:
|
|
|
|
b: [{c: dog named jim}, {c: cat named jam}]
|
|
|
|
`
|
|
|
|
|
|
|
|
var expectedProps = `a.b.0.c = "dog named jim"
|
|
|
|
a.b.1.c = "cat named jam"`
|
|
|
|
var actualProps = yamlToProps(sampleYaml, false)
|
2022-02-07 00:55:55 +00:00
|
|
|
test.AssertResult(t, expectedProps, actualProps)
|
2021-07-27 11:51:27 +00:00
|
|
|
}
|