diff --git a/Mocky.xcodeproj/project.pbxproj b/Mocky.xcodeproj/project.pbxproj index 001d9c3d..37e21ace 100644 --- a/Mocky.xcodeproj/project.pbxproj +++ b/Mocky.xcodeproj/project.pbxproj @@ -238,7 +238,6 @@ C8EF7B7921FB423A00AFA545 /* TemplateHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TemplateHelper.swift; sourceTree = ""; }; C8EF7B7A21FB423A00AFA545 /* ParameterWrapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ParameterWrapper.swift; sourceTree = ""; }; C8EF7B7B21FB423A00AFA545 /* Helpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Helpers.swift; sourceTree = ""; }; - C8EF7B7C21FB423A00AFA545 /* Mock.swifttemplate */ = {isa = PBXFileReference; lastKnownFileType = text; path = Mock.swifttemplate; sourceTree = ""; }; C8EF7B7D21FB423A00AFA545 /* Header.swifttemplate */ = {isa = PBXFileReference; lastKnownFileType = text; path = Header.swifttemplate; sourceTree = ""; }; C8EF7B7E21FB423A00AFA545 /* Main.swifttemplate */ = {isa = PBXFileReference; lastKnownFileType = text; path = Main.swifttemplate; sourceTree = ""; }; C8EF7B7F21FB423A00AFA545 /* TypeWrapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TypeWrapper.swift; sourceTree = ""; }; @@ -497,7 +496,6 @@ C8EF7B8021FB423A00AFA545 /* SubscriptWrapper.swift */, C8EF7B8321FB423A00AFA545 /* VariableWrapper.swift */, C8EF7B7E21FB423A00AFA545 /* Main.swifttemplate */, - C8EF7B7C21FB423A00AFA545 /* Mock.swifttemplate */, ); path = Templates; sourceTree = ""; @@ -1079,6 +1077,7 @@ developmentRegion = English; hasScannedForEncodings = 0; knownRegions = ( + English, en, Base, ); diff --git a/Podfile b/Podfile index ed752a05..66e928ab 100644 --- a/Podfile +++ b/Podfile @@ -29,5 +29,5 @@ target 'Mocky_Example_macOS' do end post_install do |installer| - system("rake sourcery 4.2") + system("rake sourcery") end diff --git a/Podfile.lock b/Podfile.lock index f55958a0..f45edf00 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -1,9 +1,9 @@ PODS: - - Sourcery (0.15.0) - - SwiftyMocky (3.2.0-pre): - - SwiftyMocky/Core (= 3.2.0-pre) - - SwiftyMocky/Core (3.2.0-pre): - - Sourcery + - Sourcery (0.16.0) + - SwiftyMocky (3.2.0): + - SwiftyMocky/Core (= 3.2.0) + - SwiftyMocky/Core (3.2.0): + - Sourcery (> 0.16) DEPENDENCIES: - SwiftyMocky (from `./`) @@ -17,9 +17,9 @@ EXTERNAL SOURCES: :path: "./" SPEC CHECKSUMS: - Sourcery: 5895672cae353cdbfa95f3f4aaeb75a664d76f6a - SwiftyMocky: c7e9af4f3c1db03819aa5cb85ee142ce6c10eb2b + Sourcery: 3e55aad9719ee5782ab81eb16addd44d272e4d13 + SwiftyMocky: 20ee055796bb023142e83a23bee86d43ecb6c29a -PODFILE CHECKSUM: cdf7b5fe099a9d915745d7d33c48441088136050 +PODFILE CHECKSUM: 2c8b2a04598345eef7278f0b805b84438bb9f711 COCOAPODS: 1.6.1 diff --git a/Rakefile b/Rakefile index d4ff966d..b42dbd02 100644 --- a/Rakefile +++ b/Rakefile @@ -41,6 +41,10 @@ task :template do sh "cd ./Templates && cat Main.swifttemplate >> #{destination}" end +task :xcode do + sh "open Mocky.xcworkspace" +end + ## [ Sourcery ] ################################################################ desc "Download prebuilt sourcery app." diff --git a/Sources/Templates/Mock.swifttemplate b/Sources/Templates/Mock.swifttemplate index 4bdd69ba..b81aae73 100644 --- a/Sources/Templates/Mock.swifttemplate +++ b/Sources/Templates/Mock.swifttemplate @@ -37,11 +37,11 @@ func stringArray(fromArguments arguments: [String: Any], forKey key: String) -> } _%> <%# ================================================== SwiftLint -%><%_ -%> -<%_ for rule in swiftLintRules(arguments) { -%> +<%_ for rule in swiftLintRules(argument) { -%> <%_ %><%= rule %> <%_ } -%> -<%# ================================================== SwiftLint (aggregated arguments) -%><%_ -%> -<%_ if let swiftyMockyArgs = arguments["swiftyMocky"] as? [String: Any], let rules = swiftyMockyArgs["excludedSwiftLintRules"] as? [String] { -%> +<%# ================================================== SwiftLint (aggregated argument) -%><%_ -%> +<%_ if let swiftyMockyArgs = argument["swiftyMocky"] as? [String: Any], let rules = swiftyMockyArgs["excludedSwiftLintRules"] as? [String] { -%> <%_ for rule in rules { -%> <%_ %>//swiftlint:disable <%= rule %> <%_ } -%> @@ -50,11 +50,11 @@ _%> #if MockyCustom import SwiftyMocky <%# ================================================== IMPORTS InAPP -%><%_ -%> - <%_ for projectImport in projectImports(arguments) { -%> + <%_ for projectImport in projectImports(argument) { -%> <%_ %><%= projectImport %> <%_ } -%> - <%# ============================ IMPORTS InAPP (aggregated arguments) -%><%_ -%> - <%_ if let swiftyMockyArgs = arguments["swiftyMocky"] as? [String: Any] { -%> + <%# ============================ IMPORTS InAPP (aggregated argument) -%><%_ -%> + <%_ if let swiftyMockyArgs = argument["swiftyMocky"] as? [String: Any] { -%> <%_ for projectImport in imports(swiftyMockyArgs) { -%> <%_ %><%= projectImport %> <%_ } -%> @@ -76,11 +76,11 @@ import SwiftyMocky import SwiftyMocky import XCTest <%# ================================================== IMPORTS -%><%_ -%> - <%_ for projectImport in projectImports(arguments) { -%> + <%_ for projectImport in projectImports(argument) { -%> <%_ %><%= projectImport %> <%_ } -%> - <%# ============================ IMPORTS InAPP (aggregated arguments) -%><%_ -%> - <%_ if let swiftyMockyArgs = arguments["swiftyMocky"] as? [String: Any] { -%> + <%# ============================ IMPORTS InAPP (aggregated argument) -%><%_ -%> + <%_ if let swiftyMockyArgs = argument["swiftyMocky"] as? [String: Any] { -%> <%_ for projectImport in projectImports(swiftyMockyArgs) { -%> <%_ %><%= projectImport %> <%_ } -%> diff --git a/SwiftyMocky-Tests/iOS/Mocks/Mock.generated.swift b/SwiftyMocky-Tests/iOS/Mocks/Mock.generated.swift index a95ed534..7d595ef0 100644 --- a/SwiftyMocky-Tests/iOS/Mocks/Mock.generated.swift +++ b/SwiftyMocky-Tests/iOS/Mocks/Mock.generated.swift @@ -1,4 +1,4 @@ -// Generated using Sourcery 0.15.0 — https://github.com/krzysztofzablocki/Sourcery +// Generated using Sourcery 0.16.0 — https://github.com/krzysztofzablocki/Sourcery // DO NOT EDIT diff --git a/SwiftyMocky-Tests/macOS/Mocks/Mock.generated.swift b/SwiftyMocky-Tests/macOS/Mocks/Mock.generated.swift index 0b4b5f6a..1c2103c3 100644 --- a/SwiftyMocky-Tests/macOS/Mocks/Mock.generated.swift +++ b/SwiftyMocky-Tests/macOS/Mocks/Mock.generated.swift @@ -1,4 +1,4 @@ -// Generated using Sourcery 0.15.0 — https://github.com/krzysztofzablocki/Sourcery +// Generated using Sourcery 0.16.0 — https://github.com/krzysztofzablocki/Sourcery // DO NOT EDIT diff --git a/SwiftyMocky-Tests/tvOS/Mocks/Mock.generated.swift b/SwiftyMocky-Tests/tvOS/Mocks/Mock.generated.swift index 0a354a4f..32f5afbe 100644 --- a/SwiftyMocky-Tests/tvOS/Mocks/Mock.generated.swift +++ b/SwiftyMocky-Tests/tvOS/Mocks/Mock.generated.swift @@ -1,4 +1,4 @@ -// Generated using Sourcery 0.15.0 — https://github.com/krzysztofzablocki/Sourcery +// Generated using Sourcery 0.16.0 — https://github.com/krzysztofzablocki/Sourcery // DO NOT EDIT diff --git a/SwiftyMocky.podspec b/SwiftyMocky.podspec index b1c07efb..21f12980 100644 --- a/SwiftyMocky.podspec +++ b/SwiftyMocky.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = 'SwiftyMocky' - s.version = '3.2.0-pre' + s.version = '3.2.0' s.summary = 'Unit testing library for Swift, with mock generation. Adds a set of handy methods, simplifying testing.' s.description = <<-DESC Library that uses metaprogramming technique to generate mocks based on sources, that makes testing for Swift Mockito-like. @@ -33,7 +33,7 @@ Library that uses metaprogramming technique to generate mocks based on sources, core.xcconfig = { 'OTHER_SWIFT_FLAGS' => '-DMocky' } core.frameworks = 'Foundation' core.weak_framework = "XCTest" - core.dependency 'Sourcery' + core.dependency 'Sourcery', '> 0.16' core.pod_target_xcconfig = { 'APPLICATION_EXTENSION_API_ONLY' => 'YES', 'ENABLE_BITCODE' => 'NO', diff --git a/Templates/Header.swifttemplate b/Templates/Header.swifttemplate index 9e1e4f6f..0264e38e 100644 --- a/Templates/Header.swifttemplate +++ b/Templates/Header.swifttemplate @@ -1,9 +1,9 @@ <%# ================================================== SwiftLint -%><%_ -%> -<%_ for rule in swiftLintRules(arguments) { -%> +<%_ for rule in swiftLintRules(argument) { -%> <%_ %><%= rule %> <%_ } -%> -<%# ================================================== SwiftLint (aggregated arguments) -%><%_ -%> -<%_ if let swiftyMockyArgs = arguments["swiftyMocky"] as? [String: Any], let rules = swiftyMockyArgs["excludedSwiftLintRules"] as? [String] { -%> +<%# ================================================== SwiftLint (aggregated argument) -%><%_ -%> +<%_ if let swiftyMockyArgs = argument["swiftyMocky"] as? [String: Any], let rules = swiftyMockyArgs["excludedSwiftLintRules"] as? [String] { -%> <%_ for rule in rules { -%> <%_ %>//swiftlint:disable <%= rule %> <%_ } -%> @@ -12,11 +12,11 @@ #if MockyCustom import SwiftyMocky <%# ================================================== IMPORTS InAPP -%><%_ -%> - <%_ for projectImport in projectImports(arguments) { -%> + <%_ for projectImport in projectImports(argument) { -%> <%_ %><%= projectImport %> <%_ } -%> - <%# ============================ IMPORTS InAPP (aggregated arguments) -%><%_ -%> - <%_ if let swiftyMockyArgs = arguments["swiftyMocky"] as? [String: Any] { -%> + <%# ============================ IMPORTS InAPP (aggregated argument) -%><%_ -%> + <%_ if let swiftyMockyArgs = argument["swiftyMocky"] as? [String: Any] { -%> <%_ for projectImport in imports(swiftyMockyArgs) { -%> <%_ %><%= projectImport %> <%_ } -%> @@ -38,11 +38,11 @@ import SwiftyMocky import SwiftyMocky import XCTest <%# ================================================== IMPORTS -%><%_ -%> - <%_ for projectImport in projectImports(arguments) { -%> + <%_ for projectImport in projectImports(argument) { -%> <%_ %><%= projectImport %> <%_ } -%> - <%# ============================ IMPORTS InAPP (aggregated arguments) -%><%_ -%> - <%_ if let swiftyMockyArgs = arguments["swiftyMocky"] as? [String: Any] { -%> + <%# ============================ IMPORTS InAPP (aggregated argument) -%><%_ -%> + <%_ if let swiftyMockyArgs = argument["swiftyMocky"] as? [String: Any] { -%> <%_ for projectImport in projectImports(swiftyMockyArgs) { -%> <%_ %><%= projectImport %> <%_ } -%> diff --git a/Templates/Mock.swifttemplate b/Templates/Mock.swifttemplate deleted file mode 100644 index 9f72cdfa..00000000 --- a/Templates/Mock.swifttemplate +++ /dev/null @@ -1,589 +0,0 @@ -<%# UNUSED FOR NOW, AS BREAKING INTO SEPARATE FILES BREAKS THE FLOW FOR CARTHAGE -%> -<%- includeFile("ArgumentsHelper.swift") -%> -<%# ================================================== SwiftLint -%><%_ -%> -<%_ for rule in swiftLintRules(arguments) { -%> - <%_ %><%= rule %> -<%_ } -%> -<%# ================================================== SwiftLint (aggregated arguments) -%><%_ -%> -<%_ if let swiftyMockyArgs = arguments["swiftyMocky"] as? [String: Any], let rules = swiftyMockyArgs["excludedSwiftLintRules"] as? [String] { -%> - <%_ for rule in rules { -%> - <%_ %>//swiftlint:disable <%= rule %> - <%_ } -%> -<%_ } -%> - -#if MockyCustom -import SwiftyMocky -<%# ================================================== IMPORTS InAPP -%><%_ -%> - <%_ for projectImport in projectImports(arguments) { -%> - <%_ %><%= projectImport %> - <%_ } -%> - <%# ============================ IMPORTS InAPP (aggregated arguments) -%><%_ -%> - <%_ if let swiftyMockyArgs = arguments["swiftyMocky"] as? [String: Any] { -%> - <%_ if let imported = swiftyMockyArgs["import"] as? String { -%> - <%_ %>import <%= imported %> - <%_ } -%> - <%_ if let allImported = swiftyMockyArgs["import"] as? [String] { -%> - <%_ for imported in allImported { -%> - <%_ %>import <%= imported %> - <%_ } -%> - <%_ } -%> - <%_ if let testable = swiftyMockyArgs["testable"] as? String { -%> - <%_ %>@testable import <%= testable %> - <%_ } -%> - <%_ if let allTestable = swiftyMockyArgs["testable"] as? [String] { -%> - <%_ for testable in allTestable { -%> - <%_ %>@testable import <%= testable %> - <%_ } -%> - <%_ } -%> - <%_ } -%> - - public final class MockyAssertion { - public static var handler: ((Bool, String, StaticString, UInt) -> Void)? - } - - func MockyAssert(_ expression: @autoclosure () -> Bool, _ message: @autoclosure () -> String = "Verification failed", file: StaticString = #file, line: UInt = #line) { - guard let handler = MockyAssertion.handler else { - assert(expression, message, file: file, line: line) - return - } - - handler(expression(), message(), file, line) - } -#elseif Mocky -import SwiftyMocky -import XCTest -<%# ================================================== IMPORTS -%><%_ -%> - <%_ for projectImport in projectImports(arguments) { -%> - <%_ %><%= projectImport %> - <%_ } -%> - <%# ============================ IMPORTS InAPP (aggregated arguments) -%><%_ -%> - <%_ if let swiftyMockyArgs = arguments["swiftyMocky"] as? [String: Any] { -%> - <%_ if let imported = swiftyMockyArgs["import"] as? String { -%> - <%_ %>import <%= imported %> - <%_ } -%> - <%_ if let allImported = swiftyMockyArgs["import"] as? [String] { -%> - <%_ for imported in allImported { -%> - <%_ %>import <%= imported %> - <%_ } -%> - <%_ } -%> - <%_ if let testable = swiftyMockyArgs["testable"] as? String { -%> - <%_ %>@testable import <%= testable %> - <%_ } -%> - <%_ if let allTestable = swiftyMockyArgs["testable"] as? [String] { -%> - <%_ for testable in allTestable { -%> - <%_ %>@testable import <%= testable %> - <%_ } -%> - <%_ } -%> - <%_ } -%> - - func MockyAssert(_ expression: @autoclosure () -> Bool, _ message: @autoclosure () -> String = "Verification failed", file: StaticString = #file, line: UInt = #line) { - XCTAssert(expression(), message(), file: file, line: line) - } -#else -import Sourcery -import SourceryRuntime -#endif - -<%# ================================================== HELPERS -%> -<%- includeFile("Current") -%> -<%- includeFile("TemplateHelper") -%> -<%- includeFile("Helpers") -%> -<%- includeFile("ParameterWrapper") -%> -<%- includeFile("TypeWrapper") -%> -<%- includeFile("MethodWrapper") -%> -<%- includeFile("SubscriptWrapper") -%> -<%- includeFile("VariableWrapper") -%> - - -<%# ================================================== SETUP -%><%_ -%> -<%_ var all = types.all - all += types.protocols.map { $0 } -%> - -<%_ for type in all { -%><%_ -%> -<%_ let autoMockable: Bool = type.inheritedTypes.contains("AutoMockable") || type.annotations["AutoMockable"] != nil - let protocolToDecorate = types.protocols.first(where: { $0.name == (type.annotations["mock"] as? String) }) - let inlineMockable = protocolToDecorate != nil - guard let aProtocol = autoMockable ? type : protocolToDecorate else { continue } - let associatedTypes: [String]? = Helpers.extractAssociatedTypes(from: aProtocol) - let genericTypesModifier: String = Helpers.extractGenericTypesModifier(associatedTypes) - let genericTypesConstraints: String = Helpers.extractGenericTypesConstraints(associatedTypes) - let allSubscripts = aProtocol.allSubscripts - let allVariables = uniques(variables: aProtocol.allVariables.filter({ !$0.isStatic })) - let containsVariables = !allVariables.isEmpty - let allStaticVariables = uniques(variables: aProtocol.allVariables.filter({ $0.isStatic })) - let containsStaticVariables = !allStaticVariables.isEmpty - let allMethods = uniques(methods: aProtocol.allMethods.filter({ !$0.isStatic })) - let allMethodsForMethodType = uniquesWithoutGenericConstraints(methods: aProtocol.allMethods.filter({ !$0.isStatic })) - let allStaticMethods = uniques(methods: aProtocol.allMethods.filter({ $0.isStatic })) - let allStaticMethodsForMethodType = uniquesWithoutGenericConstraints(methods: aProtocol.allMethods.filter({ $0.isStatic })) - let conformsToStaticMock = !allStaticMethods.isEmpty || !allStaticVariables.isEmpty - let conformsToMock = !allMethods.isEmpty || !allVariables.isEmpty -%><%_ -%><%_ -%> -<%_ if autoMockable { -%> -// MARK: - <%= type.name %> -open class <%= type.name %>Mock<%= genericTypesModifier %>:<%= type.annotations["ObjcProtocol"] != nil ? " NSObject," : "" %> <%= type.name %>, Mock<%= conformsToStaticMock ? ", StaticMock" : "" %><%= genericTypesConstraints %> { - open init(sequencing sequencingPolicy: SequencingPolicy = .lastWrittenResolvedFirst, stubbing stubbingPolicy: StubbingPolicy = .wrap, file: StaticString = #file, line: UInt = #line) { - self.sequencingPolicy = sequencingPolicy - self.stubbingPolicy = stubbingPolicy - self.file = file - self.line = line - } - -<%_ } else { -%> -// sourcery:inline:auto:<%= type.name %>.autoMocked -<%_ } -%> -<%# ================================================== MAIN CLASS -%><%_ -%> - <%# ================================================== MOCK INTERNALS -%><%_ -%> - var matcher: Matcher = Matcher.default - var stubbingPolicy: StubbingPolicy = .wrap - var sequencingPolicy: SequencingPolicy = .lastWrittenResolvedFirst - private var invocations: [MethodType] = [] - private var methodReturnValues: [Given] = [] - private var methodPerformValues: [Perform] = [] - private var file: StaticString? - private var line: UInt? - - public typealias PropertyStub = Given - public typealias MethodStub = Given - public typealias SubscriptStub = Given - - /// Convenience method - call setupMock() to extend debug information when failure occurs - public func setupMock(file: StaticString = #file, line: UInt = #line) { - self.file = file - self.line = line - } - <%_ -%> - <%# ================================================== STATIC MOCK INTERNALS -%><%_ -%> - <%_ if conformsToStaticMock { -%> - static var matcher: Matcher = Matcher.default - static var stubbingPolicy: StubbingPolicy = .wrap - static var sequencingPolicy: SequencingPolicy = .lastWrittenResolvedFirst - static private var invocations: [StaticMethodType] = [] - static private var methodReturnValues: [StaticGiven] = [] - static private var methodPerformValues: [StaticPerform] = [] - public typealias StaticPropertyStub = StaticGiven - public typealias StaticMethodStub = StaticGiven - public static func clear() { - invocations = [] - methodReturnValues = [] - methodPerformValues = [] - } - <%_ } -%> - - <%# ================================================== VARIABLES -%><%_ -%> - <%_ for variable in allVariables { -%> - <%_ if autoMockable { -%> - <%= stubProperty(variable,"\(type.name)Mock") %> - <%_ } else { %> - <%= stubProperty(variable,"\(type.name)") %> - <%_ } %> - <%_ } %> <%_ -%> - - <%# ================================================== STATIC VARIABLES -%><%_ -%> - <%_ for variable in allStaticVariables { -%> - <%_ if autoMockable { -%> - <%= stubProperty(variable,"\(type.name)Mock") %> - <%_ } else { %> - <%= stubProperty(variable,"\(type.name)") %> - <%_ } %> - <%_ } %> <%_ -%> - - <%# ================================================== METHOD REGISTRATIONS -%><%_ -%> - <%_ MethodWrapper.clear() -%> - <%_ SubscriptWrapper.clear() -%> - <%_ if autoMockable { -%> - <%_ Current.selfType = "\(type.name)Mock\(genericTypesModifier)" -%> - <%_ } else { %> - <%_ Current.selfType = "\(type.name)Mock\(genericTypesModifier)" -%> - <%_ } %> - <%_ let wrappedSubscripts = allSubscripts.map(wrapSubscript) -%> - <%_ let wrappedMethods = allMethods.map(wrapMethod).filter({ $0.wrappedInMethodType() }) -%> - <%_ let wrappedVariables = allVariables.map(justWrap) -%> - <%_ let wrappedMethodsForMethodType = allMethodsForMethodType.map(wrapMethod).filter({ $0.wrappedInMethodType() }) -%> - <%_ let wrappedInitializers = allMethods.map(wrapMethod).filter({ $0.method.isInitializer }) -%> - <%_ let wrappedStaticMethods = allStaticMethods.map(wrapMethod).filter({ $0.wrappedInMethodType() }) -%> - <%_ let wrappedStaticVariables = allStaticVariables.map(justWrap) -%> - <%_ let wrappedStaticMethodsForMethodType = allStaticMethodsForMethodType.map(wrapMethod).filter({ $0.wrappedInMethodType() }) -%> - <%_ for variable in allVariables { propertyRegister(variable) } -%> - <%_ for variable in allStaticVariables { propertyRegister(variable) } -%> - <%_ for method in wrappedMethods { method.register() } -%> - <%_ for wrapped in wrappedSubscripts { wrapped.register() } -%> - <%_ for method in wrappedStaticMethods { method.register() } -%><%_ -%> - <%_ let variableCasesCount: Int = wrappedVariables.reduce(0) { return $0 + $1.casesCount } -%><%_ -%> - <%_ let subscriptsCasesCount: Int = wrappedSubscripts.reduce(0) { return $0 + $1.casesCount } -%><%_ -%> - <%_ let staticVariableCasesCount: Int = wrappedStaticVariables.reduce(0) { return $0 + $1.casesCount } -%><%_ -%> - <%_ let generateOldAccessorsForMethods: Bool = !areThereCollisions(between: wrappedMethods) -%><%_ -%> - <%_ let generateOldAccessorsForStaticMethods: Bool = !areThereCollisions(between: wrappedStaticMethods) -%><%_ -%> - - <%# ================================================== STATIC STUBS -%><%_ -%> - <%_ for method in wrappedStaticMethods { -%> - <%= method.functionPrototype _%> { - <%= method.stubBody() _%> - } - - <%_ } %><%_ -%> - <%_ -%> - <%# ================================================== INITIALIZERS -%><%_ -%> - <%_ for method in wrappedInitializers { -%> - <%= method.functionPrototype _%> { } - - <%_ } -%><%_ -%> - <%_ -%><%_ -%> - <%# ================================================== STUBS -%><%_ -%> - <%_ for method in wrappedMethods { -%> - <%= method.functionPrototype _%> { - <%= method.stubBody() _%> - } - - <%_ } -%> - <%_ for wrapped in wrappedSubscripts { -%> - <%= wrapped.subscriptCall() _%> - - <%_ } -%> - <%# ================================================== STATIC METHOD TYPE -%><%_ -%> - <%_ if conformsToStaticMock { -%> - fileprivate enum StaticMethodType { - <%_ for method in wrappedStaticMethodsForMethodType { -%> - case <%= method.methodTypeDeclarationWithParameters() _%> - <%_ } %> <%_ for variable in allStaticVariables { -%> - <%= propertyMethodTypes(variable) %> - <%_ } %> <%_ %> - <%_ -%> - static func compareParameters(lhs: StaticMethodType, rhs: StaticMethodType, matcher: Matcher) -> Bool { - switch (lhs, rhs) { <%_ for method in wrappedStaticMethodsForMethodType { %> - <%= method.equalCase -%><% for parameter in method.parameters { %> - <%= parameter.comparator -%> <% } %> - return true <% } %> - <%_ for variable in allStaticVariables { -%> - <%= propertyMethodTypesCompare(variable) %> - <%_ } %> <%_ -%> <%_ if wrappedStaticMethods.count + staticVariableCasesCount > 1 { -%> - default: return false - <%_ } -%> - } - } - <%_ %> - func intValue() -> Int { - switch self { <%_ for method in wrappedStaticMethodsForMethodType { %> - <%= method.intValueCase -%><% } %> - <%_ for variable in allStaticVariables { -%> - <%= propertyMethodTypesIntValue(variable) %> - <%_ } %> <%_ -%> - } - } - } - - class StaticGiven: StubbedMethod { - fileprivate var method: StaticMethodType - - private init(method: StaticMethodType, products: [StubProduct]) { - self.method = method - super.init(products) - } - - <%_ for variable in allStaticVariables { -%> - <%= wrapProperty(variable).givenConstructorName(prefix: "Static") -%> { - <%= wrapProperty(variable).givenConstructor(prefix: "Static") _%> - } - <%_ } %> <%_ %> - <%_ for method in wrappedStaticMethodsForMethodType.filter({ !$0.method.returnTypeName.isVoid && !$0.method.isInitializer }) { -%> - <%= method.givenConstructorName(prefix: "Static") -%> { - <%= method.givenConstructor(prefix: "Static") _%> - } - <%_ if generateOldAccessorsForStaticMethods, method.containsEmptyArgumentLabels() { -%> - <%= method.givenConstructorName(prefix: "Static", deprecated: true) %> { - <%= method.givenConstructor(prefix: "Static") _%> - } - <%_ } -%> - <%_ } -%> - <%_ for method in wrappedStaticMethodsForMethodType.filter({ !$0.method.throws && !$0.method.rethrows && !$0.method.returnTypeName.isVoid && !$0.method.isInitializer }) { -%> - <%= method.givenProduceConstructorName(prefix: "Static") -%> { - <%= method.givenProduceConstructor(prefix: "Static") _%> - } - <%_ } -%> - <%_ for method in wrappedStaticMethodsForMethodType.filter({ ($0.method.throws || $0.method.rethrows) && !$0.method.isInitializer }) { -%> - <%= method.givenConstructorNameThrows(prefix: "Static") -%> { - <%= method.givenConstructorThrows(prefix: "Static") _%> - } - <%_ if generateOldAccessorsForStaticMethods, method.containsEmptyArgumentLabels() { -%> - <%= method.givenConstructorNameThrows(prefix: "Static", deprecated: true) %> { - <%= method.givenConstructorThrows(prefix: "Static") _%> - } - <%_ } -%> - <%= method.givenProduceConstructorNameThrows(prefix: "Static") -%> { - <%= method.givenProduceConstructorThrows(prefix: "Static") _%> - } - <%_ } %> <%_ -%> - } - - struct StaticVerify { - fileprivate var method: StaticMethodType - - <%_ for method in wrappedStaticMethodsForMethodType { -%> - <%= method.verificationProxyConstructorName(prefix: "Static") -%> { <%= method.verificationProxyConstructor(prefix: "Static") _%> } - <%_ if generateOldAccessorsForStaticMethods, method.containsEmptyArgumentLabels() { -%> - <%= method.verificationProxyConstructorName(prefix: "Static", deprecated: true) %> { <%= method.verificationProxyConstructor(prefix: "Static") _%> } - <%_ } -%> - <%_ } %> <%_ -%> - <%_ for variable in allStaticVariables { -%> - <%= propertyTypes(variable) %> - <%_ } %> <%_ -%> - } - - struct StaticPerform { - fileprivate var method: StaticMethodType - var performs: Any - - <%_ for method in wrappedStaticMethodsForMethodType { -%> - <%= method.performProxyConstructorName(prefix: "Static") -%> { - <%= method.performProxyConstructor(prefix: "Static") _%> - } - <%_ if generateOldAccessorsForStaticMethods, method.containsEmptyArgumentLabels() { -%> - <%= method.performProxyConstructorName(prefix: "Static", deprecated: true) %> { - <%= method.performProxyConstructor(prefix: "Static") _%> - } - <%_ } -%> - <%_ } %> <%_ -%> - } - - <% } -%> - <%# ================================================== METHOD TYPE -%><%_ -%> - <%_ if !wrappedMethods.isEmpty || !allVariables.isEmpty || !allSubscripts.isEmpty { -%> - - fileprivate enum MethodType { - <%_ for method in wrappedMethodsForMethodType { -%> - case <%= method.methodTypeDeclarationWithParameters() _%> - <%_ } -%> <%_ for variable in allVariables { -%> - <%= propertyMethodTypes(variable) %> - <%_ } %> <%_ %> <%_ for wrapped in wrappedSubscripts { -%> - <%= wrapped.subscriptCases() _%> - <%_ } %> <%_ %> - <%_ -%> - static func compareParameters(lhs: MethodType, rhs: MethodType, matcher: Matcher) -> Bool { - switch (lhs, rhs) { <%_ for method in wrappedMethodsForMethodType { %> - <%= method.equalCase -%><% for parameter in method.parameters { %> - <%= parameter.comparator -%> <% } %> - return true <% } %> - <%_ for variable in allVariables { -%> - <%= propertyMethodTypesCompare(variable) %> - <%_ } %> <%_ -%> <%_ for wrapped in wrappedSubscripts { -%> - <%= wrapped.equalCases() %> - <%_ } %> <%_ if wrappedMethods.count + variableCasesCount + subscriptsCasesCount > 1 { -%> - default: return false - <%_ } -%> - } - } - <%_ %> - func intValue() -> Int { - switch self { <%_ for method in wrappedMethodsForMethodType { %> - <%= method.intValueCase -%><% } %> - <%_ for variable in allVariables { -%> - <%= propertyMethodTypesIntValue(variable) %> - <%_ } %> <%_ for wrapped in wrappedSubscripts { -%> - <%= wrapped.intValueCase() %> - <%_ } -%> - } - } - } - <%_ } else { %> - fileprivate struct MethodType { - static func compareParameters(lhs: MethodType, rhs: MethodType, matcher: Matcher) -> Bool { return true } - func intValue() -> Int { return 0 } - } - <%_ } -%><%_ -%> - - class Given: StubbedMethod { - fileprivate var method: MethodType - - private init(method: MethodType, products: [StubProduct]) { - self.method = method - super.init(products) - } - - <%_ for variable in allVariables { -%> - <%= wrapProperty(variable).givenConstructorName() -%> { - <%= wrapProperty(variable).givenConstructor() _%> - } - <%_ } %> <%_ %> - <%_ for method in wrappedMethodsForMethodType.filter({ !$0.method.returnTypeName.isVoid && !$0.method.isInitializer }) { -%> - <%= method.givenConstructorName() -%> { - <%= method.givenConstructor() _%> - } - <%_ if generateOldAccessorsForMethods, method.containsEmptyArgumentLabels() { -%> - <%= method.givenConstructorName(prefix: "", deprecated: true) %> { - <%= method.givenConstructor() _%> - } - <%_ } -%> - <%_ } -%> - <%_ for method in wrappedMethodsForMethodType.filter({ !$0.method.throws && !$0.method.rethrows && !$0.method.returnTypeName.isVoid && !$0.method.isInitializer }) { -%> - <%= method.givenProduceConstructorName() -%> { - <%= method.givenProduceConstructor() _%> - } - <%_ } -%> - <%_ for wrapped in wrappedSubscripts { -%> - <%= wrapped.givenConstructorName() -%> { - <%= wrapped.givenConstructor() _%> - } - <%_ } -%> - <%_ for method in wrappedMethodsForMethodType.filter({ ($0.method.throws || $0.method.rethrows) && !$0.method.isInitializer }) { -%> - <%= method.givenConstructorNameThrows() -%> { - <%= method.givenConstructorThrows() _%> - } - <%_ if generateOldAccessorsForMethods, method.containsEmptyArgumentLabels() { -%> - <%= method.givenConstructorNameThrows(prefix: "", deprecated: true) %> { - <%= method.givenConstructorThrows() _%> - } - <%_ } -%> - <%= method.givenProduceConstructorNameThrows() -%> { - <%= method.givenProduceConstructorThrows() _%> - } - <%_ } %> <%_ -%> - } - - struct Verify { - fileprivate var method: MethodType - - <%_ for method in wrappedMethodsForMethodType { -%> - <%= method.verificationProxyConstructorName() -%> { <%= method.verificationProxyConstructor() _%> } - <%_ if generateOldAccessorsForMethods, method.containsEmptyArgumentLabels() { -%> - <%= method.verificationProxyConstructorName(prefix: "", deprecated: true) %> { <%= method.verificationProxyConstructor() _%> } - <%_ } -%> - <%_ } %> <%_ -%> - <%_ for variable in allVariables { -%> - <%= propertyTypes(variable) %> - <%_ } %> <%_ -%> - <%_ for wrapped in wrappedSubscripts { -%> - <%= wrapped.verifyConstructorName() -%> { <%= wrapped.verifyConstructor() _%> } - <%_ if !wrapped.readonly { -%> - <%= wrapped.verifyConstructorName(set: true) -%> { <%= wrapped.verifyConstructor(set: true) _%> } - <%_ } -%> - <%_ } %> <%_ -%> - } - - struct Perform { - fileprivate var method: MethodType - var performs: Any - - <%_ for method in wrappedMethodsForMethodType { -%> - <%= method.performProxyConstructorName() -%> { - <%= method.performProxyConstructor() _%> - } - <%_ if generateOldAccessorsForMethods, method.containsEmptyArgumentLabels() { -%> - <%= method.performProxyConstructorName(prefix: "", deprecated: true) %> { - <%= method.performProxyConstructor() _%> - } - <%_ } -%> - <%_ } %> <%_ -%> - } - - <%# ================================================== MOCK METHODS -%><%_ -%> - public func given(_ method: Given) { - methodReturnValues.append(method) - } - - public func perform(_ method: Perform) { - methodPerformValues.append(method) - methodPerformValues.sort { $0.method.intValue() < $1.method.intValue() } - } - - public func verify(_ method: Verify, count: Count = Count.moreOrEqual(to: 1), file: StaticString = #file, line: UInt = #line) { - let invocations = matchingCalls(method.method) - MockyAssert(count.matches(invocations.count), "Expected: \(count) invocations of `\(method.method)`, but was: \(invocations.count)", file: file, line: line) - } - - private func addInvocation(_ call: MethodType) { - invocations.append(call) - } - private func methodReturnValue(_ method: MethodType) throws -> StubProduct { - let candidates = sequencingPolicy.sorted(methodReturnValues, by: { $0.method.intValue() > $1.method.intValue() }) - let matched = candidates.first(where: { $0.isValid && MethodType.compareParameters(lhs: $0.method, rhs: method, matcher: matcher) }) - guard let product = matched?.getProduct(policy: self.stubbingPolicy) else { throw MockError.notStubed } - return product - } - private func methodPerformValue(_ method: MethodType) -> Any? { - let matched = methodPerformValues.reversed().first { MethodType.compareParameters(lhs: $0.method, rhs: method, matcher: matcher) } - return matched?.performs - } - private func matchingCalls(_ method: MethodType) -> [MethodType] { - return invocations.filter { MethodType.compareParameters(lhs: $0, rhs: method, matcher: matcher) } - } - private func matchingCalls(_ method: Verify) -> Int { - return matchingCalls(method.method).count - } - private func givenGetterValue(_ method: MethodType, _ message: String) -> T { - do { - return try methodReturnValue(method).casted() - } catch { - onFatalFailure(message) - Failure(message) - } - } - private func optionalGivenGetterValue(_ method: MethodType, _ message: String) -> T? { - do { - return try methodReturnValue(method).casted() - } catch { - return nil - } - } - private func onFatalFailure(_ message: String) { - #if Mocky - guard let file = self.file, let line = self.line else { return } // Let if fail if cannot handle gratefully - SwiftyMockyTestObserver.handleMissingStubError(message: message, file: file, line: line) - #endif - } - <%# ================================================== STATIC MOCK METHODS -%><%_ -%> - <%_ if conformsToStaticMock { -%> - - static public func given(_ method: StaticGiven) { - methodReturnValues.append(method) - } - - static public func perform(_ method: StaticPerform) { - methodPerformValues.append(method) - methodPerformValues.sort { $0.method.intValue() < $1.method.intValue() } - } - - static public func verify(_ method: StaticVerify, count: Count = Count.moreOrEqual(to: 1), file: StaticString = #file, line: UInt = #line) { - let invocations = matchingCalls(method.method) - MockyAssert(count.matches(invocations.count), "Expected: \(count) invocations of `\(method.method)`, but was: \(invocations.count)", file: file, line: line) - } - - static private func addInvocation(_ call: StaticMethodType) { - invocations.append(call) - } - static private func methodReturnValue(_ method: StaticMethodType) throws -> StubProduct { - let candidates = sequencingPolicy.sorted(methodReturnValues, by: { $0.method.intValue() > $1.method.intValue() }) - let matched = candidates.first(where: { $0.isValid && StaticMethodType.compareParameters(lhs: $0.method, rhs: method, matcher: matcher) }) - guard let product = matched?.getProduct(policy: self.stubbingPolicy) else { throw MockError.notStubed } - return product - } - static private func methodPerformValue(_ method: StaticMethodType) -> Any? { - let matched = methodPerformValues.reversed().first { StaticMethodType.compareParameters(lhs: $0.method, rhs: method, matcher: matcher) } - return matched?.performs - } - static private func matchingCalls(_ method: StaticMethodType) -> [StaticMethodType] { - return invocations.filter { StaticMethodType.compareParameters(lhs: $0, rhs: method, matcher: matcher) } - } - static private func matchingCalls(_ method: StaticVerify) -> Int { - return matchingCalls(method.method).count - } - static private func givenGetterValue(_ method: StaticMethodType, _ message: String) -> T { - do { - return try methodReturnValue(method).casted() - } catch { - Failure(message) - } - } - static private func optionalGivenGetterValue(_ method: StaticMethodType, _ message: String) -> T? { - do { - return try methodReturnValue(method).casted() - } catch { - return nil - } - } - <%_ } -%> -<%_ if autoMockable { -%> -} - -<%_ } else { -%> -// sourcery:end -<%_ } -%> -<% } -%> diff --git a/get_sourcery.sh b/get_sourcery.sh index f4fc9b16..c178d3c5 100644 --- a/get_sourcery.sh +++ b/get_sourcery.sh @@ -1,7 +1,8 @@ -[[ $# > 0 ]] && VERSION="$1" || VERSION="4.2" +[[ $# > 0 ]] && VERSION="$1" || VERSION="5.0" [[ $# > 1 ]] && OUTPUT="$2" || OUTPUT="./Pods/Sourcery/bin" +SOURCERY_VERSION="0.16.0" # The version of Sourcery that is associated with this SwiftyMocky version -echo "CLONE SOURCERY FOR $VERSION INTO $OUTPUT" +echo "CLONE SOURCERY $SOURCERY_VERSION FOR Swift $VERSION INTO $OUTPUT" rm -r -f "$OUTPUT" -git clone -b "swift/$VERSION" --single-branch --depth 1 https://github.com/MakeAWishFoundation/SwiftyMocky.wiki.git "$OUTPUT" +git clone -b "sourcery/$SOURCERY_VERSION-swift$VERSION" --single-branch --depth 1 https://github.com/MakeAWishFoundation/SwiftyMocky.wiki.git "$OUTPUT"