Skip to content

Commit

Permalink
Restore type information to matched output message
Browse files Browse the repository at this point in the history
  • Loading branch information
gz-c authored and ernesto-jimenez committed Jun 9, 2018
1 parent e494407 commit f35b8ab
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
15 changes: 10 additions & 5 deletions mock/mock.go
Expand Up @@ -668,33 +668,38 @@ func (args Arguments) Diff(objects []interface{}) (string, int) {

for i := 0; i < maxArgCount; i++ {
var actual, expected interface{}
var actualFmt, expectedFmt string

if len(objects) <= i {
actual = "(Missing)"
actualFmt = "(Missing)"
} else {
actual = objects[i]
actualFmt = fmt.Sprintf("(%[1]T=%[1]v)", actual)
}

if len(args) <= i {
expected = "(Missing)"
expectedFmt = "(Missing)"
} else {
expected = args[i]
expectedFmt = fmt.Sprintf("(%[1]T=%[1]v)", expected)
}

if matcher, ok := expected.(argumentMatcher); ok {
if matcher.Matches(actual) {
output = fmt.Sprintf("%s\t%d: PASS: %v matched by %s\n", output, i, actual, matcher)
output = fmt.Sprintf("%s\t%d: PASS: %s matched by %s\n", output, i, actualFmt, matcher)
} else {
differences++
output = fmt.Sprintf("%s\t%d: PASS: %v not matched by %s\n", output, i, actual, matcher)
output = fmt.Sprintf("%s\t%d: PASS: %s not matched by %s\n", output, i, actualFmt, matcher)
}
} else if reflect.TypeOf(expected) == reflect.TypeOf((*AnythingOfTypeArgument)(nil)).Elem() {

// type checking
if reflect.TypeOf(actual).Name() != string(expected.(AnythingOfTypeArgument)) && reflect.TypeOf(actual).String() != string(expected.(AnythingOfTypeArgument)) {
// not match
differences++
output = fmt.Sprintf("%s\t%d: FAIL: type %v != type %s - %v\n", output, i, expected, reflect.TypeOf(actual).Name(), actual)
output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, expected, reflect.TypeOf(actual).Name(), actualFmt)
}

} else {
Expand All @@ -703,11 +708,11 @@ func (args Arguments) Diff(objects []interface{}) (string, int) {

if assert.ObjectsAreEqual(expected, Anything) || assert.ObjectsAreEqual(actual, Anything) || assert.ObjectsAreEqual(actual, expected) {
// match
output = fmt.Sprintf("%s\t%d: PASS: %v == %v\n", output, i, actual, expected)
output = fmt.Sprintf("%s\t%d: PASS: %s == %s\n", output, i, actualFmt, expectedFmt)
} else {
// not match
differences++
output = fmt.Sprintf("%s\t%d: FAIL: %v != %v\n", output, i, actual, expected)
output = fmt.Sprintf("%s\t%d: FAIL: %s != %s\n", output, i, actualFmt, expectedFmt)
}
}

Expand Down
20 changes: 10 additions & 10 deletions mock/mock_test.go
Expand Up @@ -1187,8 +1187,8 @@ func Test_Arguments_Diff(t *testing.T) {
diff, count = args.Diff([]interface{}{"Hello World", 456, "false"})

assert.Equal(t, 2, count)
assert.Contains(t, diff, `456 != 123`)
assert.Contains(t, diff, `false != true`)
assert.Contains(t, diff, `(int=456) != (int=123)`)
assert.Contains(t, diff, `(string=false) != (bool=true)`)

}

Expand All @@ -1200,7 +1200,7 @@ func Test_Arguments_Diff_DifferentNumberOfArgs(t *testing.T) {
diff, count = args.Diff([]interface{}{"string", 456, "false", "extra"})

assert.Equal(t, 3, count)
assert.Contains(t, diff, `extra != (Missing)`)
assert.Contains(t, diff, `(string=extra) != (Missing)`)

}

Expand Down Expand Up @@ -1242,7 +1242,7 @@ func Test_Arguments_Diff_WithAnythingOfTypeArgument_Failing(t *testing.T) {
diff, count = args.Diff([]interface{}{"string", 123, true})

assert.Equal(t, 1, count)
assert.Contains(t, diff, `string != type int - 123`)
assert.Contains(t, diff, `string != type int - (int=123)`)

}

Expand All @@ -1254,14 +1254,14 @@ func Test_Arguments_Diff_WithArgMatcher(t *testing.T) {

diff, count := args.Diff([]interface{}{"string", 124, true})
assert.Equal(t, 1, count)
assert.Contains(t, diff, `124 not matched by func(int) bool`)
assert.Contains(t, diff, `(int=124) not matched by func(int) bool`)

diff, count = args.Diff([]interface{}{"string", false, true})
assert.Equal(t, 1, count)
assert.Contains(t, diff, `false not matched by func(int) bool`)
assert.Contains(t, diff, `(bool=false) not matched by func(int) bool`)

diff, count = args.Diff([]interface{}{"string", 123, false})
assert.Contains(t, diff, `123 matched by func(int) bool`)
assert.Contains(t, diff, `(int=123) matched by func(int) bool`)

diff, count = args.Diff([]interface{}{"string", 123, true})
assert.Equal(t, 0, count)
Expand Down Expand Up @@ -1444,7 +1444,7 @@ func TestArgumentMatcherToPrintMismatch(t *testing.T) {
defer func() {
if r := recover(); r != nil {
matchingExp := regexp.MustCompile(
`\s+mock: Unexpected Method Call\s+-*\s+GetTime\(int\)\s+0: 1\s+The closest call I have is:\s+GetTime\(mock.argumentMatcher\)\s+0: mock.argumentMatcher\{.*?\}\s+Diff:.*1 not matched by func\(int\) bool`)
`\s+mock: Unexpected Method Call\s+-*\s+GetTime\(int\)\s+0: 1\s+The closest call I have is:\s+GetTime\(mock.argumentMatcher\)\s+0: mock.argumentMatcher\{.*?\}\s+Diff:.*\(int=1\) not matched by func\(int\) bool`)
assert.Regexp(t, matchingExp, r)
}
}()
Expand All @@ -1460,7 +1460,7 @@ func TestArgumentMatcherToPrintMismatch(t *testing.T) {
func TestClosestCallMismatchedArgumentInformationShowsTheClosest(t *testing.T) {
defer func() {
if r := recover(); r != nil {
matchingExp := regexp.MustCompile(unexpectedCallRegex(`TheExampleMethod(int,int,int)`, `0: 1\s+1: 1\s+2: 2`, `0: 1\s+1: 1\s+2: 1`, `0: PASS: 1 == 1\s+1: PASS: 1 == 1\s+2: FAIL: 2 != 1`))
matchingExp := regexp.MustCompile(unexpectedCallRegex(`TheExampleMethod(int,int,int)`, `0: 1\s+1: 1\s+2: 2`, `0: 1\s+1: 1\s+2: 1`, `0: PASS: \(int=1\) == \(int=1\)\s+1: PASS: \(int=1\) == \(int=1\)\s+2: FAIL: \(int=2\) != \(int=1\)`))
assert.Regexp(t, matchingExp, r)
}
}()
Expand All @@ -1475,7 +1475,7 @@ func TestClosestCallMismatchedArgumentInformationShowsTheClosest(t *testing.T) {
func TestClosestCallMismatchedArgumentValueInformation(t *testing.T) {
defer func() {
if r := recover(); r != nil {
matchingExp := regexp.MustCompile(unexpectedCallRegex(`GetTime(int)`, "0: 1", "0: 999", `0: FAIL: 1 != 999`))
matchingExp := regexp.MustCompile(unexpectedCallRegex(`GetTime(int)`, "0: 1", "0: 999", `0: FAIL: \(int=1\) != \(int=999\)`))
assert.Regexp(t, matchingExp, r)
}
}()
Expand Down

0 comments on commit f35b8ab

Please sign in to comment.