Skip to content

Commit f229450

Browse files
committed
subsequent YAML documents were ignored
In YAMLEq(), yaml documents in the input after the first document were silently not considered in the comparison. Make it compare all the docs. I checked JSONEq() and it always fails if you try to pass it JSON Lines documents, even if they are the same.
1 parent 181cea6 commit f229450

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

assert/assertions.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/json"
77
"errors"
88
"fmt"
9+
"io"
910
"math"
1011
"os"
1112
"path/filepath"
@@ -1626,19 +1627,33 @@ func YAMLEq(t TestingT, expected string, actual string, msgAndArgs ...interface{
16261627
if h, ok := t.(tHelper); ok {
16271628
h.Helper()
16281629
}
1629-
var expectedYAMLAsInterface, actualYAMLAsInterface interface{}
1630+
var expectedYAMLAsInterface, actualYAMLAsInterface []interface{}
16301631

1631-
if err := yaml.Unmarshal([]byte(expected), &expectedYAMLAsInterface); err != nil {
1632+
if err := yamlUnmarshalAllDocuments(strings.NewReader(expected), &expectedYAMLAsInterface); err != nil {
16321633
return Fail(t, fmt.Sprintf("Expected value ('%s') is not valid yaml.\nYAML parsing error: '%s'", expected, err.Error()), msgAndArgs...)
16331634
}
16341635

1635-
if err := yaml.Unmarshal([]byte(actual), &actualYAMLAsInterface); err != nil {
1636+
if err := yamlUnmarshalAllDocuments(strings.NewReader(actual), &actualYAMLAsInterface); err != nil {
16361637
return Fail(t, fmt.Sprintf("Input ('%s') needs to be valid yaml.\nYAML error: '%s'", actual, err.Error()), msgAndArgs...)
16371638
}
16381639

16391640
return Equal(t, expectedYAMLAsInterface, actualYAMLAsInterface, msgAndArgs...)
16401641
}
16411642

1643+
func yamlUnmarshalAllDocuments(in io.Reader, out *[]interface{}) error {
1644+
decoder := yaml.NewDecoder(in)
1645+
for {
1646+
var document interface{}
1647+
if err := decoder.Decode(&document); err != nil {
1648+
if errors.Is(err, io.EOF) {
1649+
return nil
1650+
}
1651+
return err
1652+
}
1653+
*out = append(*out, document)
1654+
}
1655+
}
1656+
16421657
func typeAndKind(v interface{}) (reflect.Type, reflect.Kind) {
16431658
t := reflect.TypeOf(v)
16441659
k := t.Kind()

assert/assertions_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1942,6 +1942,56 @@ func TestYAMLEq_ArraysOfDifferentOrder(t *testing.T) {
19421942
False(t, YAMLEq(mockT, `["foo", {"hello": "world", "nested": "hash"}]`, `[{ "hello": "world", "nested": "hash"}, "foo"]`))
19431943
}
19441944

1945+
func TestYAMLEq_MultipleDocs(t *testing.T) {
1946+
mockT := new(testing.T)
1947+
expected := `
1948+
first: document
1949+
---
1950+
second: document
1951+
`
1952+
actual := `
1953+
first: document
1954+
---
1955+
second: ccc-combo-breaker
1956+
`
1957+
False(t, YAMLEq(mockT, expected, actual))
1958+
}
1959+
1960+
func TestYAMLEq_MultipleDocsMismatchedNumber(t *testing.T) {
1961+
mockT := new(testing.T)
1962+
expected := `
1963+
first: document
1964+
---
1965+
second: document
1966+
`
1967+
actual := `
1968+
first: document
1969+
`
1970+
False(t, YAMLEq(mockT, expected, actual))
1971+
}
1972+
1973+
func TestYAMLEq_MultipleDocsInvalidYAML(t *testing.T) {
1974+
mockT := new(testing.T)
1975+
expected := `
1976+
first: document
1977+
---
1978+
second: document
1979+
also: key
1980+
`
1981+
False(t, YAMLEq(mockT, expected, expected))
1982+
}
1983+
1984+
func TestYAMLEq_MultipleDocsMatching(t *testing.T) {
1985+
mockT := new(testing.T)
1986+
expected := `
1987+
first: document
1988+
---
1989+
second:
1990+
also: key
1991+
`
1992+
True(t, YAMLEq(mockT, expected, expected))
1993+
}
1994+
19451995
type diffTestingStruct struct {
19461996
A string
19471997
B int

0 commit comments

Comments
 (0)