read: test reading specific document of multidocument

This commit is contained in:
therealplato 2018-05-02 18:52:24 +12:00
parent 26153b3eb5
commit da9edc9e31
4 changed files with 33 additions and 0 deletions

1
.gitignore vendored
View File

@ -36,3 +36,4 @@ parts/
prime/ prime/
.snapcraft/ .snapcraft/
yq*.snap yq*.snap
*.swp

View File

@ -0,0 +1,9 @@
---
a: Document One
b:
c: 1
---
a: Document Two
b:
c: 2

View File

@ -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 { func writeTempYamlFile(content string) string {
tmpfile, _ := ioutil.TempFile("", "testyaml") tmpfile, _ := ioutil.TempFile("", "testyaml")
defer func() { defer func() {

View File

@ -38,6 +38,23 @@ func TestReadString(t *testing.T) {
assertResult(t, "hi", result) 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) { func TestOrder(t *testing.T) {
result, _ := read([]string{"examples/order.yaml"}) result, _ := read([]string{"examples/order.yaml"})
formattedResult, _ := yamlToString(result) formattedResult, _ := yamlToString(result)