Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"encoding/json"
"errors"
"fmt"
"io"
"math"
"os"
"reflect"
Expand Down Expand Up @@ -1865,19 +1866,33 @@
if h, ok := t.(tHelper); ok {
h.Helper()
}
var expectedYAMLAsInterface, actualYAMLAsInterface interface{}
var expectedYAMLAsInterface, actualYAMLAsInterface []interface{}

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

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

return Equal(t, expectedYAMLAsInterface, actualYAMLAsInterface, msgAndArgs...)
}

func yamlUnmarshalAllDocuments(in io.Reader, out *[]interface{}) error {
decoder := yaml.NewDecoder(in)

Check failure on line 1883 in assert/assertions.go

View workflow job for this annotation

GitHub Actions / build (stable)

undefined: yaml.NewDecoder

Check failure on line 1883 in assert/assertions.go

View workflow job for this annotation

GitHub Actions / build (oldstable)

undefined: yaml.NewDecoder

Check failure on line 1883 in assert/assertions.go

View workflow job for this annotation

GitHub Actions / test (1.22)

undefined: yaml.NewDecoder

Check failure on line 1883 in assert/assertions.go

View workflow job for this annotation

GitHub Actions / test (1.21)

undefined: yaml.NewDecoder
for {
var document interface{}
if err := decoder.Decode(&document); err != nil {
if errors.Is(err, io.EOF) {
return nil
}
return err
}
*out = append(*out, document)
}
}

func typeAndKind(v interface{}) (reflect.Type, reflect.Kind) {
t := reflect.TypeOf(v)
k := t.Kind()
Expand Down
50 changes: 50 additions & 0 deletions assert/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2588,6 +2588,56 @@ func TestYAMLEq_ArraysOfDifferentOrder(t *testing.T) {
False(t, YAMLEq(mockT, `["foo", {"hello": "world", "nested": "hash"}]`, `[{ "hello": "world", "nested": "hash"}, "foo"]`))
}

func TestYAMLEq_MultipleDocs(t *testing.T) {
mockT := new(testing.T)
expected := `
first: document
---
second: document
`
actual := `
first: document
---
second: ccc-combo-breaker
`
False(t, YAMLEq(mockT, expected, actual))
}

func TestYAMLEq_MultipleDocsMismatchedNumber(t *testing.T) {
mockT := new(testing.T)
expected := `
first: document
---
second: document
`
actual := `
first: document
`
False(t, YAMLEq(mockT, expected, actual))
}

func TestYAMLEq_MultipleDocsInvalidYAML(t *testing.T) {
mockT := new(testing.T)
expected := `
first: document
---
second: document
also: key
`
False(t, YAMLEq(mockT, expected, expected))
}

func TestYAMLEq_MultipleDocsMatching(t *testing.T) {
mockT := new(testing.T)
expected := `
first: document
---
second:
also: key
`
True(t, YAMLEq(mockT, expected, expected))
}

type diffTestingStruct struct {
A string
B int
Expand Down
Loading