From 59a17f507a1ed1ecb2038b94d51572f851cf0459 Mon Sep 17 00:00:00 2001 From: Bracken Dawson Date: Thu, 11 Sep 2025 09:19:34 +0200 Subject: [PATCH 1/4] Constrain untested behavior of YAMLEq * Identical but invalid YAML is an assertion error. * Subsequent documents are unchecked. --- assert/assertions_test.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/assert/assertions_test.go b/assert/assertions_test.go index 4db48f38b..5e5c71328 100644 --- a/assert/assertions_test.go +++ b/assert/assertions_test.go @@ -2852,6 +2852,31 @@ func TestYAMLEq_ArraysOfDifferentOrder(t *testing.T) { False(t, YAMLEq(mockT, `["foo", {"hello": "world", "nested": "hash"}]`, `[{ "hello": "world", "nested": "hash"}, "foo"]`)) } +func TestYAMLEq_OnlyFirstDocument(t *testing.T) { + t.Parallel() + + mockT := new(testing.T) + True(t, YAMLEq(mockT, + `--- +doc1: same +--- +doc2: different +`, + `--- +doc1: same +--- +doc2: notsame +`, + )) +} + +func TestYAMLEq_InvalidIdenticalYAML(t *testing.T) { + t.Parallel() + + mockT := new(testing.T) + False(t, YAMLEq(mockT, `}`, `}`)) +} + type diffTestingStruct struct { A string B int From a78e37330e674113a8f906c77a2c7cbc37db101f Mon Sep 17 00:00:00 2001 From: Bracken Dawson Date: Thu, 11 Sep 2025 09:22:38 +0200 Subject: [PATCH 2/4] Document that YAMLEq only compared the first document in the YAML strings. --- assert/assertion_format.go | 2 +- assert/assertion_forward.go | 4 ++-- assert/assertions.go | 2 +- require/require.go | 4 ++-- require/require_forward.go | 4 ++-- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/assert/assertion_format.go b/assert/assertion_format.go index c592f6ad5..fbd26d5c2 100644 --- a/assert/assertion_format.go +++ b/assert/assertion_format.go @@ -849,7 +849,7 @@ func WithinRangef(t TestingT, actual time.Time, start time.Time, end time.Time, return WithinRange(t, actual, start, end, append([]interface{}{msg}, args...)...) } -// YAMLEqf asserts that two YAML strings are equivalent. +// YAMLEqf asserts that the first document in the two YAML strings is equivalent. func YAMLEqf(t TestingT, expected string, actual string, msg string, args ...interface{}) bool { if h, ok := t.(tHelper); ok { h.Helper() diff --git a/assert/assertion_forward.go b/assert/assertion_forward.go index 58db92845..1dc208eb8 100644 --- a/assert/assertion_forward.go +++ b/assert/assertion_forward.go @@ -1690,7 +1690,7 @@ func (a *Assertions) WithinRangef(actual time.Time, start time.Time, end time.Ti return WithinRangef(a.t, actual, start, end, msg, args...) } -// YAMLEq asserts that two YAML strings are equivalent. +// YAMLEq asserts that the first document in the two YAML strings is equivalent. func (a *Assertions) YAMLEq(expected string, actual string, msgAndArgs ...interface{}) bool { if h, ok := a.t.(tHelper); ok { h.Helper() @@ -1698,7 +1698,7 @@ func (a *Assertions) YAMLEq(expected string, actual string, msgAndArgs ...interf return YAMLEq(a.t, expected, actual, msgAndArgs...) } -// YAMLEqf asserts that two YAML strings are equivalent. +// YAMLEqf asserts that the first document in the two YAML strings is equivalent. func (a *Assertions) YAMLEqf(expected string, actual string, msg string, args ...interface{}) bool { if h, ok := a.t.(tHelper); ok { h.Helper() diff --git a/assert/assertions.go b/assert/assertions.go index 3f75ee0eb..37ad3de41 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -1880,7 +1880,7 @@ func JSONEq(t TestingT, expected string, actual string, msgAndArgs ...interface{ return Equal(t, expectedJSONAsInterface, actualJSONAsInterface, msgAndArgs...) } -// YAMLEq asserts that two YAML strings are equivalent. +// YAMLEq asserts that the first document in the two YAML strings is equivalent. func YAMLEq(t TestingT, expected string, actual string, msgAndArgs ...interface{}) bool { if h, ok := t.(tHelper); ok { h.Helper() diff --git a/require/require.go b/require/require.go index 2d02f9bce..2e77e2d85 100644 --- a/require/require.go +++ b/require/require.go @@ -2135,7 +2135,7 @@ func WithinRangef(t TestingT, actual time.Time, start time.Time, end time.Time, t.FailNow() } -// YAMLEq asserts that two YAML strings are equivalent. +// YAMLEq asserts that the first document in the two YAML strings is equivalent. func YAMLEq(t TestingT, expected string, actual string, msgAndArgs ...interface{}) { if h, ok := t.(tHelper); ok { h.Helper() @@ -2146,7 +2146,7 @@ func YAMLEq(t TestingT, expected string, actual string, msgAndArgs ...interface{ t.FailNow() } -// YAMLEqf asserts that two YAML strings are equivalent. +// YAMLEqf asserts that the first document in the two YAML strings is equivalent. func YAMLEqf(t TestingT, expected string, actual string, msg string, args ...interface{}) { if h, ok := t.(tHelper); ok { h.Helper() diff --git a/require/require_forward.go b/require/require_forward.go index e6f7e9446..60e5beed7 100644 --- a/require/require_forward.go +++ b/require/require_forward.go @@ -1691,7 +1691,7 @@ func (a *Assertions) WithinRangef(actual time.Time, start time.Time, end time.Ti WithinRangef(a.t, actual, start, end, msg, args...) } -// YAMLEq asserts that two YAML strings are equivalent. +// YAMLEq asserts that the first document in the two YAML strings is equivalent. func (a *Assertions) YAMLEq(expected string, actual string, msgAndArgs ...interface{}) { if h, ok := a.t.(tHelper); ok { h.Helper() @@ -1699,7 +1699,7 @@ func (a *Assertions) YAMLEq(expected string, actual string, msgAndArgs ...interf YAMLEq(a.t, expected, actual, msgAndArgs...) } -// YAMLEqf asserts that two YAML strings are equivalent. +// YAMLEqf asserts that the first document in the two YAML strings is equivalent. func (a *Assertions) YAMLEqf(expected string, actual string, msg string, args ...interface{}) { if h, ok := a.t.(tHelper); ok { h.Helper() From e8c7bc92c56e6d6c9b65e0711b3ec725211e9f5d Mon Sep 17 00:00:00 2001 From: Bracken Dawson Date: Fri, 12 Sep 2025 08:35:56 +0200 Subject: [PATCH 3/4] Add example to YAMLEq --- assert/assertion_format.go | 12 ++++++++++++ assert/assertion_forward.go | 24 ++++++++++++++++++++++++ assert/assertions.go | 12 ++++++++++++ require/require.go | 24 ++++++++++++++++++++++++ require/require_forward.go | 24 ++++++++++++++++++++++++ 5 files changed, 96 insertions(+) diff --git a/assert/assertion_format.go b/assert/assertion_format.go index fbd26d5c2..b8f3259ee 100644 --- a/assert/assertion_format.go +++ b/assert/assertion_format.go @@ -850,6 +850,18 @@ func WithinRangef(t TestingT, actual time.Time, start time.Time, end time.Time, } // YAMLEqf asserts that the first document in the two YAML strings is equivalent. +// +// expected := `--- +// key: value +// --- +// key: this is a second document, it is not evaluated +// ` +// actual := `--- +// key: value +// --- +// key: this is a subsequent document, it is not evaluated +// ` +// assert.YAMLEqf(t, expected, actual, "error message %s", "formatted") func YAMLEqf(t TestingT, expected string, actual string, msg string, args ...interface{}) bool { if h, ok := t.(tHelper); ok { h.Helper() diff --git a/assert/assertion_forward.go b/assert/assertion_forward.go index 1dc208eb8..e24b38e64 100644 --- a/assert/assertion_forward.go +++ b/assert/assertion_forward.go @@ -1691,6 +1691,18 @@ func (a *Assertions) WithinRangef(actual time.Time, start time.Time, end time.Ti } // YAMLEq asserts that the first document in the two YAML strings is equivalent. +// +// expected := `--- +// key: value +// --- +// key: this is a second document, it is not evaluated +// ` +// actual := `--- +// key: value +// --- +// key: this is a subsequent document, it is not evaluated +// ` +// a.YAMLEq(expected, actual) func (a *Assertions) YAMLEq(expected string, actual string, msgAndArgs ...interface{}) bool { if h, ok := a.t.(tHelper); ok { h.Helper() @@ -1699,6 +1711,18 @@ func (a *Assertions) YAMLEq(expected string, actual string, msgAndArgs ...interf } // YAMLEqf asserts that the first document in the two YAML strings is equivalent. +// +// expected := `--- +// key: value +// --- +// key: this is a second document, it is not evaluated +// ` +// actual := `--- +// key: value +// --- +// key: this is a subsequent document, it is not evaluated +// ` +// a.YAMLEqf(expected, actual, "error message %s", "formatted") func (a *Assertions) YAMLEqf(expected string, actual string, msg string, args ...interface{}) bool { if h, ok := a.t.(tHelper); ok { h.Helper() diff --git a/assert/assertions.go b/assert/assertions.go index 37ad3de41..8d22f814c 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -1881,6 +1881,18 @@ func JSONEq(t TestingT, expected string, actual string, msgAndArgs ...interface{ } // YAMLEq asserts that the first document in the two YAML strings is equivalent. +// +// expected := `--- +// key: value +// --- +// key: this is a second document, it is not evaluated +// ` +// actual := `--- +// key: value +// --- +// key: this is a subsequent document, it is not evaluated +// ` +// assert.YAMLEq(t, expected, actual) func YAMLEq(t TestingT, expected string, actual string, msgAndArgs ...interface{}) bool { if h, ok := t.(tHelper); ok { h.Helper() diff --git a/require/require.go b/require/require.go index 2e77e2d85..1df1ce93b 100644 --- a/require/require.go +++ b/require/require.go @@ -2136,6 +2136,18 @@ func WithinRangef(t TestingT, actual time.Time, start time.Time, end time.Time, } // YAMLEq asserts that the first document in the two YAML strings is equivalent. +// +// expected := `--- +// key: value +// --- +// key: this is a second document, it is not evaluated +// ` +// actual := `--- +// key: value +// --- +// key: this is a subsequent document, it is not evaluated +// ` +// require.YAMLEq(t, expected, actual) func YAMLEq(t TestingT, expected string, actual string, msgAndArgs ...interface{}) { if h, ok := t.(tHelper); ok { h.Helper() @@ -2147,6 +2159,18 @@ func YAMLEq(t TestingT, expected string, actual string, msgAndArgs ...interface{ } // YAMLEqf asserts that the first document in the two YAML strings is equivalent. +// +// expected := `--- +// key: value +// --- +// key: this is a second document, it is not evaluated +// ` +// actual := `--- +// key: value +// --- +// key: this is a subsequent document, it is not evaluated +// ` +// require.YAMLEqf(t, expected, actual, "error message %s", "formatted") func YAMLEqf(t TestingT, expected string, actual string, msg string, args ...interface{}) { if h, ok := t.(tHelper); ok { h.Helper() diff --git a/require/require_forward.go b/require/require_forward.go index 60e5beed7..13053be03 100644 --- a/require/require_forward.go +++ b/require/require_forward.go @@ -1692,6 +1692,18 @@ func (a *Assertions) WithinRangef(actual time.Time, start time.Time, end time.Ti } // YAMLEq asserts that the first document in the two YAML strings is equivalent. +// +// expected := `--- +// key: value +// --- +// key: this is a second document, it is not evaluated +// ` +// actual := `--- +// key: value +// --- +// key: this is a subsequent document, it is not evaluated +// ` +// a.YAMLEq(expected, actual) func (a *Assertions) YAMLEq(expected string, actual string, msgAndArgs ...interface{}) { if h, ok := a.t.(tHelper); ok { h.Helper() @@ -1700,6 +1712,18 @@ func (a *Assertions) YAMLEq(expected string, actual string, msgAndArgs ...interf } // YAMLEqf asserts that the first document in the two YAML strings is equivalent. +// +// expected := `--- +// key: value +// --- +// key: this is a second document, it is not evaluated +// ` +// actual := `--- +// key: value +// --- +// key: this is a subsequent document, it is not evaluated +// ` +// a.YAMLEqf(expected, actual, "error message %s", "formatted") func (a *Assertions) YAMLEqf(expected string, actual string, msg string, args ...interface{}) { if h, ok := a.t.(tHelper); ok { h.Helper() From 2b945c3738bc32d58217d0f8a37ac32f0311643e Mon Sep 17 00:00:00 2001 From: Bracken Date: Fri, 12 Sep 2025 09:14:40 +0200 Subject: [PATCH 4/4] Update assert/assertions.go Co-authored-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com> --- assert/assertion_format.go | 2 +- assert/assertion_forward.go | 4 ++-- assert/assertions.go | 2 +- require/require.go | 4 ++-- require/require_forward.go | 4 ++-- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/assert/assertion_format.go b/assert/assertion_format.go index b8f3259ee..66029153a 100644 --- a/assert/assertion_format.go +++ b/assert/assertion_format.go @@ -849,7 +849,7 @@ func WithinRangef(t TestingT, actual time.Time, start time.Time, end time.Time, return WithinRange(t, actual, start, end, append([]interface{}{msg}, args...)...) } -// YAMLEqf asserts that the first document in the two YAML strings is equivalent. +// YAMLEqf asserts that the first documents in the two YAML strings are equivalent. // // expected := `--- // key: value diff --git a/assert/assertion_forward.go b/assert/assertion_forward.go index e24b38e64..b72a4a124 100644 --- a/assert/assertion_forward.go +++ b/assert/assertion_forward.go @@ -1690,7 +1690,7 @@ func (a *Assertions) WithinRangef(actual time.Time, start time.Time, end time.Ti return WithinRangef(a.t, actual, start, end, msg, args...) } -// YAMLEq asserts that the first document in the two YAML strings is equivalent. +// YAMLEq asserts that the first documents in the two YAML strings are equivalent. // // expected := `--- // key: value @@ -1710,7 +1710,7 @@ func (a *Assertions) YAMLEq(expected string, actual string, msgAndArgs ...interf return YAMLEq(a.t, expected, actual, msgAndArgs...) } -// YAMLEqf asserts that the first document in the two YAML strings is equivalent. +// YAMLEqf asserts that the first documents in the two YAML strings are equivalent. // // expected := `--- // key: value diff --git a/assert/assertions.go b/assert/assertions.go index 8d22f814c..590bb8922 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -1880,7 +1880,7 @@ func JSONEq(t TestingT, expected string, actual string, msgAndArgs ...interface{ return Equal(t, expectedJSONAsInterface, actualJSONAsInterface, msgAndArgs...) } -// YAMLEq asserts that the first document in the two YAML strings is equivalent. +// YAMLEq asserts that the first documents in the two YAML strings are equivalent. // // expected := `--- // key: value diff --git a/require/require.go b/require/require.go index 1df1ce93b..9cd13df22 100644 --- a/require/require.go +++ b/require/require.go @@ -2135,7 +2135,7 @@ func WithinRangef(t TestingT, actual time.Time, start time.Time, end time.Time, t.FailNow() } -// YAMLEq asserts that the first document in the two YAML strings is equivalent. +// YAMLEq asserts that the first documents in the two YAML strings are equivalent. // // expected := `--- // key: value @@ -2158,7 +2158,7 @@ func YAMLEq(t TestingT, expected string, actual string, msgAndArgs ...interface{ t.FailNow() } -// YAMLEqf asserts that the first document in the two YAML strings is equivalent. +// YAMLEqf asserts that the first documents in the two YAML strings are equivalent. // // expected := `--- // key: value diff --git a/require/require_forward.go b/require/require_forward.go index 13053be03..deae0cd13 100644 --- a/require/require_forward.go +++ b/require/require_forward.go @@ -1691,7 +1691,7 @@ func (a *Assertions) WithinRangef(actual time.Time, start time.Time, end time.Ti WithinRangef(a.t, actual, start, end, msg, args...) } -// YAMLEq asserts that the first document in the two YAML strings is equivalent. +// YAMLEq asserts that the first documents in the two YAML strings are equivalent. // // expected := `--- // key: value @@ -1711,7 +1711,7 @@ func (a *Assertions) YAMLEq(expected string, actual string, msgAndArgs ...interf YAMLEq(a.t, expected, actual, msgAndArgs...) } -// YAMLEqf asserts that the first document in the two YAML strings is equivalent. +// YAMLEqf asserts that the first documents in the two YAML strings are equivalent. // // expected := `--- // key: value