2020-11-17 22:37:42 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2021-02-02 07:17:59 +00:00
|
|
|
"container/list"
|
2020-11-17 22:37:42 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mikefarah/yq/v4/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
var multiDocSample = `a: banana
|
|
|
|
---
|
|
|
|
a: apple
|
|
|
|
---
|
|
|
|
a: coconut
|
|
|
|
`
|
|
|
|
|
2021-07-19 10:12:04 +00:00
|
|
|
var multiDocSampleLeadingExpected = `# go cats
|
|
|
|
---
|
|
|
|
a: banana
|
|
|
|
---
|
|
|
|
a: apple
|
|
|
|
---
|
|
|
|
# cool
|
|
|
|
a: coconut
|
|
|
|
`
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
func nodeToList(candidate *CandidateNode) *list.List {
|
|
|
|
elMap := list.New()
|
|
|
|
elMap.PushBack(candidate)
|
|
|
|
return elMap
|
|
|
|
}
|
|
|
|
|
2020-11-17 22:37:42 +00:00
|
|
|
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()
|
2021-02-02 07:17:59 +00:00
|
|
|
sample1 := nodeToList(el.Value.(*CandidateNode))
|
2020-11-17 22:37:42 +00:00
|
|
|
|
|
|
|
el = el.Next()
|
2021-02-02 07:17:59 +00:00
|
|
|
sample2 := nodeToList(el.Value.(*CandidateNode))
|
2020-11-17 22:37:42 +00:00
|
|
|
|
|
|
|
el = el.Next()
|
2021-02-02 07:17:59 +00:00
|
|
|
sample3 := nodeToList(el.Value.(*CandidateNode))
|
2020-11-17 22:37:42 +00:00
|
|
|
|
2021-07-19 09:58:47 +00:00
|
|
|
err = printer.PrintResults(sample1, "")
|
2020-11-19 11:11:26 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-07-19 09:58:47 +00:00
|
|
|
err = printer.PrintResults(sample2, "")
|
2020-11-19 11:11:26 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-07-19 09:58:47 +00:00
|
|
|
err = printer.PrintResults(sample3, "")
|
2020-11-19 11:11:26 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-07-19 10:12:04 +00:00
|
|
|
func TestPrinterMultipleDocsInSequenceWithLeadingContent(t *testing.T) {
|
|
|
|
var output bytes.Buffer
|
|
|
|
var writer = bufio.NewWriter(&output)
|
|
|
|
printer := NewPrinter(writer, false, true, false, 2, true)
|
|
|
|
|
|
|
|
inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
el := inputs.Front()
|
|
|
|
sample1 := nodeToList(el.Value.(*CandidateNode))
|
|
|
|
|
|
|
|
el = el.Next()
|
|
|
|
sample2 := nodeToList(el.Value.(*CandidateNode))
|
|
|
|
|
|
|
|
el = el.Next()
|
|
|
|
sample3 := nodeToList(el.Value.(*CandidateNode))
|
|
|
|
|
|
|
|
err = printer.PrintResults(sample1, "# go cats\n---\n")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = printer.PrintResults(sample2, "---\n")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = printer.PrintResults(sample3, "---\n# cool\n")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
writer.Flush()
|
|
|
|
|
|
|
|
test.AssertResult(t, multiDocSampleLeadingExpected, 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
|
2021-02-02 07:17:59 +00:00
|
|
|
sample1 := nodeToList(elNode)
|
2020-12-15 03:33:50 +00:00
|
|
|
|
|
|
|
el = el.Next()
|
|
|
|
elNode = el.Value.(*CandidateNode)
|
|
|
|
elNode.Document = 0
|
|
|
|
elNode.FileIndex = 1
|
2021-02-02 07:17:59 +00:00
|
|
|
sample2 := nodeToList(elNode)
|
2020-12-15 03:33:50 +00:00
|
|
|
|
|
|
|
el = el.Next()
|
|
|
|
elNode = el.Value.(*CandidateNode)
|
|
|
|
elNode.Document = 0
|
|
|
|
elNode.FileIndex = 2
|
2021-02-02 07:17:59 +00:00
|
|
|
sample3 := nodeToList(elNode)
|
2020-12-15 03:33:50 +00:00
|
|
|
|
2021-07-19 09:58:47 +00:00
|
|
|
err = printer.PrintResults(sample1, "")
|
2020-12-15 03:33:50 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-07-19 09:58:47 +00:00
|
|
|
err = printer.PrintResults(sample2, "")
|
2020-12-15 03:33:50 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-07-19 09:58:47 +00:00
|
|
|
err = printer.PrintResults(sample3, "")
|
2020-12-15 03:33:50 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
writer.Flush()
|
|
|
|
test.AssertResult(t, multiDocSample, output.String())
|
2020-11-17 22:37:42 +00:00
|
|
|
}
|
|
|
|
|
2021-07-19 10:12:04 +00:00
|
|
|
func TestPrinterMultipleFilesInSequenceWithLeadingContent(t *testing.T) {
|
|
|
|
var output bytes.Buffer
|
|
|
|
var writer = bufio.NewWriter(&output)
|
|
|
|
printer := NewPrinter(writer, false, true, false, 2, true)
|
|
|
|
|
|
|
|
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 := nodeToList(elNode)
|
|
|
|
|
|
|
|
el = el.Next()
|
|
|
|
elNode = el.Value.(*CandidateNode)
|
|
|
|
elNode.Document = 0
|
|
|
|
elNode.FileIndex = 1
|
|
|
|
sample2 := nodeToList(elNode)
|
|
|
|
|
|
|
|
el = el.Next()
|
|
|
|
elNode = el.Value.(*CandidateNode)
|
|
|
|
elNode.Document = 0
|
|
|
|
elNode.FileIndex = 2
|
|
|
|
sample3 := nodeToList(elNode)
|
|
|
|
|
|
|
|
err = printer.PrintResults(sample1, "# go cats\n---\n")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = printer.PrintResults(sample2, "---\n")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = printer.PrintResults(sample3, "---\n# cool\n")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
writer.Flush()
|
|
|
|
test.AssertResult(t, multiDocSampleLeadingExpected, 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)
|
|
|
|
}
|
|
|
|
|
2021-07-19 09:58:47 +00:00
|
|
|
err = printer.PrintResults(inputs, "")
|
2020-11-19 11:11:26 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-11-17 22:37:42 +00:00
|
|
|
|
|
|
|
writer.Flush()
|
|
|
|
test.AssertResult(t, multiDocSample, output.String())
|
|
|
|
}
|
|
|
|
|
2021-07-19 10:12:04 +00:00
|
|
|
func TestPrinterMultipleDocsInSinglePrintWithLeadingDoc(t *testing.T) {
|
|
|
|
var output bytes.Buffer
|
|
|
|
var writer = bufio.NewWriter(&output)
|
|
|
|
printer := NewPrinter(writer, false, true, false, 2, true)
|
|
|
|
|
|
|
|
inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = printer.PrintResults(inputs, "# go cats\n---\n")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
writer.Flush()
|
|
|
|
expected := `# go cats
|
|
|
|
---
|
|
|
|
a: banana
|
|
|
|
---
|
|
|
|
a: apple
|
|
|
|
---
|
|
|
|
a: coconut
|
|
|
|
`
|
|
|
|
test.AssertResult(t, expected, output.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrinterMultipleDocsInSinglePrintWithLeadingDocTrailing(t *testing.T) {
|
|
|
|
var output bytes.Buffer
|
|
|
|
var writer = bufio.NewWriter(&output)
|
|
|
|
printer := NewPrinter(writer, false, true, false, 2, true)
|
|
|
|
|
|
|
|
inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = printer.PrintResults(inputs, "---\n")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
writer.Flush()
|
|
|
|
expected := `---
|
|
|
|
a: banana
|
|
|
|
---
|
|
|
|
a: apple
|
|
|
|
---
|
|
|
|
a: coconut
|
|
|
|
`
|
|
|
|
test.AssertResult(t, expected, output.String())
|
|
|
|
}
|
|
|
|
|
2021-07-19 23:18:40 +00:00
|
|
|
func TestPrinterScalarWithLeadingCont(t *testing.T) {
|
|
|
|
var output bytes.Buffer
|
|
|
|
var writer = bufio.NewWriter(&output)
|
|
|
|
printer := NewPrinter(writer, false, true, false, 2, true)
|
|
|
|
|
|
|
|
node, err := NewExpressionParser().ParseExpression(".a")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
streamEvaluator := NewStreamEvaluator()
|
|
|
|
_, err = streamEvaluator.Evaluate("sample", strings.NewReader(multiDocSample), node, printer, "# blah\n")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
writer.Flush()
|
|
|
|
expected := `banana
|
|
|
|
---
|
|
|
|
apple
|
|
|
|
---
|
|
|
|
coconut
|
|
|
|
`
|
|
|
|
test.AssertResult(t, expected, output.String())
|
|
|
|
}
|
|
|
|
|
2020-11-17 22:37:42 +00:00
|
|
|
func TestPrinterMultipleDocsJson(t *testing.T) {
|
|
|
|
var output bytes.Buffer
|
|
|
|
var writer = bufio.NewWriter(&output)
|
2021-03-19 01:40:34 +00:00
|
|
|
// note printDocSeparators is true, it should still not print document separators
|
|
|
|
// when outputing JSON.
|
|
|
|
printer := NewPrinter(writer, true, true, false, 0, true)
|
2020-11-17 22:37:42 +00:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-07-19 23:18:40 +00:00
|
|
|
err = printer.PrintResults(inputs, "# ignore this")
|
2020-11-19 11:11:26 +00:00
|
|
|
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())
|
|
|
|
}
|