@@ -1557,7 +1557,7 @@ func TestArgumentMatcherToPrintMismatch(t *testing.T) {
1557
1557
func TestClosestCallMismatchedArgumentInformationShowsTheClosest (t * testing.T ) {
1558
1558
defer func () {
1559
1559
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\)` ))
1561
1561
assert .Regexp (t , matchingExp , r )
1562
1562
}
1563
1563
}()
@@ -1569,10 +1569,46 @@ func TestClosestCallMismatchedArgumentInformationShowsTheClosest(t *testing.T) {
1569
1569
m .TheExampleMethod (1 , 1 , 2 )
1570
1570
}
1571
1571
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
+
1572
1608
func TestClosestCallMismatchedArgumentValueInformation (t * testing.T ) {
1573
1609
defer func () {
1574
1610
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\)` ))
1576
1612
assert .Regexp (t , matchingExp , r )
1577
1613
}
1578
1614
}()
@@ -1583,9 +1619,33 @@ func TestClosestCallMismatchedArgumentValueInformation(t *testing.T) {
1583
1619
_ = m .GetTime (1 )
1584
1620
}
1585
1621
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
+
1586
1646
func unexpectedCallRegex (method , calledArg , expectedArg , diff string ) string {
1587
1647
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` ,
1589
1649
rMethod , calledArg , rMethod , expectedArg , diff )
1590
1650
}
1591
1651
0 commit comments