10000 feat: add JSONEqBytes function · go-openapi/testify@bff0b8c · GitHub
[go: up one dir, main page]

Skip to content

Commit bff0b8c

Browse files
Gabriel Crispinofredbi
authored andcommitted
feat: add JSONEqBytes function
test: JSONEqBytes doc: update documentation comment in JSONEqBytes chore: run go generate and go fmt
1 parent 0cfc3ad commit bff0b8c

File tree

7 files changed

+140
-6
lines changed

7 files changed

+140
-6
lines changed

assert/assertion_format.go

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assert/assertion_forward.go

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Original file line numberDiff line numberDiff line change
@@ -1855,31 +1855,38 @@ func NoDirExists(t TestingT, path string, msgAndArgs ...interface{}) bool {
18551855
return Fail(t, fmt.Sprintf("directory %q exists", path), msgAndArgs...)
18561856
}
18571857

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

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

18711871
// Shortcut if same bytes
1872-
if actual == expected {
1872+
if bytes.Equal(actual, expected) {
18731873
return true
18741874
}
18751875

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

18801880
return Equal(t, expectedJSONAsInterface, actualJSONAsInterface, msgAndArgs...)
18811881
}
18821882

1883+
// JSONEq asserts that two JSON strings are equivalent.
1884+
//
1885+
// assert.JSONEq(t, `{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`)
1886+
func JSONEq(t TestingT, expected, actual string, msgAndArgs ...interface{}) bool {
1887+
return JSONEqBytes(t, []byte(expected), []byte(actual), msgAndArgs)
1888+
}
1889+
18831890
// YAMLEq asserts that the first documents in the two YAML strings are equivalent.
18841891
//
18851892
// expected := `---

Original file line numberDiff line numberDiff line change
@@ -2783,6 +2783,57 @@ func TestJSONEq_ArraysOfDifferentOrder(t *testing.T) {
27832783
False(t, JSONEq(mockT, `["foo", {"hello": "world", "nested": "hash"}]`, `[{ "hello": "world", "nested": "hash"}, "foo"]`))
27842784
}
27852785

2786+
func TestJSONEqBytes_EqualSONString(t *testing.T) {
2787+
mockT := new(testing.T)
2788+
True(t, JSONEqBytes(mockT, []byte(`{"hello": "world", "foo": "bar"}`), []byte(`{"hello": "world", "foo": "bar"}`)))
2789+
}
2790+
2791+
func TestJSONEqBytes_EquivalentButNotEqual(t *testing.T) {
2792+
mockT := new(testing.T)
2793+
True(t, JSONEqBytes(mockT, []byte(`{"hello": "world", "foo": "bar"}`), []byte(`{"foo": "bar", "hello": "world"}`)))
2794+
}
2795+
2796+
func TestJSONEqBytes_HashOfArraysAndHashes(t *testing.T) {
2797+
mockT := new(testing.T)
2798+
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}"),
2799+
[]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}")))
2800+
}
2801+
2802+
func TestJSONEqBytes_Array(t *testing.T) {
2803+
mockT := new(testing.T)
2804+
True(t, JSONEqBytes(mockT, []byte(`["foo", {"hello": "world", "nested": "hash"}]`), []byte(`["foo", {"nested": "hash", "hello": "world"}]`)))
2805+
}
2806+
2807+
func TestJSONEqBytes_HashAndArrayNotEquivalent(t *testing.T) {
2808+
mockT := new(testing.T)
2809+
False(t, JSONEqBytes(mockT, []byte(`["foo", {"hello": "world", "nested": "hash"}]`), []byte(`{"foo": "bar", {"nested": "hash", "hello": "world"}}`)))
2810+
}
2811+
2812+
func TestJSONEqBytes_HashesNotEquivalent(t *testing.T) {
2813+
mockT := new(testing.T)
2814+
False(t, JSONEqBytes(mockT, []byte(`{"foo": "bar"}`), []byte(`{"foo": "bar", "hello": "world"}`)))
2815+
}
2816+
2817+
func TestJSONEqBytes_ActualIsNotJSON(t *testing.T) {
2818+
mockT := new(testing.T)
2819+
False(t, JSONEqBytes(mockT, []byte(`{"foo": "bar"}`), []byte("Not JSON")))
2820+
}
2821+
2822+
func TestJSONEqBytes_ExpectedIsNotJSON(t *testing.T) {
2823+
mockT := new(testing.T)
2824+
False(t, JSONEqBytes(mockT, []byte("Not JSON"), []byte(`{"foo": "bar", "hello": "world"}`)))
2825+
}
2826+
2827+
func TestJSONEqBytes_ExpectedAndActualNotJSON(t *testing.T) {
2828+
mockT := new(testing.T)
2829+
False(t, JSONEqBytes(mockT, []byte("Not JSON"), []byte("Not JSON")))
2830+
}
2831+
2832+
func TestJSONEqBytes_ArraysOfDifferentOrder(t *testing.T) {
2833+
mockT := new(testing.T)
2834+
False(t, JSONEqBytes(mockT, []byte(`["foo", {"hello": "world", "nested": "hash"}]`), []byte(`[{ "hello": "world", "nested": "hash"}, "foo"]`)))
2835+
}
2836+
27862837
type diffTestingStruct struct {
27872838
A string
27882839
B int