From d0c3da12627f47dca808cc66970a3687b6ba0fc0 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Wed, 1 Dec 2021 12:37:40 +1100 Subject: [PATCH] Added encoder tests --- pkg/yqlib/encoder_csv_test.go | 47 +++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 pkg/yqlib/encoder_csv_test.go diff --git a/pkg/yqlib/encoder_csv_test.go b/pkg/yqlib/encoder_csv_test.go new file mode 100644 index 00000000..b29b1e03 --- /dev/null +++ b/pkg/yqlib/encoder_csv_test.go @@ -0,0 +1,47 @@ +package yqlib + +import ( + "bufio" + "bytes" + "strings" + "testing" + + "github.com/mikefarah/yq/v4/test" +) + +func yamlToCsv(sampleYaml string, separator rune) string { + var output bytes.Buffer + writer := bufio.NewWriter(&output) + + var jsonEncoder = NewCsvEncoder(writer, separator) + inputs, err := readDocuments(strings.NewReader(sampleYaml), "sample.yml", 0) + if err != nil { + panic(err) + } + node := inputs.Front().Value.(*CandidateNode).Node + err = jsonEncoder.Encode(node) + if err != nil { + panic(err) + } + writer.Flush() + + return strings.TrimSuffix(output.String(), "\n") +} + +var sampleYaml = `["apple", apple2, "comma, in, value", "new +line", 3, 3.40, true, "tab here"]` + +func TestCsvEncoder(t *testing.T) { + var expectedCsv = `apple,apple2,"comma, in, value",new line,3,3.40,true,tab here` + + var actualCsv = yamlToCsv(sampleYaml, ',') + test.AssertResult(t, expectedCsv, actualCsv) +} + +func TestTsvEncoder(t *testing.T) { + + var expectedCsv = `apple apple2 comma, in, value new line 3 3.40 true "tab here"` + + var actualCsv = yamlToCsv(sampleYaml, '\t') + test.AssertResult(t, expectedCsv, actualCsv) +}