From edb4599dfc381b0ebbc956619c0610ca95ff9ac3 Mon Sep 17 00:00:00 2001 From: Roger Guldbrandsen Date: Fri, 1 Feb 2019 21:43:43 +0000 Subject: [PATCH] be friendlier with the assert message when the JSON representation are very long --- array.go | 7 ++++++- integration_test.go | 13 +++++++++++++ string.go | 6 +++++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/array.go b/array.go index f8f8038..e6561db 100644 --- a/array.go +++ b/array.go @@ -9,7 +9,12 @@ import ( func (a *Asserter) checkArray(path string, act, exp []interface{}) { if len(act) != len(exp) { a.Printer.Errorf("length of arrays at '%s' were different. Expected array to be of length %d, but contained %d element(s)", path, len(exp), len(act)) - a.Printer.Errorf("actual JSON at '%s' was: %+v, but expected JSON was: %+v", path, serialize(act), serialize(exp)) + serializedAct, serializedExp := serialize(act), serialize(exp) + if len(serializedAct+serializedExp) < 50 { + a.Printer.Errorf("actual JSON at '%s' was: %+v, but expected JSON was: %+v", path, serializedAct, serializedExp) + } else { + a.Printer.Errorf("actual JSON at '%s' was:\n%+v\nbut expected JSON was:\n%+v", path, serializedAct, serializedExp) + } return } for i := range act { diff --git a/integration_test.go b/integration_test.go index 14eb605..9a44335 100644 --- a/integration_test.go +++ b/integration_test.go @@ -32,6 +32,12 @@ func TestAssertf(t *testing.T) { {name: "identical strings", act: `"hello world"`, exp: `"hello world"`, msgs: []string{}}, {name: "identical empty strings", act: `""`, exp: `""`, msgs: []string{}}, {name: "different strings", act: `"hello"`, exp: `"world"`, msgs: []string{`expected string at '$' to be 'world' but was 'hello'`}}, + {name: "different strings", act: `"lorem ipsum dolor sit amet lorem ipsum dolor sit amet"`, exp: `"lorem ipsum dolor sit amet lorem ipsum dolor sit amet why do I have to be the test string?"`, msgs: []string{ + `expected string at '$' to be +'lorem ipsum dolor sit amet lorem ipsum dolor sit amet why do I have to be the test string?' +but was +'lorem ipsum dolor sit amet lorem ipsum dolor sit amet'`, + }}, {name: "empty v non-empty string", act: `""`, exp: `"world"`, msgs: []string{`expected string at '$' to be 'world' but was ''`}}, {name: "identical objects", act: `{"hello": "world"}`, exp: `{"hello":"world"}`, msgs: []string{}}, {name: "different keys in objects", act: `{"world": "hello"}`, exp: `{"hello":"world"}`, msgs: []string{ @@ -57,6 +63,13 @@ func TestAssertf(t *testing.T) { `length of arrays at '$' were different. Expected array to be of length 0, but contained 1 element(s)`, `actual JSON at '$' was: [null], but expected JSON was: []`, }}, + {name: "non-empty array v empty array", act: `[1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0]`, exp: `[1,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0]`, msgs: []string{ + `length of arrays at '$' were different. Expected array to be of length 22, but contained 30 element(s)`, + `actual JSON at '$' was: +[1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0] +but expected JSON was: +[1,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0]`, + }}, {name: "identical non-empty arrays", act: `["hello"]`, exp: `["hello"]`, msgs: []string{}}, {name: "different non-empty arrays", act: `["hello"]`, exp: `["world"]`, msgs: []string{ `expected string at '$[0]' to be 'world' but was 'hello'`, diff --git a/string.go b/string.go index 6f4a66e..82e91e9 100644 --- a/string.go +++ b/string.go @@ -8,7 +8,11 @@ import ( func (a *Asserter) checkString(path, act, exp string) { if act != exp { - a.Printer.Errorf("expected string at '%s' to be '%s' but was '%s'", path, exp, act) + if len(exp+act) < 50 { + a.Printer.Errorf("expected string at '%s' to be '%s' but was '%s'", path, exp, act) + } else { + a.Printer.Errorf("expected string at '%s' to be\n'%s'\nbut was\n'%s'", path, exp, act) + } } }