From da9edc9e3137ea107caa2c40d3b1e953959f59f0 Mon Sep 17 00:00:00 2001 From: therealplato Date: Wed, 2 May 2018 18:52:24 +1200 Subject: [PATCH] read: test reading specific document of multidocument --- .gitignore | 1 + examples/multidocument.yaml | 9 +++++++++ utils_test.go | 6 ++++++ yq_test.go | 17 +++++++++++++++++ 4 files changed, 33 insertions(+) create mode 100644 examples/multidocument.yaml diff --git a/.gitignore b/.gitignore index 06ed739b..2a9644ab 100644 --- a/.gitignore +++ b/.gitignore @@ -36,3 +36,4 @@ parts/ prime/ .snapcraft/ yq*.snap +*.swp diff --git a/examples/multidocument.yaml b/examples/multidocument.yaml new file mode 100644 index 00000000..cbcf509e --- /dev/null +++ b/examples/multidocument.yaml @@ -0,0 +1,9 @@ +--- +a: Document One +b: + c: 1 +--- +a: Document Two +b: + c: 2 + diff --git a/utils_test.go b/utils_test.go index 88789194..cf041833 100644 --- a/utils_test.go +++ b/utils_test.go @@ -60,6 +60,12 @@ func assertResultWithContext(t *testing.T, expectedValue interface{}, actualValu } } +func assertAnyErr(t *testing.T, actualValue error) { + if actualValue == nil { + t.Error("Expected error, got nil") + } +} + func writeTempYamlFile(content string) string { tmpfile, _ := ioutil.TempFile("", "testyaml") defer func() { diff --git a/yq_test.go b/yq_test.go index b00cc4b4..b91cd186 100644 --- a/yq_test.go +++ b/yq_test.go @@ -38,6 +38,23 @@ func TestReadString(t *testing.T) { assertResult(t, "hi", result) } +func TestReadMultipleDocuments(t *testing.T) { + t.Run("multiple documents assumes first", func(t *testing.T) { + result, _ := read([]string{"examples/multidocument.yaml", "b.c"}) + assertResult(t, 1, result) + }) + t.Run("multiple documents with leading @n reads from document n", func(t *testing.T) { + result, _ := read([]string{"examples/multidocument.yaml", "@0.b.c"}) + assertResult(t, 1, result) + + result, _ = read([]string{"examples/multidocument.yaml", "@1.b.c"}) + assertResult(t, 2, result) + + _, err := read([]string{"examples/multidocument.yaml", "@2.b.c"}) + assertAnyErr(t, err) + }) +} + func TestOrder(t *testing.T) { result, _ := read([]string{"examples/order.yaml"}) formattedResult, _ := yamlToString(result)