yq/pkg/yqlib/printer_test.go

146 lines
2.8 KiB
Go
Raw Normal View History

2020-11-17 22:37:42 +00:00
package yqlib
import (
"bufio"
"bytes"
"strings"
"testing"
"github.com/mikefarah/yq/v4/test"
)
var multiDocSample = `a: banana
---
a: apple
---
a: coconut
`
func TestPrinterMultipleDocsInSequence(t *testing.T) {
var output bytes.Buffer
var writer = bufio.NewWriter(&output)
printer := NewPrinter(writer, false, true, false, 2, true)
2020-11-20 11:57:32 +00:00
inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0)
2020-11-17 22:37:42 +00:00
if err != nil {
panic(err)
}
el := inputs.Front()
sample1 := nodeToMap(el.Value.(*CandidateNode))
el = el.Next()
sample2 := nodeToMap(el.Value.(*CandidateNode))
el = el.Next()
sample3 := nodeToMap(el.Value.(*CandidateNode))
2020-11-19 11:11:26 +00:00
err = printer.PrintResults(sample1)
if err != nil {
panic(err)
}
err = printer.PrintResults(sample2)
if err != nil {
panic(err)
}
err = printer.PrintResults(sample3)
if err != nil {
panic(err)
}
2020-11-17 22:37:42 +00:00
writer.Flush()
test.AssertResult(t, multiDocSample, output.String())
2020-12-15 03:33:50 +00:00
}
func TestPrinterMultipleFilesInSequence(t *testing.T) {
var output bytes.Buffer
var writer = bufio.NewWriter(&output)
printer := NewPrinter(writer, false, true, false, 2, true)
2020-11-17 22:37:42 +00:00
2020-12-15 03:33:50 +00:00
inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0)
if err != nil {
panic(err)
}
el := inputs.Front()
elNode := el.Value.(*CandidateNode)
elNode.Document = 0
elNode.FileIndex = 0
sample1 := nodeToMap(elNode)
el = el.Next()
elNode = el.Value.(*CandidateNode)
elNode.Document = 0
elNode.FileIndex = 1
sample2 := nodeToMap(elNode)
el = el.Next()
elNode = el.Value.(*CandidateNode)
elNode.Document = 0
elNode.FileIndex = 2
sample3 := nodeToMap(elNode)
err = printer.PrintResults(sample1)
if err != nil {
panic(err)
}
err = printer.PrintResults(sample2)
if err != nil {
panic(err)
}
err = printer.PrintResults(sample3)
if err != nil {
panic(err)
}
writer.Flush()
test.AssertResult(t, multiDocSample, output.String())
2020-11-17 22:37:42 +00:00
}
func TestPrinterMultipleDocsInSinglePrint(t *testing.T) {
var output bytes.Buffer
var writer = bufio.NewWriter(&output)
printer := NewPrinter(writer, false, true, false, 2, true)
2020-11-20 11:57:32 +00:00
inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0)
2020-11-17 22:37:42 +00:00
if err != nil {
panic(err)
}
2020-11-19 11:11:26 +00:00
err = printer.PrintResults(inputs)
if err != nil {
panic(err)
}
2020-11-17 22:37:42 +00:00
writer.Flush()
test.AssertResult(t, multiDocSample, output.String())
}
func TestPrinterMultipleDocsJson(t *testing.T) {
var output bytes.Buffer
var writer = bufio.NewWriter(&output)
printer := NewPrinter(writer, true, true, false, 0, false)
2020-11-20 11:57:32 +00:00
inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0)
2020-11-17 22:37:42 +00:00
if err != nil {
panic(err)
}
2020-11-19 11:11:26 +00:00
err = printer.PrintResults(inputs)
if err != nil {
panic(err)
}
2020-11-17 22:37:42 +00:00
expected := `{"a":"banana"}
{"a":"apple"}
{"a":"coconut"}
`
writer.Flush()
test.AssertResult(t, expected, output.String())
}