Skip to content

Commit

Permalink
Merge pull request #31 from maxatome/containsKeyError
Browse files Browse the repository at this point in the history
ContainsKey error reworked
  • Loading branch information
maxatome authored Nov 2, 2018
2 parents 62a2360 + 45f8172 commit 4286567
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
16 changes: 12 additions & 4 deletions td_contains_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package testdeep

import (
"bytes"
"reflect"

"github.com/maxatome/go-testdeep/internal/ctxerr"
Expand Down Expand Up @@ -55,14 +56,21 @@ func ContainsKey(expectedValue interface{}) TestDeep {
return &c
}

func (c *tdContainsKey) doesNotContainKey(ctx ctxerr.Context, got interface{}) *ctxerr.Error {
func (c *tdContainsKey) doesNotContainKey(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error {
if ctx.BooleanError {
return ctxerr.BooleanError
}

keys := append(make([]reflect.Value, 0, got.Len()), got.MapKeys()...)

buf := bytes.NewBufferString("expected key: ")
buf.WriteString(util.ToString(c.expectedValue))
buf.WriteString("\n not in keys: ")
util.SliceToBuffer(buf, keys)

return ctx.CollectError(&ctxerr.Error{
Message: "does not contain key",
Got: got,
Expected: c,
Message: "does not contain key",
Summary: types.RawString(buf.String()),
})
}

Expand Down
25 changes: 15 additions & 10 deletions td_contains_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ func TestContainsKey(t *testing.T) {

checkError(t, got, testdeep.ContainsKey(35),
expectedError{
Message: mustBe("does not contain key"),
Path: mustBe("DATA"),
Got: mustContain("(int) 34"), // as well as other items in fact...
Expected: mustBe("ContainsKey((int) 35)"),
Message: mustBe("does not contain key"),
Path: mustBe("DATA"),
Summary: mustMatch(`expected key: \(int\) 35
not in keys: \(\(int\) (12|28|34),
\(int\) (12|28|34),
\(int\) (12|28|34)\)`),
}, testName)
}
}
Expand All @@ -53,10 +55,11 @@ func TestContainsKeyNil(t *testing.T) {

checkError(t, got, testdeep.ContainsKey((*uint8)(nil)),
expectedError{
Message: mustBe("does not contain key"),
Path: mustBe("DATA"),
Got: mustContain("12345642"),
Expected: mustBe("ContainsKey((*uint8)(<nil>))"),
Message: mustBe("does not contain key"),
Path: mustBe("DATA"),
Summary: mustMatch(`expected key: \(\*uint8\)\(<nil>\)
not in keys: \(\(\*int\)\((<nil>|.*12345642.*)\),
\(\*int\)\((<nil>|.*12345642.*)\)\)`),
}, testName)
}

Expand All @@ -66,8 +69,10 @@ func TestContainsKeyNil(t *testing.T) {
expectedError{
Message: mustBe("does not contain key"),
Path: mustBe("DATA"),
// Got
Expected: mustBe("ContainsKey(nil)"),
Summary: mustMatch(`expected key: nil
not in keys: \("(foo|bar|zip)",
"(foo|bar|zip)",
"(foo|bar|zip)"\)`),
})

checkError(t, "foobar", testdeep.ContainsKey(nil),
Expand Down

0 comments on commit 4286567

Please sign in to comment.