From 2ac68b9707a40aff8933bd8259cc53548e63a418 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Mengu=C3=A9?= Date: Wed, 28 May 2025 18:51:47 +0200 Subject: [PATCH] assert.JSONEq: shortcut if same strings Shortcut in assert.JSONEq once we have validated that 'expected' is valid JSON, and 'actual' is the exact same string. --- assert/assertions.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/assert/assertions.go b/assert/assertions.go index effd026d5..6fc02a340 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -1853,6 +1853,11 @@ func JSONEq(t TestingT, expected string, actual string, msgAndArgs ...interface{ return Fail(t, fmt.Sprintf("Expected value ('%s') is not valid json.\nJSON parsing error: '%s'", expected, err.Error()), msgAndArgs...) } + // Shortcut if same bytes + if actual == expected { + return true + } + if err := json.Unmarshal([]byte(actual), &actualJSONAsInterface); err != nil { return Fail(t, fmt.Sprintf("Input ('%s') needs to be valid json.\nJSON parsing error: '%s'", actual, err.Error()), msgAndArgs...) }