2020-11-17 05:17:38 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mikefarah/yq/v4/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
var sampleYaml = `zabbix: winner
|
|
|
|
apple: great
|
|
|
|
banana:
|
|
|
|
- {cobra: kai, angus: bob}
|
|
|
|
`
|
|
|
|
|
|
|
|
var expectedJson = `{
|
|
|
|
"zabbix": "winner",
|
|
|
|
"apple": "great",
|
|
|
|
"banana": [
|
|
|
|
{
|
|
|
|
"cobra": "kai",
|
|
|
|
"angus": "bob"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
func TestJsonEncoderPreservesObjectOrder(t *testing.T) {
|
|
|
|
var output bytes.Buffer
|
|
|
|
writer := bufio.NewWriter(&output)
|
|
|
|
|
|
|
|
var jsonEncoder = NewJsonEncoder(writer, 2)
|
|
|
|
inputs, err := readDocuments(strings.NewReader(sampleYaml), "sample.yml")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
node := inputs.Front().Value.(*CandidateNode).Node
|
2020-11-19 11:11:26 +00:00
|
|
|
err = jsonEncoder.Encode(node)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-11-17 05:17:38 +00:00
|
|
|
writer.Flush()
|
|
|
|
test.AssertResult(t, expectedJson, output.String())
|
|
|
|
|
|
|
|
}
|