Skip to content

Commit

Permalink
Merge pull request #48 from nelsam/any-value
Browse files Browse the repository at this point in the history
pers: add an 'Any' value that can be used in matchers
  • Loading branch information
nelsam committed Mar 18, 2020
2 parents 2f86516 + 6c02eed commit cf10f1a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
14 changes: 10 additions & 4 deletions pers/havemethodexecuted.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ import (
"time"
)

type any int

// Any is a special value to tell pers to allow any value at the position used.
// For example, you can assert only on the second argument with:
// HaveMethodExecuted("Foo", WithArgs(Any, 22))
const Any any = -1

// HaveMethodExecutedOption is an option function for the HaveMethodExecutedMatcher.
type HaveMethodExecutedOption func(HaveMethodExecutedMatcher) HaveMethodExecutedMatcher

Expand Down Expand Up @@ -145,11 +152,10 @@ func diff(actual, expected interface{}) (matched bool, actualOutput, expectedOut
return diffV(reflect.ValueOf(actual), reflect.ValueOf(expected))
}

type span struct {
start, end int
}

func diffV(av, ev reflect.Value) (matched bool, actualOutput, expectedOutput string) {
if ev.Interface() == Any {
return true, fmt.Sprintf("%#v", av.Interface()), "{ANY}"
}
if av.Kind() != ev.Kind() {
format := ">type mismatch: %#v<"
return false, fmt.Sprintf(format, av.Interface()), fmt.Sprintf(format, ev.Interface())
Expand Down
15 changes: 11 additions & 4 deletions pers/havemethodexecuted_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ func TestHaveMethodExecuted(t *testing.T) {

for _, test := range []struct {
name string
arg0 int
arg1 string
arg0 interface{}
arg1 interface{}
err error
}{
{
Expand All @@ -164,18 +164,25 @@ func TestHaveMethodExecuted(t *testing.T) {
err: errors.New(`pers: Foo was called with (>123<, "this is a value"); expected (>122<, "this is a value")`),
},
{
name: "fails due to a mismatch on the first argument",
name: "fails due to a mismatch on the second argument",
arg0: 123,
arg1: "this is a val",
err: errors.New(`pers: Foo was called with (123, >"this is a value"<); expected (122, >"this is a val"<)`),
err: errors.New(`pers: Foo was called with (123, >"this is a value"<); expected (123, >"this is a val"<)`),
},
{
name: "passes when arguments match",
arg0: 123,
arg1: "this is a value",
err: nil,
},
{
name: "passes when Any is passed in",
arg0: pers.Any,
arg1: pers.Any,
err: nil,
},
} {
test := test
o.Spec(test.name, func(t *testing.T, expect expectation) {
fm := newFakeMock()
fm.FooOutput.Err <- nil
Expand Down

0 comments on commit cf10f1a

Please sign in to comment.