Skip to content

Commit eb8c41e

Browse files
uberboboyan-soubachov
authored andcommitted
Add more tests to mock package
1 parent a5830c5 commit eb8c41e

File tree

1 file changed

+63
-3
lines changed

1 file changed

+63
-3
lines changed

mock/mock_test.go

+63-3
Original file line numberDiff line numberDiff line change
@@ -1557,7 +1557,7 @@ func TestArgumentMatcherToPrintMismatch(t *testing.T) {
15571557
func TestClosestCallMismatchedArgumentInformationShowsTheClosest(t *testing.T) {
15581558
defer func() {
15591559
if r := recover(); r != nil {
1560-
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\)`))
1560+
matchingExp := regexp.MustCompile(unexpectedCallRegex(`TheExampleMethod(int,int,int)`, `0: 1\s+1: 1\s+2: 2`, `0: 1\s+1: 1\s+2: 1`, `Diff: 0: PASS: \(int=1\) == \(int=1\)\s+1: PASS: \(int=1\) == \(int=1\)\s+2: FAIL: \(int=2\) != \(int=1\)`))
15611561
assert.Regexp(t, matchingExp, r)
15621562
}
15631563
}()
@@ -1569,10 +1569,46 @@ func TestClosestCallMismatchedArgumentInformationShowsTheClosest(t *testing.T) {
15691569
m.TheExampleMethod(1, 1, 2)
15701570
}
15711571

1572+
func TestClosestCallFavorsFirstMock(t *testing.T) {
1573+
defer func() {
1574+
if r := recover(); r != nil {
1575+
diffRegExp := `Difference found in argument 0:\s+--- Expected\s+\+\+\+ Actual\s+@@ -2,4 \+2,4 @@\s+\(bool\) true,\s+- \(bool\) true,\s+- \(bool\) true\s+\+ \(bool\) false,\s+\+ \(bool\) false\s+}\s+`
1576+
matchingExp := regexp.MustCompile(unexpectedCallRegex(`TheExampleMethod7([]bool)`, `0: \[\]bool{true, false, false}`, `0: \[\]bool{true, true, true}`, diffRegExp))
1577+
assert.Regexp(t, matchingExp, r)
1578+
}
1579+
}()
1580+
1581+
m := new(TestExampleImplementation)
1582+
m.On("TheExampleMethod7", []bool{true, true, true}).Return(nil).Once()
1583+
m.On("TheExampleMethod7", []bool{false, false, false}).Return(nil).Once()
1584+
1585+
m.TheExampleMethod7([]bool{true, false, false})
1586+
}
1587+
1588+
func TestClosestCallUsesRepeatabilityToFindClosest(t *testing.T) {
1589+
defer func() {
1590+
if r := recover(); r != nil {
1591+
diffRegExp := `Difference found in argument 0:\s+--- Expected\s+\+\+\+ Actual\s+@@ -1,4 \+1,4 @@\s+\(\[\]bool\) \(len=3\) {\s+- \(bool\) false,\s+- \(bool\) false,\s+\+ \(bool\) true,\s+\+ \(bool\) true,\s+\(bool\) false\s+`
1592+
matchingExp := regexp.MustCompile(unexpectedCallRegex(`TheExampleMethod7([]bool)`, `0: \[\]bool{true, true, false}`, `0: \[\]bool{false, false, false}`, diffRegExp))
1593+
assert.Regexp(t, matchingExp, r)
1594+
}
1595+
}()
1596+
1597+
m := new(TestExampleImplementation)
1598+
m.On("TheExampleMethod7", []bool{true, true, true}).Return(nil).Once()
1599+
m.On("TheExampleMethod7", []bool{false, false, false}).Return(nil).Once()
1600+
1601+
m.TheExampleMethod7([]bool{true, true, true})
1602+
1603+
// Since the first mocked call has already been used, it now has no repeatability,
1604+
// thus the second mock should be shown as the closest match
1605+
m.TheExampleMethod7([]bool{true, true, false})
1606+
}
1607+
15721608
func TestClosestCallMismatchedArgumentValueInformation(t *testing.T) {
15731609
defer func() {
15741610
if r := recover(); r != nil {
1575-
matchingExp := regexp.MustCompile(unexpectedCallRegex(`GetTime(int)`, "0: 1", "0: 999", `0: FAIL: \(int=1\) != \(int=999\)`))
1611+
matchingExp := regexp.MustCompile(unexpectedCallRegex(`GetTime(int)`, "0: 1", "0: 999", `Diff: 0: FAIL: \(int=1\) != \(int=999\)`))
15761612
assert.Regexp(t, matchingExp, r)
15771613
}
15781614
}()
@@ -1583,9 +1619,33 @@ func TestClosestCallMismatchedArgumentValueInformation(t *testing.T) {
15831619
_ = m.GetTime(1)
15841620
}
15851621

1622+
func Test_isBetterMatchThanReturnsFalseIfCandidateCallIsNil(t *testing.T) {
1623+
assert.False(t, matchCandidate{}.isBetterMatchThan(matchCandidate{}))
1624+
}
1625+
1626+
func Test_isBetterMatchThanReturnsTrueIfOtherCandidateCallIsNil(t *testing.T) {
1627+
assert.True(t, matchCandidate{call: &Call{}}.isBetterMatchThan(matchCandidate{}))
1628+
}
1629+
1630+
func Test_isBetterMatchThanReturnsFalseIfDiffCountIsGreaterThanOther(t *testing.T) {
1631+
assert.False(t, matchCandidate{call: &Call{}, diffCount: 2}.isBetterMatchThan(matchCandidate{call: &Call{}, diffCount: 1}))
1632+
}
1633+
1634+
func Test_isBetterMatchThanReturnsTrueIfDiffCountIsLessThanOther(t *testing.T) {
1635+
assert.True(t, matchCandidate{call: &Call{}, diffCount: 1}.isBetterMatchThan(matchCandidate{call: &Call{}, diffCount: 2}))
1636+
}
1637+
1638+
func Test_isBetterMatchThanReturnsTrueIfRepeatabilityIsGreaterThanOther(t *testing.T) {
1639+
assert.True(t, matchCandidate{call: &Call{Repeatability: 1}, diffCount: 1}.isBetterMatchThan(matchCandidate{call: &Call{Repeatability: -1}, diffCount: 1}))
1640+
}
1641+
1642+
func Test_isBetterMatchThanReturnsFalseIfRepeatabilityIsLessThanOrEqualToOther(t *testing.T) {
1643+
assert.False(t, matchCandidate{call: &Call{Repeatability: 1}, diffCount: 1}.isBetterMatchThan(matchCandidate{call: &Call{Repeatability: 1}, diffCount: 1}))
1644+
}
1645+
15861646
func unexpectedCallRegex(method, calledArg, expectedArg, diff string) string {
15871647
rMethod := regexp.QuoteMeta(method)
1588-
return fmt.Sprintf(`\s+mock: Unexpected Method Call\s+-*\s+%s\s+%s\s+The closest call I have is:\s+%s\s+%s\s+Diff: %s`,
1648+
return fmt.Sprintf(`\s+mock: Unexpected Method Call\s+-*\s+%s\s+%s\s+The closest call I have is:\s+%s\s+%s\s+%s`,
15891649
rMethod, calledArg, rMethod, expectedArg, diff)
15901650
}
15911651

0 commit comments

Comments
 (0)