From 688992aa87ada0fd603ffa73e3dfb615d691a82d Mon Sep 17 00:00:00 2001 From: Aaron Kaswen-Wilk Date: Mon, 30 Dec 2024 13:30:11 +0100 Subject: [PATCH] fix ci failures --- assert/assertion_format.go | 25 +++++++++ assert/assertion_forward.go | 49 +++++++++++++++++ assert/assertions.go | 4 +- assert/{internal => }/jsonmatch/matches_by.go | 0 require/require.go | 55 +++++++++++++++++++ require/require_forward.go | 49 +++++++++++++++++ 6 files changed, 180 insertions(+), 2 deletions(-) rename assert/{internal => }/jsonmatch/matches_by.go (100%) diff --git a/assert/assertion_format.go b/assert/assertion_format.go index 190634165..5afc612d3 100644 --- a/assert/assertion_format.go +++ b/assert/assertion_format.go @@ -3,6 +3,7 @@ package assert import ( + jsonmatch "github.com/stretchr/testify/assert/jsonmatch" http "net/http" url "net/url" time "time" @@ -456,6 +457,30 @@ func JSONEqf(t TestingT, expected string, actual string, msg string, args ...int return JSONEq(t, expected, actual, append([]interface{}{msg}, args...)...) } +// JSONMatchesByf asserts that two JSON strings are equivalent using one or more custom JSON matchers. +// the value passed into the matcher will be on of the following types: +// - string +// - float64 +// - bool +// - nil +// - []interface{} +// - map[string]interface{} +// +// Can use nil for matchers to test equality of pure json. +// +// Note: in cases where expected and actual are not equal, the value for the matcher will be displayed as unequal, +// regardless of the output of the matcher function. This should be fixed in a future release. +// +// assert.JSONMatchesByf(t, `{ "foo": "$NOT_EMPTY" }`, `{ "foo": "baz" }`, assert.ValueMatchers{ +// "$NOT_EMPTY": func(v interface) bool { return v != "" }, +// }) +func JSONMatchesByf(t TestingT, expected string, actual string, matchers jsonmatch.Matchers, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return JSONMatchesBy(t, expected, actual, matchers, append([]interface{}{msg}, args...)...) +} + // Lenf asserts that the specified object has specific length. // Lenf also fails if the object has a type that len() not accept. // diff --git a/assert/assertion_forward.go b/assert/assertion_forward.go index 21629087b..72ce059ff 100644 --- a/assert/assertion_forward.go +++ b/assert/assertion_forward.go @@ -3,6 +3,7 @@ package assert import ( + jsonmatch "github.com/stretchr/testify/assert/jsonmatch" http "net/http" url "net/url" time "time" @@ -904,6 +905,54 @@ func (a *Assertions) JSONEqf(expected string, actual string, msg string, args .. return JSONEqf(a.t, expected, actual, msg, args...) } +// JSONMatchesBy asserts that two JSON strings are equivalent using one or more custom JSON matchers. +// the value passed into the matcher will be on of the following types: +// - string +// - float64 +// - bool +// - nil +// - []interface{} +// - map[string]interface{} +// +// Can use nil for matchers to test equality of pure json. +// +// Note: in cases where expected and actual are not equal, the value for the matcher will be displayed as unequal, +// regardless of the output of the matcher function. This should be fixed in a future release. +// +// a.JSONMatchesBy(`{ "foo": "$NOT_EMPTY" }`, `{ "foo": "baz" }`, assert.ValueMatchers{ +// "$NOT_EMPTY": func(v interface) bool { return v != "" }, +// }) +func (a *Assertions) JSONMatchesBy(expected string, actual string, matchers jsonmatch.Matchers, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return JSONMatchesBy(a.t, expected, actual, matchers, msgAndArgs...) +} + +// JSONMatchesByf asserts that two JSON strings are equivalent using one or more custom JSON matchers. +// the value passed into the matcher will be on of the following types: +// - string +// - float64 +// - bool +// - nil +// - []interface{} +// - map[string]interface{} +// +// Can use nil for matchers to test equality of pure json. +// +// Note: in cases where expected and actual are not equal, the value for the matcher will be displayed as unequal, +// regardless of the output of the matcher function. This should be fixed in a future release. +// +// a.JSONMatchesByf(`{ "foo": "$NOT_EMPTY" }`, `{ "foo": "baz" }`, assert.ValueMatchers{ +// "$NOT_EMPTY": func(v interface) bool { return v != "" }, +// }) +func (a *Assertions) JSONMatchesByf(expected string, actual string, matchers jsonmatch.Matchers, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return JSONMatchesByf(a.t, expected, actual, matchers, msg, args...) +} + // Len asserts that the specified object has specific length. // Len also fails if the object has a type that len() not accept. // diff --git a/assert/assertions.go b/assert/assertions.go index 57d6d4cee..b2bdcc1f7 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -20,8 +20,8 @@ import ( "github.com/davecgh/go-spew/spew" "github.com/pmezard/go-difflib/difflib" + "github.com/stretchr/testify/assert/jsonmatch" // Wrapper around gopkg.in/yaml.v3 - "github.com/stretchr/testify/assert/internal/jsonmatch" "github.com/stretchr/testify/assert/yaml" ) @@ -1819,7 +1819,7 @@ type ValueMatchers = jsonmatch.Matchers // // Can use nil for matchers to test equality of pure json. // -// Note: in cases where expected and actual are not equal, the value for the matcher will be displayed as unequal, +// Note: in cases where expected and actual are not equal, the value for the matcher will be displayed as unequal, // regardless of the output of the matcher function. This should be fixed in a future release. // // assert.JSONMatchesBy(t, `{ "foo": "$NOT_EMPTY" }`, `{ "foo": "baz" }`, assert.ValueMatchers{ diff --git a/assert/internal/jsonmatch/matches_by.go b/assert/jsonmatch/matches_by.go similarity index 100% rename from assert/internal/jsonmatch/matches_by.go rename to assert/jsonmatch/matches_by.go diff --git a/require/require.go b/require/require.go index d8921950d..bc8468230 100644 --- a/require/require.go +++ b/require/require.go @@ -4,6 +4,7 @@ package require import ( assert "github.com/stretchr/testify/assert" + jsonmatch "github.com/stretchr/testify/assert/jsonmatch" http "net/http" url "net/url" time "time" @@ -1145,6 +1146,60 @@ func JSONEqf(t TestingT, expected string, actual string, msg string, args ...int t.FailNow() } +// JSONMatchesBy asserts that two JSON strings are equivalent using one or more custom JSON matchers. +// the value passed into the matcher will be on of the following types: +// - string +// - float64 +// - bool +// - nil +// - []interface{} +// - map[string]interface{} +// +// Can use nil for matchers to test equality of pure json. +// +// Note: in cases where expected and actual are not equal, the value for the matcher will be displayed as unequal, +// regardless of the output of the matcher function. This should be fixed in a future release. +// +// require.JSONMatchesBy(t, `{ "foo": "$NOT_EMPTY" }`, `{ "foo": "baz" }`, require.ValueMatchers{ +// "$NOT_EMPTY": func(v interface) bool { return v != "" }, +// }) +func JSONMatchesBy(t TestingT, expected string, actual string, matchers jsonmatch.Matchers, msgAndArgs ...interface{}) { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if assert.JSONMatchesBy(t, expected, actual, matchers, msgAndArgs...) { + return + } + t.FailNow() +} + +// JSONMatchesByf asserts that two JSON strings are equivalent using one or more custom JSON matchers. +// the value passed into the matcher will be on of the following types: +// - string +// - float64 +// - bool +// - nil +// - []interface{} +// - map[string]interface{} +// +// Can use nil for matchers to test equality of pure json. +// +// Note: in cases where expected and actual are not equal, the value for the matcher will be displayed as unequal, +// regardless of the output of the matcher function. This should be fixed in a future release. +// +// require.JSONMatchesByf(t, `{ "foo": "$NOT_EMPTY" }`, `{ "foo": "baz" }`, require.ValueMatchers{ +// "$NOT_EMPTY": func(v interface) bool { return v != "" }, +// }) +func JSONMatchesByf(t TestingT, expected string, actual string, matchers jsonmatch.Matchers, msg string, args ...interface{}) { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if assert.JSONMatchesByf(t, expected, actual, matchers, msg, args...) { + return + } + t.FailNow() +} + // Len asserts that the specified object has specific length. // Len also fails if the object has a type that len() not accept. // diff --git a/require/require_forward.go b/require/require_forward.go index 1bd87304f..554d440cc 100644 --- a/require/require_forward.go +++ b/require/require_forward.go @@ -4,6 +4,7 @@ package require import ( assert "github.com/stretchr/testify/assert" + jsonmatch "github.com/stretchr/testify/assert/jsonmatch" http "net/http" url "net/url" time "time" @@ -905,6 +906,54 @@ func (a *Assertions) JSONEqf(expected string, actual string, msg string, args .. JSONEqf(a.t, expected, actual, msg, args...) } +// JSONMatchesBy asserts that two JSON strings are equivalent using one or more custom JSON matchers. +// the value passed into the matcher will be on of the following types: +// - string +// - float64 +// - bool +// - nil +// - []interface{} +// - map[string]interface{} +// +// Can use nil for matchers to test equality of pure json. +// +// Note: in cases where expected and actual are not equal, the value for the matcher will be displayed as unequal, +// regardless of the output of the matcher function. This should be fixed in a future release. +// +// a.JSONMatchesBy(`{ "foo": "$NOT_EMPTY" }`, `{ "foo": "baz" }`, assert.ValueMatchers{ +// "$NOT_EMPTY": func(v interface) bool { return v != "" }, +// }) +func (a *Assertions) JSONMatchesBy(expected string, actual string, matchers jsonmatch.Matchers, msgAndArgs ...interface{}) { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + JSONMatchesBy(a.t, expected, actual, matchers, msgAndArgs...) +} + +// JSONMatchesByf asserts that two JSON strings are equivalent using one or more custom JSON matchers. +// the value passed into the matcher will be on of the following types: +// - string +// - float64 +// - bool +// - nil +// - []interface{} +// - map[string]interface{} +// +// Can use nil for matchers to test equality of pure json. +// +// Note: in cases where expected and actual are not equal, the value for the matcher will be displayed as unequal, +// regardless of the output of the matcher function. This should be fixed in a future release. +// +// a.JSONMatchesByf(`{ "foo": "$NOT_EMPTY" }`, `{ "foo": "baz" }`, assert.ValueMatchers{ +// "$NOT_EMPTY": func(v interface) bool { return v != "" }, +// }) +func (a *Assertions) JSONMatchesByf(expected string, actual string, matchers jsonmatch.Matchers, msg string, args ...interface{}) { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + JSONMatchesByf(a.t, expected, actual, matchers, msg, args...) +} + // Len asserts that the specified object has specific length. // Len also fails if the object has a type that len() not accept. //