Skip to content

Commit

Permalink
be friendlier with the assert message when the JSON representation ar…
Browse files Browse the repository at this point in the history
…e very long
  • Loading branch information
kinbiko committed Feb 1, 2019
1 parent 13003a0 commit edb4599
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
7 changes: 6 additions & 1 deletion array.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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'`,
Expand Down
6 changes: 5 additions & 1 deletion string.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

Expand Down

0 comments on commit edb4599

Please sign in to comment.