TRAVIS_OS_NAME=osx TRAVIS_RUBY_VERSION=default <<<<<< ENV .swift-version Example.playground/Contents.swift Example.playground/contents.xcplayground InstantMock.podspec InstantMock.xcodeproj/project.pbxproj InstantMock.xcodeproj/project.xcworkspace/contents.xcworkspacedata InstantMock.xcodeproj/xcshareddata/xcschemes/InstantMock iOS.xcscheme InstantMock.xcodeproj/xcshareddata/xcschemes/InstantMock macOS.xcscheme InstantMock.xcodeproj/xcshareddata/xcschemes/InstantMock tvOS.xcscheme InstantMock.xcodeproj/xcshareddata/xcschemes/InstantMockTests iOS.xcscheme InstantMock.xcodeproj/xcshareddata/xcschemes/InstantMockTests macOS.xcscheme InstantMock.xcodeproj/xcshareddata/xcschemes/InstantMockTests tvOS.xcscheme InstantMock.xcworkspace/contents.xcworkspacedata InstantMock.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist InstantMock.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings LICENSE Package.pins Package.swift Sources/Info.plist Sources/InstantMock.h Sources/InstantMock/Arguments/Arg+Closure.swift Sources/InstantMock/Arguments/Arg.swift Sources/InstantMock/Arguments/Argument/Argument.swift Sources/InstantMock/Arguments/Argument/ArgumentAny.swift Sources/InstantMock/Arguments/Argument/ArgumentCapture.swift Sources/InstantMock/Arguments/Argument/ArgumentClosure.swift Sources/InstantMock/Arguments/Argument/ArgumentValue.swift Sources/InstantMock/Arguments/Argument/ArgumentVerify/ArgumentVerify.swift Sources/InstantMock/Arguments/Argument/ArgumentVerify/ArgumentVerifyMandatory.swift Sources/InstantMock/Arguments/Argument/ArgumentVerify/ArgumentVerifyOptional.swift Sources/InstantMock/Arguments/Captor/ArgumentCaptor.swift Sources/InstantMock/Arguments/Captor/ArgumentCaptorValues.swift Sources/InstantMock/Arguments/Captor/ArgumentClosureCaptor.swift Sources/InstantMock/Arguments/Captor/ArgumentClosureCaptor/ArgumentClosureCaptor+FiveArgs.swift Sources/InstantMock/Arguments/Captor/ArgumentClosureCaptor/ArgumentClosureCaptor+FourArgs.swift Sources/InstantMock/Arguments/Captor/ArgumentClosureCaptor/ArgumentClosureCaptor+NoArg.swift Sources/InstantMock/Arguments/Captor/ArgumentClosureCaptor/ArgumentClosureCaptor+OneArg.swift Sources/InstantMock/Arguments/Captor/ArgumentClosureCaptor/ArgumentClosureCaptor+ThreeArgs.swift Sources/InstantMock/Arguments/Captor/ArgumentClosureCaptor/ArgumentClosureCaptor+TwoArgs.swift Sources/InstantMock/Arguments/Helpers/ArgumentFactory.swift Sources/InstantMock/Arguments/Helpers/ArgumentStorage.swift Sources/InstantMock/Arguments/Helpers/ArgumentsConfiguration.swift Sources/InstantMock/Arguments/Helpers/ArgumentsMatcher.swift Sources/InstantMock/DefaultValue/DefaultClosureHandler.swift Sources/InstantMock/DefaultValue/DefaultValueHandler.swift Sources/InstantMock/Factories/ExpectationFactory.swift Sources/InstantMock/Interceptors/CallInterceptor.swift Sources/InstantMock/Interceptors/Expectation.swift Sources/InstantMock/Interceptors/Helpers/CallConfiguration.swift Sources/InstantMock/Interceptors/Helpers/CallInterceptorStorage.swift Sources/InstantMock/Interceptors/Stub.swift Sources/InstantMock/Mock.swift Sources/InstantMock/MockUsable/Array+MockUsable.swift Sources/InstantMock/MockUsable/Bool+MockUsable.swift Sources/InstantMock/MockUsable/Dictionary+MockUsable.swift Sources/InstantMock/MockUsable/Double+MockUsable.swift Sources/InstantMock/MockUsable/Int+MockUsable.swift Sources/InstantMock/MockUsable/MockUsable.swift Sources/InstantMock/MockUsable/Set+MockUsable.swift Sources/InstantMock/MockUsable/String+MockUsable.swift Sources/InstantMock/Property/Property.swift Sources/InstantMock/Verifications/Assertion.swift Sources/InstantMock/Verifications/Verifier.swift Tests/Info.plist Tests/InstantMockTests/Integration Tests/BasicMockTests.swift Tests/InstantMockTests/Integration Tests/CaptureMockTests.swift Tests/InstantMockTests/Integration Tests/ClosureMockTests.swift Tests/InstantMockTests/Integration Tests/MockCreationModalityTests.swift Tests/InstantMockTests/Integration Tests/ThrowingMockTests.swift Tests/InstantMockTests/Integration Tests/TypesMockTests.swift Tests/InstantMockTests/Integration Tests/VerifyMockTests.swift Tests/InstantMockTests/Mocks/ArgumentAnyMock.swift Tests/InstantMockTests/Mocks/ArgumentCaptureMock.swift Tests/InstantMockTests/Mocks/ArgumentClosureMock.swift Tests/InstantMockTests/Mocks/ArgumentFactoryMock.swift Tests/InstantMockTests/Mocks/ArgumentStorageMock.swift Tests/InstantMockTests/Mocks/ArgumentValueMock.swift Tests/InstantMockTests/Mocks/ArgumentVerifyMandatoryMock.swift Tests/InstantMockTests/Mocks/ArgumentVerifyOptionalMock.swift Tests/InstantMockTests/Mocks/AssertionMock.swift Tests/InstantMockTests/Mocks/ExpectationFactoryMock.swift Tests/InstantMockTests/Mocks/VerifierMock.swift Tests/InstantMockTests/Unit Tests/Arguments/Arg+ClosureTests.swift Tests/InstantMockTests/Unit Tests/Arguments/Arg/ArgumentAnyTests.swift Tests/InstantMockTests/Unit Tests/Arguments/Arg/ArgumentCaptureTests.swift Tests/InstantMockTests/Unit Tests/Arguments/Arg/ArgumentClosureTests.swift Tests/InstantMockTests/Unit Tests/Arguments/Arg/ArgumentValueTests.swift Tests/InstantMockTests/Unit Tests/Arguments/Arg/ArgumentVerifyMandatoryTests.swift Tests/InstantMockTests/Unit Tests/Arguments/Arg/ArgumentVerifyOptionalTests.swift Tests/InstantMockTests/Unit Tests/Arguments/ArgTests.swift Tests/InstantMockTests/Unit Tests/Arguments/Captor/ArgumentCaptorTests.swift Tests/InstantMockTests/Unit Tests/Arguments/Captor/ArgumentCaptorValuesTests.swift Tests/InstantMockTests/Unit Tests/Arguments/Captor/ArgumentClosureCaptor/ArgumentClosureCaptor+TwoArgs.swift Tests/InstantMockTests/Unit Tests/Arguments/Captor/ArgumentClosureCaptor/ArgumentClosureCaptorTests+FiveArgs.swift Tests/InstantMockTests/Unit Tests/Arguments/Captor/ArgumentClosureCaptor/ArgumentClosureCaptorTests+FourArgs.swift Tests/InstantMockTests/Unit Tests/Arguments/Captor/ArgumentClosureCaptor/ArgumentClosureCaptorTests+NoArg.swift Tests/InstantMockTests/Unit Tests/Arguments/Captor/ArgumentClosureCaptor/ArgumentClosureCaptorTests+OneArg.swift Tests/InstantMockTests/Unit Tests/Arguments/Captor/ArgumentClosureCaptor/ArgumentClosureCaptorTests+ThreeArgs.swift Tests/InstantMockTests/Unit Tests/Arguments/Helpers/ArgumentStorageTests.swift Tests/InstantMockTests/Unit Tests/Arguments/Helpers/ArgumentsConfigurationTests.swift Tests/InstantMockTests/Unit Tests/Arguments/Helpers/ArgumentsMatcherTests.swift Tests/InstantMockTests/Unit Tests/DefaultValue/DefaultClosureHandlerTests.swift Tests/InstantMockTests/Unit Tests/DefaultValue/DefaultValueHandlerTests.swift Tests/InstantMockTests/Unit Tests/Factories/ExpectationFactoryTests.swift Tests/InstantMockTests/Unit Tests/Interceptors/CallInterceptorStorageTests.swift Tests/InstantMockTests/Unit Tests/Interceptors/CallInterceptorTests.swift Tests/InstantMockTests/Unit Tests/Interceptors/ExpectationTests.swift Tests/InstantMockTests/Unit Tests/Interceptors/Helpers/CallConfigurationTests.swift Tests/InstantMockTests/Unit Tests/Interceptors/StubTests.swift Tests/InstantMockTests/Unit Tests/MockUsable/ArrayMockUsableTests.swift Tests/InstantMockTests/Unit Tests/MockUsable/BoolMockUsableTests.swift Tests/InstantMockTests/Unit Tests/MockUsable/DictionaryMockUsableTests.swift Tests/InstantMockTests/Unit Tests/MockUsable/DoubleMockUsableTests.swift Tests/InstantMockTests/Unit Tests/MockUsable/IntMockUsableTests.swift Tests/InstantMockTests/Unit Tests/MockUsable/SetMockUsableTests.swift Tests/InstantMockTests/Unit Tests/MockUsable/StringMockUsableTests.swift Tests/InstantMockTests/Unit Tests/Verifications/VerifierTests.swift Tests/LinuxMain.swift <<<<<< network # path=InstantMockTestsiOS.xctest.coverage.txt /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Integration Tests/BasicMockTests.swift: 1| |// 2| |// BasicMockTests.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 06/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import XCTest 10| |@testable import InstantMock 11| | 12| | 13| | 14| |protocol BasicProtocol { 15| | var prop: String { get set } 16| | func basic(arg1: String, arg2: Int) -> String 17| | func basicOpt(arg1: String?, arg2: Int?) -> String? 18| | func basicType(type: String.Type) 19| |} 20| | 21| | 22| | 23| |final class BasicMock: Mock, BasicProtocol { 24| | 25| | var prop: String { 26| 5| get { return super.call()! } 27| 3| set { super.call(newValue) } 28| | } 29| | 30| 26| func basic(arg1: String, arg2: Int) -> String { 31| 26| return super.call(arg1, arg2)! 32| 26| } 33| | 34| 4| func basicOpt(arg1: String?, arg2: Int?) -> String? { 35| 4| return super.call(arg1, arg2) 36| 4| } 37| | 38| 2| func basicType(type: String.Type) { 39| 2| return super.call(type) 40| 2| } 41| | 42| |} 43| | 44| | 45| | 46| |final class BasicMockTests: XCTestCase { 47| | 48| | private var mock: BasicMock! 49| | private var assertionMock: AssertionMock! 50| | 51| | 52| 17| override func setUp() { 53| 17| super.setUp() 54| 17| self.assertionMock = AssertionMock() 55| 17| let expectationFactory = ExpectationFactoryMock(withAssertionMock: self.assertionMock) 56| 17| self.mock = BasicMock(expectationFactory) 57| 17| } 58| | 59| | 60| | static var allTests = [ 61| | ("testExpect", testExpect), 62| | ("testExpect_optional_nil", testExpect_optional_nil), 63| | ("testExpect_optional_nonnil", testExpect_optional_nonnil), 64| | ("testExpect_count", testExpect_count), 65| | ("testExpect_count_zero", testExpect_count_zero), 66| | ("testExpect_type", testExpect_type), 67| | ("testReject", testReject), 68| | ("testReject_count", testReject_count), 69| | ("testStub", testStub), 70| | ("testSeveralStubs", testSeveralStubs), 71| | ("testExpectAndStub", testExpectAndStub), 72| | ("testStub_returnAndDo", testStub_returnAndDo), 73| | ("testExpectProperty_value", testExpectProperty_value), 74| | ("testExpectProperty_any", testExpectProperty_any), 75| | ("testExpectProperty_value_setter_getter", testExpectProperty_value_setter_getter), 76| | ("testResetExpectations", testResetExpectations), 77| | ("testResetStubs", testResetStubs), 78| | ] 79| | 80| | 81| 1| func testExpect() { 82| 1| mock.expect().call(mock.basic(arg1: Arg.eq("Hello"), arg2: Arg.any())) 83| 1| mock.verify() 84| 1| XCTAssertFalse(self.assertionMock.succeeded) 85| 1| 86| 1| _ = mock.basic(arg1: "Hello", arg2: 2) 87| 1| mock.verify() 88| 1| XCTAssertTrue(self.assertionMock.succeeded) 89| 1| } 90| | 91| | 92| 1| func testExpect_optional_nil() { 93| 1| mock.expect().call(mock.basicOpt(arg1: Arg.eq(nil), arg2: Arg.any())) 94| 1| 95| 1| _ = mock.basicOpt(arg1: nil, arg2: nil) 96| 1| mock.verify() 97| 1| XCTAssertTrue(self.assertionMock.succeeded) 98| 1| } 99| | 100| | 101| 1| func testExpect_optional_nonnil() { 102| 1| mock.expect().call(mock.basicOpt(arg1: Arg.eq("Hello"), arg2: Arg.any())) 103| 1| 104| 1| _ = mock.basicOpt(arg1: "Hello", arg2: 12) 105| 1| mock.verify() 106| 1| XCTAssertTrue(self.assertionMock.succeeded) 107| 1| } 108| | 109| | 110| 1| func testExpect_count() { 111| 1| mock.expect().call(mock.basic(arg1: Arg.eq("Hello"), arg2: Arg.any()), count: 2) 112| 1| mock.verify() 113| 1| XCTAssertFalse(self.assertionMock.succeeded) 114| 1| 115| 1| _ = mock.basic(arg1: "Hello", arg2: 2) 116| 1| mock.verify() 117| 1| XCTAssertFalse(self.assertionMock.succeeded) 118| 1| 119| 1| _ = mock.basic(arg1: "Hello", arg2: 3) 120| 1| mock.verify() 121| 1| XCTAssertTrue(self.assertionMock.succeeded) 122| 1| } 123| | 124| | 125| 1| func testExpect_count_zero() { 126| 1| mock.expect().call(mock.basic(arg1: Arg.eq("Hello2"), arg2: Arg.any()), count: 0) 127| 1| _ = mock.basic(arg1: "Hello", arg2: 3) 128| 1| mock.verify() 129| 1| XCTAssertTrue(self.assertionMock.succeeded) 130| 1| } 131| | 132| | 133| 1| func testExpect_type() { 134| 1| mock.expect().call(mock.basicType(type: Arg.eq(String.self))) 135| 1| mock.basicType(type: String.self) 136| 1| mock.verify() 137| 1| XCTAssertTrue(self.assertionMock.succeeded) 138| 1| } 139| | 140| | 141| 1| func testReject() { 142| 1| mock.reject().call(mock.basic(arg1: Arg.eq("Hello"), arg2: Arg.any())) 143| 1| mock.verify() 144| 1| XCTAssertTrue(self.assertionMock.succeeded) 145| 1| 146| 1| _ = mock.basic(arg1: "Hello", arg2: 2) 147| 1| mock.verify() 148| 1| XCTAssertFalse(self.assertionMock.succeeded) 149| 1| } 150| | 151| | 152| 1| func testReject_count() { 153| 1| mock.reject().call(mock.basic(arg1: Arg.eq("Hello"), arg2: Arg.any()), count: 2) 154| 1| mock.verify() 155| 1| XCTAssertTrue(self.assertionMock.succeeded) 156| 1| 157| 1| _ = mock.basic(arg1: "Hello", arg2: 2) 158| 1| mock.verify() 159| 1| XCTAssertTrue(self.assertionMock.succeeded) 160| 1| 161| 1| _ = mock.basic(arg1: "Hello", arg2: 3) 162| 1| mock.verify() 163| 1| XCTAssertFalse(self.assertionMock.succeeded) 164| 1| } 165| | 166| | 167| 1| func testStub() { 168| 1| var callbackValue: String? 169| 1| mock.stub().call(mock.basic(arg1: Arg.eq("Hello"), arg2: Arg.any())).andReturn("string").andDo { _ in 170| 1| callbackValue = "something" 171| 1| } 172| 1| 173| 1| let ret = mock.basic(arg1: "Hello", arg2: 2) 174| 1| XCTAssertEqual(ret, "string") 175| 1| XCTAssertEqual(callbackValue, "something") 176| 1| } 177| | 178| | 179| 1| func testSeveralStubs() { 180| 1| 181| 1| mock.stub().call(mock.basic(arg1: Arg.eq("Hello"), arg2: Arg.any())).andReturn("string") 182| 1| mock.stub().call(mock.basic(arg1: Arg.eq("Hello"), arg2: Arg.eq(2))).andReturn("string2") 183| 1| 184| 1| let ret = mock.basic(arg1: "Hello", arg2: 2) 185| 1| XCTAssertEqual(ret, "string2") 186| 1| } 187| | 188| | 189| 1| func testExpectAndStub() { 190| 1| 191| 1| var callbackValue: String? 192| 1| mock.expect().call(mock.basic(arg1: Arg.eq("Hello"), arg2: Arg.any())).andReturn("string").andDo {_ in 193| 1| callbackValue = "something" 194| 1| } 195| 1| 196| 1| let ret = mock.basic(arg1: "Hello", arg2: 2) 197| 1| XCTAssertEqual(ret, "string") 198| 1| XCTAssertEqual(callbackValue, "something") 199| 1| } 200| | 201| | 202| 1| func testStub_returnAndDo() { 203| 1| 204| 1| var ret = "" 205| 2| mock.stub().call(mock.basic(arg1: Arg.eq("Hello"), arg2: Arg.any())).andReturn(closure: { _ in 206| 2| ret = ret + "a" 207| 2| return ret 208| 2| }) 209| 1| 210| 1| var retValue = mock.basic(arg1: "Hello", arg2: 2) 211| 1| XCTAssertEqual(retValue, "a") 212| 1| 213| 1| retValue = mock.basic(arg1: "Hello", arg2: 2) 214| 1| XCTAssertEqual(retValue, "aa") 215| 1| } 216| | 217| | 218| 1| func testExpectProperty_value() { 219| 1| mock.expect().call(mock.property.set(mock.prop, value: Arg.eq("test"))) 220| 1| mock.verify() 221| 1| XCTAssertFalse(self.assertionMock.succeeded) 222| 1| 223| 1| mock.prop = "test" 224| 1| mock.verify() 225| 1| XCTAssertTrue(self.assertionMock.succeeded) 226| 1| } 227| | 228| | 229| 1| func testExpectProperty_any() { 230| 1| mock.expect().call(mock.property.set(mock.prop, value: Arg.any())) 231| 1| mock.verify() 232| 1| XCTAssertFalse(self.assertionMock.succeeded) 233| 1| 234| 1| mock.prop = "test" 235| 1| mock.verify() 236| 1| XCTAssertTrue(self.assertionMock.succeeded) 237| 1| } 238| | 239| | 240| | // test dedicated to making sure there is no mixup between property setter and getter 241| 1| func testExpectProperty_value_setter_getter() { 242| 1| mock.expect().call(mock.property.set(mock.prop, value: Arg.eq("test"))) 243| 1| mock.expect().call(mock.prop).andReturn("test2") 244| 1| 245| 1| mock.prop = "test" 246| 1| let ret = mock.prop 247| 1| 248| 1| mock.verify() 249| 1| XCTAssertEqual(ret, "test2") 250| 1| XCTAssertTrue(self.assertionMock.succeeded) 251| 1| } 252| | 253| | 254| 1| func testResetExpectations() { 255| 1| mock.reject().call(mock.basic(arg1: Arg.any(), arg2: Arg.any())) 256| 1| mock.resetExpectations() 257| 1| _ = mock.basic(arg1: "", arg2: 0) 258| 1| mock.verify() 259| 1| } 260| | 261| | 262| 1| func testResetStubs() { 263| 1| mock.stub().call(mock.basic(arg1: Arg.any(), arg2: Arg.any())).andReturn("string") 264| 1| mock.resetStubs() 265| 1| let ret = mock.basic(arg1: "", arg2: 2) 266| 1| XCTAssertNotEqual(ret, "string") 267| 1| } 268| | 269| |} /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Integration Tests/CaptureMockTests.swift: 1| |// 2| |// CaptureMockTests.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 13/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import XCTest 10| |@testable import InstantMock 11| | 12| | 13| |final class SomeCaptureObject {} 14| | 15| |protocol CaptureProtocol { 16| | 17| | // basic cases 18| | func someFunc(arg1: String, arg2: Int) -> String 19| | func someFunc(arg: String, closure: @escaping ((Int, SomeCaptureObject) -> String)) 20| | 21| | // number of args for closure 22| | func otherFuncNoArg(closure: @escaping () -> Int) 23| | func otherFuncOneArg(closure: @escaping (Int) -> Int) 24| | func otherFuncTwoArgs(closure: @escaping (Int, Int) -> Int) 25| | func otherFuncThreeArgs(closure: @escaping (Int, Int, Int) -> Int) 26| | func otherFuncFourArgs(closure: @escaping (Int, Int, Int, Int) -> Int) 27| | func otherFuncFiveArgs(closure: @escaping (Int, Int, Int, Int, Int) -> Int) 28| |} 29| | 30| | 31| |final class CaptureMock: Mock, CaptureProtocol { 32| | 33| 3| func someFunc(arg1: String, arg2: Int) -> String { 34| 3| return super.call(arg1, arg2)! 35| 3| } 36| | 37| 2| func someFunc(arg: String, closure: @escaping ((Int, SomeCaptureObject) -> String)) { 38| 2| super.call(arg, closure) 39| 2| } 40| | 41| 2| func otherFuncNoArg(closure: @escaping () -> Int) { 42| 2| super.call(closure) 43| 2| } 44| | 45| 2| func otherFuncOneArg(closure: @escaping (Int) -> Int) { 46| 2| super.call(closure) 47| 2| } 48| | 49| 2| func otherFuncTwoArgs(closure: @escaping (Int, Int) -> Int) { 50| 2| super.call(closure) 51| 2| } 52| | 53| 2| func otherFuncThreeArgs(closure: @escaping (Int, Int, Int) -> Int) { 54| 2| super.call(closure) 55| 2| } 56| | 57| 2| func otherFuncFourArgs(closure: @escaping (Int, Int, Int, Int) -> Int) { 58| 2| super.call(closure) 59| 2| } 60| | 61| 2| func otherFuncFiveArgs(closure: @escaping (Int, Int, Int, Int, Int) -> Int) { 62| 2| super.call(closure) 63| 2| } 64| | 65| |} 66| | 67| | 68| | 69| |final class CaptureMockTests: XCTestCase { 70| | 71| | private var mock: CaptureMock! 72| | private var assertionMock: AssertionMock! 73| | 74| | 75| 8| override func setUp() { 76| 8| super.setUp() 77| 8| self.assertionMock = AssertionMock() 78| 8| let expectationFactory = ExpectationFactoryMock(withAssertionMock: self.assertionMock) 79| 8| self.mock = CaptureMock(expectationFactory) 80| 8| } 81| | 82| | 83| | static var allTests = [ 84| | ("testExpect_capture", testExpect_capture), 85| | ("testExpect_capture_closure", testExpect_capture_closure), 86| | ("testExpect_capture_closure_noArg", testExpect_capture_closure_noArg), 87| | ("testExpect_capture_closure_oneArg", testExpect_capture_closure_oneArg), 88| | ("testExpect_capture_closure_twoArgs", testExpect_capture_closure_twoArgs), 89| | ("testExpect_capture_closure_threeArgs", testExpect_capture_closure_threeArgs), 90| | ("testExpect_capture_closure_fourArgs", testExpect_capture_closure_fourArgs), 91| | ("testExpect_capture_closure_fiveArgs", testExpect_capture_closure_fiveArgs), 92| | ] 93| | 94| | 95| 1| func testExpect_capture() { 96| 1| 97| 1| let captureStr = ArgumentCaptor() 98| 1| let captureInt = ArgumentCaptor() 99| 1| mock.expect().call(mock.someFunc(arg1: captureStr.capture(), arg2: captureInt.capture())) 100| 1| 101| 1| _ = mock.someFunc(arg1: "Paris", arg2: 32) 102| 1| mock.verify() 103| 1| XCTAssertTrue(self.assertionMock.succeeded) 104| 1| 105| 1| XCTAssertEqual(captureStr.value, "Paris") 106| 1| XCTAssertEqual(captureInt.value, 32) 107| 1| 108| 1| XCTAssertEqual(captureStr.allValues.first!, "Paris") 109| 1| XCTAssertEqual(captureInt.allValues.first!, 32) 110| 1| 111| 1| _ = mock.someFunc(arg1: "London", arg2: 42) 112| 1| XCTAssertEqual(captureStr.value, "London") 113| 1| XCTAssertEqual(captureInt.value, 42) 114| 1| 115| 2| XCTAssertEqual(captureStr.allValues.compactMap {$0}, ["Paris", "London"]) 116| 2| XCTAssertEqual(captureInt.allValues.compactMap {$0}, [32, 42]) 117| 1| } 118| | 119| | 120| 1| func testExpect_capture_closure() { 121| 1| 122| 1| let captor = ArgumentClosureCaptor<(Int, SomeCaptureObject) -> String>() 123| 1| mock.expect().call(mock.someFunc(arg: Arg.any(), closure: captor.capture())) 124| 1| 125| 1| mock.someFunc(arg: "Hello") { (num, obj) -> String in 126| 1| return "\(num)" 127| 1| } 128| 1| 129| 1| mock.verify() 130| 1| XCTAssertTrue(self.assertionMock.succeeded) 131| 1| 132| 1| XCTAssertNotNil(captor.value) 133| 1| let ret = captor.value!(2, SomeCaptureObject()) 134| 1| XCTAssertEqual(ret, "2") 135| 1| XCTAssertEqual(captor.allValues.count, 1) 136| 1| } 137| | 138| | 139| 1| func testExpect_capture_closure_noArg() { 140| 1| let captor = ArgumentClosureCaptor<() -> Int>() 141| 1| mock.expect().call(mock.otherFuncNoArg(closure: captor.capture())) 142| 1| 143| 1| mock.otherFuncNoArg { return 42 } 144| 1| mock.verify() 145| 1| XCTAssertTrue(self.assertionMock.succeeded) 146| 1| 147| 1| XCTAssertNotNil(captor.value) 148| 1| let ret = captor.value!() 149| 1| XCTAssertEqual(ret, 42) 150| 1| XCTAssertEqual(captor.allValues.count, 1) 151| 1| } 152| | 153| | 154| 1| func testExpect_capture_closure_oneArg() { 155| 1| let captor = ArgumentClosureCaptor<(Int) -> Int>() 156| 1| mock.expect().call(mock.otherFuncOneArg(closure: captor.capture())) 157| 1| 158| 1| mock.otherFuncOneArg { num in return num } 159| 1| mock.verify() 160| 1| XCTAssertTrue(self.assertionMock.succeeded) 161| 1| 162| 1| XCTAssertNotNil(captor.value) 163| 1| let ret = captor.value!(3) 164| 1| XCTAssertEqual(ret, 3) 165| 1| XCTAssertEqual(captor.allValues.count, 1) 166| 1| } 167| | 168| | 169| 1| func testExpect_capture_closure_twoArgs() { 170| 1| let captor = ArgumentClosureCaptor<(Int, Int) -> Int>() 171| 1| mock.expect().call(mock.otherFuncTwoArgs(closure: captor.capture())) 172| 1| 173| 1| mock.otherFuncTwoArgs { num1, num2 in return num1 + num2 } 174| 1| mock.verify() 175| 1| XCTAssertTrue(self.assertionMock.succeeded) 176| 1| 177| 1| XCTAssertNotNil(captor.value) 178| 1| let ret = captor.value!(3, 5) 179| 1| XCTAssertEqual(ret, 8) 180| 1| XCTAssertEqual(captor.allValues.count, 1) 181| 1| } 182| | 183| | 184| 1| func testExpect_capture_closure_threeArgs() { 185| 1| let captor = ArgumentClosureCaptor<(Int, Int, Int) -> Int>() 186| 1| mock.expect().call(mock.otherFuncThreeArgs(closure: captor.capture())) 187| 1| 188| 1| mock.otherFuncThreeArgs { num1, num2, num3 in return num1 + num2 + num3 } 189| 1| mock.verify() 190| 1| XCTAssertTrue(self.assertionMock.succeeded) 191| 1| 192| 1| XCTAssertNotNil(captor.value) 193| 1| let ret = captor.value!(1, 2, 3) 194| 1| XCTAssertEqual(ret, 6) 195| 1| XCTAssertEqual(captor.allValues.count, 1) 196| 1| } 197| | 198| | 199| 1| func testExpect_capture_closure_fourArgs() { 200| 1| let captor = ArgumentClosureCaptor<(Int, Int, Int, Int) -> Int>() 201| 1| mock.expect().call(mock.otherFuncFourArgs(closure: captor.capture())) 202| 1| 203| 1| mock.otherFuncFourArgs { num1, num2, num3, num4 in return num1 + num2 + num3 + num4 } 204| 1| mock.verify() 205| 1| XCTAssertTrue(self.assertionMock.succeeded) 206| 1| 207| 1| XCTAssertNotNil(captor.value) 208| 1| let ret = captor.value!(1, 2, 3, 4) 209| 1| XCTAssertEqual(ret, 10) 210| 1| XCTAssertEqual(captor.allValues.count, 1) 211| 1| } 212| | 213| | 214| 1| func testExpect_capture_closure_fiveArgs() { 215| 1| let captor = ArgumentClosureCaptor<(Int, Int, Int, Int, Int) -> Int>() 216| 1| mock.expect().call(mock.otherFuncFiveArgs(closure: captor.capture())) 217| 1| 218| 1| mock.otherFuncFiveArgs { num1, num2, num3, num4, num5 in return num1 + num2 + num3 + num4 + num5 } 219| 1| mock.verify() 220| 1| XCTAssertTrue(self.assertionMock.succeeded) 221| 1| 222| 1| XCTAssertNotNil(captor.value) 223| 1| let ret = captor.value!(1, 2, 3, 4, 5) 224| 1| XCTAssertEqual(ret, 15) 225| 1| XCTAssertEqual(captor.allValues.count, 1) 226| 1| } 227| | 228| |} 229| | /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Integration Tests/ClosureMockTests.swift: 1| |// 2| |// ClosureMockTests.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 08/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |import XCTest 11| |@testable import InstantMock 12| | 13| | 14| |final class SomeClosureObject {} 15| | 16| | 17| |protocol ClosureProtocol { 18| | 19| | // mandatory and optional 20| | func someFunc(arg: String, closure: @escaping (_ arg1: String, _ arg2: SomeClosureObject) -> Int) 21| | func someFuncOpt(arg: String?, closure: ((_ arg1: String, _ arg2: SomeClosureObject) -> Int)?) 22| | 23| | // number of args 24| | func otherFuncNoArg(closure: @escaping () -> String) 25| | func otherFuncOneArg(closure: @escaping (String) -> String) 26| | func otherFuncTwoArgs(closure: @escaping (String, String) -> String) 27| | func otherFuncThreeArgs(closure: @escaping (String, String, String) -> String) 28| | func otherFuncFourArgs(closure: @escaping (String, String, String, String) -> String) 29| | func otherFuncFiveArgs(closure: @escaping (String, String, String, String, String) -> String) 30| |} 31| | 32| | 33| |final class ClosureMock: Mock, ClosureProtocol { 34| | 35| 2| func someFunc(arg: String, closure: @escaping (String, SomeClosureObject) -> Int) { 36| 2| super.call(arg, closure) 37| 2| } 38| | 39| 4| func someFuncOpt(arg: String?, closure: ((String, SomeClosureObject) -> Int)?) { 40| 4| super.call(arg, closure) 41| 4| } 42| | 43| 2| func otherFuncNoArg(closure: @escaping () -> String) { 44| 2| super.call(closure) 45| 2| } 46| | 47| 2| func otherFuncOneArg(closure: @escaping (String) -> String) { 48| 2| super.call(closure) 49| 2| } 50| | 51| 2| func otherFuncTwoArgs(closure: @escaping (String, String) -> String) { 52| 2| super.call(closure) 53| 2| } 54| | 55| 2| func otherFuncThreeArgs(closure: @escaping (String, String, String) -> String) { 56| 2| super.call(closure) 57| 2| } 58| | 59| 2| func otherFuncFourArgs(closure: @escaping (String, String, String, String) -> String) { 60| 2| super.call(closure) 61| 2| } 62| | 63| 2| func otherFuncFiveArgs(closure: @escaping (String, String, String, String, String) -> String) { 64| 2| super.call(closure) 65| 2| } 66| | 67| |} 68| | 69| | 70| | 71| |final class ClosureMockTests: XCTestCase { 72| | 73| | private var mock: ClosureMock! 74| | private var assertionMock: AssertionMock! 75| | 76| | 77| 9| override func setUp() { 78| 9| super.setUp() 79| 9| self.assertionMock = AssertionMock() 80| 9| let expectationFactory = ExpectationFactoryMock(withAssertionMock: self.assertionMock) 81| 9| self.mock = ClosureMock(expectationFactory) 82| 9| } 83| | 84| | 85| | static var allTests = [ 86| | ("testExpect", testExpect), 87| | ("testExpect_optionalNil", testExpect_optionalNil), 88| | ("testExpect_optionalNotNil", testExpect_optionalNotNil), 89| | ("testExpect_noArg", testExpect_noArg), 90| | ("testExpect_oneArg", testExpect_oneArg), 91| | ("testExpect_twoArgs", testExpect_twoArgs), 92| | ("testExpect_threeArgs", testExpect_threeArgs), 93| | ("testExpect_fourArgs", testExpect_fourArgs), 94| | ("testExpect_fiveArgs", testExpect_fiveArgs), 95| | ] 96| | 97| | 98| 1| func testExpect() { 99| 1| self.mock.expect().call(self.mock.someFunc( 100| 1| arg: Arg.any(), 101| 1| closure: Arg.closure() 102| 1| )) 103| 1| 104| 1| self.mock.someFunc(arg: "Hello", closure: { (str, obj) -> Int in 105| 0| return 42 106| 0| }) 107| 1| 108| 1| self.mock.verify() 109| 1| XCTAssertTrue(self.assertionMock.succeeded) 110| 1| } 111| | 112| | 113| 1| func testExpect_optionalNil() { 114| 1| self.mock.expect().call(self.mock.someFuncOpt( 115| 1| arg: Arg.any(), 116| 1| closure: Arg.closure() 117| 1| )) 118| 1| 119| 1| self.mock.someFuncOpt(arg: "Hello", closure: nil) 120| 1| 121| 1| self.mock.verify() 122| 1| XCTAssertTrue(self.assertionMock.succeeded) 123| 1| } 124| | 125| | 126| 1| func testExpect_optionalNotNil() { 127| 1| self.mock.expect().call(self.mock.someFuncOpt( 128| 1| arg: Arg.any(), 129| 1| closure: Arg.closure() 130| 1| )) 131| 1| 132| 1| self.mock.someFuncOpt(arg: "Hello", closure: { (str, obj) -> Int in 133| 0| return 42 134| 0| }) 135| 1| 136| 1| self.mock.verify() 137| 1| XCTAssertTrue(self.assertionMock.succeeded) 138| 1| } 139| | 140| | 141| 1| func testExpect_noArg() { 142| 1| self.mock.expect().call(self.mock.otherFuncNoArg(closure: Arg.closure())) 143| 1| 144| 1| self.mock.otherFuncNoArg(closure: { return "noarg" }) 145| 1| 146| 1| self.mock.verify() 147| 1| XCTAssertTrue(self.assertionMock.succeeded) 148| 1| } 149| | 150| | 151| 1| func testExpect_oneArg() { 152| 1| self.mock.expect().call(self.mock.otherFuncOneArg(closure: Arg.closure())) 153| 1| 154| 1| self.mock.otherFuncOneArg(closure: { _ in return "oneArg" }) 155| 1| 156| 1| self.mock.verify() 157| 1| XCTAssertTrue(self.assertionMock.succeeded) 158| 1| } 159| | 160| | 161| 1| func testExpect_twoArgs() { 162| 1| self.mock.expect().call(self.mock.otherFuncTwoArgs(closure: Arg.closure())) 163| 1| 164| 1| self.mock.otherFuncTwoArgs(closure: { _, _ in return "twoArgs" }) 165| 1| 166| 1| self.mock.verify() 167| 1| XCTAssertTrue(self.assertionMock.succeeded) 168| 1| } 169| | 170| | 171| 1| func testExpect_threeArgs() { 172| 1| self.mock.expect().call(self.mock.otherFuncThreeArgs(closure: Arg.closure())) 173| 1| 174| 1| self.mock.otherFuncThreeArgs(closure: { _, _, _ in return "threeArgs" }) 175| 1| 176| 1| self.mock.verify() 177| 1| XCTAssertTrue(self.assertionMock.succeeded) 178| 1| } 179| | 180| | 181| 1| func testExpect_fourArgs() { 182| 1| self.mock.expect().call(self.mock.otherFuncFourArgs(closure: Arg.closure())) 183| 1| 184| 1| self.mock.otherFuncFourArgs(closure: { _, _, _, _ in return "fourArgs" }) 185| 1| 186| 1| self.mock.verify() 187| 1| XCTAssertTrue(self.assertionMock.succeeded) 188| 1| } 189| | 190| | 191| 1| func testExpect_fiveArgs() { 192| 1| self.mock.expect().call(self.mock.otherFuncFiveArgs(closure: Arg.closure())) 193| 1| 194| 1| self.mock.otherFuncFiveArgs(closure: { _, _, _, _, _ in return "fiveArgs" }) 195| 1| 196| 1| self.mock.verify() 197| 1| XCTAssertTrue(self.assertionMock.succeeded) 198| 1| } 199| | 200| |} /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Integration Tests/MockCreationModalityTests.swift: 1| |// 2| |// MockCreationModalityTests.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 08/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import XCTest 10| |@testable import InstantMock 11| | 12| | 13| |protocol SomeProtocol { 14| | func someFunc(arg1: String, arg2: Int) -> String 15| |} 16| | 17| | 18| |final class InheritanceMock: Mock, SomeProtocol { 19| | 20| 2| func someFunc(arg1: String, arg2: Int) -> String { 21| 2| return super.call(arg1, arg2)! 22| 2| } 23| | 24| |} 25| | 26| | 27| |final class DelegateItMock: Any, MockDelegate, SomeProtocol { 28| | 29| | private let expectationFactory: ExpectationFactory 30| | private let mock: Mock! 31| | 32| 1| init(withExpectationFactory factory: ExpectationFactory) { 33| 1| self.expectationFactory = factory 34| 1| self.mock = Mock(expectationFactory) 35| 1| } 36| | 37| 2| var it: Mock { 38| 2| return self.mock 39| 2| } 40| | 41| 2| func someFunc(arg1: String, arg2: Int) -> String { 42| 2| return self.mock.call(arg1, arg2)! 43| 2| } 44| | 45| |} 46| | 47| | 48| |final class DelegateFullMock: Any, MockDelegate, MockExpectation, MockStub, SomeProtocol { 49| | 50| | private let expectationFactory: ExpectationFactory 51| | private let mock: Mock! 52| | 53| 1| init(withExpectationFactory factory: ExpectationFactory) { 54| 1| self.expectationFactory = factory 55| 1| self.mock = Mock(expectationFactory) 56| 1| } 57| | 58| | 59| 0| var it: Mock { 60| 0| return self.mock 61| 0| } 62| | 63| 1| func expect() -> Expectation { 64| 1| return self.mock.expect() 65| 1| } 66| | 67| 0| func reject() -> Expectation { 68| 0| return self.mock.reject() 69| 0| } 70| | 71| 1| func verify(file: StaticString? = #file, line: UInt? = #line) { 72| 1| self.mock.verify(file: file, line: line) 73| 1| } 74| | 75| 0| func stub() -> Stub { 76| 0| return self.mock.stub() 77| 0| } 78| | 79| 2| func someFunc(arg1: String, arg2: Int) -> String { 80| 2| return self.mock.call(arg1, arg2)! 81| 2| } 82| | 83| |} 84| | 85| | 86| |final class MockCreationModalityTests: XCTestCase { 87| | 88| | private var assertionMock: AssertionMock! 89| | private var expectationFactory: ExpectationFactoryMock! 90| | 91| | 92| 3| override func setUp() { 93| 3| super.setUp() 94| 3| self.assertionMock = AssertionMock() 95| 3| self.expectationFactory = ExpectationFactoryMock(withAssertionMock: self.assertionMock) 96| 3| } 97| | 98| | 99| | static var allTests = [ 100| | ("testExpect_inheritanceMock", testExpect_inheritanceMock), 101| | ("testExpect_delegateItMock", testExpect_delegateItMock), 102| | ("testExpect_delegateFullMock", testExpect_delegateFullMock), 103| | ] 104| | 105| | 106| 1| func testExpect_inheritanceMock() { 107| 1| let mock = InheritanceMock(self.expectationFactory) 108| 1| mock.expect().call(mock.someFunc(arg1: Arg.eq("Hello"), arg2: Arg.any())) 109| 1| 110| 1| _ = mock.someFunc(arg1: "Hello", arg2: 2) 111| 1| 112| 1| mock.verify() 113| 1| XCTAssertTrue(self.assertionMock.succeeded) 114| 1| } 115| | 116| | 117| 1| func testExpect_delegateItMock() { 118| 1| let mock = DelegateItMock(withExpectationFactory: self.expectationFactory) 119| 1| mock.it.expect().call(mock.someFunc(arg1: Arg.eq("Hello"), arg2: Arg.any())) 120| 1| 121| 1| _ = mock.someFunc(arg1: "Hello", arg2: 2) 122| 1| 123| 1| mock.it.verify() 124| 1| XCTAssertTrue(self.assertionMock.succeeded) 125| 1| } 126| | 127| | 128| 1| func testExpect_delegateFullMock() { 129| 1| let mock = DelegateFullMock(withExpectationFactory: self.expectationFactory) 130| 1| mock.expect().call(mock.someFunc(arg1: Arg.eq("Hello"), arg2: Arg.any())) 131| 1| 132| 1| _ = mock.someFunc(arg1: "Hello", arg2: 2) 133| 1| 134| 1| mock.verify() 135| 1| XCTAssertTrue(self.assertionMock.succeeded) 136| 1| } 137| | 138| |} /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Integration Tests/ThrowingMockTests.swift: 1| |// 2| |// ThrowingMockTests.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 22/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import XCTest 10| |@testable import InstantMock 11| | 12| | 13| |protocol ThrowingProtocol { 14| | func throwing() throws 15| | func throwingAndReturn() throws -> String 16| |} 17| | 18| | 19| |final class ThrowingMock: Mock, ThrowingProtocol { 20| | 21| 2| func throwing() throws { 22| 2| try super.callThrowing() 23| 2| } 24| | 25| 4| func throwingAndReturn() throws -> String { 26| 4| return try super.callThrowing()! 27| 4| } 28| | 29| |} 30| | 31| | 32| | 33| |final class ThrowingMockTests: XCTestCase { 34| | 35| | private enum ThrowingError: Error { 36| | case some 37| | } 38| | 39| | private var mock: ThrowingMock! 40| | private var assertionMock: AssertionMock! 41| | 42| | 43| 3| override func setUp() { 44| 3| super.setUp() 45| 3| self.assertionMock = AssertionMock() 46| 3| let expectationFactory = ExpectationFactoryMock(withAssertionMock: self.assertionMock) 47| 3| self.mock = ThrowingMock(expectationFactory) 48| 3| } 49| | 50| | 51| | static var allTests = [ 52| | ("testStub_andThrow", testStub_andThrow), 53| | ("testStub_andThrow_returnNotThrow", testStub_andThrow_returnNotThrow), 54| | ("testStub_andThrow_returnThrow", testStub_andThrow_returnThrow), 55| | ] 56| | 57| | 58| 1| func testStub_andThrow() { 59| 1| mock.stub().call(try! mock.throwing()).andThrow(ThrowingError.some) 60| 1| XCTAssertThrowsError(try mock.throwing()) { (error) -> Void in 61| 1| XCTAssertEqual(error as? ThrowingError, ThrowingError.some) 62| 1| } 63| 1| } 64| | 65| | 66| 1| func testStub_andThrow_returnNotThrow() { 67| 1| mock.stub().call(try! mock.throwingAndReturn()).andReturn("something") 68| 1| 69| 1| var ret: String? = nil 70| 1| XCTAssertNoThrow(ret = try mock.throwingAndReturn()) 71| 1| XCTAssertEqual(ret, "something") 72| 1| } 73| | 74| | 75| 1| func testStub_andThrow_returnThrow() { 76| 1| mock.stub().call(try! mock.throwingAndReturn()).andThrow(ThrowingError.some) 77| 1| XCTAssertThrowsError(try mock.throwingAndReturn()) { (error) -> Void in 78| 1| XCTAssertEqual(error as? ThrowingError, ThrowingError.some) 79| 1| } 80| 1| } 81| | 82| |} /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Integration Tests/TypesMockTests.swift: 1| |// 2| |// TypesMockTests.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 08/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import XCTest 10| |@testable import InstantMock 11| | 12| | 13| |final class SomeObject: NSObject { } 14| | 15| | 16| |protocol TypesProtocol { 17| | func empty() 18| | func boolean(_ bool: Bool) -> Bool 19| | func integer(_ int: Int) -> Int 20| | func double(_ double: Double) -> Double 21| | func string(_ str: String) -> String 22| | func stringOpt(_ str: String) -> String? 23| | func object(_ obj: SomeObject) -> SomeObject? 24| | func set(_ set: Set) -> Set 25| | func array(_ array: Array) -> Array 26| | func dictionary(_ dictionary: Dictionary) -> Dictionary 27| |} 28| | 29| | 30| |final class TypesMock: Mock, TypesProtocol { 31| | 32| 2| func empty() { 33| 2| let _: Void = super.call() 34| 2| } 35| | 36| 2| func boolean(_ bool: Bool) -> Bool { 37| 2| return super.call(bool)! 38| 2| } 39| | 40| 2| func integer(_ int: Int) -> Int { 41| 2| return super.call(int)! 42| 2| } 43| | 44| 2| func double(_ double: Double) -> Double { 45| 2| return super.call(double)! 46| 2| } 47| | 48| 2| func string(_ str: String) -> String { 49| 2| return super.call(str)! 50| 2| } 51| | 52| 2| func stringOpt(_ str: String) -> String? { 53| 2| return super.call(str) 54| 2| } 55| | 56| 2| func object(_ obj: SomeObject) -> SomeObject? { 57| 2| return super.call(obj) 58| 2| } 59| | 60| 2| func set(_ set: Set) -> Set { 61| 2| return super.call(set)! 62| 2| } 63| | 64| 2| func array(_ array: Array) -> Array { 65| 2| return super.call(array)! 66| 2| } 67| | 68| 2| func dictionary(_ dictionary: Dictionary) -> Dictionary { 69| 2| return super.call(dictionary)! 70| 2| } 71| | 72| |} 73| | 74| | 75| |final class TypesMockTests: XCTestCase { 76| | 77| | private var mock: TypesMock! 78| | private var assertionMock: AssertionMock! 79| | 80| | 81| 10| override func setUp() { 82| 10| super.setUp() 83| 10| self.assertionMock = AssertionMock() 84| 10| let expectationFactory = ExpectationFactoryMock(withAssertionMock: self.assertionMock) 85| 10| self.mock = TypesMock(expectationFactory) 86| 10| } 87| | 88| | 89| | static var allTests = [ 90| | ("testEmpty", testEmpty), 91| | ("testBool", testBool), 92| | ("testInt", testInt), 93| | ("testDouble", testDouble), 94| | ("testString", testString), 95| | ("testString_nil", testString_nil), 96| | ("testObject", testObject), 97| | ("testSet", testSet), 98| | ("testArray", testArray), 99| | ("testDictionary", testDictionary), 100| | ] 101| | 102| | 103| 1| func testEmpty() { 104| 1| self.mock.expect().call(self.mock.empty()) 105| 1| 106| 1| self.mock.verify() 107| 1| XCTAssertFalse(self.assertionMock.succeeded) 108| 1| 109| 1| self.mock.empty() 110| 1| 111| 1| self.mock.verify() 112| 1| XCTAssertTrue(self.assertionMock.succeeded) 113| 1| } 114| | 115| | 116| 1| func testBool() { 117| 1| self.mock.expect().call(self.mock.boolean(Arg.any())) 118| 1| 119| 1| self.mock.verify() 120| 1| XCTAssertFalse(self.assertionMock.succeeded) 121| 1| 122| 1| _ = self.mock.boolean(true) 123| 1| 124| 1| self.mock.verify() 125| 1| XCTAssertTrue(self.assertionMock.succeeded) 126| 1| } 127| | 128| | 129| 1| func testInt() { 130| 1| self.mock.expect().call(self.mock.integer(Arg.any())) 131| 1| 132| 1| self.mock.verify() 133| 1| XCTAssertFalse(self.assertionMock.succeeded) 134| 1| 135| 1| _ = self.mock.integer(12) 136| 1| 137| 1| self.mock.verify() 138| 1| XCTAssertTrue(self.assertionMock.succeeded) 139| 1| } 140| | 141| | 142| 1| func testDouble() { 143| 1| self.mock.expect().call(self.mock.double(Arg.any())) 144| 1| 145| 1| self.mock.verify() 146| 1| XCTAssertFalse(self.assertionMock.succeeded) 147| 1| 148| 1| _ = self.mock.double(12.0) 149| 1| 150| 1| self.mock.verify() 151| 1| XCTAssertTrue(self.assertionMock.succeeded) 152| 1| } 153| | 154| | 155| 1| func testString() { 156| 1| 157| 1| self.mock.expect().call(self.mock.string(Arg.any())) 158| 1| 159| 1| self.mock.verify() 160| 1| XCTAssertFalse(self.assertionMock.succeeded) 161| 1| 162| 1| _ = self.mock.string("arg") 163| 1| 164| 1| self.mock.verify() 165| 1| XCTAssertTrue(self.assertionMock.succeeded) 166| 1| } 167| | 168| | 169| 1| func testString_nil() { 170| 1| self.mock.expect().call(self.mock.stringOpt(Arg.any())) 171| 1| 172| 1| self.mock.verify() 173| 1| XCTAssertFalse(self.assertionMock.succeeded) 174| 1| 175| 1| _ = self.mock.stringOpt("arg") 176| 1| 177| 1| self.mock.verify() 178| 1| XCTAssertTrue(self.assertionMock.succeeded) 179| 1| } 180| | 181| | 182| 1| func testObject() { 183| 1| let object = SomeObject() 184| 1| self.mock.expect().call(self.mock.object(Arg.eq(object))) 185| 1| 186| 1| self.mock.verify() 187| 1| XCTAssertFalse(self.assertionMock.succeeded) 188| 1| 189| 1| _ = mock.object(object) 190| 1| 191| 1| self.mock.verify() 192| 1| XCTAssertTrue(self.assertionMock.succeeded) 193| 1| } 194| | 195| | 196| 1| func testSet() { 197| 1| var set = Set() 198| 1| set.insert(1) 199| 1| 200| 1| self.mock.expect().call(self.mock.set(Arg.any())) 201| 1| 202| 1| self.mock.verify() 203| 1| XCTAssertFalse(self.assertionMock.succeeded) 204| 1| 205| 1| _ = self.mock.set(set) 206| 1| 207| 1| self.mock.verify() 208| 1| XCTAssertTrue(self.assertionMock.succeeded) 209| 1| } 210| | 211| | 212| 1| func testArray() { 213| 1| var array = Array() 214| 1| array.append(12) 215| 1| 216| 1| self.mock.expect().call(self.mock.array(Arg.any())) 217| 1| 218| 1| self.mock.verify() 219| 1| XCTAssertFalse(self.assertionMock.succeeded) 220| 1| 221| 1| _ = self.mock.array(array) 222| 1| 223| 1| self.mock.verify() 224| 1| XCTAssertTrue(self.assertionMock.succeeded) 225| 1| } 226| | 227| | 228| 1| func testDictionary() { 229| 1| var dict = Dictionary() 230| 1| dict["key"] = 12 231| 1| 232| 1| self.mock.expect().call(self.mock.dictionary(Arg.any())) 233| 1| 234| 1| self.mock.verify() 235| 1| XCTAssertFalse(self.assertionMock.succeeded) 236| 1| 237| 1| _ = self.mock.dictionary(dict) 238| 1| 239| 1| self.mock.verify() 240| 1| XCTAssertTrue(self.assertionMock.succeeded) 241| 1| } 242| | 243| |} /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Integration Tests/VerifyMockTests.swift: 1| |// 2| |// VerifyMockTests.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 13/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import XCTest 10| |@testable import InstantMock 11| | 12| | 13| |protocol VerifyProtocol { 14| | func someFunc(arg1: String, arg2: Int) -> String 15| | func someFuncOpt(arg1: String?, arg2: Int?) -> String? 16| |} 17| | 18| | 19| |final class VerifyMock: Mock, VerifyProtocol { 20| | 21| 7| func someFunc(arg1: String, arg2: Int) -> String { 22| 7| return super.call(arg1, arg2)! 23| 7| } 24| | 25| 3| func someFuncOpt(arg1: String?, arg2: Int?) -> String? { 26| 3| return super.call(arg1, arg2) 27| 3| } 28| | 29| |} 30| | 31| | 32| |final class VerifyMockTests: XCTestCase { 33| | 34| | private var mock: VerifyMock! 35| | private var assertionMock: AssertionMock! 36| | 37| | 38| 3| override func setUp() { 39| 3| super.setUp() 40| 3| self.assertionMock = AssertionMock() 41| 3| let expectationFactory = ExpectationFactoryMock(withAssertionMock: self.assertionMock) 42| 3| self.mock = VerifyMock(expectationFactory) 43| 3| } 44| | 45| | 46| | static var allTests = [ 47| | ("testExpect_stringVerify", testExpect_stringVerify), 48| | ("testStub_intVerify", testStub_intVerify), 49| | ("testExpect_stringOpt", testExpect_stringOpt), 50| | ] 51| | 52| | 53| 1| func testExpect_stringVerify() { 54| 1| 55| 4| mock.expect().call(mock.someFunc(arg1: Arg.verify({ str in str.hasPrefix("hello")}), arg2: Arg.any())) 56| 1| _ = mock.someFunc(arg1: "salut", arg2: 12) 57| 1| mock.verify() 58| 1| XCTAssertFalse(self.assertionMock.succeeded) 59| 1| 60| 1| _ = mock.someFunc(arg1: "hello de lu", arg2: 12) 61| 1| mock.verify() 62| 1| XCTAssertTrue(self.assertionMock.succeeded) 63| 1| } 64| | 65| | 66| 1| func testStub_intVerify() { 67| 1| 68| 1| // configure two verifiers that are totally unjoined 69| 4| mock.expect().call(mock.someFunc(arg1: Arg.any(), arg2: Arg.verify( { val in val % 2 == 0 }))).andReturn("first") 70| 4| mock.expect().call(mock.someFunc(arg1: Arg.any(), arg2: Arg.verify( { val in val % 2 == 1 }))).andReturn("second") 71| 1| 72| 1| var ret = mock.someFunc(arg1: "anything", arg2: 12) 73| 1| XCTAssertEqual(ret, "first") 74| 1| mock.verify() 75| 1| XCTAssertFalse(self.assertionMock.succeeded) 76| 1| 77| 1| ret = mock.someFunc(arg1: "anything", arg2: 13) 78| 1| XCTAssertEqual(ret, "second") 79| 1| 80| 1| mock.verify() 81| 1| XCTAssertTrue(self.assertionMock.succeeded) 82| 1| } 83| | 84| | 85| 1| func testExpect_stringOpt() { 86| 4| mock.expect().call(mock.someFuncOpt(arg1: Arg.verify({ str in str == nil }), 87| 4| arg2: Arg.verify({ val in val == nil }))) 88| 1| _ = mock.someFuncOpt(arg1: nil, arg2: 12) 89| 1| mock.verify() 90| 1| XCTAssertFalse(self.assertionMock.succeeded) 91| 1| 92| 1| _ = mock.someFuncOpt(arg1: nil, arg2: nil) 93| 1| mock.verify() 94| 1| XCTAssertTrue(self.assertionMock.succeeded) 95| 1| } 96| | 97| |} 98| | /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Mocks/ArgumentAnyMock.swift: 1| |// 2| |// ArgumentAnyMock.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 13/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import InstantMock 10| | 11| | 12| |class ArgumentAnyMock: ArgumentAny { 13| | 14| 5| var description: String { return "argument_any_mock" } 15| | 16| 3| func match(_ value: Any?) -> Bool { 17| 3| return true 18| 3| } 19| | 20| |} /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Mocks/ArgumentCaptureMock.swift: 1| |// 2| |// ArgumentCaptureMock.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 13/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import InstantMock 10| | 11| | 12| |class ArgumentCaptureMock: ArgumentCapture { 13| | 14| 7| var description: String { return "argument_capture_mock" } 15| | 16| 0| func match(_ value: Any?) -> Bool { 17| 0| return true 18| 0| } 19| | 20| | var value: Any? 21| 15| func setValue(_ value: Any?) { 22| 15| self.value = value 23| 15| } 24| | 25| 7| var allValues: [Any?] { 26| 7| return [value] 27| 7| } 28| | 29| |} /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Mocks/ArgumentClosureMock.swift: 1| |// 2| |// ArgumentClosureMock.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 21/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import InstantMock 10| | 11| | 12| |class ArgumentClosureMock: ArgumentClosure { 13| | 14| 0| var description: String { return "argument_closure_mock" } 15| | 16| 0| func match(_ value: Any?) -> Bool { 17| 0| return true 18| 0| } 19| | 20| |} /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Mocks/ArgumentFactoryMock.swift: 1| |// 2| |// ArgumentFactoryMock.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 13/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import InstantMock 10| | 11| | 12| |class ArgumentFactoryMock: ArgumentFactory { 13| | 14| | 15| | var argumentValue: ArgumentValue? 16| | var argumentAny: ArgumentAny? 17| | var argumentClosure: ArgumentClosure? 18| | var argumentVerify: ArgumentVerify? 19| | var argumentVerifyOpt: ArgumentVerify? 20| | var argumentCapture: ArgumentCapture? 21| | 22| | 23| 6| func argument(value: T?) -> ArgumentValue { 24| 6| let argValue = ArgumentValueMock(value) 25| 6| self.argumentValue = argValue 26| 6| return argValue 27| 6| } 28| | 29| | 30| 3| func argumentAny(_ typeDescription: String) -> ArgumentAny { 31| 3| let argAny = ArgumentAnyMock() 32| 3| self.argumentAny = argAny 33| 3| return argAny 34| 3| } 35| | 36| | 37| 6| func argumentClosure(_ typeDescription: String) -> ArgumentClosure { 38| 6| let argClosure = ArgumentClosureMock() 39| 6| self.argumentClosure = argClosure 40| 6| return argClosure 41| 6| } 42| | 43| | 44| 2| func argument(condition: @escaping (T) -> Bool) -> ArgumentVerify { 45| 2| let argVerify = ArgumentVerifyMandatoryMock(condition) 46| 2| self.argumentVerify = argVerify 47| 2| return argVerify 48| 2| } 49| | 50| | 51| 0| func argument(condition: @escaping (T?) -> Bool) -> ArgumentVerify { 52| 0| let argVerify = ArgumentVerifyOptionalMock(condition) 53| 0| self.argumentVerify = argVerify 54| 0| return argVerify 55| 0| } 56| | 57| | 58| 28| func argumentCapture(_ typeDescription: String) -> ArgumentCapture { 59| 28| let argCapture = ArgumentCaptureMock() 60| 28| self.argumentCapture = argCapture 61| 28| return argCapture 62| 28| } 63| | 64| |} 65| | /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Mocks/ArgumentStorageMock.swift: 1| |// 2| |// ArgumentStorageMock.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 13/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import InstantMock 10| | 11| | 12| |class ArgumentStorageMock: ArgumentStorage { 13| | 14| 45| func store(_ arg: Argument) { 15| 45| args.append(arg) 16| 45| } 17| | 18| | var args = [Argument]() 19| 0| func all() -> [Argument] { 20| 0| return args 21| 0| } 22| | 23| 0| func flush() { 24| 0| args.removeAll() 25| 0| } 26| | 27| |} /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Mocks/ArgumentValueMock.swift: 1| |// 2| |// ArgumentValueMock.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 13/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import InstantMock 10| | 11| | 12| |class ArgumentValueMock: ArgumentValueTyped { 13| | 14| | var value: T? 15| 36| required init(_ value: T?) { 16| 36| self.value = value 17| 36| } 18| | 19| 7| var description: String { return "argument_value_mock" } 20| | 21| | var matchValue: T? 22| | var shouldMatch = true 23| 4| func match(_ value: Any?) -> Bool { 24| 4| if let tValue = value as? T { 25| 4| self.matchValue = tValue 26| 4| } 27| 4| return shouldMatch 28| 4| } 29| | 30| |} /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Mocks/ArgumentVerifyMandatoryMock.swift: 1| |// 2| |// ArgumentVerifyMandatoryMock.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 13/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import InstantMock 10| | 11| | 12| |class ArgumentVerifyMandatoryMock: ArgumentVerifyMandatoryTyped { 13| | 14| | var condition: (T) -> Bool 15| 11| required init(_ condition: @escaping ((T) -> Bool)) { 16| 11| self.condition = condition 17| 11| 18| 11| } 19| | 20| 0| var description: String { return "argument_verify_mock" } 21| | 22| 0| func match(_ value: Any?) -> Bool { 23| 0| return true 24| 0| } 25| | 26| |} 27| | /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Mocks/ArgumentVerifyOptionalMock.swift: 1| |// 2| |// ArgumentVerifyOptionalMock.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 20/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import InstantMock 10| | 11| | 12| |class ArgumentVerifyOptionalMock: ArgumentVerifyOptionalTyped { 13| | 14| | var condition: (T?) -> Bool 15| 0| required init(_ condition: @escaping ((T?) -> Bool)) { 16| 0| self.condition = condition 17| 0| 18| 0| } 19| | 20| 0| var description: String { return "argument_verify_mock" } 21| | 22| 0| func match(_ value: Any?) -> Bool { 23| 0| return true 24| 0| } 25| | 26| |} /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Mocks/AssertionMock.swift: 1| |// 2| |// AssertionMock.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 08/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import InstantMock 10| | 11| | 12| |/** Mock for assertions, just register the passed parameters */ 13| |class AssertionMock: Assertion { 14| | 15| | var description: String? 16| | var file: StaticString? 17| | var line: UInt? 18| | 19| | var succeeded = false 20| | 21| 48| func success(file: StaticString?, line: UInt?) { 22| 48| self.succeeded = true 23| 48| self.file = file 24| 48| self.line = line 25| 48| } 26| | 27| 22| func fail(_ description: String?, file: StaticString?, line: UInt?) { 28| 22| self.succeeded = false 29| 22| self.description = description 30| 22| self.file = file 31| 22| self.line = line 32| 22| } 33| | 34| |} /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Mocks/ExpectationFactoryMock.swift: 1| |// 2| |// ExpectationFactoryMock.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 08/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import InstantMock 10| | 11| | 12| |class ExpectationFactoryMock: ExpectationFactory { 13| | 14| | let assertionMock: AssertionMock 15| | 16| 53| init(withAssertionMock assertionMock: AssertionMock) { 17| 53| self.assertionMock = assertionMock 18| 53| } 19| | 20| 45| func expectation(withStub stub: Stub) -> Expectation { 21| 45| return Expectation(withStub: stub, assertion: assertionMock) 22| 45| } 23| | 24| 3| func rejection(withStub stub: Stub) -> Expectation { 25| 3| return Expectation(withStub: stub, reject: true, assertion: assertionMock) 26| 3| } 27| | 28| |} /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Mocks/VerifierMock.swift: 1| |// 2| |// VerifierMock.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 12/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import InstantMock 10| | 11| | 12| |/** Mock for Verifier */ 13| |class VerifierMock: Verifier { 14| | 15| | var arg: Any? 16| | var value: Any? 17| | 18| | var called = false 19| | 20| 1| func equal(_ arg: Any?, to value: Any?) -> Bool { 21| 1| self.arg = arg 22| 1| self.value = value 23| 1| self.called = true 24| 1| return false 25| 1| } 26| | 27| |} /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Unit Tests/Arguments/Arg+ClosureTests.swift: 1| |// 2| |// Arg+ClosureTests.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 18/07/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |import XCTest 11| |@testable import InstantMock 12| | 13| | 14| |final class ArgClosureTests: XCTestCase { 15| | 16| | private var argStorage: ArgumentStorageMock! 17| | 18| 6| override func setUp() { 19| 6| super.setUp() 20| 6| self.argStorage = ArgumentStorageMock() 21| 6| } 22| | 23| | static var allTests = [ 24| | ("testClosure_noArg", testClosure_noArg), 25| | ("testClosure_oneArg", testClosure_oneArg), 26| | ("testClosure_twoArgs", testClosure_twoArgs), 27| | ("testClosure_threeArgs", testClosure_threeArgs), 28| | ("testClosure_fourArgs", testClosure_fourArgs), 29| | ("testClosure_fiveArgs", testClosure_fiveArgs), 30| | ] 31| | 32| | 33| 1| func testClosure_noArg() { 34| 1| let factory = ArgumentFactoryMock<() -> Int>() 35| 1| let closure = Arg.closure(argFactory: factory, argStorage: self.argStorage) as () -> Int 36| 1| 37| 1| XCTAssertNotNil(closure) 38| 1| XCTAssertEqual(self.argStorage.args.count, 1) 39| 1| } 40| | 41| | 42| 1| func testClosure_oneArg() { 43| 1| let factory = ArgumentFactoryMock<(String) -> Int>() 44| 1| let closure = Arg.closure(argFactory: factory, argStorage: self.argStorage) as (String) -> Int 45| 1| 46| 1| XCTAssertNotNil(closure) 47| 1| XCTAssertEqual(self.argStorage.args.count, 1) 48| 1| } 49| | 50| | 51| 1| func testClosure_twoArgs() { 52| 1| let factory = ArgumentFactoryMock<(String, String) -> Int>() 53| 1| let closure = Arg.closure(argFactory: factory, argStorage: self.argStorage) as (String, String) -> Int 54| 1| 55| 1| XCTAssertNotNil(closure) 56| 1| XCTAssertEqual(self.argStorage.args.count, 1) 57| 1| } 58| | 59| | 60| 1| func testClosure_threeArgs() { 61| 1| let factory = ArgumentFactoryMock<(String, String, String) -> Int>() 62| 1| let closure = Arg.closure(argFactory: factory, argStorage: self.argStorage) 63| 1| as (String, String, String) -> Int 64| 1| 65| 1| XCTAssertNotNil(closure) 66| 1| XCTAssertEqual(self.argStorage.args.count, 1) 67| 1| } 68| | 69| | 70| 1| func testClosure_fourArgs() { 71| 1| let factory = ArgumentFactoryMock<(String, String, String, String) -> Int>() 72| 1| let closure = Arg.closure(argFactory: factory, argStorage: self.argStorage) 73| 1| as (String, String, String, String) -> Int 74| 1| 75| 1| XCTAssertNotNil(closure) 76| 1| XCTAssertEqual(self.argStorage.args.count, 1) 77| 1| } 78| | 79| | 80| 1| func testClosure_fiveArgs() { 81| 1| let factory = ArgumentFactoryMock<(String, String, String, String, String) -> Int>() 82| 1| let closure = Arg.closure(argFactory: factory, argStorage: self.argStorage) 83| 1| as (String, String, String, String, String) -> Int 84| 1| 85| 1| XCTAssertNotNil(closure) 86| 1| XCTAssertEqual(self.argStorage.args.count, 1) 87| 1| } 88| | 89| |} 90| | 91| | /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Unit Tests/Arguments/Arg/ArgumentAnyTests.swift: 1| |// 2| |// ArgumentAnyTests.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 12/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import XCTest 10| |@testable import InstantMock 11| | 12| | 13| |final class ArgumentAnyTests: XCTestCase { 14| | 15| | static var allTests = [ 16| | ("testDescription", testDescription), 17| | ("testMatch", testMatch), 18| | ] 19| | 20| | 21| 1| func testDescription() { 22| 1| let value = ArgumentAnyImpl("String") 23| 1| let desc = value.description 24| 1| XCTAssertEqual(desc, "any") 25| 1| } 26| | 27| | 28| 1| func testMatch() { 29| 1| let any = ArgumentAnyImpl("") 30| 1| let match = any.match("Anything") 31| 1| XCTAssertTrue(match) 32| 1| } 33| | 34| |} /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Unit Tests/Arguments/Arg/ArgumentCaptureTests.swift: 1| |// 2| |// ArgumentCaptureTests.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 12/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |import XCTest 11| |@testable import InstantMock 12| | 13| | 14| |final class ArgumentCaptureTests: XCTestCase { 15| | 16| | private var capture: ArgumentCaptureImpl! 17| | 18| 8| override func setUp() { 19| 8| super.setUp() 20| 8| self.capture = ArgumentCaptureImpl("String") 21| 8| } 22| | 23| | 24| | static var allTests = [ 25| | ("testMatch", testMatch), 26| | ("testDescription", testDescription), 27| | ("testValue_empty", testValue_empty), 28| | ("testAllValues_empty", testAllValues_empty), 29| | ("testValue_setValue_nil", testValue_setValue_nil), 30| | ("testValue_setValue_wrongType", testValue_setValue_wrongType), 31| | ("testValue_setValue_simple", testValue_setValue_simple), 32| | ("testValue_setValue_several", testValue_setValue_several), 33| | ] 34| | 35| | 36| 1| func testMatch() { 37| 1| let match = self.capture.match("Anything") 38| 1| XCTAssertTrue(match) 39| 1| } 40| | 41| | 42| 1| func testDescription() { 43| 1| let desc = self.capture.description 44| 1| XCTAssertEqual(desc, "captured") 45| 1| } 46| | 47| | 48| 1| func testValue_empty() { 49| 1| let value = self.capture.value 50| 1| XCTAssertNil(value) 51| 1| } 52| | 53| | 54| 1| func testAllValues_empty() { 55| 1| let values = self.capture.allValues 56| 1| XCTAssertEqual(values.count, 0) 57| 1| } 58| | 59| | 60| 1| func testValue_setValue_nil() { 61| 1| self.capture.setValue(nil) 62| 1| 63| 1| let value = self.capture.value 64| 1| XCTAssertNil(value) 65| 1| 66| 1| let values = self.capture.allValues 67| 1| XCTAssertEqual(values.count, 1) 68| 1| } 69| | 70| | 71| 1| func testValue_setValue_wrongType() { 72| 1| self.capture.setValue(12) 73| 1| 74| 1| let value = self.capture.value 75| 1| XCTAssertNil(value) 76| 1| 77| 1| let values = self.capture.allValues 78| 1| XCTAssertEqual(values.count, 0) 79| 1| } 80| | 81| | 82| 1| func testValue_setValue_simple() { 83| 1| self.capture.setValue("azerty") 84| 1| 85| 1| let value = self.capture.valueTyped 86| 1| XCTAssertEqual("azerty", value) 87| 1| 88| 1| let values = self.capture.allValues 89| 1| XCTAssertEqual(values.count, 1) 90| 1| } 91| | 92| | 93| 1| func testValue_setValue_several() { 94| 1| self.capture.setValue("azerty") 95| 1| self.capture.setValue("qwerty") 96| 1| 97| 1| let value = self.capture.valueTyped 98| 1| XCTAssertEqual("qwerty", value) 99| 1| 100| 1| let values = self.capture.allValuesTyped 101| 1| XCTAssertEqual(values.count, 2) 102| 1| XCTAssertEqual(values[0], "azerty") 103| 1| XCTAssertEqual(values[1], "qwerty") 104| 1| } 105| | 106| |} /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Unit Tests/Arguments/Arg/ArgumentClosureTests.swift: 1| |// 2| |// ArgumentClosureTests.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 21/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import XCTest 10| |@testable import InstantMock 11| | 12| | 13| |final class ArgumentClosureTests: XCTestCase { 14| | 15| | 16| | static var allTests = [ 17| | ("testDescription", testDescription), 18| | ("testMatch", testMatch), 19| | ] 20| | 21| | 22| 1| func testDescription() { 23| 1| let value = ArgumentClosureImpl("(String) -> Int") 24| 1| let desc = value.description 25| 1| XCTAssertEqual(desc, "closure<(String) -> Int>") 26| 1| } 27| | 28| | 29| 1| func testMatch() { 30| 1| let any = ArgumentClosureImpl("") 31| 1| let match = any.match("Anything") 32| 1| XCTAssertTrue(match) 33| 1| } 34| | 35| |} /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Unit Tests/Arguments/Arg/ArgumentValueTests.swift: 1| |// 2| |// ArgumentValueTests.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 12/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import XCTest 10| |@testable import InstantMock 11| | 12| | 13| |final class ArgumentValueTests: XCTestCase { 14| | 15| | private var verifier: VerifierMock! 16| | 17| | 18| 2| override func setUp() { 19| 2| super.setUp() 20| 2| self.verifier = VerifierMock() 21| 2| } 22| | 23| | 24| | static var allTests = [ 25| | ("testDescription", testDescription), 26| | ("testMatch", testMatch), 27| | ] 28| | 29| | 30| 1| func testDescription() { 31| 1| var value = ArgumentValueImpl("Hello", verifier: self.verifier) 32| 1| var desc = value.description 33| 1| XCTAssertEqual(desc, "Hello") 34| 1| 35| 1| value = ArgumentValueImpl(nil, verifier: self.verifier) 36| 1| desc = value.description 37| 1| XCTAssertEqual(desc, "nil") 38| 1| } 39| | 40| | 41| 1| func testMatch() { 42| 1| let value = ArgumentValueImpl("Hello", verifier: self.verifier) 43| 1| let match = value.match("Hello2") 44| 1| 45| 1| XCTAssertFalse(match) 46| 1| XCTAssertTrue(self.verifier.called) 47| 1| XCTAssertEqual(self.verifier.value as! String, "Hello") 48| 1| XCTAssertEqual(self.verifier.arg as! String, "Hello2") 49| 1| } 50| | 51| |} /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Unit Tests/Arguments/Arg/ArgumentVerifyMandatoryTests.swift: 1| |// 2| |// ArgumentVerifyMandatoryTests.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 12/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import XCTest 10| |@testable import InstantMock 11| | 12| | 13| |final class ArgumentVerifyMandatoryTests: XCTestCase { 14| | 15| | private var arg: ArgumentVerifyMandatoryImpl! 16| | 17| 3| override func setUp() { 18| 3| super.setUp() 19| 3| self.arg = ArgumentVerifyMandatoryImpl({ str in true }) 20| 3| } 21| | 22| | 23| | static var allTests = [ 24| | ("testDescription", testDescription), 25| | ("testMatch_wrongType", testMatch_wrongType), 26| | ("testMatch_success", testMatch_success), 27| | ] 28| | 29| | 30| 1| func testDescription() { 31| 1| let desc = self.arg.description 32| 1| XCTAssertEqual(desc, "conditioned<(String) -> Bool>") 33| 1| } 34| | 35| | 36| 1| func testMatch_wrongType() { 37| 1| let match = self.arg.match(12) 38| 1| XCTAssertFalse(match) 39| 1| } 40| | 41| | 42| 1| func testMatch_success() { 43| 1| let match = self.arg.match("Hello") 44| 1| XCTAssertTrue(match) 45| 1| } 46| | 47| |} /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Unit Tests/Arguments/Arg/ArgumentVerifyOptionalTests.swift: 1| |// 2| |// ArgumentVerifyOptionalTests.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 20/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import XCTest 10| |@testable import InstantMock 11| | 12| | 13| |final class ArgumentVerifyOptionalTests: XCTestCase { 14| | 15| | private var arg: ArgumentVerifyOptionalImpl! 16| | 17| 4| override func setUp() { 18| 4| super.setUp() 19| 4| self.arg = ArgumentVerifyOptionalImpl({ str in true }) 20| 4| } 21| | 22| | 23| | static var allTests = [ 24| | ("testDescription", testDescription), 25| | ("testMatch_wrongType", testMatch_wrongType), 26| | ("testMatch_success", testMatch_success), 27| | ("testMatch_success_nil", testMatch_success_nil), 28| | ] 29| | 30| | 31| 1| func testDescription() { 32| 1| let desc = self.arg.description 33| 1| XCTAssertEqual(desc, "conditioned<(Optional) -> Bool>") 34| 1| } 35| | 36| | 37| 1| func testMatch_wrongType() { 38| 1| let match = self.arg.match(12) 39| 1| XCTAssertFalse(match) 40| 1| } 41| | 42| | 43| 1| func testMatch_success() { 44| 1| let match = self.arg.match("Hello") 45| 1| XCTAssertTrue(match) 46| 1| } 47| | 48| | 49| 1| func testMatch_success_nil() { 50| 1| let match = self.arg.match(nil) 51| 1| XCTAssertTrue(match) 52| 1| } 53| | 54| |} /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Unit Tests/Arguments/ArgTests.swift: 1| |// 2| |// ArgTests.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 12/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |import XCTest 11| |@testable import InstantMock 12| | 13| | 14| |final class DummyArg {} 15| | 16| |final class ArgTests: XCTestCase { 17| | 18| | private var argStorage: ArgumentStorageMock! 19| | 20| 7| override func setUp() { 21| 7| super.setUp() 22| 7| self.argStorage = ArgumentStorageMock() 23| 7| } 24| | 25| | 26| | static var allTests = [ 27| | ("testEq_string", testEq_string), 28| | ("testEq_int", testEq_int), 29| | ("testEq_intOptional", testEq_intOptional), 30| | ("testEq_object", testEq_object), 31| | ("testAny_string", testAny_string), 32| | ("testAny_int", testAny_int), 33| | ("testVerify_string", testVerify_string), 34| | ] 35| | 36| | 37| 1| func testEq_string() { 38| 1| let factory = ArgumentFactoryMock() 39| 1| let val = Arg.eq("Hello", argFactory: factory, argStorage: self.argStorage) 40| 1| XCTAssertEqual(val, "Hello") 41| 1| XCTAssertEqual(self.argStorage.args.count, 1) 42| 1| 43| 1| var argumentValue = self.argStorage.args.last as? ArgumentValueMock 44| 1| XCTAssertNotNil(argumentValue) 45| 1| XCTAssertEqual(argumentValue!.value, "Hello") 46| 1| 47| 1| _ = Arg.eq("HelloDelu", argFactory: factory, argStorage: self.argStorage) 48| 1| XCTAssertEqual(self.argStorage.args.count, 2) 49| 1| 50| 1| argumentValue = self.argStorage.args.last as? ArgumentValueMock 51| 1| XCTAssertNotNil(argumentValue) 52| 1| XCTAssertEqual(argumentValue!.value, "HelloDelu") 53| 1| } 54| | 55| | 56| 1| func testEq_int() { 57| 1| let factory = ArgumentFactoryMock() 58| 1| let val = Arg.eq(42, argFactory: factory, argStorage: self.argStorage) 59| 1| XCTAssertEqual(val, 42) 60| 1| XCTAssertEqual(self.argStorage.args.count, 1) 61| 1| 62| 1| let argumentValue = self.argStorage.args.last as? ArgumentValueMock 63| 1| XCTAssertNotNil(argumentValue) 64| 1| XCTAssertEqual(argumentValue!.value, 42) 65| 1| } 66| | 67| | 68| 1| func testEq_intOptional() { 69| 1| let factory = ArgumentFactoryMock() 70| 1| var arg: Int? = nil 71| 1| var val = Arg.eq(arg, argFactory: factory, argStorage: self.argStorage) 72| 1| XCTAssertNil(val) 73| 1| XCTAssertEqual(self.argStorage.args.count, 1) 74| 1| 75| 1| var argumentValue = self.argStorage.args.last as? ArgumentValueMock 76| 1| XCTAssertNotNil(argumentValue) 77| 1| XCTAssertEqual(argumentValue!.value, nil) 78| 1| 79| 1| arg = 42 80| 1| val = Arg.eq(arg, argFactory: factory, argStorage: self.argStorage) 81| 1| XCTAssertEqual(val, 42) 82| 1| XCTAssertEqual(self.argStorage.args.count, 2) 83| 1| 84| 1| argumentValue = self.argStorage.args.last as? ArgumentValueMock 85| 1| XCTAssertNotNil(argumentValue) 86| 1| XCTAssertEqual(argumentValue!.value, 42) 87| 1| } 88| | 89| | 90| 1| func testEq_object() { 91| 1| let factory = ArgumentFactoryMock() 92| 1| let dummyArg = DummyArg() 93| 1| let val = Arg.eq(dummyArg, argFactory: factory, argStorage: self.argStorage) 94| 1| XCTAssertTrue(dummyArg === val) 95| 1| XCTAssertEqual(self.argStorage.args.count, 1) 96| 1| 97| 1| let argumentValue = self.argStorage.args.last as? ArgumentValueMock 98| 1| XCTAssertNotNil(argumentValue) 99| 1| XCTAssertTrue(dummyArg === argumentValue!.value) 100| 1| } 101| | 102| | 103| 1| func testAny_string() { 104| 1| let factory = ArgumentFactoryMock() 105| 1| let any: String = Arg.any(argFactory: factory, argStorage: self.argStorage) 106| 1| XCTAssertEqual(any, String.any) 107| 1| XCTAssertEqual(self.argStorage.args.count, 1) 108| 1| 109| 1| var argumentAny = self.argStorage.args.last as? ArgumentAnyMock 110| 1| XCTAssertNotNil(argumentAny) 111| 1| 112| 1| _ = Arg.any(argFactory: factory, argStorage: self.argStorage) 113| 1| XCTAssertEqual(self.argStorage.args.count, 2) 114| 1| argumentAny = self.argStorage.args.last as? ArgumentAnyMock 115| 1| XCTAssertNotNil(argumentAny) 116| 1| } 117| | 118| | 119| 1| func testAny_int() { 120| 1| let factory = ArgumentFactoryMock() 121| 1| let any: Int = Arg.any(argFactory: factory, argStorage: self.argStorage) 122| 1| XCTAssertEqual(any, Int.any) 123| 1| XCTAssertEqual(self.argStorage.args.count, 1) 124| 1| 125| 1| let argumentAny = self.argStorage.args.last as? ArgumentAnyMock 126| 1| XCTAssertNotNil(argumentAny) 127| 1| } 128| | 129| | 130| 1| func testVerify_string() { 131| 1| let factory = ArgumentFactoryMock() 132| 1| let val = Arg.verify({ str in true }, argFactory: factory, argStorage: self.argStorage) as String 133| 1| 134| 1| XCTAssertEqual(val, String.any) 135| 1| XCTAssertEqual(self.argStorage.args.count, 1) 136| 1| 137| 1| _ = Arg.verify({ str in false }, argFactory: factory, argStorage: self.argStorage) as String 138| 1| XCTAssertEqual(self.argStorage.args.count, 2) 139| 1| } 140| | 141| |} 142| | /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Unit Tests/Arguments/Captor/ArgumentCaptorTests.swift: 1| |// 2| |// ArgumentCaptorTests.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 12/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |import XCTest 11| |@testable import InstantMock 12| | 13| | 14| |final class ArgumentCaptorTests: XCTestCase { 15| | 16| | private var storageMock: ArgumentStorageMock! 17| | private var factoryMock: ArgumentFactoryMock! 18| | private var captor: ArgumentCaptor! 19| | 20| | 21| 3| override func setUp() { 22| 3| super.setUp() 23| 3| self.storageMock = ArgumentStorageMock() 24| 3| self.factoryMock = ArgumentFactoryMock() 25| 3| self.captor = ArgumentCaptor() 26| 3| } 27| | 28| | 29| | static var allTests = [ 30| | ("testCapture", testCapture), 31| | ("testValue", testValue), 32| | ("testAllValues", testAllValues), 33| | ] 34| | 35| | 36| 1| func testCapture() { 37| 1| let val = captor.capture(argFactory: self.factoryMock, argStorage: self.storageMock) 38| 1| 39| 1| XCTAssertEqual(val, String.any) 40| 1| XCTAssertEqual(self.storageMock.args.count, 1) 41| 1| 42| 1| let argumentCapture = self.storageMock.args.last as? ArgumentCapture 43| 1| XCTAssertNotNil(argumentCapture) 44| 1| 45| 1| _ = captor.capture(argFactory: self.factoryMock, argStorage: self.storageMock) 46| 1| XCTAssertEqual(self.storageMock.args.count, 2) 47| 1| } 48| | 49| | 50| 1| func testValue() { 51| 1| _ = captor.capture(argFactory: self.factoryMock, argStorage: self.storageMock) 52| 1| 53| 1| let capture = self.factoryMock.argumentCapture 54| 1| capture?.setValue("SomeValue") 55| 1| 56| 1| let value = captor.value 57| 1| XCTAssertEqual(value, "SomeValue") 58| 1| } 59| | 60| | 61| 1| func testAllValues() { 62| 1| _ = captor.capture(argFactory: self.factoryMock, argStorage: self.storageMock) 63| 1| 64| 1| let capture = self.factoryMock.argumentCapture 65| 1| capture?.setValue("SomeValue") 66| 1| 67| 1| let values = captor.allValues 68| 1| XCTAssertEqual(values.first as? String, "SomeValue") 69| 1| } 70| | 71| |} /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Unit Tests/Arguments/Captor/ArgumentCaptorValuesTests.swift: 1| |// 2| |// ArgumentCaptorValuesTests.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 21/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import XCTest 10| |@testable import InstantMock 11| | 12| | 13| |final class ArgumentCaptorValuesTests: XCTestCase { 14| | 15| | 16| | static var allTests = [ 17| | ("testValue", testValue), 18| | ("testAllValues", testAllValues), 19| | ] 20| | 21| | 22| 1| func testValue() { 23| 1| var argumentCapture: ArgumentCaptureImpl? = nil 24| 1| var captor = ArgumentCaptorValuesImpl(argumentCapture) 25| 1| XCTAssertNil(captor.value) 26| 1| 27| 1| argumentCapture = ArgumentCaptureImpl("") 28| 1| argumentCapture?.setValue("something") 29| 1| captor = ArgumentCaptorValuesImpl(argumentCapture) 30| 1| XCTAssertNotNil(captor.value) 31| 1| XCTAssertEqual(captor.value!, "something") 32| 1| } 33| | 34| | 35| 1| func testAllValues() { 36| 1| var argumentCapture: ArgumentCaptureImpl? = nil 37| 1| var captor = ArgumentCaptorValuesImpl(argumentCapture) 38| 1| XCTAssertEqual(captor.allValues.count, 0) 39| 1| 40| 1| argumentCapture = ArgumentCaptureImpl("") 41| 1| argumentCapture?.setValue("something") 42| 1| captor = ArgumentCaptorValuesImpl(argumentCapture) 43| 1| XCTAssertEqual(captor.allValues.count, 1) 44| 1| XCTAssertEqual(captor.allValues.first!, "something") 45| 1| } 46| | 47| |} 48| | /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Unit Tests/Arguments/Captor/ArgumentClosureCaptor/ArgumentClosureCaptor+TwoArgs.swift: 1| |// 2| |// ArgumentClosureCaptorTests+TwoArgs.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 21/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import XCTest 10| |@testable import InstantMock 11| | 12| | 13| |final class ArgumentClosureCaptorTests_TwoArgs: XCTestCase { 14| | 15| | private var storageMock: ArgumentStorageMock! 16| | private var factoryMock: ArgumentFactoryMock<(Int, Int) -> String>! 17| | private var captor: ArgumentClosureCaptor<(Int, Int) -> String>! 18| | 19| | 20| 3| override func setUp() { 21| 3| super.setUp() 22| 3| self.storageMock = ArgumentStorageMock() 23| 3| self.factoryMock = ArgumentFactoryMock<(Int, Int) -> String>() 24| 3| self.captor = ArgumentClosureCaptor<(Int, Int) -> String>() 25| 3| } 26| | 27| | 28| | static var allTests = [ 29| | ("testCapture", testCapture), 30| | ("testValue", testValue), 31| | ("testAllValues", testAllValues), 32| | ] 33| | 34| | 35| 1| func testCapture() { 36| 1| let val = captor.capture(argFactory: self.factoryMock, argStorage: self.storageMock) as (Int, Int) -> String 37| 1| 38| 1| XCTAssertNotNil(val) 39| 1| XCTAssertEqual(self.storageMock.args.count, 1) 40| 1| 41| 1| let argumentCapture = self.storageMock.args.last as? ArgumentCapture 42| 1| XCTAssertNotNil(argumentCapture) 43| 1| 44| 1| _ = captor.capture(argFactory: self.factoryMock, argStorage: self.storageMock) as (Int, Int) -> String 45| 1| XCTAssertEqual(self.storageMock.args.count, 2) 46| 1| } 47| | 48| | 49| 1| func testValue() { 50| 1| _ = captor.capture(argFactory: self.factoryMock, argStorage: self.storageMock) as (Int, Int) -> String 51| 1| 52| 1| let closure: (Int, Int) -> String = { _, _ in return "SomeValue" } 53| 1| 54| 1| let capture = self.factoryMock.argumentCapture 55| 1| capture?.setValue(closure) 56| 1| 57| 1| let value = captor.value 58| 1| XCTAssertNotNil(value) 59| 1| 60| 1| let ret = value!(12, 13) 61| 1| XCTAssertEqual(ret, "SomeValue") 62| 1| } 63| | 64| | 65| 1| func testAllValues() { 66| 1| _ = captor.capture(argFactory: self.factoryMock, argStorage: self.storageMock) as (Int, Int) -> String 67| 1| 68| 1| let closure: (Int, Int) -> String = { _, _ in return "SomeValue" } 69| 1| 70| 1| let capture = self.factoryMock.argumentCapture 71| 1| capture?.setValue(closure) 72| 1| 73| 1| let values = captor.allValues 74| 1| XCTAssertNotNil(values.first!) 75| 1| 76| 1| let ret = values.first!!(12, 13) 77| 1| XCTAssertEqual(ret, "SomeValue") 78| 1| } 79| | 80| |} 81| | 82| | /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Unit Tests/Arguments/Captor/ArgumentClosureCaptor/ArgumentClosureCaptorTests+FiveArgs.swift: 1| |// 2| |// ArgumentClosureCaptorTests+FiveArgs.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 21/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import XCTest 10| |@testable import InstantMock 11| | 12| | 13| |final class ArgumentClosureCaptorTests_FiveArgs: XCTestCase { 14| | 15| | private var storageMock: ArgumentStorageMock! 16| | private var factoryMock: ArgumentFactoryMock<(Int, Int, Int, Int, Int) -> String>! 17| | private var captor: ArgumentClosureCaptor<(Int, Int, Int, Int, Int) -> String>! 18| | 19| | 20| 3| override func setUp() { 21| 3| super.setUp() 22| 3| self.storageMock = ArgumentStorageMock() 23| 3| self.factoryMock = ArgumentFactoryMock<(Int, Int, Int, Int, Int) -> String>() 24| 3| self.captor = ArgumentClosureCaptor<(Int, Int, Int, Int, Int) -> String>() 25| 3| } 26| | 27| | 28| | static var allTests = [ 29| | ("testCapture", testCapture), 30| | ("testValue", testValue), 31| | ("testAllValues", testAllValues), 32| | ] 33| | 34| | 35| 1| func testCapture() { 36| 1| let val = captor.capture(argFactory: self.factoryMock, argStorage: self.storageMock) 37| 1| as (Int, Int, Int, Int, Int) -> String 38| 1| 39| 1| XCTAssertNotNil(val) 40| 1| XCTAssertEqual(self.storageMock.args.count, 1) 41| 1| 42| 1| let argumentCapture = self.storageMock.args.last as? ArgumentCapture 43| 1| XCTAssertNotNil(argumentCapture) 44| 1| 45| 1| _ = captor.capture(argFactory: self.factoryMock, argStorage: self.storageMock) 46| 1| as (Int, Int, Int, Int, Int) -> String 47| 1| XCTAssertEqual(self.storageMock.args.count, 2) 48| 1| } 49| | 50| | 51| 1| func testValue() { 52| 1| _ = captor.capture(argFactory: self.factoryMock, argStorage: self.storageMock) 53| 1| as (Int, Int, Int, Int, Int) -> String 54| 1| 55| 1| let closure: (Int, Int, Int, Int, Int) -> String = { _, _, _, _, _ in return "SomeValue" } 56| 1| 57| 1| let capture = self.factoryMock.argumentCapture 58| 1| capture?.setValue(closure) 59| 1| 60| 1| let value = captor.value 61| 1| XCTAssertNotNil(value) 62| 1| 63| 1| let ret = value!(12, 13, 14, 15, 16) 64| 1| XCTAssertEqual(ret, "SomeValue") 65| 1| } 66| | 67| | 68| 1| func testAllValues() { 69| 1| _ = captor.capture(argFactory: self.factoryMock, argStorage: self.storageMock) 70| 1| as (Int, Int, Int, Int, Int) -> String 71| 1| 72| 1| let closure: (Int, Int, Int, Int, Int) -> String = { _, _, _, _, _ in return "SomeValue" } 73| 1| 74| 1| let capture = self.factoryMock.argumentCapture 75| 1| capture?.setValue(closure) 76| 1| 77| 1| let values = captor.allValues 78| 1| XCTAssertNotNil(values.first!) 79| 1| 80| 1| let ret = values.first!!(12, 13, 14, 15, 16) 81| 1| XCTAssertEqual(ret, "SomeValue") 82| 1| } 83| | 84| |} /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Unit Tests/Arguments/Captor/ArgumentClosureCaptor/ArgumentClosureCaptorTests+FourArgs.swift: 1| |// 2| |// ArgumentClosureCaptorTests+FourArgs.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 21/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import XCTest 10| |@testable import InstantMock 11| | 12| | 13| |final class ArgumentClosureCaptorTests_FourArgs: XCTestCase { 14| | 15| | private var storageMock: ArgumentStorageMock! 16| | private var factoryMock: ArgumentFactoryMock<(Int, Int, Int, Int) -> String>! 17| | private var captor: ArgumentClosureCaptor<(Int, Int, Int, Int) -> String>! 18| | 19| | 20| 3| override func setUp() { 21| 3| super.setUp() 22| 3| self.storageMock = ArgumentStorageMock() 23| 3| self.factoryMock = ArgumentFactoryMock<(Int, Int, Int, Int) -> String>() 24| 3| self.captor = ArgumentClosureCaptor<(Int, Int, Int, Int) -> String>() 25| 3| } 26| | 27| | 28| | static var allTests = [ 29| | ("testCapture", testCapture), 30| | ("testValue", testValue), 31| | ("testAllValues", testAllValues), 32| | ] 33| | 34| | 35| 1| func testCapture() { 36| 1| let val = captor.capture(argFactory: self.factoryMock, argStorage: self.storageMock) 37| 1| as (Int, Int, Int, Int) -> String 38| 1| 39| 1| XCTAssertNotNil(val) 40| 1| XCTAssertEqual(self.storageMock.args.count, 1) 41| 1| 42| 1| let argumentCapture = self.storageMock.args.last as? ArgumentCapture 43| 1| XCTAssertNotNil(argumentCapture) 44| 1| 45| 1| _ = captor.capture(argFactory: self.factoryMock, argStorage: self.storageMock) 46| 1| as (Int, Int, Int, Int) -> String 47| 1| XCTAssertEqual(self.storageMock.args.count, 2) 48| 1| } 49| | 50| | 51| 1| func testValue() { 52| 1| _ = captor.capture(argFactory: self.factoryMock, argStorage: self.storageMock) 53| 1| as (Int, Int, Int, Int) -> String 54| 1| 55| 1| let closure: (Int, Int, Int, Int) -> String = { _, _, _, _ in return "SomeValue" } 56| 1| 57| 1| let capture = self.factoryMock.argumentCapture 58| 1| capture?.setValue(closure) 59| 1| 60| 1| let value = captor.value 61| 1| XCTAssertNotNil(value) 62| 1| 63| 1| let ret = value!(12, 13, 14, 15) 64| 1| XCTAssertEqual(ret, "SomeValue") 65| 1| } 66| | 67| | 68| 1| func testAllValues() { 69| 1| _ = captor.capture(argFactory: self.factoryMock, argStorage: self.storageMock) 70| 1| as (Int, Int, Int, Int) -> String 71| 1| 72| 1| let closure: (Int, Int, Int, Int) -> String = { _, _, _, _ in return "SomeValue" } 73| 1| 74| 1| let capture = self.factoryMock.argumentCapture 75| 1| capture?.setValue(closure) 76| 1| 77| 1| let values = captor.allValues 78| 1| XCTAssertNotNil(values.first!) 79| 1| 80| 1| let ret = values.first!!(12, 13, 14, 15) 81| 1| XCTAssertEqual(ret, "SomeValue") 82| 1| } 83| | 84| |} 85| | /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Unit Tests/Arguments/Captor/ArgumentClosureCaptor/ArgumentClosureCaptorTests+NoArg.swift: 1| |// 2| |// ArgumentClosureCaptorTests+NoArg.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 21/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import XCTest 10| |@testable import InstantMock 11| | 12| | 13| |final class ArgumentClosureCaptorTests_NoArg: XCTestCase { 14| | 15| | private var storageMock: ArgumentStorageMock! 16| | private var factoryMock: ArgumentFactoryMock<() -> String>! 17| | private var captor: ArgumentClosureCaptor<() -> String>! 18| | 19| | 20| 3| override func setUp() { 21| 3| super.setUp() 22| 3| self.storageMock = ArgumentStorageMock() 23| 3| self.factoryMock = ArgumentFactoryMock<() -> String>() 24| 3| self.captor = ArgumentClosureCaptor<() -> String>() 25| 3| } 26| | 27| | 28| | static var allTests = [ 29| | ("testCapture", testCapture), 30| | ("testValue", testValue), 31| | ("testAllValues", testAllValues), 32| | ] 33| | 34| | 35| 1| func testCapture() { 36| 1| let val = captor.capture(argFactory: self.factoryMock, argStorage: self.storageMock) as () -> String 37| 1| 38| 1| XCTAssertNotNil(val) 39| 1| XCTAssertEqual(self.storageMock.args.count, 1) 40| 1| 41| 1| let argumentCapture = self.storageMock.args.last as? ArgumentCapture 42| 1| XCTAssertNotNil(argumentCapture) 43| 1| 44| 1| _ = captor.capture(argFactory: self.factoryMock, argStorage: self.storageMock) as () -> String 45| 1| XCTAssertEqual(self.storageMock.args.count, 2) 46| 1| } 47| | 48| | 49| 1| func testValue() { 50| 1| _ = captor.capture(argFactory: self.factoryMock, argStorage: self.storageMock) as () -> String 51| 1| 52| 1| let closure: () -> String = { return "SomeValue" } 53| 1| 54| 1| let capture = self.factoryMock.argumentCapture 55| 1| capture?.setValue(closure) 56| 1| 57| 1| let value = captor.value 58| 1| XCTAssertNotNil(value) 59| 1| 60| 1| let ret = value!() 61| 1| XCTAssertEqual(ret, "SomeValue") 62| 1| } 63| | 64| | 65| 1| func testAllValues() { 66| 1| _ = captor.capture(argFactory: self.factoryMock, argStorage: self.storageMock) as () -> String 67| 1| 68| 1| let closure: () -> String = { return "SomeValue" } 69| 1| 70| 1| let capture = self.factoryMock.argumentCapture 71| 1| capture?.setValue(closure) 72| 1| 73| 1| let values = captor.allValues 74| 1| XCTAssertNotNil(values.first!) 75| 1| 76| 1| let ret = values.first!!() 77| 1| XCTAssertEqual(ret, "SomeValue") 78| 1| } 79| | 80| |} /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Unit Tests/Arguments/Captor/ArgumentClosureCaptor/ArgumentClosureCaptorTests+OneArg.swift: 1| |// 2| |// ArgumentClosureCaptorTests+OneArg.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 21/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import XCTest 10| |@testable import InstantMock 11| | 12| | 13| |final class ArgumentClosureCaptorTests_OneArg: XCTestCase { 14| | 15| | private var storageMock: ArgumentStorageMock! 16| | private var factoryMock: ArgumentFactoryMock<(Int) -> String>! 17| | private var captor: ArgumentClosureCaptor<(Int) -> String>! 18| | 19| | 20| 3| override func setUp() { 21| 3| super.setUp() 22| 3| self.storageMock = ArgumentStorageMock() 23| 3| self.factoryMock = ArgumentFactoryMock<(Int) -> String>() 24| 3| self.captor = ArgumentClosureCaptor<(Int) -> String>() 25| 3| } 26| | 27| | 28| | static var allTests = [ 29| | ("testCapture", testCapture), 30| | ("testValue", testValue), 31| | ("testAllValues", testAllValues), 32| | ] 33| | 34| | 35| 1| func testCapture() { 36| 1| let val = captor.capture(argFactory: self.factoryMock, argStorage: self.storageMock) as (Int) -> String 37| 1| 38| 1| XCTAssertNotNil(val) 39| 1| XCTAssertEqual(self.storageMock.args.count, 1) 40| 1| 41| 1| let argumentCapture = self.storageMock.args.last as? ArgumentCapture 42| 1| XCTAssertNotNil(argumentCapture) 43| 1| 44| 1| _ = captor.capture(argFactory: self.factoryMock, argStorage: self.storageMock) as (Int) -> String 45| 1| XCTAssertEqual(self.storageMock.args.count, 2) 46| 1| } 47| | 48| | 49| 1| func testValue() { 50| 1| _ = captor.capture(argFactory: self.factoryMock, argStorage: self.storageMock) as (Int) -> String 51| 1| 52| 1| let closure: (Int) -> String = { _ in return "SomeValue" } 53| 1| 54| 1| let capture = self.factoryMock.argumentCapture 55| 1| capture?.setValue(closure) 56| 1| 57| 1| let value = captor.value 58| 1| XCTAssertNotNil(value) 59| 1| 60| 1| let ret = value!(12) 61| 1| XCTAssertEqual(ret, "SomeValue") 62| 1| } 63| | 64| | 65| 1| func testAllValues() { 66| 1| _ = captor.capture(argFactory: self.factoryMock, argStorage: self.storageMock) as (Int) -> String 67| 1| 68| 1| let closure: (Int) -> String = { _ in return "SomeValue" } 69| 1| 70| 1| let capture = self.factoryMock.argumentCapture 71| 1| capture?.setValue(closure) 72| 1| 73| 1| let values = captor.allValues 74| 1| XCTAssertNotNil(values.first!) 75| 1| 76| 1| let ret = values.first!!(12) 77| 1| XCTAssertEqual(ret, "SomeValue") 78| 1| } 79| | 80| |} 81| | /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Unit Tests/Arguments/Captor/ArgumentClosureCaptor/ArgumentClosureCaptorTests+ThreeArgs.swift: 1| |// 2| |// ArgumentClosureCaptorTests+ThreeArgs.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 21/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import XCTest 10| |@testable import InstantMock 11| | 12| | 13| |final class ArgumentClosureCaptorTests_ThreeArgs: XCTestCase { 14| | 15| | private var storageMock: ArgumentStorageMock! 16| | private var factoryMock: ArgumentFactoryMock<(Int, Int, Int) -> String>! 17| | private var captor: ArgumentClosureCaptor<(Int, Int, Int) -> String>! 18| | 19| | 20| 3| override func setUp() { 21| 3| super.setUp() 22| 3| self.storageMock = ArgumentStorageMock() 23| 3| self.factoryMock = ArgumentFactoryMock<(Int, Int, Int) -> String>() 24| 3| self.captor = ArgumentClosureCaptor<(Int, Int, Int) -> String>() 25| 3| } 26| | 27| | 28| | static var allTests = [ 29| | ("testCapture", testCapture), 30| | ("testValue", testValue), 31| | ("testAllValues", testAllValues), 32| | ] 33| | 34| | 35| 1| func testCapture() { 36| 1| let val = captor.capture(argFactory: self.factoryMock, argStorage: self.storageMock) 37| 1| as (Int, Int, Int) -> String 38| 1| 39| 1| XCTAssertNotNil(val) 40| 1| XCTAssertEqual(self.storageMock.args.count, 1) 41| 1| 42| 1| let argumentCapture = self.storageMock.args.last as? ArgumentCapture 43| 1| XCTAssertNotNil(argumentCapture) 44| 1| 45| 1| _ = captor.capture(argFactory: self.factoryMock, argStorage: self.storageMock) 46| 1| as (Int, Int, Int) -> String 47| 1| XCTAssertEqual(self.storageMock.args.count, 2) 48| 1| } 49| | 50| | 51| 1| func testValue() { 52| 1| _ = captor.capture(argFactory: self.factoryMock, argStorage: self.storageMock) 53| 1| as (Int, Int, Int) -> String 54| 1| 55| 1| let closure: (Int, Int, Int) -> String = { _, _, _ in return "SomeValue" } 56| 1| 57| 1| let capture = self.factoryMock.argumentCapture 58| 1| capture?.setValue(closure) 59| 1| 60| 1| let value = captor.value 61| 1| XCTAssertNotNil(value) 62| 1| 63| 1| let ret = value!(12, 13, 14) 64| 1| XCTAssertEqual(ret, "SomeValue") 65| 1| } 66| | 67| | 68| 1| func testAllValues() { 69| 1| _ = captor.capture(argFactory: self.factoryMock, argStorage: self.storageMock) as (Int, Int, Int) -> String 70| 1| 71| 1| let closure: (Int, Int, Int) -> String = { _, _, _ in return "SomeValue" } 72| 1| 73| 1| let capture = self.factoryMock.argumentCapture 74| 1| capture?.setValue(closure) 75| 1| 76| 1| let values = captor.allValues 77| 1| XCTAssertNotNil(values.first!) 78| 1| 79| 1| let ret = values.first!!(12, 13, 14) 80| 1| XCTAssertEqual(ret, "SomeValue") 81| 1| } 82| | 83| |} /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Unit Tests/Arguments/Helpers/ArgumentStorageTests.swift: 1| |// 2| |// ArgumentStorageTests.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 13/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import XCTest 10| |@testable import InstantMock 11| | 12| | 13| |final class ArgumentStorageTests: XCTestCase { 14| | 15| | private var storage: ArgumentStorageImpl! 16| | 17| 2| override func setUp() { 18| 2| super.setUp() 19| 2| self.storage = ArgumentStorageImpl() 20| 2| } 21| | 22| | 23| | static var allTests = [ 24| | ("testStore", testStore), 25| | ("testFlush", testFlush), 26| | ] 27| | 28| | 29| 1| func testStore() { 30| 1| var count = self.storage.all().count 31| 1| XCTAssertEqual(count, 0) 32| 1| 33| 1| let arg1 = ArgumentValueMock("Hello") 34| 1| self.storage.store(arg1) 35| 1| 36| 1| count = self.storage.all().count 37| 1| XCTAssertEqual(count, 1) 38| 1| 39| 1| let arg2 = ArgumentValueMock("Hello2") 40| 1| self.storage.store(arg2) 41| 1| count = self.storage.all().count 42| 1| XCTAssertEqual(count, 2) 43| 1| } 44| | 45| | 46| 1| func testFlush() { 47| 1| self.storage.flush() 48| 1| var count = self.storage.all().count 49| 1| XCTAssertEqual(count, 0) 50| 1| 51| 1| let arg1 = ArgumentValueMock("Hello") 52| 1| self.storage.store(arg1) 53| 1| 54| 1| self.storage.flush() 55| 1| 56| 1| count = self.storage.all().count 57| 1| XCTAssertEqual(count, 0) 58| 1| } 59| | 60| |} /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Unit Tests/Arguments/Helpers/ArgumentsConfigurationTests.swift: 1| |// 2| |// ArgumentsConfigurationTests.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 06/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import XCTest 10| |@testable import InstantMock 11| | 12| | 13| |final class DummyArgConfiguration {} 14| | 15| | 16| |final class ArgumentsConfigurationTests: XCTestCase { 17| | 18| | 19| | static var allTests = [ 20| | ("testDescription_empty", testDescription_empty), 21| | ("testDescription_oneValue", testDescription_oneValue), 22| | ("testDescription_severalValues", testDescription_severalValues), 23| | ("testEquality", testEquality), 24| | ("testGreaterThan", testGreaterThan), 25| | ] 26| | 27| | 28| 1| func testDescription_empty() { 29| 1| let list = [Argument]() 30| 1| let ret = ArgumentsConfiguration(list).description 31| 1| XCTAssertEqual(ret, "none") 32| 1| } 33| | 34| | 35| 1| func testDescription_oneValue() { 36| 1| let list = [ArgumentAnyMock()] 37| 1| let ret = ArgumentsConfiguration(list).description 38| 1| XCTAssertEqual(ret, "argument_any_mock") 39| 1| } 40| | 41| | 42| 1| func testDescription_severalValues() { 43| 1| var list = [Argument]() 44| 1| list.append(ArgumentValueMock(12)) 45| 1| list.append(ArgumentAnyMock()) 46| 1| 47| 1| let ret = ArgumentsConfiguration(list).description 48| 1| XCTAssertEqual(ret, "argument_value_mock, argument_any_mock") 49| 1| } 50| | 51| | 52| 1| func testEquality() { 53| 1| let args1 = ArgumentsConfiguration([ArgumentValueMock(12), ArgumentVerifyMandatoryMock({str in true})]) 54| 1| XCTAssertEqual(args1, args1) 55| 1| 56| 1| var args2 = ArgumentsConfiguration([ArgumentValueMock(13), ArgumentVerifyMandatoryMock({str in true})]) 57| 1| XCTAssertEqual(args1, args2) 58| 1| 59| 1| args2 = ArgumentsConfiguration([ArgumentValueMock(13), ArgumentValueMock(14)]) 60| 1| XCTAssertNotEqual(args1, args2) 61| 1| 62| 1| args2 = ArgumentsConfiguration([ArgumentValueMock(13), ArgumentAnyMock()]) 63| 1| XCTAssertNotEqual(args1, args2) 64| 1| 65| 1| args2 = ArgumentsConfiguration([ArgumentVerifyMandatoryMock({str in false}), ArgumentVerifyMandatoryMock({str in true})]) 66| 1| XCTAssertNotEqual(args1, args2) 67| 1| 68| 1| args2 = ArgumentsConfiguration([ArgumentVerifyMandatoryMock({str in false}), ArgumentAnyMock()]) 69| 1| XCTAssertNotEqual(args1, args2) 70| 1| } 71| | 72| | 73| 1| func testGreaterThan() { 74| 1| let args1 = ArgumentsConfiguration([ArgumentValueMock(12), ArgumentVerifyMandatoryMock({str in true})]) 75| 1| var args2 = ArgumentsConfiguration([ArgumentValueMock(13), ArgumentValueMock(14)]) 76| 1| XCTAssertGreaterThan(args2, args1) 77| 1| 78| 1| args2 = ArgumentsConfiguration([ArgumentValueMock(13), ArgumentAnyMock()]) 79| 1| XCTAssertGreaterThan(args1, args2) 80| 1| 81| 1| args2 = ArgumentsConfiguration([ArgumentVerifyMandatoryMock({str in false}), ArgumentVerifyMandatoryMock({str in true})]) 82| 1| XCTAssertGreaterThan(args1, args2) 83| 1| 84| 1| args2 = ArgumentsConfiguration([ArgumentVerifyMandatoryMock({str in false}), ArgumentAnyMock()]) 85| 1| XCTAssertGreaterThan(args1, args2) 86| 1| } 87| | 88| |} /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Unit Tests/Arguments/Helpers/ArgumentsMatcherTests.swift: 1| |// 2| |// ArgumentsMatcherTests.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 07/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import XCTest 10| |@testable import InstantMock 11| | 12| |final class DummyArgsMatcher {} 13| | 14| | 15| |final class ArgumentsMatcherTests: XCTestCase { 16| | 17| | 18| | static var allTests = [ 19| | ("testMatch_wrongNumberOfArgs", testMatch_wrongNumberOfArgs), 20| | ("testMatch_isAny", testMatch_isAny), 21| | ("testMatch_severalValues", testMatch_severalValues), 22| | ] 23| | 24| | 25| 1| func testMatch_wrongNumberOfArgs() { 26| 1| let config = ArgumentsConfiguration([Argument]()) 27| 1| 28| 1| var args = [Any?]() 29| 1| args.append("something") 30| 1| 31| 1| let match = ArgumentsMatcher(args).match(config) 32| 1| XCTAssertFalse(match) 33| 1| } 34| | 35| | 36| 1| func testMatch_isAny() { 37| 1| var args = [Any?]() 38| 1| args.append(nil) 39| 1| 40| 1| let config = ArgumentsConfiguration([ArgumentAnyMock()]) 41| 1| 42| 1| let match = ArgumentsMatcher(args).match(config) 43| 1| XCTAssertTrue(match) 44| 1| } 45| | 46| | 47| 1| func testMatch_severalValues() { 48| 1| var args = [Any?]() 49| 1| args.append("some_string1") 50| 1| args.append("some_string2") 51| 1| 52| 1| let argMock1 = ArgumentValueMock("string1") 53| 1| let argMock2 = ArgumentValueMock("string2") 54| 1| 55| 1| let config = ArgumentsConfiguration([argMock1, argMock2]) 56| 1| 57| 1| let match = ArgumentsMatcher(args).match(config) 58| 1| XCTAssertTrue(match) 59| 1| 60| 1| XCTAssertEqual(argMock1.matchValue, "some_string1") 61| 1| XCTAssertEqual(argMock2.matchValue, "some_string2") 62| 1| } 63| | 64| |} /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Unit Tests/DefaultValue/DefaultClosureHandlerTests.swift: 1| |// 2| |// DefaultClosureHandlerTests.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 21/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import XCTest 10| |@testable import InstantMock 11| | 12| | 13| |final class DefaultClosureHandlerTests: XCTestCase { 14| | 15| | static var allTests = [ 16| | ("testIt_noArgs", testIt_noArgs), 17| | ("testIt_oneArg", testIt_oneArg), 18| | ("testIt_twoArgs", testIt_twoArgs), 19| | ("testIt_threeArgs", testIt_threeArgs), 20| | ("testIt_fourArgs", testIt_fourArgs), 21| | ("testIt_fiveArgs", testIt_fiveArgs), 22| | ] 23| | 24| | 25| 1| func testIt_noArgs() { 26| 1| let closure = DefaultClosureHandler.it() as () -> Int 27| 1| XCTAssertNotNil(closure) 28| 1| } 29| | 30| | 31| 1| func testIt_oneArg() { 32| 1| let closure = DefaultClosureHandler.it() as (String) -> Int 33| 1| XCTAssertNotNil(closure) 34| 1| } 35| | 36| | 37| 1| func testIt_twoArgs() { 38| 1| let closure = DefaultClosureHandler.it() as (String, String) -> Int 39| 1| XCTAssertNotNil(closure) 40| 1| } 41| | 42| | 43| 1| func testIt_threeArgs() { 44| 1| let closure = DefaultClosureHandler.it() as (String, String, String) -> Int 45| 1| XCTAssertNotNil(closure) 46| 1| } 47| | 48| | 49| 1| func testIt_fourArgs() { 50| 1| let closure = DefaultClosureHandler.it() as (String, String, String, String) -> Int 51| 1| XCTAssertNotNil(closure) 52| 1| } 53| | 54| | 55| 1| func testIt_fiveArgs() { 56| 1| let closure = DefaultClosureHandler.it() as (String, String, String, String, String) -> Int 57| 1| XCTAssertNotNil(closure) 58| 1| } 59| | 60| |} 61| | /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Unit Tests/DefaultValue/DefaultValueHandlerTests.swift: 1| |// 2| |// DefaultValueHandlerTests.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 12/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |import XCTest 11| |@testable import InstantMock 12| | 13| | 14| |final class DummyDefaultValueHandler {} 15| | 16| | 17| |final class DefaultValueHandlerTests: XCTestCase { 18| | 19| | 20| | static var allTests = [ 21| | ("testIt_notMockUsable", testIt_notMockUsable), 22| | ("testIt_string", testIt_string), 23| | ] 24| | 25| | 26| 1| func testIt_notMockUsable() { 27| 1| let defaultValueHandler = DefaultValueHandler() 28| 1| XCTAssertNil(defaultValueHandler.it) 29| 1| } 30| | 31| | 32| 1| func testIt_string() { 33| 1| let defaultValueHandler = DefaultValueHandler() 34| 1| XCTAssertEqual(defaultValueHandler.it, String.any) 35| 1| } 36| | 37| |} /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Unit Tests/Factories/ExpectationFactoryTests.swift: 1| |// 2| |// ExpectationFactoryTests.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 05/05/2019. 6| |// Copyright 2019 pirishd. All rights reserved. 7| |// 8| | 9| |import Foundation 10| |import XCTest 11| |@testable import InstantMock 12| | 13| | 14| |final class ExpectationFactoryTests: XCTestCase { 15| | 16| | 17| | static var allTests = [ 18| | ("testExpectationWithStub", testExpectationWithStub), 19| | ("testRejectionWithStub", testRejectionWithStub), 20| | ] 21| | 22| | 23| 1| func testExpectationWithStub() { 24| 1| let expectation = ExpectationFactoryImpl().expectation(withStub: Stub()) 25| 1| XCTAssertNotNil(expectation) 26| 1| 27| 1| } 28| | 29| 1| func testRejectionWithStub() { 30| 1| let rejection = ExpectationFactoryImpl().rejection(withStub: Stub()) 31| 1| XCTAssertNotNil(rejection) 32| 1| } 33| | 34| |} /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Unit Tests/Interceptors/CallInterceptorStorageTests.swift: 1| |// 2| |// CallInterceptorRepositoryTests.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 06/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import XCTest 10| |@testable import InstantMock 11| | 12| | 13| |final class CallInterceptorStorageTests: XCTestCase { 14| | 15| | private var repository: CallInterceptorStorage! 16| | private var stub: Stub! 17| | 18| | 19| 4| override func setUp() { 20| 4| super.setUp() 21| 4| self.repository = CallInterceptorStorage() 22| 4| self.stub = Stub() 23| 4| } 24| | 25| | 26| | static var allTests = [ 27| | ("testInterceptors", testInterceptors), 28| | ("testRegisterInterceptors", testRegisterInterceptors), 29| | ("testAll", testAll), 30| | ("testRemoveAll", testRemoveAll), 31| | ] 32| | 33| | 34| 1| func testInterceptors() { 35| 1| let interceptors = self.repository.interceptors(for: "") 36| 1| XCTAssertEqual(interceptors.count, 0) 37| 1| 38| 1| self.repository.store(interceptor: self.stub, for: "someFunction") 39| 1| XCTAssertEqual(interceptors.count, 0) 40| 1| } 41| | 42| | 43| 1| func testRegisterInterceptors() { 44| 1| self.repository.store(interceptor: self.stub, for: "someFunction") 45| 1| var interceptors = self.repository.interceptors(for: "someFunction") 46| 1| 47| 1| XCTAssertEqual(interceptors.count, 1) 48| 1| XCTAssertTrue(interceptors.first === self.stub) 49| 1| 50| 1| let stub2 = Stub() 51| 1| self.repository.store(interceptor: stub2, for: "someFunction") 52| 1| interceptors = self.repository.interceptors(for: "someFunction") 53| 1| 54| 1| XCTAssertEqual(interceptors.count, 2) 55| 1| XCTAssertTrue(interceptors.last === stub2) 56| 1| } 57| | 58| | 59| 1| func testAll() { 60| 1| self.repository.store(interceptor: self.stub, for: "someFunction") 61| 1| 62| 1| let stub2 = Stub() 63| 1| self.repository.store(interceptor: stub2, for: "someFunction") 64| 1| 65| 1| let stub3 = Stub() 66| 1| self.repository.store(interceptor: stub3, for: "someOtherFunction") 67| 1| 68| 1| let all = self.repository.all() 69| 1| XCTAssertEqual(all.count, 3) 70| 1| XCTAssertTrue(all.contains(where: { $0 === self.stub} )) 71| 2| XCTAssertTrue(all.contains(where: { $0 === stub2} )) 72| 3| XCTAssertTrue(all.contains(where: { $0 === stub3} )) 73| 1| } 74| | 75| 1| func testRemoveAll() { 76| 1| self.repository.store(interceptor: stub, for: "someFunction") 77| 1| let stub2 = Stub() 78| 1| self.repository.store(interceptor: stub2, for: "someFunction") 79| 1| XCTAssertEqual(self.repository.all().count, 2) 80| 1| self.repository.removeAll() 81| 1| XCTAssertEqual(self.repository.all().count, 0) 82| 1| } 83| |} /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Unit Tests/Interceptors/CallInterceptorTests.swift: 1| |// 2| |// CallInterceptorTests.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 07/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import XCTest 10| |@testable import InstantMock 11| | 12| | 13| |final class CallInterceptorTests: XCTestCase { 14| | 15| | 16| | static var allTests = [ 17| | ("testMatching_noConfig", testMatching_noConfig), 18| | ("testMatching_oneMatching", testMatching_oneMatching), 19| | ("testMatching_success", testMatching_success), 20| | ] 21| | 22| | 23| 1| func testMatching_noConfig() { 24| 1| let interceptor = CallInterceptor() 25| 1| 26| 1| let list = [interceptor] 27| 1| let ret = list.matching([Any?]()) 28| 1| 29| 1| XCTAssertEqual(ret.count, 0) 30| 1| } 31| | 32| | 33| 1| func testMatching_oneMatching() { 34| 1| let interceptor1 = CallInterceptor() 35| 1| let config1 = CallConfiguration(for: "", with: ArgumentsConfiguration([ArgumentAnyMock()])) 36| 1| interceptor1.configuration = config1 37| 1| 38| 1| let interceptor2 = CallInterceptor() 39| 1| let argumentValueMock = ArgumentValueMock("another string") 40| 1| argumentValueMock.shouldMatch = false 41| 1| let config2 = CallConfiguration(for: "", with: ArgumentsConfiguration([argumentValueMock])) 42| 1| interceptor2.configuration = config2 43| 1| 44| 1| let list = [interceptor1, interceptor2] 45| 1| let ret = list.matching(["string"]) 46| 1| 47| 1| XCTAssertEqual(ret.count, 1) 48| 1| XCTAssertTrue(ret.first === interceptor1) 49| 1| } 50| | 51| | 52| 1| func testMatching_success() { 53| 1| let interceptor1 = CallInterceptor() 54| 1| let config1 = CallConfiguration(for: "", with: ArgumentsConfiguration([ArgumentAnyMock()])) 55| 1| interceptor1.configuration = config1 56| 1| 57| 1| let interceptor2 = CallInterceptor() 58| 1| let argumentValueMock = ArgumentValueMock("string") 59| 1| let config2 = CallConfiguration(for: "", with: ArgumentsConfiguration([argumentValueMock])) 60| 1| interceptor2.configuration = config2 61| 1| 62| 1| let list = [interceptor1, interceptor2] 63| 1| let ret = list.matching(["string"]) 64| 1| 65| 1| XCTAssertEqual(ret.count, 2) 66| 1| } 67| | 68| |} /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Unit Tests/Interceptors/ExpectationTests.swift: 1| |// 2| |// ExpectationTests.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 07/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import XCTest 10| |@testable import InstantMock 11| | 12| | 13| |final class ExpectationTests: XCTestCase { 14| | 15| | private var expectation: Expectation! 16| | private var argsConfig: ArgumentsConfiguration! 17| | 18| | 19| 12| override func setUp() { 20| 12| super.setUp() 21| 12| self.expectation = Expectation(withStub: Stub()) 22| 12| self.argsConfig = ArgumentsConfiguration([Argument]()) 23| 12| } 24| | 25| | 26| | static var allTests = [ 27| | ("testHandleCall", testHandleCall), 28| | ("testVerified_basic", testVerified_basic), 29| | ("testVerified_called", testVerified_called), 30| | ("testVerified_withExpectedNumberOfCalls", testVerified_withExpectedNumberOfCalls), 31| | ("testReason_basic", testReason_basic), 32| | ("testReason_called", testReason_called), 33| | ("testReason_withExpectedNumberOfCalls", testReason_withExpectedNumberOfCalls), 34| | ("testReason_rejected_basic", testReason_rejected_basic), 35| | ("testReason_rejected_notCalled", testReason_rejected_notCalled), 36| | ("testReason_rejected_withExpectedNumberOfCalls", testReason_rejected_withExpectedNumberOfCalls), 37| | ("testVerify_accepted", testVerify_accepted), 38| | ("testVerify_rejected", testVerify_rejected), 39| | ] 40| | 41| | 42| 1| func testHandleCall() { 43| 1| let ret = try! self.expectation.handleCall([]) 44| 1| XCTAssertNil(ret) 45| 1| } 46| | 47| | 48| 1| func testVerified_basic() { 49| 1| let verified = self.expectation.verified 50| 1| XCTAssertFalse(verified) 51| 1| } 52| | 53| | 54| 1| func testVerified_called() { 55| 1| try! self.expectation.handleCall([]) 56| 1| var verified = self.expectation.verified 57| 1| XCTAssertTrue(verified) 58| 1| 59| 1| // call a second time 60| 1| try! self.expectation.handleCall([]) 61| 1| verified = self.expectation.verified 62| 1| XCTAssertTrue(verified) 63| 1| } 64| | 65| | 66| 1| func testVerified_withExpectedNumberOfCalls() { 67| 1| self.expectation.call(Int.any, count: 2) 68| 1| 69| 1| try! self.expectation.handleCall([]) 70| 1| var verified = self.expectation.verified 71| 1| XCTAssertFalse(verified) 72| 1| 73| 1| // call a second time 74| 1| try! self.expectation.handleCall([]) 75| 1| verified = self.expectation.verified 76| 1| XCTAssertTrue(verified) 77| 1| } 78| | 79| | 80| 1| func testReason_basic() { 81| 1| var reason = self.expectation.reason 82| 1| XCTAssertNil(reason) 83| 1| 84| 1| let config = CallConfiguration(for: "Func", with: self.argsConfig) 85| 1| self.expectation.configuration = config 86| 1| 87| 1| reason = self.expectation.reason 88| 1| XCTAssertEqual(reason, "Func never called with expected args (none)") 89| 1| } 90| | 91| | 92| 1| func testReason_called() { 93| 1| let config = CallConfiguration(for: "Func", with: self.argsConfig) 94| 1| self.expectation.configuration = config 95| 1| 96| 1| try! self.expectation.handleCall([]) 97| 1| var reason = self.expectation.reason 98| 1| XCTAssertNil(reason) 99| 1| 100| 1| // call a second time 101| 1| try! self.expectation.handleCall([]) 102| 1| reason = self.expectation.reason 103| 1| XCTAssertNil(reason) 104| 1| } 105| | 106| | 107| 1| func testReason_withExpectedNumberOfCalls() { 108| 1| let config = CallConfiguration(for: "Func", with: self.argsConfig) 109| 1| self.expectation.configuration = config 110| 1| 111| 1| self.expectation.call(Int.any, count: 2) 112| 1| 113| 1| try! self.expectation.handleCall([]) 114| 1| var reason = self.expectation.reason 115| 1| XCTAssertEqual(reason, "Func not called the expected number of times (1 out of 2) with expected args (none)") 116| 1| 117| 1| // call a second time 118| 1| try! self.expectation.handleCall([]) 119| 1| reason = self.expectation.reason 120| 1| XCTAssertNil(reason) 121| 1| } 122| | 123| | 124| 1| func testReason_rejected_basic() { 125| 1| let rejection = Expectation(withStub: Stub(), reject: true) 126| 1| var reason = rejection.reason 127| 1| XCTAssertNil(reason) 128| 1| 129| 1| let config = CallConfiguration(for: "Func", with: self.argsConfig) 130| 1| rejection.configuration = config 131| 1| 132| 1| try! rejection.handleCall([]) 133| 1| 134| 1| reason = rejection.reason 135| 1| XCTAssertEqual(reason, "Func called with unexpected args (none)") 136| 1| } 137| | 138| | 139| 1| func testReason_rejected_notCalled() { 140| 1| let rejection = Expectation(withStub: Stub(), reject: true) 141| 1| var reason = rejection.reason 142| 1| XCTAssertNil(reason) 143| 1| 144| 1| let config = CallConfiguration(for: "Func", with: self.argsConfig) 145| 1| rejection.configuration = config 146| 1| 147| 1| reason = rejection.reason 148| 1| XCTAssertNil(reason) 149| 1| } 150| | 151| | 152| 1| func testReason_rejected_withExpectedNumberOfCalls() { 153| 1| let rejection = Expectation(withStub: Stub(), reject: true) 154| 1| 155| 1| let config = CallConfiguration(for: "Func", with: self.argsConfig) 156| 1| rejection.configuration = config 157| 1| 158| 1| rejection.call(Int.any, count: 2) 159| 1| 160| 1| try! rejection.handleCall([]) 161| 1| var reason = rejection.reason 162| 1| XCTAssertNil(reason) 163| 1| 164| 1| // call a second time 165| 1| try! rejection.handleCall([]) 166| 1| reason = rejection.reason 167| 1| XCTAssertEqual(reason, "Func called a wrong number of times (2) with unexpected args (none)") 168| 1| } 169| | 170| | 171| 1| func testVerify_accepted() { 172| 1| let assertion = AssertionMock() 173| 1| let expectation = Expectation(withStub: Stub(), assertion: assertion) 174| 1| let config = CallConfiguration(for: "Func", with: self.argsConfig) 175| 1| expectation.configuration = config 176| 1| 177| 1| let file: StaticString = "file" 178| 1| expectation.verify(file: file, line: 28) 179| 1| XCTAssertEqual(assertion.description, "Func never called with expected args (none)") 180| 1| XCTAssertEqual(assertion.file?.description, file.description) 181| 1| XCTAssertEqual(assertion.line, 28) 182| 1| } 183| | 184| | 185| 1| func testVerify_rejected() { 186| 1| let assertion = AssertionMock() 187| 1| let rejection = Expectation(withStub: Stub(), reject: true, assertion: assertion) 188| 1| let config = CallConfiguration(for: "Func", with: self.argsConfig) 189| 1| rejection.configuration = config 190| 1| 191| 1| try! rejection.handleCall([]) 192| 1| 193| 1| let file: StaticString = "file" 194| 1| rejection.verify(file: file, line: 28) 195| 1| XCTAssertEqual(assertion.description, "Func called with unexpected args (none)") 196| 1| XCTAssertEqual(assertion.file?.description, file.description) 197| 1| XCTAssertEqual(assertion.line, 28) 198| 1| } 199| | 200| | 201| |} /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Unit Tests/Interceptors/Helpers/CallConfigurationTests.swift: 1| |// 2| |// CallConfigurationTests.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 13/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import XCTest 10| |@testable import InstantMock 11| | 12| | 13| |final class CallConfigurationTests: XCTestCase { 14| | 15| | 16| | static var allTests = [ 17| | ("testEqual", testEqual), 18| | ("testGreaterThan", testGreaterThan), 19| | ] 20| | 21| | 22| 1| func testEqual() { 23| 1| let args1 = ArgumentsConfiguration([ArgumentValueMock(12)]) 24| 1| let args2 = ArgumentsConfiguration([ArgumentValueMock(13)]) 25| 1| 26| 1| let configuration1 = CallConfiguration(for: "func", with: args1) 27| 1| let configuration2 = CallConfiguration(for: "func", with: args2) 28| 1| 29| 1| XCTAssertEqual(configuration1, configuration2) 30| 1| } 31| | 32| | 33| 1| func testGreaterThan() { 34| 1| let args1 = ArgumentsConfiguration([ArgumentValueMock(12), ArgumentValueMock(13)]) 35| 1| let args2 = ArgumentsConfiguration([ArgumentValueMock(13), ArgumentAnyMock()]) 36| 1| 37| 1| let configuration1 = CallConfiguration(for: "func", with: args1) 38| 1| let configuration2 = CallConfiguration(for: "func", with: args2) 39| 1| 40| 1| XCTAssertGreaterThan(configuration1, configuration2) 41| 1| } 42| | 43| |} /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Unit Tests/Interceptors/StubTests.swift: 1| |// 2| |// StubTests.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 08/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import XCTest 10| |@testable import InstantMock 11| | 12| | 13| |final class StubTests: XCTestCase { 14| | 15| | private var stub: Stub! 16| | private var argsConfig: ArgumentsConfiguration! 17| | 18| | 19| 19| override func setUp() { 20| 19| super.setUp() 21| 19| self.stub = Stub() 22| 19| self.argsConfig = ArgumentsConfiguration([Argument]()) 23| 19| } 24| | 25| | 26| | static var allTests = [ 27| | ("testHandleCall_nil", testHandleCall_nil), 28| | ("testHandleCall_andReturn", testHandleCall_andReturn), 29| | ("testReturns_andThrow", testReturns_andThrow), 30| | ("testHandleCall_andReturnClosure", testHandleCall_andReturnClosure), 31| | ("testHandleCall_andReturn_andReturnClosure", testHandleCall_andReturn_andReturnClosure), 32| | ("testHandleCall_andReturn_andDo", testHandleCall_andReturn_andDo), 33| | ("testHandleCall_andReturnClosure_andDo", testHandleCall_andReturnClosure_andDo), 34| | ("testHandleCall_capture", testHandleCall_capture), 35| | ("testReturns_false", testReturns_false), 36| | ("testReturns_andReturn", testReturns_andReturn), 37| | ("testReturns_andReturnClosure", testReturns_andReturnClosure), 38| | ("testBest_none", testBest_none), 39| | ("testBest_one", testBest_one), 40| | ("testBest_several", testBest_several), 41| | ("testEqual", testEqual), 42| | ("testEqual_defaultFailure", testEqual_defaultFailure), 43| | ("testGreaterThan", testGreaterThan), 44| | ("testGreaterThan_defaultFailure", testGreaterThan_defaultFailure), 45| | ] 46| | 47| | 48| 1| func testHandleCall_nil() { 49| 1| let ret = try! self.stub.handleCall([]) 50| 1| XCTAssertNil(ret) 51| 1| } 52| | 53| | 54| 1| func testHandleCall_andReturn() { 55| 1| self.stub.andReturn(36) 56| 1| let ret = try! self.stub.handleCall([]) 57| 1| XCTAssertEqual(ret as! Int, 36) 58| 1| } 59| | 60| | 61| 1| func testReturns_andThrow() { 62| 1| enum SomeError: Error { 63| 1| case some 64| 1| } 65| 1| self.stub.andThrow(SomeError.some) 66| 1| 67| 1| XCTAssertThrowsError(try self.stub.handleCall([])) { (error) -> Void in 68| 1| XCTAssertEqual(error as? SomeError, SomeError.some) 69| 1| } 70| 1| } 71| | 72| | 73| 1| func testHandleCall_andReturnClosure() { 74| 1| self.stub.andReturn(closure: { _ in return 12 }) 75| 1| let ret = try! self.stub.handleCall([]) 76| 1| XCTAssertEqual(ret as! Int, 12) 77| 1| } 78| | 79| | 80| 1| func testHandleCall_andReturn_andReturnClosure() { 81| 1| self.stub.andReturn(closure: { _ in return 12 }).andReturn(36) 82| 1| let ret = try! self.stub.handleCall([]) 83| 1| XCTAssertEqual(ret as! Int, 36) 84| 1| } 85| | 86| | 87| 1| func testHandleCall_andDo() { 88| 1| 89| 1| var something = "" 90| 1| self.stub.andDo { _ in something = "not_empty" } 91| 1| 92| 1| _ = try! self.stub.handleCall([]) 93| 1| XCTAssertEqual(something, "not_empty") 94| 1| } 95| | 96| | 97| 1| func testHandleCall_andReturn_andDo() { 98| 1| var something = "" 99| 1| self.stub.andDo({ _ in something = "not_empty" }).andReturn(36) 100| 1| 101| 1| let ret = try! self.stub.handleCall([]) 102| 1| XCTAssertEqual(something, "not_empty") 103| 1| XCTAssertEqual(ret as! Int, 36) 104| 1| } 105| | 106| | 107| 1| func testHandleCall_andReturnClosure_andDo() { 108| 1| var something = "" 109| 1| self.stub.andDo({ _ in something = "not_empty" }).andReturn(closure: { _ in return 12 }) 110| 1| 111| 1| let ret = try! self.stub.handleCall([]) 112| 1| XCTAssertEqual(something, "not_empty") 113| 1| XCTAssertEqual(ret as! Int, 12) 114| 1| } 115| | 116| | 117| 1| func testHandleCall_capture() { 118| 1| let captureMock = ArgumentCaptureMock() 119| 1| let argsConfig = ArgumentsConfiguration([captureMock]) 120| 1| self.stub.configuration = CallConfiguration(for: "func", with: argsConfig) 121| 1| 122| 1| _ = try! self.stub.handleCall(["Hello"]) 123| 1| XCTAssertEqual(captureMock.value as! String, "Hello") 124| 1| } 125| | 126| | 127| 1| func testReturns_false() { 128| 1| let returns = self.stub.returns 129| 1| XCTAssertFalse(returns) 130| 1| } 131| | 132| | 133| 1| func testReturns_andReturn() { 134| 1| self.stub.andReturn(12) 135| 1| let returns = self.stub.returns 136| 1| XCTAssertTrue(returns) 137| 1| } 138| | 139| | 140| 1| func testReturns_andReturnClosure() { 141| 1| self.stub.andReturn(closure: { _ in return 12} ) 142| 1| let returns = self.stub.returns 143| 1| XCTAssertTrue(returns) 144| 1| } 145| | 146| | 147| 1| func testBest_none() { 148| 1| let list = [Stub]() 149| 1| let best = list.best() 150| 1| XCTAssertNil(best) 151| 1| } 152| | 153| | 154| 1| func testBest_one() { 155| 1| let stub = Stub() 156| 1| 157| 1| var list = [Stub]() 158| 1| list.append(stub) 159| 1| 160| 1| let best = list.best() 161| 1| XCTAssertTrue(best === stub) 162| 1| } 163| | 164| | 165| 1| func testBest_several() { 166| 1| 167| 1| let valueMock12 = ArgumentValueMock(12) 168| 1| let valueMock37 = ArgumentValueMock(37) 169| 1| let valueMock42 = ArgumentValueMock(42) 170| 1| 171| 1| let stub1 = Stub() 172| 1| stub1.configuration = CallConfiguration(for: "func", with: ArgumentsConfiguration([ArgumentAnyMock(), valueMock37, valueMock42])) 173| 1| 174| 1| let stub2 = Stub() 175| 1| stub2.configuration = CallConfiguration(for: "func", with: ArgumentsConfiguration([valueMock12, valueMock37, valueMock42])) 176| 1| 177| 1| var list = [Stub]() 178| 1| list.append(stub1) 179| 1| list.append(stub2) 180| 1| 181| 1| let best = list.best() 182| 1| XCTAssertTrue(best === stub2) 183| 1| } 184| | 185| | 186| 1| func testEqual() { 187| 1| let stub1 = Stub() 188| 1| stub1.configuration = CallConfiguration(for: "func", with: ArgumentsConfiguration([ArgumentValueMock(12)])) 189| 1| let stub2 = Stub() 190| 1| stub2.configuration = CallConfiguration(for: "func", with: ArgumentsConfiguration([ArgumentValueMock(13)])) 191| 1| 192| 1| XCTAssertEqual(stub1, stub2) 193| 1| } 194| | 195| | 196| 1| func testEqual_defaultFailure() { 197| 1| let stub1 = Stub() 198| 1| let stub2 = Stub() 199| 1| XCTAssertNotEqual(stub1, stub2) 200| 1| } 201| | 202| | 203| 1| func testGreaterThan() { 204| 1| let stub1 = Stub() 205| 1| stub1.configuration = CallConfiguration(for: "func", with: ArgumentsConfiguration([ArgumentValueMock(12), ArgumentValueMock(13)])) 206| 1| let stub2 = Stub() 207| 1| stub2.configuration = CallConfiguration(for: "func", with: ArgumentsConfiguration([ArgumentValueMock(12), ArgumentAnyMock()])) 208| 1| 209| 1| XCTAssertGreaterThan(stub1, stub2) 210| 1| } 211| | 212| | 213| 1| func testGreaterThan_defaultFailure() { 214| 1| let stub1 = Stub() 215| 1| let stub2 = Stub() 216| 1| XCTAssertFalse(stub1 > stub2) 217| 1| } 218| | 219| |} 220| | /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Unit Tests/MockUsable/ArrayMockUsableTests.swift: 1| |// 2| |// ArrayMockUsableTests.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 08/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import XCTest 10| |@testable import InstantMock 11| | 12| | 13| |final class ArrayMockUsableTests: XCTestCase { 14| | 15| | static var allTests = [ 16| | ("testEqual_toNil", testEqual_toNil), 17| | ("testEqual_toWrongType", testEqual_toWrongType), 18| | ("testEqual_toWrongValue", testEqual_toWrongValue), 19| | ("testEqual_toExpectedValue", testEqual_toExpectedValue), 20| | ] 21| | 22| | 23| 1| func testEqual_toNil() { 24| 1| let ret = Array().equal(to: nil) 25| 1| XCTAssertFalse(ret) 26| 1| } 27| | 28| | 29| 1| func testEqual_toWrongType() { 30| 1| let ret = Array().equal(to: 12.0) 31| 1| XCTAssertFalse(ret) 32| 1| } 33| | 34| | 35| 1| func testEqual_toWrongValue() { 36| 1| var array1 = Array() 37| 1| array1.append(1) 38| 1| 39| 1| var array2 = Array() 40| 1| array2.append(2) 41| 1| 42| 1| let ret = array1.equal(to: array2) 43| 1| XCTAssertFalse(ret) 44| 1| } 45| | 46| | 47| 1| func testEqual_toExpectedValue() { 48| 1| var array1 = Array() 49| 1| array1.append(1) 50| 1| 51| 1| var array2 = Array() 52| 1| array2.append(1) 53| 1| 54| 1| let ret = array1.equal(to: array2) 55| 1| XCTAssertTrue(ret) 56| 1| } 57| | 58| |} /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Unit Tests/MockUsable/BoolMockUsableTests.swift: 1| |// 2| |// BoolMockUsableTests.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 06/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import XCTest 10| |@testable import InstantMock 11| | 12| | 13| |final class BoolMockUsableTests: XCTestCase { 14| | 15| | static var allTests = [ 16| | ("testEqual_toNil", testEqual_toNil), 17| | ("testEqual_toWrongType", testEqual_toWrongType), 18| | ("testEqual_toWrongValue", testEqual_toWrongValue), 19| | ("testEqual_toExpectedValue", testEqual_toExpectedValue), 20| | ] 21| | 22| | 23| 1| func testEqual_toNil() { 24| 1| let ret = false.equal(to: nil) 25| 1| XCTAssertFalse(ret) 26| 1| } 27| | 28| | 29| 1| func testEqual_toWrongType() { 30| 1| let ret = false.equal(to: 12.0) 31| 1| XCTAssertFalse(ret) 32| 1| } 33| | 34| | 35| 1| func testEqual_toWrongValue() { 36| 1| let ret = false.equal(to: true) 37| 1| XCTAssertFalse(ret) 38| 1| } 39| | 40| | 41| 1| func testEqual_toExpectedValue() { 42| 1| let ret = false.equal(to: false) 43| 1| XCTAssertTrue(ret) 44| 1| } 45| | 46| | 47| |} /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Unit Tests/MockUsable/DictionaryMockUsableTests.swift: 1| |// 2| |// DictionaryMockUsableTests.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 08/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import XCTest 10| |@testable import InstantMock 11| | 12| | 13| |final class DictionaryMockUsableTests: XCTestCase { 14| | 15| | 16| | static var allTests = [ 17| | ("testEqual_toNil", testEqual_toNil), 18| | ("testEqual_toWrongType", testEqual_toWrongType), 19| | ("testEqual_toWrongValue", testEqual_toWrongValue), 20| | ("testEqual_toExpectedValue", testEqual_toExpectedValue), 21| | ] 22| | 23| | 24| 1| func testEqual_toNil() { 25| 1| let ret = Dictionary().equal(to: nil) 26| 1| XCTAssertFalse(ret) 27| 1| } 28| | 29| | 30| 1| func testEqual_toWrongType() { 31| 1| let ret = Dictionary().equal(to: 12.0) 32| 1| XCTAssertFalse(ret) 33| 1| } 34| | 35| | 36| 1| func testEqual_toWrongKey() { 37| 1| var dict1 = Dictionary() 38| 1| dict1["key"] = 1 39| 1| 40| 1| var dict2 = Dictionary() 41| 1| dict2["key2"] = 1 42| 1| 43| 1| let ret = dict1.equal(to: dict2) 44| 1| XCTAssertFalse(ret) 45| 1| } 46| | 47| | 48| 1| func testEqual_toWrongValue() { 49| 1| var dict1 = Dictionary() 50| 1| dict1["key"] = 1 51| 1| 52| 1| var dict2 = Dictionary() 53| 1| dict2["key"] = 2 54| 1| 55| 1| let ret = dict1.equal(to: dict2) 56| 1| XCTAssertFalse(ret) 57| 1| } 58| | 59| | 60| 1| func testEqual_toExpectedValue() { 61| 1| var dict1 = Dictionary() 62| 1| dict1["key"] = 1 63| 1| 64| 1| var dict2 = Dictionary() 65| 1| dict2["key"] = 1 66| 1| 67| 1| let ret = dict1.equal(to: dict2) 68| 1| XCTAssertTrue(ret) 69| 1| } 70| | 71| | 72| 1| func testEqual_toWrongOrder() { 73| 1| let dict1: [String: Any] = [ 74| 1| "foo": 1, 75| 1| "bar": 2 76| 1| ] 77| 1| let dict2: [String: Any] = [ 78| 1| "bar": 2, 79| 1| "foo": 1 80| 1| ] 81| 1| 82| 1| XCTAssertTrue(dict1.equal(to: dict2)) 83| 1| } 84| | 85| |} /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Unit Tests/MockUsable/DoubleMockUsableTests.swift: 1| |// 2| |// DoubleMockUsableTests.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 06/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import XCTest 10| |@testable import InstantMock 11| | 12| | 13| |final class DoubleMockUsableTests: XCTestCase { 14| | 15| | 16| | static var allTests = [ 17| | ("testEqual_toNil", testEqual_toNil), 18| | ("testEqual_toWrongType", testEqual_toWrongType), 19| | ("testEqual_toWrongValue", testEqual_toWrongValue), 20| | ("testEqual_toExpectedValue", testEqual_toExpectedValue), 21| | ] 22| | 23| | 24| 1| func testEqual_toNil() { 25| 1| let ret = 12.0.equal(to: nil) 26| 1| XCTAssertFalse(ret) 27| 1| } 28| | 29| | 30| 1| func testEqual_toWrongType() { 31| 1| let ret = 12.0.equal(to: 12) 32| 1| XCTAssertFalse(ret) 33| 1| } 34| | 35| | 36| 1| func testEqual_toWrongValue() { 37| 1| let ret = 12.0.equal(to: 13.0) 38| 1| XCTAssertFalse(ret) 39| 1| } 40| | 41| | 42| 1| func testEqual_toExpectedValue() { 43| 1| let ret = 12.0.equal(to: 12.0) 44| 1| XCTAssertTrue(ret) 45| 1| } 46| | 47| | 48| |} /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Unit Tests/MockUsable/IntMockUsableTests.swift: 1| |// 2| |// IntMockUsableTests.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 06/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import XCTest 10| |@testable import InstantMock 11| | 12| | 13| |final class IntMockUsableTests: XCTestCase { 14| | 15| | 16| | static var allTests = [ 17| | ("testEqual_toNil", testEqual_toNil), 18| | ("testEqual_toWrongType", testEqual_toWrongType), 19| | ("testEqual_toWrongValue", testEqual_toWrongValue), 20| | ("testEqual_toExpectedValue", testEqual_toExpectedValue), 21| | ] 22| | 23| | 24| 1| func testEqual_toNil() { 25| 1| let ret = 12.equal(to: nil) 26| 1| XCTAssertFalse(ret) 27| 1| } 28| | 29| | 30| 1| func testEqual_toWrongType() { 31| 1| let ret = 12.equal(to: 12.0) 32| 1| XCTAssertFalse(ret) 33| 1| } 34| | 35| | 36| 1| func testEqual_toWrongValue() { 37| 1| let ret = 12.equal(to: 13) 38| 1| XCTAssertFalse(ret) 39| 1| } 40| | 41| | 42| 1| func testEqual_toExpectedValue() { 43| 1| let ret = 12.equal(to: 12) 44| 1| XCTAssertTrue(ret) 45| 1| } 46| | 47| | 48| |} /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Unit Tests/MockUsable/SetMockUsableTests.swift: 1| |// 2| |// SetMockUsableTests.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 08/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import XCTest 10| |@testable import InstantMock 11| | 12| | 13| |final class SetMockUsableTests: XCTestCase { 14| | 15| | 16| | static var allTests = [ 17| | ("testEqual_toNil", testEqual_toNil), 18| | ("testEqual_toWrongType", testEqual_toWrongType), 19| | ("testEqual_toWrongValue", testEqual_toWrongValue), 20| | ("testEqual_toExpectedValue", testEqual_toExpectedValue), 21| | ] 22| | 23| | 24| 1| func testEqual_toNil() { 25| 1| let ret = Set().equal(to: nil) 26| 1| XCTAssertFalse(ret) 27| 1| } 28| | 29| | 30| 1| func testEqual_toWrongType() { 31| 1| let ret = Set().equal(to: 12.0) 32| 1| XCTAssertFalse(ret) 33| 1| } 34| | 35| | 36| 1| func testEqual_toWrongValue() { 37| 1| var set1 = Set() 38| 1| set1.insert(1) 39| 1| 40| 1| var set2 = Set() 41| 1| set2.insert(2) 42| 1| 43| 1| let ret = set1.equal(to: set2) 44| 1| XCTAssertFalse(ret) 45| 1| } 46| | 47| | 48| 1| func testEqual_toExpectedValue() { 49| 1| var set1 = Set() 50| 1| set1.insert(1) 51| 1| 52| 1| var set2 = Set() 53| 1| set2.insert(1) 54| 1| 55| 1| let ret = set1.equal(to: set2) 56| 1| XCTAssertTrue(ret) 57| 1| } 58| | 59| |} /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Unit Tests/MockUsable/StringMockUsableTests.swift: 1| |// 2| |// StringMockUsableTests.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 06/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import XCTest 10| |@testable import InstantMock 11| | 12| | 13| |final class StringMockUsableTests: XCTestCase { 14| | 15| | 16| | static var allTests = [ 17| | ("testEqual_toNil", testEqual_toNil), 18| | ("testEqual_toWrongType", testEqual_toWrongType), 19| | ("testEqual_toWrongValue", testEqual_toWrongValue), 20| | ("testEqual_toExpectedValue", testEqual_toExpectedValue), 21| | ] 22| | 23| | 24| 1| func testEqual_toNil() { 25| 1| let ret = "any".equal(to: nil) 26| 1| XCTAssertFalse(ret) 27| 1| } 28| | 29| | 30| 1| func testEqual_toWrongType() { 31| 1| let ret = "any".equal(to: 12) 32| 1| XCTAssertFalse(ret) 33| 1| } 34| | 35| | 36| 1| func testEqual_toWrongValue() { 37| 1| let ret = "any".equal(to: "other") 38| 1| XCTAssertFalse(ret) 39| 1| } 40| | 41| | 42| 1| func testEqual_toExpectedValue() { 43| 1| let ret = "any".equal(to: "any") 44| 1| XCTAssertTrue(ret) 45| 1| } 46| | 47| | 48| |} /Users/travis/build/pirishd/InstantMock/Tests/InstantMockTests/Unit Tests/Verifications/VerifierTests.swift: 1| |// 2| |// VerifierTests.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 08/05/2017. 6| |// Copyright 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import XCTest 10| |@testable import InstantMock 11| | 12| | 13| |final class VerifierTests: XCTestCase { 14| | 15| | private var verifier: VerifierImpl! 16| | 17| | 18| 13| override func setUp() { 19| 13| super.setUp() 20| 13| self.verifier = VerifierImpl() 21| 13| } 22| | 23| | 24| | static var allTests = [ 25| | ("testEqual_bothNil", testEqual_bothNil), 26| | ("testEqual_firstNil", testEqual_firstNil), 27| | ("testEqual_secondNil", testEqual_secondNil), 28| | ("testEqual_strings", testEqual_strings), 29| | ("testEqual_stringsWithDifferentValues", testEqual_stringsWithDifferentValues), 30| | ("testEqual_references", testEqual_references), 31| | ("testMatch_differentReferences", testMatch_differentReferences), 32| | ("testMatch_closure_failure", testMatch_closure_failure), 33| | ("testEqualArray_success", testEqualArray_success), 34| | ("testEqualArray_failure", testEqualArray_failure), 35| | ("testVoid", testVoid), 36| | ("testTypes_success", testTypes_success), 37| | ("testTypes_failure", testTypes_failure), 38| | ] 39| | 40| | 41| 1| func testEqual_bothNil() { 42| 1| let ret = self.verifier.equal(nil, to: nil) 43| 1| XCTAssertTrue(ret) 44| 1| } 45| | 46| | 47| 1| func testEqual_firstNil() { 48| 1| let ret = self.verifier.equal(nil, to: "something") 49| 1| XCTAssertFalse(ret) 50| 1| } 51| | 52| | 53| 1| func testEqual_secondNil() { 54| 1| let ret = self.verifier.equal("else", to: nil) 55| 1| XCTAssertFalse(ret) 56| 1| } 57| | 58| | 59| 1| func testEqual_strings() { 60| 1| let ret = self.verifier.equal("something", to: "something") 61| 1| XCTAssertTrue(ret) 62| 1| } 63| | 64| | 65| 1| func testEqual_stringsWithDifferentValues() { 66| 1| let ret = self.verifier.equal("something", to: "something else") 67| 1| XCTAssertFalse(ret) 68| 1| } 69| | 70| | 71| 1| func testEqual_references() { 72| 1| let instance = DummyArgsMatcher() 73| 1| let ret = self.verifier.equal(instance, to: instance) 74| 1| XCTAssertTrue(ret) 75| 1| } 76| | 77| | 78| 1| func testMatch_differentReferences() { 79| 1| let instance = DummyArgsMatcher() 80| 1| let otherInstance = DummyArgsMatcher() 81| 1| 82| 1| let ret = self.verifier.equal(instance, to: otherInstance) 83| 1| XCTAssertFalse(ret) 84| 1| } 85| | 86| | 87| 1| func testMatch_closure_failure() { 88| 1| let closure: (() -> Void) = {} 89| 1| let closure2: ((String) -> Void) = { str in } 90| 1| 91| 1| let ret = self.verifier.equal(closure, to: closure2) 92| 1| XCTAssertFalse(ret) 93| 1| } 94| | 95| | 96| 1| func testEqualArray_success() { 97| 1| let array1 = ["val1", "val2"] 98| 1| let array2 = ["val1", "val2"] 99| 1| 100| 1| let ret = self.verifier.equalArray(array1, to: array2) 101| 1| XCTAssertTrue(ret) 102| 1| } 103| | 104| | 105| 1| func testEqualArray_failure() { 106| 1| let array1 = ["val1", "val2"] 107| 1| var array2 = ["val1"] 108| 1| 109| 1| var ret = self.verifier.equalArray(array1, to: array2) 110| 1| XCTAssertFalse(ret) 111| 1| 112| 1| array2 = ["val1", "val3"] 113| 1| ret = self.verifier.equalArray(array1, to: array2) 114| 1| XCTAssertFalse(ret) 115| 1| } 116| | 117| | 118| 1| func testVoid() { 119| 1| let void1: Void = () 120| 1| let void2: Void = () 121| 1| 122| 1| var ret = self.verifier.equal(void1, to: "bad") 123| 1| XCTAssertFalse(ret) 124| 1| 125| 1| ret = self.verifier.equal(void1, to: void2) 126| 1| XCTAssertTrue(ret) 127| 1| } 128| | 129| | 130| 1| func testTypes_success() { 131| 1| let type1 = String.self 132| 1| let type2 = String.self 133| 1| 134| 1| let ret = self.verifier.equal(type1, to: type2) 135| 1| XCTAssertTrue(ret) 136| 1| } 137| | 138| | 139| 1| func testTypes_failure() { 140| 1| let type1 = String.self 141| 1| let type2 = Bool.self 142| 1| let str = "Hello" 143| 1| 144| 1| var ret = self.verifier.equal(type1, to: str) 145| 1| XCTAssertFalse(ret) 146| 1| 147| 1| ret = self.verifier.equal(type1, to: type2) 148| 1| XCTAssertFalse(ret) 149| 1| } 150| | 151| |} 152| | <<<<<< EOF # path=InstantMock.framework.coverage.txt /Users/travis/build/pirishd/InstantMock/Sources/InstantMock/Arguments/Arg+Closure.swift: 1| |// 2| |// Arg+Closure.swift 3| |// InstantMock iOS 4| |// 5| |// Created by Patrick on 18/07/2017. 6| |// Copyright © 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |/** Extension for Arg, dedicated to matching a closure */ 11| |extension Arg { 12| | 13| | /** Register a closure with no arg */ 14| 1| public static func closure() -> T where T == () -> Ret { 15| 1| let factory = ArgumentFactoryImpl() 16| 1| return Arg.closure(argFactory: factory, argStorage: ArgumentStorageImpl.instance) as () -> Ret 17| 1| } 18| | 19| | 20| | /** Register a closure with one arg (for dependency injection) */ 21| | static func closure(argFactory: F, argStorage: ArgumentStorage) -> () -> Ret 22| 2| where F: ArgumentFactory, F.Value == T { 23| 2| 24| 2| // create and store instance 25| 2| let typeDescription = "\(T.self)" 26| 2| let arg = argFactory.argumentClosure(typeDescription) 27| 2| argStorage.store(arg) 28| 2| 29| 2| // return default value 30| 2| return DefaultClosureHandler.it() as () -> Ret 31| 2| } 32| | 33| | 34| | /** Register a closure with one arg */ 35| 1| public static func closure() -> T where T == (Arg1) -> Ret { 36| 1| let factory = ArgumentFactoryImpl() 37| 1| return Arg.closure(argFactory: factory, argStorage: ArgumentStorageImpl.instance) as (Arg1) -> Ret 38| 1| } 39| | 40| | 41| | /** Register a closure with one arg (for dependency injection) */ 42| | static func closure(argFactory: F, argStorage: ArgumentStorage) -> (Arg1) -> Ret 43| 2| where F: ArgumentFactory, F.Value == T { 44| 2| 45| 2| // create and store instance 46| 2| let typeDescription = "\(T.self)" 47| 2| let arg = argFactory.argumentClosure(typeDescription) 48| 2| argStorage.store(arg) 49| 2| 50| 2| // return default value 51| 2| return DefaultClosureHandler.it() as (Arg1) -> Ret 52| 2| } 53| | 54| | 55| | /** Register a closure with two args */ 56| 4| public static func closure() -> T where T == (Arg1, Arg2) -> Ret { 57| 4| let factory = ArgumentFactoryImpl() 58| 4| return Arg.closure(argFactory: factory, argStorage: ArgumentStorageImpl.instance) as (Arg1, Arg2) -> Ret 59| 4| } 60| | 61| | 62| | /** Register a closure with two args (for dependency injection) */ 63| | static func closure(argFactory: F, argStorage: ArgumentStorage) -> (Arg1, Arg2) -> Ret 64| 5| where F: ArgumentFactory, F.Value == T { 65| 5| 66| 5| // create and store instance 67| 5| let typeDescription = "\(T.self)" 68| 5| let arg = argFactory.argumentClosure(typeDescription) 69| 5| argStorage.store(arg) 70| 5| 71| 5| // return default value 72| 5| return DefaultClosureHandler.it() as (Arg1, Arg2) -> Ret 73| 5| } 74| | 75| | 76| | /** Register a closure with three args */ 77| 1| public static func closure() -> T where T == (Arg1, Arg2, Arg3) -> Ret { 78| 1| let factory = ArgumentFactoryImpl() 79| 1| return Arg.closure(argFactory: factory, argStorage: ArgumentStorageImpl.instance) as (Arg1, Arg2, Arg3) -> Ret 80| 1| } 81| | 82| | 83| | /** Register a closure with three args (for dependency injection) */ 84| | static func closure(argFactory: F, argStorage: ArgumentStorage) 85| 2| -> (Arg1, Arg2, Arg3) -> Ret where F: ArgumentFactory, F.Value == T { 86| 2| 87| 2| // create and store instance 88| 2| let typeDescription = "\(T.self)" 89| 2| let arg = argFactory.argumentClosure(typeDescription) 90| 2| argStorage.store(arg) 91| 2| 92| 2| // return default value 93| 2| return DefaultClosureHandler.it() as (Arg1, Arg2, Arg3) -> Ret 94| 2| } 95| | 96| | 97| | /** Register a closure with four args */ 98| 1| public static func closure() -> T where T == (Arg1, Arg2, Arg3, Arg4) -> Ret { 99| 1| let factory = ArgumentFactoryImpl() 100| 1| return Arg.closure(argFactory: factory, argStorage: ArgumentStorageImpl.instance) 101| 1| as (Arg1, Arg2, Arg3, Arg4) -> Ret 102| 1| } 103| | 104| | 105| | /** Register a closure with four args (for dependency injection) */ 106| | static func closure(argFactory: F, argStorage: ArgumentStorage) 107| 2| -> (Arg1, Arg2, Arg3, Arg4) -> Ret where F: ArgumentFactory, F.Value == T { 108| 2| 109| 2| // create and store instance 110| 2| let typeDescription = "\(T.self)" 111| 2| let arg = argFactory.argumentClosure(typeDescription) 112| 2| argStorage.store(arg) 113| 2| 114| 2| // return default value 115| 2| return DefaultClosureHandler.it() as (Arg1, Arg2, Arg3, Arg4) -> Ret 116| 2| } 117| | 118| | 119| | /** Register a closure with five args */ 120| | public static func closure() -> T 121| 1| where T == (Arg1, Arg2, Arg3, Arg4, Arg5) -> Ret { 122| 1| 123| 1| let factory = ArgumentFactoryImpl() 124| 1| return Arg.closure(argFactory: factory, argStorage: ArgumentStorageImpl.instance) 125| 1| as (Arg1, Arg2, Arg3, Arg4, Arg5) -> Ret 126| 1| } 127| | 128| | 129| | /** Register a closure with two args (for dependency injection) */ 130| | static func closure(argFactory: F, argStorage: ArgumentStorage) 131| 2| -> (Arg1, Arg2, Arg3, Arg4, Arg5) -> Ret where F: ArgumentFactory, F.Value == T { 132| 2| 133| 2| // create and store instance 134| 2| let typeDescription = "\(T.self)" 135| 2| let arg = argFactory.argumentClosure(typeDescription) 136| 2| argStorage.store(arg) 137| 2| 138| 2| // return default value 139| 2| return DefaultClosureHandler.it() as (Arg1, Arg2, Arg3, Arg4, Arg5) -> Ret 140| 2| } 141| | 142| |} /Users/travis/build/pirishd/InstantMock/Sources/InstantMock/Arguments/Arg.swift: 1| |// 2| |// Arg.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 12/05/2017. 6| |// Copyright © 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |/** This class represents a generic Argument being registered */ 11| |public final class Arg { 12| | 13| | 14| | // MARK: Arguments matching exact values 15| | 16| | /** Register a mandatory value */ 17| 18| public static func eq(_ val: T) -> T { 18| 18| let factory = ArgumentFactoryImpl() 19| 18| return Arg.eq(val, argFactory: factory, argStorage: ArgumentStorageImpl.instance) 20| 18| } 21| | 22| | 23| | /** Register an optional value */ 24| 2| public static func eq(_ val: T?) -> T? { 25| 2| let factory = ArgumentFactoryImpl() 26| 2| return Arg.eq(val, argFactory: factory, argStorage: ArgumentStorageImpl.instance) 27| 2| } 28| | 29| | 30| | /** Register a mandatory value with factory (for dependency injection) */ 31| | static func eq(_ val: T, argFactory: F, argStorage: ArgumentStorage) -> T 32| 22| where F: ArgumentFactory, F.Value == T { 33| 22| 34| 22| let arg = argFactory.argument(value: val) 35| 22| argStorage.store(arg) 36| 22| return val 37| 22| } 38| | 39| | 40| | /** Register an optional value with factory (for dependency injection) */ 41| | static func eq(_ val: T?, argFactory: F, argStorage: ArgumentStorage) -> T? 42| 4| where F: ArgumentFactory, F.Value == T { 43| 4| 44| 4| let arg = argFactory.argument(value: val) 45| 4| argStorage.store(arg) 46| 4| return val 47| 4| } 48| | 49| | 50| | // MARK: Arguments matching a condition 51| | 52| | /** Register a closure to be verified, with mandatory type */ 53| 3| public static func verify(_ condition: @escaping (T) -> Bool) -> T { 54| 3| let factory = ArgumentFactoryImpl() 55| 3| return Arg.verify(condition, argFactory: factory, argStorage: ArgumentStorageImpl.instance) 56| 3| } 57| | 58| | 59| | /** Register a closure to be verified, with optional type */ 60| 2| public static func verify(_ condition: @escaping (T?) -> Bool) -> T? { 61| 2| let factory = ArgumentFactoryImpl() 62| 2| return Arg.verify(condition, argFactory: factory, argStorage: ArgumentStorageImpl.instance) 63| 2| } 64| | 65| | 66| | /** Register a closure to be verified, with mandatory type */ 67| | static func verify(_ condition: @escaping (T) -> Bool, argFactory: F, argStorage: ArgumentStorage) -> T 68| 5| where F: ArgumentFactory, F.Value == T { 69| 5| 70| 5| let arg = argFactory.argument(condition: condition) 71| 5| argStorage.store(arg) 72| 5| 73| 5| // return default value 74| 5| guard let ret = DefaultValueHandler().it else { 75| 0| fatalError("Unexpected type, only `MockUsable` types can be used with `verify`") 76| 5| } 77| 5| return ret 78| 5| } 79| | 80| | 81| | /** Register a closure to be verified, with optional type */ 82| | static func verify(_ condition: @escaping (T?) -> Bool, argFactory: F, argStorage: ArgumentStorage) -> T? 83| 2| where F: ArgumentFactory, F.Value == T { 84| 2| 85| 2| // create and store instance 86| 2| let arg = argFactory.argument(condition: condition) 87| 2| argStorage.store(arg) 88| 2| 89| 2| // return default nil value 90| 2| return nil 91| 2| } 92| | 93| |} 94| | 95| | 96| |/** Extension for `MockUsable` args */ 97| |extension Arg where T: MockUsable { 98| | 99| | 100| | // MARK: Arguments matching any values 101| | 102| | /** Register any value returning mandatory value */ 103| 30| public static func any() -> T { 104| 30| let factory = ArgumentFactoryImpl() 105| 30| return Arg.any(argFactory: factory, argStorage: ArgumentStorageImpl.instance) 106| 30| } 107| | 108| | 109| | /** Register any value returning optional value */ 110| 4| public static func any() -> T? { 111| 4| let factory = ArgumentFactoryImpl() 112| 4| return Arg.any(argFactory: factory, argStorage: ArgumentStorageImpl.instance) 113| 4| } 114| | 115| | 116| | /** Register any value (for dependency injection) */ 117| 37| static func any(argFactory: F, argStorage: ArgumentStorage) -> T where F: ArgumentFactory, F.Value == T { 118| 37| 119| 37| // create and store instance 120| 37| let typeDescription = "\(T.self)" 121| 37| let arg = argFactory.argumentAny(typeDescription) 122| 37| argStorage.store(arg) 123| 37| 124| 37| // return default value 125| 37| guard let ret = DefaultValueHandler().it else { 126| 0| fatalError("Unexpected type, only `MockUsable` types can be used with `any`") 127| 37| } 128| 37| return ret 129| 37| } 130| | 131| |} /Users/travis/build/pirishd/InstantMock/Sources/InstantMock/Arguments/Argument/ArgumentAny.swift: 1| |// 2| |// ArgumentAny.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 12/05/2017. 6| |// Copyright © 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |/** Main protocol for an argument that verifies any values */ 11| |public protocol ArgumentAny: Argument {} 12| | 13| | 14| |/** Main implementation of the configuration of an argument that verifies any values */ 15| |final class ArgumentAnyImpl: ArgumentAny { 16| | 17| | /// Type description for the arg 18| | private let typeDescription: String 19| | 20| | 21| | /** Initialize with given type description */ 22| 36| init(_ typeDescription: String) { 23| 36| self.typeDescription = typeDescription 24| 36| } 25| | 26| |} 27| | 28| | 29| |/** Extension that performs matching */ 30| |extension ArgumentAnyImpl: ArgumentMatching { 31| | 32| 67| func match(_ value: Any?) -> Bool { 33| 67| return true 34| 67| } 35| | 36| |} 37| | 38| | 39| |/** Extension to return a description */ 40| |extension ArgumentAnyImpl: CustomStringConvertible { 41| | 42| 17| var description: String { 43| 17| return "any<\(self.typeDescription)>" 44| 17| } 45| | 46| |} /Users/travis/build/pirishd/InstantMock/Sources/InstantMock/Arguments/Argument/ArgumentCapture.swift: 1| |// 2| |// ArgumentCapture.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 12/05/2017. 6| |// Copyright © 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |/** Protocol for capturing arguments */ 11| |public protocol ArgumentCapture: Argument { 12| | func setValue(_ value: Any?) 13| | var allValues: [Any?] { get } 14| | var value: Any? { get } 15| |} 16| | 17| | 18| |/** This class represents the configuration of an argument that captures passed values */ 19| |final class ArgumentCaptureImpl: ArgumentCapture { 20| | 21| | 22| | /// Type description for the arg 23| | private let typeDescription: String 24| | 25| | /// Inner captured values 26| | private var values = [T?]() 27| | 28| | /// Last captured value 29| 23| var value: Any? { 30| 23| return self.valueTyped 31| 23| } 32| | 33| | /// Last captured value (typed) 34| 25| var valueTyped: T? { 35| 25| var ret: T? = nil 36| 25| if let val = self.values.last { 37| 23| ret = val 38| 25| } 39| 25| return ret 40| 25| } 41| | 42| | /// All captured values 43| 17| var allValues: [Any?] { 44| 17| return self.allValuesTyped 45| 17| } 46| | 47| | /// All captured values (typed) 48| 18| var allValuesTyped: [T?] { 49| 18| return values 50| 18| } 51| | 52| | 53| | // MARK: Initializer 54| | 55| | /** Initialize with given type description */ 56| 19| init(_ typeDescription: String) { 57| 19| self.typeDescription = typeDescription 58| 19| } 59| | 60| |} 61| | 62| | 63| |/** Extension that performs matching */ 64| |extension ArgumentCaptureImpl: ArgumentMatching { 65| | 66| 23| func match(_ value: Any?) -> Bool { 67| 23| // all capture objects match anything 68| 23| return true 69| 23| } 70| | 71| |} 72| | 73| | 74| |/** Extension that sets a captured value */ 75| |extension ArgumentCaptureImpl { 76| | 77| 18| func setValue(_ value: Any?) { 78| 18| if value == nil { 79| 1| self.values.append(nil as T?) 80| 18| } else if let tValue = value as? T { 81| 16| self.values.append(tValue) 82| 18| } 83| 18| } 84| | 85| |} 86| | 87| | 88| |/** Extension to return a description */ 89| |extension ArgumentCaptureImpl: CustomStringConvertible { 90| | 91| 1| var description: String { 92| 1| return "captured<\(typeDescription)>" 93| 1| } 94| | 95| |} /Users/travis/build/pirishd/InstantMock/Sources/InstantMock/Arguments/Argument/ArgumentClosure.swift: 1| |// 2| |// ArgumentClosure.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 21/05/2017. 6| |// Copyright © 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |/** Main protocol for an argument that verifies a closure */ 11| |public protocol ArgumentClosure: Argument {} 12| | 13| | 14| |/** Main implementation of the configuration of an argument that verifies a closure */ 15| |final class ArgumentClosureImpl: ArgumentClosure { 16| | 17| | /// Type description for the arg 18| | private let typeDescription: String 19| | 20| | 21| | /** Initialize with given type description */ 22| 11| init(_ typeDescription: String) { 23| 11| self.typeDescription = typeDescription 24| 11| } 25| | 26| |} 27| | 28| | 29| |/** Extension that performs matching */ 30| |extension ArgumentClosureImpl: ArgumentMatching { 31| | 32| 19| func match(_ value: Any?) -> Bool { 33| 19| return true 34| 19| } 35| | 36| |} 37| | 38| | 39| |/** Extension to return a description */ 40| |extension ArgumentClosureImpl: CustomStringConvertible { 41| | 42| 1| var description: String { 43| 1| return "closure<\(self.typeDescription)>" 44| 1| } 45| | 46| |} /Users/travis/build/pirishd/InstantMock/Sources/InstantMock/Arguments/Argument/ArgumentValue.swift: 1| |// 2| |// ArgumentValue.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 12/05/2017. 6| |// Copyright © 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |/** Main protocol for an argument that must verify a precise value */ 11| |public protocol ArgumentValue: Argument {} 12| | 13| | 14| |/** Protocol for an argument that must verify a precise value of given type */ 15| |public protocol ArgumentValueTyped: ArgumentValue { 16| | associatedtype Value 17| | var value: Value? { get } 18| | init(_ value: Value?) 19| |} 20| | 21| | 22| |/** Main implementation of the configuration of an argument that must verify a precise value */ 23| |final class ArgumentValueImpl: ArgumentValueTyped { 24| | 25| | /// Value that must match 26| | let value: T? 27| | 28| | /// Verifier to be used for the match 29| | private let verifier: Verifier 30| | 31| | 32| | // MARK: Initializers 33| | 34| | /** Initialize new instance with provided value */ 35| 20| convenience required init(_ value: T?) { 36| 20| self.init(value, verifier: VerifierImpl.instance) 37| 20| } 38| | 39| | /** For dependency injection */ 40| 23| init(_ value: T?, verifier: Verifier) { 41| 23| self.value = value 42| 23| self.verifier = verifier 43| 23| } 44| | 45| |} 46| | 47| | 48| |/** Extension that performs matching */ 49| |extension ArgumentValueImpl: ArgumentMatching { 50| | 51| 41| func match(_ value: Any?) -> Bool { 52| 41| return self.verifier.equal(value, to: self.value) 53| 41| } 54| | 55| |} 56| | 57| | 58| |/** Extension to return a description */ 59| |extension ArgumentValueImpl: CustomStringConvertible { 60| | 61| 9| var description: String { 62| 9| var ret = "" 63| 9| 64| 9| if let value = self.value { 65| 8| ret = "\(value)" 66| 9| } else { 67| 1| ret = "nil" 68| 9| } 69| 9| 70| 9| return ret 71| 9| } 72| | 73| |} 74| | /Users/travis/build/pirishd/InstantMock/Sources/InstantMock/Arguments/Argument/ArgumentVerify/ArgumentVerifyMandatory.swift: 1| |// 2| |// ArgumentVerifyMandatory.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 20/05/2017. 6| |// Copyright © 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |/** Protocol for an argument that must verify a precise condition of given mandatory type */ 11| |public protocol ArgumentVerifyMandatoryTyped: ArgumentVerify { 12| | associatedtype Value 13| | var condition: ((Value) -> Bool) { get } 14| | init(_ condition: @escaping ((Value) -> Bool)) 15| |} 16| | 17| | 18| |/** Main implementation of the configuration of an argument that must verify a precise condition */ 19| |final class ArgumentVerifyMandatoryImpl: ArgumentVerifyMandatoryTyped { 20| | 21| | /// Condition that must be verified 22| | let condition: ((T) -> Bool) 23| | 24| | 25| | /** Initialize new instance with provided condition */ 26| 6| required init(_ condition: @escaping ((T) -> Bool)) { 27| 6| self.condition = condition 28| 6| } 29| | 30| |} 31| | 32| | 33| |/** Extension that performs matching */ 34| |extension ArgumentVerifyMandatoryImpl: ArgumentMatching { 35| | 36| 14| func match(_ value: Any?) -> Bool { 37| 14| var ret = false 38| 14| 39| 14| // make sure to have a condition and that value matches the required type 40| 14| if let tValue = value as? T { 41| 13| ret = self.condition(tValue) // evaluate the condition 42| 14| } 43| 14| 44| 14| return ret 45| 14| } 46| | 47| |} 48| | 49| | 50| |/** Extension to return a description */ 51| |extension ArgumentVerifyMandatoryImpl: CustomStringConvertible { 52| | 53| 3| var description: String { 54| 3| return "conditioned<\(type(of: self.condition))>" 55| 3| } 56| | 57| |} /Users/travis/build/pirishd/InstantMock/Sources/InstantMock/Arguments/Argument/ArgumentVerify/ArgumentVerifyOptional.swift: 1| |// 2| |// ArgumentVerifyOptional.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 20/05/2017. 6| |// Copyright © 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |/** Protocol for an argument that must verify a precise condition of given optional type */ 11| |public protocol ArgumentVerifyOptionalTyped: ArgumentVerify { 12| | associatedtype Value 13| | var condition: ((Value?) -> Bool) { get } 14| | init(_ condition: @escaping ((Value?) -> Bool)) 15| |} 16| | 17| | 18| |/** Main implementation of the configuration of an argument that must verify a precise condition */ 19| |final class ArgumentVerifyOptionalImpl: ArgumentVerifyOptionalTyped { 20| | 21| | /// Condition that must be verified 22| | let condition: ((T?) -> Bool) 23| | 24| | 25| | /** Initialize new instance with provided condition */ 26| 6| required init(_ condition: @escaping ((T?) -> Bool)) { 27| 6| self.condition = condition 28| 6| } 29| | 30| |} 31| | 32| | 33| |/** Extension that performs matching */ 34| |extension ArgumentVerifyOptionalImpl: ArgumentMatching { 35| | 36| 11| func match(_ value: Any?) -> Bool { 37| 11| var ret = false 38| 11| 39| 11| // make sure to have a condition and that value matches the required type 40| 11| if let tValue = value as? T? { 41| 10| ret = self.condition(tValue) // evaluate the condition 42| 11| } 43| 11| 44| 11| return ret 45| 11| } 46| | 47| |} 48| | 49| | 50| |/** Extension to return a description */ 51| |extension ArgumentVerifyOptionalImpl: CustomStringConvertible { 52| | 53| 3| var description: String { 54| 3| return "conditioned<\(type(of: self.condition))>" 55| 3| } 56| | 57| |} /Users/travis/build/pirishd/InstantMock/Sources/InstantMock/Arguments/Captor/ArgumentCaptor.swift: 1| |// 2| |// ArgumentCaptor.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 12/05/2017. 6| |// Copyright © 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |/** This class enables to capture arguments of any types (except closures) */ 11| |public final class ArgumentCaptor { 12| | 13| | /// Argument that captures values 14| | private var arg: ArgumentCapture? 15| | 16| | /// Delegate for returning captured values 17| 4| private lazy var delegate: ArgumentCaptorValuesImpl = { 18| 4| return ArgumentCaptorValuesImpl(self.arg) 19| 4| }() 20| | 21| | /** Main initializer */ 22| 5| public init() {} 23| | 24| |} 25| | 26| | 27| |/** Extension that performs the actual capture */ 28| |extension ArgumentCaptor { 29| | 30| | /** Capture an argument of expected type */ 31| 2| public func capture() -> T { 32| 2| let factory = ArgumentFactoryImpl() 33| 2| return self.capture(argFactory: factory, argStorage: ArgumentStorageImpl.instance) 34| 2| } 35| | 36| | 37| | /** Capture an argument of expected type (for dependency injection) */ 38| 6| func capture(argFactory: F, argStorage: ArgumentStorage) -> T where F: ArgumentFactory, F.Value == T { 39| 6| 40| 6| // store instance 41| 6| let typeDescription = "\(T.self)" 42| 6| let arg = argFactory.argumentCapture(typeDescription) 43| 6| self.arg = arg 44| 6| argStorage.store(arg) 45| 6| 46| 6| // otherwise, try to guess the default value from required type 47| 6| guard let ret = DefaultValueHandler().it else { 48| 0| fatalError("Unexpected type, only `MockUsable` types can be used with `captors`") 49| 6| } 50| 6| return ret 51| 6| } 52| | 53| |} 54| | 55| | 56| |/** Extension for returning captured valued */ 57| |extension ArgumentCaptor: ArgumentCaptorValues { 58| | 59| 5| public var value: T? { 60| 5| return self.delegate.value 61| 5| } 62| | 63| 5| public var allValues: [T?] { 64| 5| return self.delegate.allValues 65| 5| } 66| | 67| |} /Users/travis/build/pirishd/InstantMock/Sources/InstantMock/Arguments/Captor/ArgumentCaptorValues.swift: 1| |// 2| |// ArgumentCaptorValues.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 21/05/2017. 6| |// Copyright © 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |/** Protocol for returning captured values */ 11| |protocol ArgumentCaptorValues { 12| | associatedtype Value 13| | var value: Value? { get } 14| | var allValues: [Value?] { get } 15| |} 16| | 17| | 18| |/** Main implementation for returning captured values */ 19| |public final class ArgumentCaptorValuesImpl: ArgumentCaptorValues { 20| | 21| | /// Argument capture instance 22| | private var arg: ArgumentCapture? 23| | 24| | /// Captured value 25| 28| public var value: T? { 26| 28| var ret: T? 27| 28| if let captureArg = self.arg { 28| 27| ret = captureArg.value as? T 29| 28| } 30| 28| return ret 31| 28| } 32| | 33| | /// All captured values 34| 21| public var allValues: [T?] { 35| 21| var ret = [T?]() 36| 21| if let captureArg = self.arg { 37| 22| ret = captureArg.allValues.map { $0 as? T }.compactMap { $0 } 38| 21| } 39| 21| return ret 40| 21| } 41| | 42| | 43| | /** Initializer */ 44| 27| init(_ arg: ArgumentCapture?) { 45| 27| self.arg = arg 46| 27| } 47| | 48| |} 49| | /Users/travis/build/pirishd/InstantMock/Sources/InstantMock/Arguments/Captor/ArgumentClosureCaptor.swift: 1| |// 2| |// ArgumentClosureCaptor.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 21/05/2017. 6| |// Copyright © 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |/** This class enables to capture arguments of type closure */ 11| |public final class ArgumentClosureCaptor { 12| | 13| | /// Argument that captures values 14| | var arg: ArgumentCapture? 15| | 16| | /// Delegate for returning captured values 17| 19| private lazy var delegate: ArgumentCaptorValuesImpl = { 18| 19| return ArgumentCaptorValuesImpl(self.arg) 19| 19| }() 20| | 21| | /** Main initializer */ 22| 25| public init() {} 23| | 24| |} 25| | 26| | 27| |/** Extension for returning captured valued */ 28| |extension ArgumentClosureCaptor: ArgumentCaptorValues { 29| | 30| 20| public var value: T? { 31| 20| return self.delegate.value 32| 20| } 33| | 34| 13| public var allValues: [T?] { 35| 13| return self.delegate.allValues 36| 13| } 37| | 38| |} /Users/travis/build/pirishd/InstantMock/Sources/InstantMock/Arguments/Captor/ArgumentClosureCaptor/ArgumentClosureCaptor+FiveArgs.swift: 1| |// 2| |// ArgumentClosureCaptor+FiveArgs.swift 3| |// InstantMock iOS 4| |// 5| |// Created by Patrick on 22/07/2017. 6| |// Copyright © 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |/** Extension that performs the actual capture for five args */ 11| |extension ArgumentClosureCaptor { 12| | 13| | 14| | /** Capture an argument of expected type */ 15| 1| public func capture() -> T where T == (Arg1, Arg2, Arg3, Arg4, Arg5) -> Ret { 16| 1| let factory = ArgumentFactoryImpl() 17| 1| return self.capture(argFactory: factory, argStorage: ArgumentStorageImpl.instance) 18| 1| as (Arg1, Arg2, Arg3, Arg4, Arg5) -> Ret 19| 1| } 20| | 21| | 22| | /** Capture an argument of expected type (for dependency injection) */ 23| | func capture(argFactory: F, argStorage: ArgumentStorage) 24| 5| -> (Arg1, Arg2, Arg3, Arg4, Arg5) -> Ret where F: ArgumentFactory, F.Value == T { 25| 5| 26| 5| // store instance 27| 5| let typeDescription = "\(T.self)" 28| 5| let arg = argFactory.argumentCapture(typeDescription) 29| 5| self.arg = arg 30| 5| argStorage.store(arg) 31| 5| 32| 5| return DefaultClosureHandler.it() as (Arg1, Arg2, Arg3, Arg4, Arg5) -> Ret 33| 5| } 34| | 35| |} 36| | /Users/travis/build/pirishd/InstantMock/Sources/InstantMock/Arguments/Captor/ArgumentClosureCaptor/ArgumentClosureCaptor+FourArgs.swift: 1| |// 2| |// ArgumentClosureCaptor+FourArgs.swift 3| |// InstantMock iOS 4| |// 5| |// Created by Patrick on 22/07/2017. 6| |// Copyright © 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |/** Extension that performs the actual capture for four args */ 11| |extension ArgumentClosureCaptor { 12| | 13| | 14| | /** Capture an argument of expected type */ 15| 1| public func capture() -> T where T == (Arg1, Arg2, Arg3, Arg4) -> Ret { 16| 1| let factory = ArgumentFactoryImpl() 17| 1| return self.capture(argFactory: factory, argStorage: ArgumentStorageImpl.instance) 18| 1| as (Arg1, Arg2, Arg3, Arg4) -> Ret 19| 1| } 20| | 21| | 22| | /** Capture an argument of expected type (for dependency injection) */ 23| | func capture(argFactory: F, argStorage: ArgumentStorage) 24| 5| -> (Arg1, Arg2, Arg3, Arg4) -> Ret where F: ArgumentFactory, F.Value == T { 25| 5| 26| 5| // store instance 27| 5| let typeDescription = "\(T.self)" 28| 5| let arg = argFactory.argumentCapture(typeDescription) 29| 5| self.arg = arg 30| 5| argStorage.store(arg) 31| 5| 32| 5| return DefaultClosureHandler.it() as (Arg1, Arg2, Arg3, Arg4) -> Ret 33| 5| } 34| | 35| |} 36| | /Users/travis/build/pirishd/InstantMock/Sources/InstantMock/Arguments/Captor/ArgumentClosureCaptor/ArgumentClosureCaptor+NoArg.swift: 1| |// 2| |// ArgumentClosureCaptor+NoArg.swift 3| |// InstantMock iOS 4| |// 5| |// Created by Patrick on 22/07/2017. 6| |// Copyright © 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |/** Extension that performs the actual capture for no arg */ 11| |extension ArgumentClosureCaptor { 12| | 13| | 14| | /** Capture an argument of expected type */ 15| 1| public func capture() -> T where T == () -> Ret { 16| 1| let factory = ArgumentFactoryImpl() 17| 1| return self.capture(argFactory: factory, argStorage: ArgumentStorageImpl.instance) as () -> Ret 18| 1| } 19| | 20| | 21| | /** Capture an argument of expected type (for dependency injection) */ 22| | func capture(argFactory: F, argStorage: ArgumentStorage) -> () -> Ret 23| 5| where F: ArgumentFactory, F.Value == T { 24| 5| 25| 5| // store instance 26| 5| let typeDescription = "\(T.self)" 27| 5| let arg = argFactory.argumentCapture(typeDescription) 28| 5| self.arg = arg 29| 5| argStorage.store(arg) 30| 5| 31| 5| return DefaultClosureHandler.it() as () -> Ret 32| 5| } 33| | 34| |} 35| | /Users/travis/build/pirishd/InstantMock/Sources/InstantMock/Arguments/Captor/ArgumentClosureCaptor/ArgumentClosureCaptor+OneArg.swift: 1| |// 2| |// ArgumentClosureCaptor+OneArg.swift 3| |// InstantMock iOS 4| |// 5| |// Created by Patrick on 22/07/2017. 6| |// Copyright © 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |/** Extension that performs the actual capture for one arg */ 11| |extension ArgumentClosureCaptor { 12| | 13| | 14| | /** Capture an argument of expected type */ 15| 1| public func capture() -> T where T == (Arg1) -> Ret { 16| 1| let factory = ArgumentFactoryImpl() 17| 1| return self.capture(argFactory: factory, argStorage: ArgumentStorageImpl.instance) as (Arg1) -> Ret 18| 1| } 19| | 20| | 21| | /** Capture an argument of expected type (for dependency injection) */ 22| | func capture(argFactory: F, argStorage: ArgumentStorage) -> (Arg1) -> Ret 23| 5| where F: ArgumentFactory, F.Value == T { 24| 5| 25| 5| // store instance 26| 5| let typeDescription = "\(T.self)" 27| 5| let arg = argFactory.argumentCapture(typeDescription) 28| 5| self.arg = arg 29| 5| argStorage.store(arg) 30| 5| 31| 5| return DefaultClosureHandler.it() as (Arg1) -> Ret 32| 5| } 33| | 34| |} /Users/travis/build/pirishd/InstantMock/Sources/InstantMock/Arguments/Captor/ArgumentClosureCaptor/ArgumentClosureCaptor+ThreeArgs.swift: 1| |// 2| |// ArgumentClosureCaptor+ThreeArgs.swift 3| |// InstantMock iOS 4| |// 5| |// Created by Patrick on 22/07/2017. 6| |// Copyright © 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |/** Extension that performs the actual capture for three args */ 11| |extension ArgumentClosureCaptor { 12| | 13| | 14| | /** Capture an argument of expected type */ 15| 1| public func capture() -> T where T == (Arg1, Arg2, Arg3) -> Ret { 16| 1| let factory = ArgumentFactoryImpl() 17| 1| return self.capture(argFactory: factory, argStorage: ArgumentStorageImpl.instance) as (Arg1, Arg2, Arg3) -> Ret 18| 1| } 19| | 20| | 21| | /** Capture an argument of expected type (for dependency injection) */ 22| | func capture(argFactory: F, argStorage: ArgumentStorage) -> (Arg1, Arg2, Arg3) -> Ret 23| 5| where F: ArgumentFactory, F.Value == T { 24| 5| 25| 5| // store instance 26| 5| let typeDescription = "\(T.self)" 27| 5| let arg = argFactory.argumentCapture(typeDescription) 28| 5| self.arg = arg 29| 5| argStorage.store(arg) 30| 5| 31| 5| return DefaultClosureHandler.it() as (Arg1, Arg2, Arg3) -> Ret 32| 5| } 33| | 34| |} 35| | 36| | /Users/travis/build/pirishd/InstantMock/Sources/InstantMock/Arguments/Captor/ArgumentClosureCaptor/ArgumentClosureCaptor+TwoArgs.swift: 1| |// 2| |// ArgumentClosureCaptor+TwoArgs.swift 3| |// InstantMock iOS 4| |// 5| |// Created by Patrick on 22/07/2017. 6| |// Copyright © 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |/** Extension that performs the actual capture for two args */ 11| |extension ArgumentClosureCaptor { 12| | 13| | 14| | /** Capture an argument of expected type */ 15| 2| public func capture() -> T where T == (Arg1, Arg2) -> Ret { 16| 2| let factory = ArgumentFactoryImpl() 17| 2| return self.capture(argFactory: factory, argStorage: ArgumentStorageImpl.instance) as (Arg1, Arg2) -> Ret 18| 2| } 19| | 20| | 21| | /** Capture an argument of expected type (for dependency injection) */ 22| | func capture(argFactory: F, argStorage: ArgumentStorage) -> (Arg1, Arg2) -> Ret 23| 6| where F: ArgumentFactory, F.Value == T { 24| 6| 25| 6| // store instance 26| 6| let typeDescription = "\(T.self)" 27| 6| let arg = argFactory.argumentCapture(typeDescription) 28| 6| self.arg = arg 29| 6| argStorage.store(arg) 30| 6| 31| 6| return DefaultClosureHandler.it() as (Arg1, Arg2) -> Ret 32| 6| } 33| | 34| |} 35| | /Users/travis/build/pirishd/InstantMock/Sources/InstantMock/Arguments/Helpers/ArgumentFactory.swift: 1| |// 2| |// ArgumentFactory.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 13/05/2017. 6| |// Copyright © 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |/** Protocol for argument factory that aims at creating arguments */ 11| |public protocol ArgumentFactory { 12| | associatedtype Value 13| | 14| | /// Create a new argument value 15| | func argument(value: Value?) -> ArgumentValue 16| | 17| | /// Create a new any argument 18| | func argumentAny(_ typeDescription: String) -> ArgumentAny 19| | 20| | // Create a new argument closure 21| | func argumentClosure(_ typeDescription: String) -> ArgumentClosure 22| | 23| | // Create a new argument that must verify provided condition 24| | func argument(condition: @escaping (Value) -> Bool) -> ArgumentVerify 25| | func argument(condition: @escaping (Value?) -> Bool) -> ArgumentVerify 26| | 27| | // Create a new argument capture 28| | func argumentCapture(_ typeDescription: String) -> ArgumentCapture 29| |} 30| | 31| | 32| |/** Implementation of argument factory */ 33| |final class ArgumentFactoryImpl: ArgumentFactory { 34| | 35| 20| func argument(value: T?) -> ArgumentValue { 36| 20| return ArgumentValueImpl(value) 37| 20| } 38| | 39| 34| func argumentAny(_ typeDescription: String) -> ArgumentAny { 40| 34| return ArgumentAnyImpl(typeDescription) 41| 34| } 42| | 43| 9| func argumentClosure(_ typeDescription: String) -> ArgumentClosure { 44| 9| return ArgumentClosureImpl(typeDescription) 45| 9| } 46| | 47| 3| func argument(condition: @escaping (T) -> Bool) -> ArgumentVerify { 48| 3| return ArgumentVerifyMandatoryImpl(condition) 49| 3| } 50| | 51| 2| func argument(condition: @escaping (T?) -> Bool) -> ArgumentVerify { 52| 2| return ArgumentVerifyOptionalImpl(condition) 53| 2| } 54| | 55| 9| func argumentCapture(_ typeDescription: String) -> ArgumentCapture { 56| 9| return ArgumentCaptureImpl(typeDescription) 57| 9| } 58| | 59| |} /Users/travis/build/pirishd/InstantMock/Sources/InstantMock/Arguments/Helpers/ArgumentStorage.swift: 1| |// 2| |// ArgumentStorage.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 12/05/2017. 6| |// Copyright © 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |/** Protocol for storing some arguments */ 11| |public protocol ArgumentStorage { 12| | 13| | /// Store an argument 14| | func store(_ arg: Argument) 15| | 16| | /// Get the list of all the arguments 17| | func all() -> [Argument] 18| | 19| | /// Flush any stored argument 20| | func flush() 21| | 22| |} 23| | 24| | 25| |/** Implementation for storing arguments */ 26| |final class ArgumentStorageImpl { 27| | 28| | /// Singleton 29| | static let instance = ArgumentStorageImpl() 30| | 31| | /// Actual storage array 32| | private var storage = [Argument]() 33| |} 34| | 35| | 36| |/** Extension for actually store arguments */ 37| |extension ArgumentStorageImpl: ArgumentStorage{ 38| | 39| 80| func store(_ arg: Argument) { 40| 80| self.storage.append(arg) 41| 80| } 42| | 43| 64| func all() -> [Argument] { 44| 64| return self.storage 45| 64| } 46| | 47| 58| func flush() { 48| 58| self.storage.removeAll() 49| 58| } 50| | 51| |} /Users/travis/build/pirishd/InstantMock/Sources/InstantMock/Arguments/Helpers/ArgumentsConfiguration.swift: 1| |// 2| |// ArgumentsConfiguration.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 12/05/2017. 6| |// Copyright © 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |/** Class representing a configuration of arguments */ 11| |final class ArgumentsConfiguration { 12| | 13| | /// Actual expected arguments 14| | let args: [Argument] 15| | 16| | 17| | /** Initialize with expected arguments */ 18| 122| init(_ args: [Argument]) { 19| 122| self.args = args 20| 122| } 21| | 22| |} 23| | 24| | 25| |/** Extension that returns a description of a configuration */ 26| |extension ArgumentsConfiguration: CustomStringConvertible { 27| | 28| 29| var description: String { 29| 29| var value = "" 30| 29| 31| 29| if self.args.count > 0 { 32| 30| let valuesDescriptions = self.args.map { $0.description } 33| 21| value = valuesDescriptions.joined(separator: ", ") 34| 29| } else { 35| 8| value = "none" 36| 29| } 37| 29| 38| 29| return value 39| 29| } 40| | 41| |} 42| | 43| |/** Extension that returns the number of args of any kinds */ 44| |extension ArgumentsConfiguration { 45| | 46| | /** Returns the number of arguments that must verify a certain value */ 47| 46| func numberOfArgValues() -> Int { 48| 92| return self.args.filter { $0 is ArgumentValue }.count 49| 46| } 50| | 51| | /** Returns the number of arguments that must verify a certain condition */ 52| 12| func numberOfArgVerify() -> Int { 53| 20| return self.args.filter { $0 is ArgumentVerify }.count 54| 12| } 55| | 56| |} 57| | 58| | 59| |/** Configurations are comparable depending on how the args must be verified */ 60| |extension ArgumentsConfiguration: Comparable {} 61| | 62| 8|func ==(lhs: ArgumentsConfiguration, rhs: ArgumentsConfiguration) -> Bool { 63| 8| // configurations are equal if they have the same number of args that must verify a precise value or condition 64| 8| return lhs.numberOfArgValues() == rhs.numberOfArgValues() && lhs.numberOfArgVerify() == rhs.numberOfArgVerify() 65| 8|} 66| | 67| | 68| 8|func <(lhs: ArgumentsConfiguration, rhs: ArgumentsConfiguration) -> Bool { 69| 8| // when the number of args that must verify a certain value precisely is the same, look for conditions 70| 8| if lhs.numberOfArgValues() == rhs.numberOfArgValues() { 71| 1| return lhs.numberOfArgVerify() < rhs.numberOfArgVerify() 72| 7| } 73| 7| return lhs.numberOfArgValues() < rhs.numberOfArgValues() 74| 8|} /Users/travis/build/pirishd/InstantMock/Sources/InstantMock/Arguments/Helpers/ArgumentsMatcher.swift: 1| |// 2| |// ArgsMatcher.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 07/05/2017. 6| |// Copyright © 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |/** Class designed to match some arguments against an argument configuration */ 11| |final class ArgumentsMatcher { 12| | 13| | /// Provided arguments 14| | private let args: [Any?] 15| | 16| | /** Initialize new instance with arguments and configuration */ 17| 128| init(_ args: [Any?]) { 18| 128| self.args = args 19| 128| } 20| | 21| |} 22| | 23| | 24| |/** Extension for performing the actual matching */ 25| |extension ArgumentsMatcher { 26| | 27| | 28| | /** Perform actual match */ 29| 128| func match(_ argsConfig: ArgumentsConfiguration) -> Bool { 30| 128| 31| 128| // make sure the number of arguments matches the number of expected 32| 128| if self.args.count != argsConfig.args.count { 33| 5| return false 34| 123| } 35| 123| 36| 123| // match arguments one by one 37| 123| if argsConfig.args.count > 0 { 38| 173| for i in 0...argsConfig.args.count-1 { 39| 173| let arg = args[i] 40| 173| let argConfig = argsConfig.args[i] 41| 173| 42| 173| if !argConfig.match(arg) { 43| 11| return false 44| 162| } 45| 162| } 46| 112| } 47| 112| 48| 112| return true 49| 123| } 50| | 51| |} /Users/travis/build/pirishd/InstantMock/Sources/InstantMock/DefaultValue/DefaultClosureHandler.swift: 1| |// 2| |// DefaultClosureHandler.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 08/05/2017. 6| |// Copyright © 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |/** Helper that provides a simple way to create closure arguments with any argument and return value types */ 11| |final class DefaultClosureHandler { 12| | 13| | /** Provide a default closure with provided signature (no args) */ 14| 8| public static func it() -> () -> Ret { 15| 8| let closure: () -> Ret = { return () as! Ret } 16| 8| return closure 17| 8| } 18| | 19| | /** Provide a default closure with provided signature (one arg) */ 20| 8| public static func it() -> (Arg1) -> Ret { 21| 8| let closure: (Arg1) -> Ret = { _ in return () as! Ret } 22| 8| return closure 23| 8| } 24| | 25| | /** Provide a default closure with provided signature (2 args) */ 26| 12| public static func it() -> (Arg1, Arg2) -> Ret { 27| 12| let closure: (Arg1, Arg2) -> Ret = { _, _ in return () as! Ret } 28| 12| return closure 29| 12| } 30| | 31| | /** Provide a default closure with provided signature (3 args) */ 32| 8| public static func it() -> (Arg1, Arg2, Arg3) -> Ret { 33| 8| let closure: (Arg1, Arg2, Arg3) -> Ret = { _, _, _ in return () as! Ret } 34| 8| return closure 35| 8| } 36| | 37| | /** Provide a default closure with provided signature (4 args) */ 38| 8| public static func it() -> (Arg1, Arg2, Arg3, Arg4) -> Ret { 39| 8| let closure: (Arg1, Arg2, Arg3, Arg4) -> Ret = { _, _, _, _ in return () as! Ret } 40| 8| return closure 41| 8| } 42| | 43| | /** Provide a default closure with provided signature (5 args) */ 44| 8| public static func it() -> (Arg1, Arg2, Arg3, Arg4, Arg5) -> Ret { 45| 8| let closure: (Arg1, Arg2, Arg3, Arg4, Arg5) -> Ret = { _, _, _, _, _ in return () as! Ret } 46| 8| return closure 47| 8| } 48| | 49| |} /Users/travis/build/pirishd/InstantMock/Sources/InstantMock/DefaultValue/DefaultValueHandler.swift: 1| |// 2| |// DefaultValueHandler.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 12/05/2017. 6| |// Copyright © 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |/** This class is a helper that provides default values */ 11| |final class DefaultValueHandler { 12| | 13| | 14| | /** Return the default value, or nil if type is neither `MockUsable`, nor `Closure` */ 15| 159| var it: T? { 16| 159| var ret: T? 17| 159| 18| 159| // try to fetch the default value of `MockUsable` types 19| 159| if let mockUsableType = T.self as? MockUsable.Type { 20| 113| if let value = mockUsableType.anyValue as? T { 21| 113| ret = value 22| 113| } 23| 159| } 24| 159| 25| 159| return ret 26| 159| } 27| | 28| |} /Users/travis/build/pirishd/InstantMock/Sources/InstantMock/Factories/ExpectationFactory.swift: 1| |// 2| |// ExpectationFactory.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 08/05/2017. 6| |// Copyright © 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |/** Protocol for creating new `Expectation` instances */ 11| |public protocol ExpectationFactory { 12| | 13| | /// Create new `Expectation` instance that must be fulfilled, with provided `Stub` 14| | func expectation(withStub stub: Stub) -> Expectation 15| | 16| | /// Create new `Expectation` instantce that must be rejected, with provided `Stub` 17| | func rejection(withStub stub: Stub) -> Expectation 18| |} 19| | 20| | 21| |/** Main implementation */ 22| |public final class ExpectationFactoryImpl: ExpectationFactory { 23| | 24| | /// Singleton 25| | public static let instance = ExpectationFactoryImpl() 26| | 27| 1| public func expectation(withStub stub: Stub) -> Expectation { 28| 1| return Expectation(withStub: stub) 29| 1| } 30| | 31| 1| public func rejection(withStub stub: Stub) -> Expectation { 32| 1| return Expectation(withStub: stub, reject: true) 33| 1| } 34| | 35| |} /Users/travis/build/pirishd/InstantMock/Sources/InstantMock/Interceptors/CallInterceptor.swift: 1| |// 2| |// CallInterceptor.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 06/05/2017. 6| |// Copyright © 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |/** Base class for call interceptors */ 11| |public class CallInterceptor { 12| | 13| | /// Configuration for the call 14| | var configuration: CallConfiguration? 15| | 16| | 17| | /** Method is being called */ 18| | @discardableResult 19| 0| func handleCall(_ args: [Any?]) throws -> Any? { 20| 0| fatalError("[CallInterceptor] handleCall: virtual method, must be overloaded in subclasses") 21| 0| } 22| | 23| |} 24| | 25| | 26| |/** Extension for a list of interceptors */ 27| |extension Collection where Iterator.Element: CallInterceptor { 28| | 29| | /** Filter a list of interceptors if they match the provided arguments */ 30| 125| func matching(_ args: [Any?]) -> [Iterator.Element] { 31| 125| 32| 125| // create arg matcher 33| 125| let argsMatcher = ArgumentsMatcher(args) 34| 125| 35| 125| // return filtered list 36| 126| return self.filter { interceptor in 37| 126| var matching = false 38| 126| if let config = interceptor.configuration { 39| 125| matching = argsMatcher.match(config.args) 40| 126| } 41| 126| return matching 42| 126| } 43| 125| 44| 125| } 45| | 46| |} /Users/travis/build/pirishd/InstantMock/Sources/InstantMock/Interceptors/Expectation.swift: 1| |// 2| |// Expectation.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 06/05/2017. 6| |// Copyright © 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |/** This class represents an expectation to be verified */ 11| |public final class Expectation: CallInterceptor { 12| | 13| | 14| | /// Expected number of calls 15| | private var expectedNumberOfCalls: Int? 16| | 17| | /// Actual number of calls 18| | private var numberOfCalls: Int = 0 19| | 20| | /// Expectation must be rejected 21| | private var reject: Bool 22| | 23| | /// Stub instance 24| | private let stub: Stub 25| | 26| | /// Assertion 27| | private let assertion: Assertion 28| | 29| | 30| | // MARK: Initializers 31| | 32| | /** Initialize with provided stub */ 33| 17| convenience init(withStub stub: Stub, reject: Bool = false) { 34| 17| self.init(withStub: stub, reject: reject, assertion: AssertionImpl.instance) 35| 17| } 36| | 37| | 38| | /** Initialize with provided stub and assertion (for dependency injection) */ 39| 67| public init(withStub stub: Stub, reject: Bool = false, assertion: Assertion) { 40| 67| self.stub = stub 41| 67| self.reject = reject 42| 67| self.assertion = assertion 43| 67| } 44| | 45| | 46| | // MARK: Call 47| | 48| | /** Method is being called */ 49| | @discardableResult 50| 62| override func handleCall(_ args: [Any?]) throws -> Any? { 51| 62| self.numberOfCalls = self.numberOfCalls + 1 52| 62| return nil // don't care about return values 53| 62| } 54| | 55| |} 56| | 57| | 58| | 59| |// MARK: Registration 60| |extension Expectation { 61| | 62| | /** register call */ 63| | @discardableResult 64| 51| public func call(_ value: T, count: Int? = nil) -> Stub { 65| 51| self.expectedNumberOfCalls = count 66| 51| return self.stub 67| 51| } 68| | 69| |} 70| | 71| | 72| |// MARK: Verification 73| |extension Expectation { 74| | 75| | 76| | /// Flag indicating if the expectation was verified 77| 109| var verified: Bool { 78| 109| if let expected = self.expectedNumberOfCalls { 79| 16| return self.numberOfCalls == expected 80| 93| } 81| 93| return self.numberOfCalls > 0 82| 109| } 83| | 84| | 85| | /// Reason for a failure, nil if expectation verified 86| 34| var reason: String? { 87| 34| if self.reject { 88| 9| return self.rejectedReason 89| 25| } 90| 25| return self.acceptedReason 91| 34| } 92| | 93| | 94| | /// Reason for an unfulfilled expectation 95| 25| private var acceptedReason: String? { 96| 25| var value: String? 97| 25| 98| 25| if !self.verified, let configuration = self.configuration { 99| 21| var details = configuration.function + " " 100| 21| 101| 21| if let expected = self.expectedNumberOfCalls { 102| 3| details = details + "not called the expected number of times (\(self.numberOfCalls) out of \(expected))" 103| 21| } else { 104| 18| details = details + "never called" 105| 21| } 106| 21| 107| 21| details = details + " with expected args (\(configuration.args))" 108| 21| value = details 109| 25| } 110| 25| 111| 25| return value 112| 25| } 113| | 114| | 115| | /// Reason for an expectation that should have been rejected 116| 9| private var rejectedReason: String? { 117| 9| var value: String? 118| 9| 119| 9| if self.verified, let configuration = self.configuration { 120| 5| var details = configuration.function + " " 121| 5| 122| 5| if let expected = self.expectedNumberOfCalls { 123| 2| details = details + "called a wrong number of times (\(expected))" 124| 5| } else { 125| 3| details = details + "called" 126| 5| } 127| 5| 128| 5| details = details + " with unexpected args (\(configuration.args))" 129| 5| value = details 130| 9| } 131| 9| 132| 9| return value 133| 9| } 134| | 135| | 136| | /** Verify current expectation */ 137| 70| func verify(file: StaticString?, line: UInt?) { 138| 70| let success = (!self.reject && self.verified) || (self.reject && !self.verified) 139| 70| 140| 70| if success { 141| 48| self.assertion.success(file: file, line: line) 142| 70| } else { 143| 22| self.assertion.fail(self.reason, file: file, line: line) 144| 70| } 145| 70| } 146| | 147| |} /Users/travis/build/pirishd/InstantMock/Sources/InstantMock/Interceptors/Helpers/CallConfiguration.swift: 1| |// 2| |// CallConfiguration.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 08/05/2017. 6| |// Copyright © 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |/** Class representing the configuration of a call */ 11| |final class CallConfiguration { 12| | 13| | /// Function called 14| | let function: String 15| | 16| | /// Arguments configuration 17| | let args: ArgumentsConfiguration 18| | 19| | 20| | /** Initialize with function and args configuration */ 21| 127| init(for function: String, with args: ArgumentsConfiguration) { 22| 127| self.function = function 23| 127| self.args = args 24| 127| } 25| | 26| |} 27| | 28| |/** Two configurations are comparable, in terms of arguments */ 29| |extension CallConfiguration: Comparable {} 30| | 31| 2|func ==(lhs: CallConfiguration, rhs: CallConfiguration) -> Bool { 32| 2| return lhs.args == rhs.args 33| 2|} 34| | 35| 4|func <(lhs: CallConfiguration, rhs: CallConfiguration) -> Bool { 36| 4| return lhs.args < rhs.args 37| 4|} /Users/travis/build/pirishd/InstantMock/Sources/InstantMock/Interceptors/Helpers/CallInterceptorStorage.swift: 1| |// 2| |// CallInterceptorStorage.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 06/05/2017. 6| |// Copyright © 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |/** Class responsible for storing call interceptors */ 11| |final class CallInterceptorStorage { 12| | 13| | 14| | /** Storing dictionary */ 15| | private var repository = [String: [T]]() 16| | 17| | 18| | /** 19| | Register an interceptor 20| | - parameter interceptor: call interceptor to register 21| | - parameter function: function name triggering this interceptor 22| | - parameter args: list of arguments passed to the function for this interceptor 23| | */ 24| 112| func store(interceptor: T, for function: String) { 25| 112| var interceptors = self.repository[function] ?? [T]() 26| 112| interceptors.append(interceptor) 27| 112| self.repository[function] = interceptors 28| 112| } 29| | 30| | 31| | /** 32| | Return the list of interceptors for the given function 33| | - parameter function: function name for retrieving interceptors 34| | - returns: list of interceptors 35| | */ 36| 125| func interceptors(for function: String) -> [T] { 37| 125| return self.repository[function] ?? [T]() 38| 125| } 39| | 40| | 41| | /** Return the list of all the interceptors */ 42| 69| func all() -> [T] { 43| 69| return self.repository.values.flatMap { (values: [T]) -> [T] in 44| 68| return values 45| 68| } 46| 69| } 47| | 48| | /** Invalidates repository */ 49| 3| func removeAll() { 50| 3| self.repository.removeAll() 51| 3| } 52| |} /Users/travis/build/pirishd/InstantMock/Sources/InstantMock/Interceptors/Stub.swift: 1| |// 2| |// Stub.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 06/05/2017. 6| |// Copyright © 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |/** This class represents a stub */ 11| |public final class Stub: CallInterceptor { 12| | 13| | /// Flag indicating that a return value is expected 14| | private var hasReturnValue = false 15| | 16| | /// The actual return value 17| | private var returnValue: Any? 18| | 19| | /// Closure determining the return value 20| | private var returnValueClosure: ((_ args: [Any?]) -> Any?)? 21| | 22| | /// Closure to be called before returning 23| | private var closure: ((_ args: [Any?]) -> Void)? 24| | 25| | /// Error to be thrown 26| | private var error: Error? 27| | 28| | /// Flag indicating that the stub is configured to return something 29| 58| var returns: Bool { 30| 58| return self.hasReturnValue || self.returnValueClosure != nil 31| 58| } 32| | 33| | 34| | // MARK: Call 35| | 36| | /** Method is being called */ 37| | @discardableResult 38| 66| override func handleCall(_ args: [Any?]) throws -> Any? { 39| 66| var ret: Any? 40| 66| 41| 66| // capture args 42| 66| if let configuration = self.configuration { 43| 58| self.capture(args, configArgs: configuration.args) 44| 66| } 45| 66| 46| 66| // call closure if required 47| 66| if let closure = self.closure { closure(args) } 48| 66| 49| 66| // throw error if required 50| 66| if let error = self.error { 51| 3| throw error 52| 63| } 53| 63| 54| 63| // return value if one specified 55| 63| if self.hasReturnValue { 56| 10| ret = self.returnValue 57| 63| } 58| 63| // otherwise, compute from closure 59| 63| else if let returnValueClosure = self.returnValueClosure { 60| 4| ret = returnValueClosure(args) 61| 63| } 62| 63| 63| 63| return ret 64| 63| } 65| | 66| | 67| | /** Capture arguments */ 68| 58| private func capture(_ args: [Any?], configArgs: ArgumentsConfiguration) { 69| 58| guard args.count > 0 else { return } 70| 53| 71| 80| for i in 0...args.count-1 { 72| 80| let arg = args[i] 73| 80| let configArg = configArgs.args[i] 74| 80| 75| 80| if let config = configArg as? ArgumentCapture { 76| 12| config.setValue(arg) 77| 80| } 78| 80| } 79| 53| } 80| | 81| |} 82| | 83| | 84| | 85| |// MARK: Registration 86| |extension Stub { 87| | 88| | /** register call */ 89| | @discardableResult 90| 8| public func call(_ value: T) -> Stub { 91| 8| // nothing to do 92| 8| return self 93| 8| } 94| | 95| |} 96| | 97| | 98| | 99| |// MARK: Actions 100| |extension Stub { 101| | 102| | 103| | /** Register return value */ 104| | @discardableResult 105| 13| public func andReturn(_ value: Any?) -> Stub { 106| 13| self.returnValue = value 107| 13| self.hasReturnValue = true 108| 13| return self 109| 13| } 110| | 111| | 112| | /** Register return value closure */ 113| | @discardableResult 114| 5| public func andReturn(closure: @escaping (_ args: [Any?]) -> Any?) -> Stub { 115| 5| self.returnValueClosure = closure 116| 5| return self 117| 5| } 118| | 119| | 120| | /** Register closure to be called at the end */ 121| | @discardableResult 122| 5| public func andDo(_ closure: @escaping (_ args: [Any?]) -> Void) -> Stub { 123| 5| self.closure = closure 124| 5| return self 125| 5| } 126| | 127| | 128| | /** Register error to be thrown */ 129| | @discardableResult 130| 3| public func andThrow(_ error: Error) -> Stub { 131| 3| self.error = error 132| 3| return self 133| 3| } 134| | 135| |} 136| | 137| | 138| |/** Extension for a list of stubs */ 139| |extension Collection where Iterator.Element: Stub { 140| | 141| | 142| | /** Return the best stub (the most precise one) */ 143| 64| func best() -> Stub? { 144| 64| return self.sorted().first 145| 64| } 146| | 147| | 148| | /** Return list of stubs sorted by precision */ 149| 64| private func sorted() -> [Stub] { 150| 64| return self.sorted(by: { (stub1, stub2) -> Bool in 151| 2| return stub2 < stub1 152| 2| }) 153| 64| } 154| | 155| |} 156| | 157| | 158| |/** Two stubs are comparable, in terms of argument configuration */ 159| |extension Stub: Comparable {} 160| | 161| | 162| 2|public func ==(lhs: Stub, rhs: Stub) -> Bool { 163| 2| if let configuration1 = lhs.configuration, let configuration2 = rhs.configuration { 164| 1| return configuration1 == configuration2 165| 1| } 166| 1| return false 167| 2|} 168| | 169| | 170| 4|public func <(lhs: Stub, rhs: Stub) -> Bool { 171| 4| if let configuration1 = lhs.configuration, let configuration2 = rhs.configuration { 172| 3| return configuration1 < configuration2 173| 3| } 174| 1| return false 175| 4|} /Users/travis/build/pirishd/InstantMock/Sources/InstantMock/Mock.swift: 1| |// 2| |// Mock.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 06/05/2017. 6| |// Copyright © 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |/** Main protocol for adding mocking capabilities to any object, using delegation */ 11| |public protocol MockDelegate { 12| | 13| | /// Provide mock instance 14| | var it: Mock { get } 15| |} 16| | 17| | 18| |/** Protocol for mock expectations */ 19| |public protocol MockExpectation: MockExpectationFactory { 20| | 21| | /// Verify all expections on the current instance 22| | func verify(file: StaticString?, line: UInt?) 23| |} 24| | 25| | 26| |/** Protocol for the creation of mock expectations */ 27| |public protocol MockExpectationFactory { 28| | 29| | /// Create new expectation for current instance 30| | func expect() -> Expectation 31| | func reject() -> Expectation 32| |} 33| | 34| | 35| |/** Protocol for stubs */ 36| |public protocol MockStub { 37| | 38| | /// Create new stub for current instance 39| | func stub() -> Stub 40| |} 41| | 42| | 43| | 44| |/** 45| | A `Mock` instance is dedicated to being used in unit tests, in order to mock some specific behaviors. 46| | It can be used in two particular cases: 47| | - **verify** some behaviors with **expectations** (represented by the `Expectation` class) 48| | - **stub** behaviors, to return or perform some actions under certain conditions (stubs are represented by the 49| | `Stub` class) 50| | 51| | Example of an expectation being verified: 52| | 53| | let mock: Mock 54| | mock.expect().call(mock.someMethod()) 55| | mock.verify() 56| | 57| | Example of a stub: 58| | 59| | let mock: Mock 60| | mock.stub().call(mock.someMethod()).andReturn(someValue) 61| | 62| | */ 63| |open class Mock { 64| | 65| | // interceptors defined in registration mode 66| | private var expectationBeingRegistered: Expectation? 67| | private var stubBeingRegistered: Stub? 68| | 69| | // interceptors storage 70| | private let expectationStorage = CallInterceptorStorage() 71| | private let stubStorage = CallInterceptorStorage() 72| | 73| | // interceptors factories 74| | private let expectationFactory: ExpectationFactory 75| | 76| | // deferring registration for property mocking 77| | private var deferCallRegistration = false 78| | var deferredFunction: String? 79| | 80| | // property needed to be mocked 81| 3| public var property: Property { 82| 3| self.deferCallRegistration = true 83| 3| return Property(mock: self) 84| 3| } 85| | 86| | 87| | // MARK: Initializers 88| | 89| | /** Initialize instance with optional provided `ExpectationFactory` for dependency injection */ 90| 53| public init(_ expectationFactory: ExpectationFactory = ExpectationFactoryImpl.instance) { 91| 53| self.expectationFactory = expectationFactory 92| 53| } 93| | 94| | 95| | // MARK: Methods 96| | /** Resets all expectations and rejects */ 97| 1| public func resetExpectations() { 98| 1| self.expectationBeingRegistered = nil 99| 1| self.expectationStorage.removeAll() 100| 1| } 101| | 102| | /** Resets all stubs */ 103| 1| public func resetStubs() { 104| 1| self.stubBeingRegistered = nil 105| 1| self.stubStorage.removeAll() 106| 1| } 107| |} 108| | 109| | 110| | 111| |/* Extension for handling the creation of mock expectations */ 112| |extension Mock: MockExpectationFactory { 113| | 114| | @discardableResult 115| 45| public func expect() -> Expectation { 116| 45| 117| 45| // flush all the argument configurations from the storage 118| 45| ArgumentStorageImpl.instance.flush() 119| 45| 120| 45| let stub = Stub() 121| 45| let expectation = self.expectationFactory.expectation(withStub: stub) 122| 45| 123| 45| // mark instances as being ready for registration 124| 45| self.expectationBeingRegistered = expectation 125| 45| self.stubBeingRegistered = stub 126| 45| 127| 45| return expectation 128| 45| } 129| | 130| | 131| | @discardableResult 132| 3| public func reject() -> Expectation { 133| 3| 134| 3| // flush all the argument configurations from the storage 135| 3| ArgumentStorageImpl.instance.flush() 136| 3| 137| 3| let stub = Stub() 138| 3| let rejection = self.expectationFactory.rejection(withStub: stub) 139| 3| 140| 3| // mark instances as being ready for registration 141| 3| self.expectationBeingRegistered = rejection 142| 3| self.stubBeingRegistered = stub 143| 3| 144| 3| return rejection 145| 3| } 146| | 147| |} 148| | 149| | 150| |/* Extension for handling mock expectations */ 151| |extension Mock { 152| | 153| | 154| | /** 155| | Verify that all expectations are ok 156| | - parameter file: optional string for the name of the file being verified, default takes caller file name 157| | - parameter line: optional line for the line of the file being verified, default takes caller file line 158| | */ 159| 66| public func verify(file: StaticString? = #file, line: UInt? = #line) { 160| 68| for expectation in self.expectationStorage.all() { 161| 68| expectation.verify(file: file, line: line) 162| 68| } 163| 66| } 164| | 165| | 166| | /** 167| | Register an expectation 168| | - parameter expectation: expectation to be registered 169| | - parameter function: function for which expectations must be handled 170| | - parameter argsConfig: arguments configuration passed to the function being regsitered 171| | */ 172| 48| private func register(expectation: Expectation, for function: String, with argsConfig: ArgumentsConfiguration) { 173| 48| 174| 48| // compute configurations based on provided args 175| 48| let configuration = CallConfiguration(for: function, with: argsConfig) 176| 48| expectation.configuration = configuration 177| 48| 178| 48| // store the expectation for function 179| 48| self.expectationStorage.store(interceptor: expectation, for: function) 180| 48| 181| 48| // reset registration mode 182| 48| self.expectationBeingRegistered = nil 183| 48| } 184| | 185| | 186| | /** 187| | Handle expectations while mock is being called 188| | - parameter function: function being called 189| | - parameter args: list of arguments passed to the function being called 190| | */ 191| 61| private func handleExpectationsWhileBeingCalled(for function: String, with args: [Any?]) { 192| 61| 193| 61| // retrieve expectations for the function 194| 61| let expectations = self.expectationStorage.interceptors(for: function) 195| 61| 196| 61| // notify expectations matching args that they are fullfilled 197| 61| for expectation in expectations.matching(args) { 198| 49| do { 199| 49| try expectation.handleCall(args) 200| 49| } catch { 201| 0| fatalError("Expectation threw unexpected error=\(error)") 202| 49| } 203| 61| } 204| 61| 205| 61| } 206| | 207| |} 208| | 209| | 210| |/* Extension for handling stubs */ 211| |extension Mock: MockStub { 212| | 213| | @discardableResult 214| 8| public func stub() -> Stub { 215| 8| let stub = Stub() 216| 8| 217| 8| // flush all the argument configurations from the storage 218| 8| ArgumentStorageImpl.instance.flush() 219| 8| 220| 8| // mark instance as being ready for registration 221| 8| self.stubBeingRegistered = stub 222| 8| 223| 8| return stub 224| 8| } 225| | 226| | 227| | /** 228| | Register a stub 229| | - parameter stub: stub to be registered 230| | - parameter function: function for which stubs must be handled 231| | - parameter argsConfig: arguments configuration passed to the function being regsitered 232| | */ 233| 56| private func register(stub: Stub, for function: String, with argsConfig: ArgumentsConfiguration) { 234| 56| 235| 56| // compute configurations based on provided args 236| 56| let configuration = CallConfiguration(for: function, with: argsConfig) 237| 56| stub.configuration = configuration 238| 56| 239| 56| // store the stub for function 240| 56| self.stubStorage.store(interceptor: stub, for: function) 241| 56| 242| 56| // reset registration mode 243| 56| self.stubBeingRegistered = nil 244| 56| } 245| | 246| | 247| | /** 248| | Handle stubs while mock is being called 249| | - parameter function: function being called 250| | - parameter args: list of arguments passed to the function being called 251| | */ 252| 61| private func handleStubsWhileBeingCalled(for function: String, with args: [Any?]) throws -> (T?, Bool) { 253| 61| var ret: T? 254| 61| var useDefaultValue = true 255| 61| 256| 61| // retrieve configured stubs for the function 257| 61| let stubs = self.stubStorage.interceptors(for: function) 258| 61| 259| 61| // find the best stub and apply it 260| 61| if let stub = stubs.matching(args).best() { 261| 57| let retVal = try stub.handleCall(args) 262| 57| if retVal is T? { 263| 55| ret = retVal as? T 264| 57| } else { 265| 2| fatalError("Unexpected type of return value \(T.self)") 266| 57| } 267| 57| if stub.returns { useDefaultValue = false } 268| 61| } 269| 61| 270| 61| return (ret, useDefaultValue) 271| 61| } 272| | 273| | 274| |} 275| | 276| | 277| |/** Extension for handling calls to the mock */ 278| |extension Mock { 279| | 280| | 281| | /** Call with no return value */ 282| 39| public func call(_ args: Any?..., function: String = #function) -> Void { 283| 39| let ret: Void? = try? self.doCall(args, function: function) 284| 39| return ret ?? () 285| 39| } 286| | 287| | 288| | /** Call with return type object */ 289| 72| public func call(_ args: Any?..., function: String = #function) -> T? { 290| 72| return try? self.doCall(args, function: function) as T? 291| 72| } 292| | 293| | 294| | /** Throwing call with no return value */ 295| 2| public func callThrowing(_ args: Any?..., function: String = #function) throws -> Void { 296| 2| let ret: Void? = try self.doCall(args, function: function) 297| 2| return ret ?? () 298| 2| } 299| | 300| | 301| | /** Throwing call with return type object */ 302| 4| public func callThrowing(_ args: Any?..., function: String = #function) throws -> T? { 303| 4| return try self.doCall(args, function: function) as T? 304| 4| } 305| | 306| | 307| | /** Handle a call */ 308| | @discardableResult 309| 117| private func doCall(_ args: [Any?], function: String) throws -> T? { 310| 117| var ret: T? 311| 117| 312| 117| if self.expectationBeingRegistered != nil || self.stubBeingRegistered != nil { 313| 56| ret = self.handleRegistration(of: function, with: args) 314| 117| } else { 315| 61| ret = try self.handleCall(of: function, with: args) 316| 117| } 317| 117| 318| 117| return ret 319| 117| } 320| | 321| | 322| | /** 323| | Handle the registration 324| | - parameter function: function being registered 325| | - parameter args: args for the function being registered 326| | - returns: return value for that function 327| | */ 328| 59| func handleRegistration(of function: String, with args: [Any?]) -> T? { 329| 59| 330| 59| // create a new arguments configuration 331| 59| let argsConfig = ArgumentsConfiguration(ArgumentStorageImpl.instance.all()) 332| 59| 333| 59| // make sure the number of arguments passed matches the number of configured 334| 59| if argsConfig.args.count != args.count { 335| 0| fatalError("Invalid argument configuration, see Arg class for more information") 336| 59| } 337| 59| 338| 59| // perform actual registration 339| 59| let ret: T? = self.register(function, with: argsConfig) 340| 59| return ret 341| 59| } 342| | 343| | 344| | /** 345| | Register function 346| | - parameter function: function being registered 347| | - parameter argsConfig: arguments configuration passed to the function being regsitered 348| | - returns: return value for that function 349| | */ 350| 59| private func register(_ function: String, with argsConfig: ArgumentsConfiguration) -> T? { 351| 59| var ret: T? 352| 59| 353| 59| // defer registration (for setter mocking) 354| 59| if self.deferCallRegistration { 355| 3| self.deferredFunction = function 356| 3| self.deferCallRegistration = false 357| 59| } 358| 59| // or register immediately 359| 59| else { 360| 56| self.doRegister(function, with: argsConfig) 361| 59| } 362| 59| 363| 59| // default value 364| 59| if let value = DefaultValueHandler().it { 365| 36| ret = value 366| 59| } 367| 59| 368| 59| return ret 369| 59| } 370| | 371| | 372| | /** 373| | Performs the actual registration of a function 374| | - parameter function: function being registered 375| | - parameter argsConfig: arguments configuration passed to the function being regsitered 376| | - returns: return value for that function 377| | */ 378| 56| private func doRegister(_ function: String, with argsConfig: ArgumentsConfiguration) { 379| 56| // register expectation if necessary 380| 56| if let expectation = self.expectationBeingRegistered { 381| 48| self.register(expectation: expectation, for: function, with: argsConfig) 382| 56| } 383| 56| 384| 56| // register stub if necessary 385| 56| if let stub = self.stubBeingRegistered { 386| 56| self.register(stub: stub, for: function, with: argsConfig) 387| 56| } 388| 56| } 389| | 390| | 391| | /** 392| | Handle actual call to the function 393| | - parameter function: function being called 394| | - parameter args: arguments of the function 395| | - returns: return value for that function 396| | */ 397| 61| private func handleCall(of function: String, with args: [Any?]) throws -> T? { 398| 61| var ret: T? 399| 61| var useDefaultValue = true 400| 61| 401| 61| // notify interceptors they are being called 402| 61| self.handleExpectationsWhileBeingCalled(for: function, with: args) 403| 61| (ret, useDefaultValue) = try self.handleStubsWhileBeingCalled(for: function, with: args) 404| 61| 405| 61| // default value 406| 61| if useDefaultValue, let value = DefaultValueHandler().it { 407| 28| ret = value 408| 61| } 409| 61| 410| 61| return ret 411| 61| } 412| | 413| | 414| |} /Users/travis/build/pirishd/InstantMock/Sources/InstantMock/MockUsable/Array+MockUsable.swift: 1| |// 2| |// Array+MockUsable.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 08/05/2017. 6| |// Copyright © 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |/** Extension for making `Array` easily usage in mocks */ 11| |extension Array: MockUsable { 12| | 13| 3| public static var any: Array { 14| 3| return Array() 15| 3| } 16| | 17| 3| public static var anyValue: MockUsable { 18| 3| return Array.any 19| 3| } 20| | 21| 4| public func equal(to value: MockUsable?) -> Bool { 22| 4| guard let arrayValue = value as? Array else { return false } 23| 2| return VerifierImpl.instance.equalArray(self, to: arrayValue) 24| 4| } 25| | 26| |} /Users/travis/build/pirishd/InstantMock/Sources/InstantMock/MockUsable/Bool+MockUsable.swift: 1| |// 2| |// Bool+MockUsable.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 06/05/2017. 6| |// Copyright © 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |/** Extension for making `Bool` easily usage in mocks */ 11| |extension Bool: MockUsable { 12| | 13| | public static let any = false 14| | 15| 3| public static var anyValue: MockUsable { 16| 3| return Bool.any 17| 3| } 18| | 19| 4| public func equal(to value: MockUsable?) -> Bool { 20| 4| guard let boolValue = value as? Bool else { return false } 21| 2| return self == boolValue 22| 4| } 23| | 24| |} /Users/travis/build/pirishd/InstantMock/Sources/InstantMock/MockUsable/Dictionary+MockUsable.swift: 1| |// 2| |// Dictionary+MockUsable.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 08/05/2017. 6| |// Copyright © 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |/** Extension for making `Dictionary` easily usage in mocks */ 11| |extension Dictionary: MockUsable { 12| | 13| 3| public static var any: Dictionary { 14| 3| return Dictionary() 15| 3| } 16| | 17| 3| public static var anyValue: MockUsable { 18| 3| return Dictionary.any 19| 3| } 20| | 21| 6| public func equal(to value: MockUsable?) -> Bool { 22| 6| guard let dictValue = value as? Dictionary else { return false } 23| 4| 24| 4| // sort keys and compare them 25| 4| let dictValueKeys = dictValue.keys.sorted { key1, key2 -> Bool in 26| 1| return key1.hashValue < key2.hashValue 27| 1| } 28| 4| let keys = self.keys.sorted { key1, key2 -> Bool in 29| 1| return key1.hashValue < key2.hashValue 30| 1| } 31| 4| if !VerifierImpl.instance.equalArray(keys, to: dictValueKeys) { 32| 1| return false 33| 3| } 34| 3| 35| 3| // compare values 36| 4| for key in keys { 37| 4| if !VerifierImpl.instance.equal(self[key], to: dictValue[key]) { 38| 1| return false 39| 3| } 40| 3| } 41| 2| 42| 2| return true 43| 3| } 44| | 45| |} /Users/travis/build/pirishd/InstantMock/Sources/InstantMock/MockUsable/Double+MockUsable.swift: 1| |// 2| |// Double+MockUsable.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 06/05/2017. 6| |// Copyright © 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |/** Extension for making `Double` easily usage in mocks */ 11| |extension Double: MockUsable { 12| | 13| | public static let any = 42.0 14| | 15| 3| public static var anyValue: MockUsable { 16| 3| return Double.any 17| 3| } 18| | 19| 4| public func equal(to value: MockUsable?) -> Bool { 20| 4| guard let doubleValue = value as? Double else { return false } 21| 2| return self == doubleValue 22| 4| } 23| | 24| |} /Users/travis/build/pirishd/InstantMock/Sources/InstantMock/MockUsable/Int+MockUsable.swift: 1| |// 2| |// Int+MockUsable.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 06/05/2017. 6| |// Copyright © 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |/** Extension for making `Int` easily usage in mocks */ 11| |extension Int: MockUsable { 12| | 13| | public static let any = 42 14| | 15| 24| public static var anyValue: MockUsable { 16| 24| return Int.any 17| 24| } 18| | 19| 11| public func equal(to value: MockUsable?) -> Bool { 20| 11| guard let intValue = value as? Int else { return false } 21| 9| return self == intValue 22| 11| } 23| | 24| |} /Users/travis/build/pirishd/InstantMock/Sources/InstantMock/MockUsable/Set+MockUsable.swift: 1| |// 2| |// Set+MockUsable.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 08/05/2017. 6| |// Copyright © 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |/** Extension for making `Set` easily usage in mocks */ 11| |extension Set: MockUsable { 12| | 13| 3| public static var any: Set { 14| 3| return Set() 15| 3| } 16| | 17| 3| public static var anyValue: MockUsable { 18| 3| return Set.any 19| 3| } 20| | 21| 4| public func equal(to value: MockUsable?) -> Bool { 22| 4| guard let setValue = value as? Set else { return false } 23| 2| return self == setValue 24| 4| } 25| | 26| |} /Users/travis/build/pirishd/InstantMock/Sources/InstantMock/MockUsable/String+MockUsable.swift: 1| |// 2| |// String+MockUsable.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 06/05/2017. 6| |// Copyright © 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |/** Extension for making `String` easily usage in mocks */ 11| |extension String: MockUsable { 12| | 13| | public static let any = "anyStringValue" 14| | 15| 74| public static var anyValue: MockUsable { 16| 74| return String.any 17| 74| } 18| | 19| 48| public func equal(to value: MockUsable?) -> Bool { 20| 48| guard let stringValue = value as? String else { return false } 21| 46| return self == stringValue 22| 48| } 23| | 24| |} /Users/travis/build/pirishd/InstantMock/Sources/InstantMock/Property/Property.swift: 1| |// 2| |// Property.swift 3| |// InstantMock iOS 4| |// 5| |// Created by Patrick on 03/03/2018. 6| |// Copyright © 2018 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |/** Represents a property to be mocked */ 11| |public final class Property { 12| | 13| | /// mock dependency 14| | private weak var mock: Mock? 15| | 16| | 17| | /// Initializers 18| 3| init(mock: Mock) { 19| 3| self.mock = mock 20| 3| } 21| | 22| | 23| | /** Register a mocked setter */ 24| 3| public func set(_ prop: T, value: T) { 25| 3| if let function = self.mock?.deferredFunction { 26| 3| self.mock?.handleRegistration(of: function, with: [value]) as Void? 27| 3| } 28| 3| } 29| | 30| |} /Users/travis/build/pirishd/InstantMock/Sources/InstantMock/Verifications/Assertion.swift: 1| |// 2| |// Assertion.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 08/05/2017. 6| |// Copyright © 2017 pirishd. All rights reserved. 7| |// 8| | 9| |import XCTest 10| | 11| | 12| |/** Protocol for an Assertion */ 13| |public protocol Assertion { 14| | 15| | /** 16| | Successful assertion 17| | - parameter file: file where the failure occured 18| | - parameter line: line in the file where the failure occured 19| | */ 20| | func success(file: StaticString?, line: UInt?) 21| | 22| | /** 23| | Failed assertion 24| | - parameter description: description of the failure 25| | - parameter file: file where the failure occured 26| | - parameter line: line in the file where the failure occured 27| | */ 28| | func fail(_ description: String?, file: StaticString?, line: UInt?) 29| |} 30| | 31| | 32| |/** Standard implementation of an Assertion */ 33| |final class AssertionImpl: Assertion { 34| | 35| | /// Singleton 36| | static let instance = AssertionImpl() 37| | 38| | 39| 0| func success(file: StaticString?, line: UInt?) { 40| 0| // nothing to do 41| 0| } 42| | 43| | 44| 0| func fail(_ description: String?, file: StaticString?, line: UInt?) { 45| 0| 46| 0| let message = description ?? "" 47| 0| if let file = file, let line = line { 48| 0| XCTFail(message, file: file, line: line) 49| 0| } else { 50| 0| XCTFail(message) 51| 0| } 52| 0| 53| 0| } 54| | 55| |} /Users/travis/build/pirishd/InstantMock/Sources/InstantMock/Verifications/Verifier.swift: 1| |// 2| |// Verifier.swift 3| |// InstantMock 4| |// 5| |// Created by Patrick on 08/05/2017. 6| |// Copyright © 2017 pirishd. All rights reserved. 7| |// 8| | 9| | 10| |/** Protoocl dedicated to verify equality between values */ 11| |public protocol Verifier { 12| | func equal(_ arg: Any?, to value: Any?) -> Bool 13| |} 14| | 15| | 16| |/** Main verifier implementation */ 17| |final class VerifierImpl: Verifier { 18| | 19| | 20| | /// Singleton 21| | static let instance = VerifierImpl() 22| | 23| | 24| | /** 25| | Make sure two optional values are equal 26| | - parameter arg: first argument 27| | - paramater value: second argument 28| | - returs: true if two values are equal 29| | */ 30| 58| func equal(_ arg: Any?, to value: Any?) -> Bool { 31| 58| 32| 58| // compare nil values 33| 58| if arg == nil && value == nil { return true } 34| 55| if arg != nil && value == nil { return false } 35| 54| if arg == nil && value != nil { return false } 36| 53| 37| 53| // otherwise, perform advanced verifications 38| 53| return self.equal(arg!, to: value!) 39| 54| } 40| | 41| | 42| | /** 43| | Make sure two non-nil values are equal 44| | - parameter arg: first argument 45| | - paramater value: second argument 46| | - returs: true if two values are equal 47| | */ 48| 63| func equal(_ arg: Any, to value: Any) -> Bool { 49| 63| 50| 63| // MockUsable values 51| 63| if let mockArg = arg as? MockUsable, let mockValue = value as? MockUsable { 52| 51| return mockArg.equal(to: mockValue) 53| 51| } 54| 12| 55| 12| // try to compare by reference 56| 12| if (arg as AnyObject) === (value as AnyObject) { 57| 3| return true 58| 9| } 59| 9| 60| 9| // compare Void types 61| 9| if arg is Void && value is Void { 62| 1| return true 63| 8| } 64| 8| 65| 8| // arguments can be types 66| 8| if let argType = arg as? Any.Type, let valueType = value as? Any.Type { 67| 4| return argType == valueType 68| 4| } 69| 4| 70| 4| // default case 71| 4| return false 72| 8| } 73| | 74| | 75| | /** 76| | Make sure two arrays are equal 77| | - parameter arg: first argument 78| | - paramater value: second argument 79| | - returs: true if two values are equal 80| | */ 81| 9| func equalArray(_ arg: [Any?], to value: [Any?]) -> Bool { 82| 9| 83| 9| // make sure the two arrays have the same number of elements 84| 9| if arg.count != value.count { 85| 1| return false 86| 8| } 87| 8| 88| 8| // verify equality between array elements 89| 8| if arg.count > 0 { 90| 11| for i in 0...arg.count-1 { 91| 11| if !self.equal(arg[i], to: value[i]) { 92| 3| return false 93| 8| } 94| 8| } 95| 5| } 96| 5| 97| 5| return true 98| 8| } 99| | 100| |} <<<<<< EOF # path=fixes ./Sources/InstantMock.h:8,10,13,16,18,19 <<<<<< EOF