Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pers: add an 'Any' value that can be used in matchers #48

Merged
merged 1 commit into from
Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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