Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function for supporting JSON equality checks using byte slices #1513

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions assert/assertion_format.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions assert/assertion_forward.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 12 additions & 5 deletions assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1801,26 +1801,33 @@ func NoDirExists(t TestingT, path string, msgAndArgs ...interface{}) bool {
return Fail(t, fmt.Sprintf("directory %q exists", path), msgAndArgs...)
}

// JSONEq asserts that two JSON strings are equivalent.
// JSONEqBytes asserts that two JSON byte slices are equivalent.
//
// assert.JSONEq(t, `{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`)
func JSONEq(t TestingT, expected string, actual string, msgAndArgs ...interface{}) bool {
// assert.JSONEqBytes(t, []byte(`{"hello": "world", "foo": "bar"}`), []byte(`{"foo": "bar", "hello": "world"}`))
func JSONEqBytes(t TestingT, expected, actual []byte, msgAndArgs ...interface{}) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}
var expectedJSONAsInterface, actualJSONAsInterface interface{}

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

if err := json.Unmarshal([]byte(actual), &actualJSONAsInterface); err != nil {
if err := json.Unmarshal(actual, &actualJSONAsInterface); err != nil {
return Fail(t, fmt.Sprintf("Input ('%s') needs to be valid json.\nJSON parsing error: '%s'", actual, err.Error()), msgAndArgs...)
}

return Equal(t, expectedJSONAsInterface, actualJSONAsInterface, msgAndArgs...)
}

// JSONEq asserts that two JSON strings are equivalent.
//
// assert.JSONEq(t, `{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`)
func JSONEq(t TestingT, expected, actual string, msgAndArgs ...interface{}) bool {
return JSONEqBytes(t, []byte(expected), []byte(actual), msgAndArgs)
}

// YAMLEq asserts that two YAML strings are equivalent.
func YAMLEq(t TestingT, expected string, actual string, msgAndArgs ...interface{}) bool {
if h, ok := t.(tHelper); ok {
Expand Down
51 changes: 51 additions & 0 deletions assert/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2375,6 +2375,57 @@ func TestJSONEq_ArraysOfDifferentOrder(t *testing.T) {
False(t, JSONEq(mockT, `["foo", {"hello": "world", "nested": "hash"}]`, `[{ "hello": "world", "nested": "hash"}, "foo"]`))
}

func TestJSONEqBytes_EqualSONString(t *testing.T) {
mockT := new(testing.T)
True(t, JSONEqBytes(mockT, []byte(`{"hello": "world", "foo": "bar"}`), []byte(`{"hello": "world", "foo": "bar"}`)))
}

func TestJSONEqBytes_EquivalentButNotEqual(t *testing.T) {
mockT := new(testing.T)
True(t, JSONEqBytes(mockT, []byte(`{"hello": "world", "foo": "bar"}`), []byte(`{"foo": "bar", "hello": "world"}`)))
}

func TestJSONEqBytes_HashOfArraysAndHashes(t *testing.T) {
mockT := new(testing.T)
True(t, JSONEqBytes(mockT, []byte("{\r\n\t\"numeric\": 1.5,\r\n\t\"array\": [{\"foo\": \"bar\"}, 1, \"string\", [\"nested\", \"array\", 5.5]],\r\n\t\"hash\": {\"nested\": \"hash\", \"nested_slice\": [\"this\", \"is\", \"nested\"]},\r\n\t\"string\": \"foo\"\r\n}"),
[]byte("{\r\n\t\"numeric\": 1.5,\r\n\t\"hash\": {\"nested\": \"hash\", \"nested_slice\": [\"this\", \"is\", \"nested\"]},\r\n\t\"string\": \"foo\",\r\n\t\"array\": [{\"foo\": \"bar\"}, 1, \"string\", [\"nested\", \"array\", 5.5]]\r\n}")))
brackendawson marked this conversation as resolved.
Show resolved Hide resolved
}

func TestJSONEqBytes_Array(t *testing.T) {
mockT := new(testing.T)
True(t, JSONEqBytes(mockT, []byte(`["foo", {"hello": "world", "nested": "hash"}]`), []byte(`["foo", {"nested": "hash", "hello": "world"}]`)))
}

func TestJSONEqBytes_HashAndArrayNotEquivalent(t *testing.T) {
mockT := new(testing.T)
False(t, JSONEqBytes(mockT, []byte(`["foo", {"hello": "world", "nested": "hash"}]`), []byte(`{"foo": "bar", {"nested": "hash", "hello": "world"}}`)))
}

func TestJSONEqBytes_HashesNotEquivalent(t *testing.T) {
mockT := new(testing.T)
False(t, JSONEqBytes(mockT, []byte(`{"foo": "bar"}`), []byte(`{"foo": "bar", "hello": "world"}`)))
}

func TestJSONEqBytes_ActualIsNotJSON(t *testing.T) {
mockT := new(testing.T)
False(t, JSONEqBytes(mockT, []byte(`{"foo": "bar"}`), []byte("Not JSON")))
}

func TestJSONEqBytes_ExpectedIsNotJSON(t *testing.T) {
mockT := new(testing.T)
False(t, JSONEqBytes(mockT, []byte("Not JSON"), []byte(`{"foo": "bar", "hello": "world"}`)))
}

func TestJSONEqBytes_ExpectedAndActualNotJSON(t *testing.T) {
mockT := new(testing.T)
False(t, JSONEqBytes(mockT, []byte("Not JSON"), []byte("Not JSON")))
}

func TestJSONEqBytes_ArraysOfDifferentOrder(t *testing.T) {
mockT := new(testing.T)
False(t, JSONEqBytes(mockT, []byte(`["foo", {"hello": "world", "nested": "hash"}]`), []byte(`[{ "hello": "world", "nested": "hash"}, "foo"]`)))
}

func TestYAMLEq_EqualYAMLString(t *testing.T) {
mockT := new(testing.T)
True(t, YAMLEq(mockT, `{"hello": "world", "foo": "bar"}`, `{"hello": "world", "foo": "bar"}`))
Expand Down
26 changes: 26 additions & 0 deletions require/require.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions require/require_forward.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.