Skip to content

Commit

Permalink
subsequent YAML documents were ignored
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
brackendawson committed Feb 24, 2024
1 parent e2741fa commit 75989b6
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
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 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"math"
"os"
"reflect"
Expand Down Expand Up @@ -1760,19 +1761,33 @@ func YAMLEq(t TestingT, expected string, actual string, msgAndArgs ...interface{
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)
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 @@ -2305,6 +2305,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

0 comments on commit 75989b6

Please sign in to comment.