Skip to content

Commit b8943af

Browse files
authored
Merge pull request #33 from Quick/fix_mostRecentlyCalledMatching_nonequatable
Remove equatable requirement when using Spy.wasMostRecentlyCalled(matching:)
2 parents 8362ee5 + bda8e5d commit b8943af

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

Sources/Fakes/Spy/Spy+Testing.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ extension Spy {
6565
}
6666

6767
/// Returns whether the most recent call to the spy matches the given closure.
68-
public func wasMostRecentlyCalled(matching matcher: (Arguments) -> Bool) -> Bool where Arguments: Equatable {
68+
public func wasMostRecentlyCalled(matching matcher: (Arguments) -> Bool) -> Bool {
6969
guard let lastCall = calls.last else { return false }
7070
return matcher(lastCall)
7171
}

Tests/FakesTests/SpyTests+TestHelpers.swift

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,29 @@ struct SpyTestHelpersTest {
2020

2121
@Test func wasCalledWithArguments() {
2222
let spy = Spy<Int, Void>()
23+
let otherSpy = Spy<ANonEquatable, Void>()
2324

2425
spy(3)
26+
otherSpy(ANonEquatable(value: 3))
2527
#expect(spy.wasCalled(with: 3))
2628
#expect(spy.wasCalled(with: 2) == false)
2729

2830
#expect(spy.wasCalled(matching: { $0 == 3 }))
2931
#expect(spy.wasCalled(matching: { $0 == 2 }) == false)
3032

33+
#expect(otherSpy.wasCalled(matching: { $0.value == 3 }))
34+
#expect(otherSpy.wasCalled(matching: { $0.value == 2 }) == false)
35+
3136
spy(4)
37+
otherSpy(ANonEquatable(value: 4))
3238
#expect(spy.wasCalled(with: 3))
3339
#expect(spy.wasCalled(matching: { $0 == 3 }))
3440

3541
#expect(spy.wasCalled(with: 4))
3642
#expect(spy.wasCalled(matching: { $0 == 4 }))
43+
44+
#expect(otherSpy.wasCalled(matching: { $0.value == 4 }))
45+
#expect(otherSpy.wasCalled(matching: { $0.value == 3 }))
3746
}
3847

3948
@Test func wasCalledWithTimes() {
@@ -56,16 +65,19 @@ struct SpyTestHelpersTest {
5665

5766
@Test func wasCalledWithMultipleArgumentsAndTimes() {
5867
let spy = Spy<Int, Void>()
68+
let otherSpy = Spy<ANonEquatable, Void>()
5969

6070
spy(1)
71+
otherSpy(ANonEquatable(value: 1))
6172
spy(3)
73+
otherSpy(ANonEquatable(value: 3))
6274

6375
#expect(spy.wasCalled(with: [1, 3]))
6476
#expect(spy.wasCalled(with: [3, 1]) == false) // order matters
6577

6678
#expect(spy.wasCalled(
6779
matching: [
68-
{ $0 == 1},
80+
{ $0 == 1 },
6981
{ $0 == 3 }
7082
]
7183
))
@@ -75,24 +87,49 @@ struct SpyTestHelpersTest {
7587
{ $0 == 1 }
7688
]
7789
) == false)
90+
91+
#expect(otherSpy.wasCalled(
92+
matching: [
93+
{ $0.value == 1 },
94+
{ $0.value == 3 }
95+
]
96+
))
97+
#expect(otherSpy.wasCalled(
98+
matching: [
99+
{ $0.value == 3 },
100+
{ $0.value == 1 }
101+
]
102+
) == false)
78103
}
79104

80105
@Test func testMostRecentlyBeCalled() {
81106
let spy = Spy<Int, Void>()
107+
let otherSpy = Spy<ANonEquatable, Void>()
82108

83109
spy(1)
110+
otherSpy(ANonEquatable(value: 1))
84111
#expect(spy.wasMostRecentlyCalled(with: 1))
85112
#expect(spy.wasMostRecentlyCalled(with: 2) == false)
86113

87114
#expect(spy.wasMostRecentlyCalled(matching: { $0 == 1}))
88115
#expect(spy.wasMostRecentlyCalled(matching: { $0 == 2}) == false)
89116

117+
#expect(otherSpy.wasMostRecentlyCalled(matching: { $0.value == 1}))
118+
#expect(otherSpy.wasMostRecentlyCalled(matching: { $0.value == 2}) == false)
119+
90120
spy(2)
121+
otherSpy(ANonEquatable(value: 2))
91122
#expect(spy.wasMostRecentlyCalled(with: 1) == false)
92123
#expect(spy.wasMostRecentlyCalled(with: 2))
93124

94125
#expect(spy.wasMostRecentlyCalled(matching: { $0 == 1}) == false)
95126
#expect(spy.wasMostRecentlyCalled(matching: { $0 == 2}))
127+
128+
#expect(otherSpy.wasMostRecentlyCalled(matching: { $0.value == 1}) == false)
129+
#expect(otherSpy.wasMostRecentlyCalled(matching: { $0.value == 2}))
96130
}
97131
}
98132

133+
struct ANonEquatable {
134+
let value: Int
135+
}

0 commit comments

Comments
 (0)