Fix optional closure matching.

This commit is contained in:
Matyáš Kříž 2019-03-27 10:35:17 +01:00
parent e3ac95f5f0
commit 76f85110ac
2 changed files with 8 additions and 6 deletions

View File

@ -40,7 +40,7 @@ extension ParameterMatcher: OptionalMatchable where T: CuckooOptionalType {
public var optionalMatcher: ParameterMatcher<T.Wrapped?> {
return ParameterMatcher<T.Wrapped?> { other in
other.map { self.matchesFunction(T.from(optional: $0)) } ?? false
self.matchesFunction(T.from(optional: other))
}
}
}

View File

@ -149,13 +149,14 @@ class StubbingTest: XCTestCase {
when(stub.withThrows()).thenReturn(10)
when(stub.withNamedTuple(tuple: any())).thenReturn(11)
when(stub.subclassMethod()).thenReturn(12)
// when(stub.withOptionalClosureAndReturn(anyString(), closure: isNil())).thenReturn(2)
when(stub.withOptionalClosureAndReturn(anyString(), closure: isNil())).thenReturn(2)
when(stub.withOptionalClosureAndReturn(anyString(), closure: anyClosure())).thenReturn(3)
when(stub.withClosureAndParam(anyString(), closure: anyClosure())).thenReturn(3)
when(stub.withMultClosures(closure: anyClosure(), closureB: anyClosure(), closureC: anyClosure())).thenReturn(4)
when(stub.withThrowingClosure(closure: anyThrowingClosure())).thenReturn("throwing closure")
when(stub.withThrowingClosureThrows(closure: anyThrowingClosure())).thenReturn("closure throwing")
when(stub.withThrowingEscapingClosure(closure: anyThrowingClosure())).thenReturn("escaping closure")
// when(stub.withThrowingOptionalClosureThrows(closure: anyOptionalThrowingClosure())).thenReturn("optional closure throwing")
when(stub.withThrowingOptionalClosureThrows(closure: anyOptionalThrowingClosure())).thenReturn("optional closure throwing")
when(stub.methodWithParameter(anyString())).thenReturn("parameter string")
when(stub.methodWithParameter(anyInt())).thenReturn("parameter int")
@ -202,7 +203,8 @@ class StubbingTest: XCTestCase {
XCTAssertEqual(try! mock.withThrows(), 10)
XCTAssertEqual(mock.withNamedTuple(tuple: (a: "A", b: "B")), 11)
XCTAssertEqual(mock.subclassMethod(), 12)
XCTAssertEqual(mock.withOptionalClosureAndReturn("a", closure: nil), 2)
XCTAssertEqual(mock.withOptionalClosureAndReturn("a", closure: Optional.none), 2)
XCTAssertEqual(mock.withOptionalClosureAndReturn("a", closure: { _ in }), 3)
XCTAssertEqual(mock.withClosureAndParam("a", closure: { _ in 0 }), 3)
XCTAssertEqual(mock.withMultClosures(closure: { _ in 0 }, closureB: { _ in 1 }, closureC: { _ in 2 }), 4)
XCTAssertEqual(mock.withThrowingClosure { p throws in
@ -266,13 +268,13 @@ class StubbingTest: XCTestCase {
verify(mock, times(1)).withThrows()
verify(mock, times(1)).withNamedTuple(tuple: any())
verify(mock, times(1)).subclassMethod()
// verify(mock, times(1)).withOptionalClosureAndReturn(anyString(), closure: isNil())
verify(mock, times(1)).withOptionalClosureAndReturn(anyString(), closure: isNil())
verify(mock, times(1)).withClosureAndParam(anyString(), closure: anyClosure())
verify(mock, times(1)).withMultClosures(closure: anyClosure(), closureB: anyClosure(), closureC: anyClosure())
verify(mock, times(1)).withThrowingClosure(closure: anyThrowingClosure())
verify(mock, times(1)).withThrowingClosureThrows(closure: anyThrowingClosure())
verify(mock, times(1)).withThrowingEscapingClosure(closure: anyThrowingClosure())
// verify(mock, times(1)).withThrowingOptionalClosureThrows(closure: anyOptionalThrowingClosure())
verify(mock, times(1)).withThrowingOptionalClosureThrows(closure: anyOptionalThrowingClosure())
verify(mock, times(1)).methodWithParameter(anyString())
verify(mock, times(1)).methodWithParameter(anyInt())