diff --git a/MockGenerator/MockGenerator.swift b/MockGenerator/MockGenerator.swift index f4537fe..14bec62 100644 --- a/MockGenerator/MockGenerator.swift +++ b/MockGenerator/MockGenerator.swift @@ -18,7 +18,7 @@ extension MockGenerator { guard !isEmpty(mockClass: mockClass) else { throw error("Could not find a class or protocol on \(element.name)") } - let mockLines = getMockBody(from: mockClass) + let mockLines = getMockBody(from: mockClass, element: element) guard !mockLines.isEmpty else { throw error("Found inherited types but there was nothing to mock") } @@ -40,9 +40,9 @@ extension MockGenerator { return mockClass.protocols.isEmpty && mockClass.inheritedClass == nil } - private func getMockBody(from mockClass: MockClass) -> [String] { + private func getMockBody(from mockClass: MockClass, element: TypeDeclaration) -> [String] { let view = CallbackMockView { [templateName] model in - let view = MustacheView(templateName: templateName) + let view = MustacheView(templateName: templateName, element: element) view.render(model: model) return view.result } diff --git a/MockGenerator/MustacheView.swift b/MockGenerator/MustacheView.swift index 76d4e86..3c0122c 100644 --- a/MockGenerator/MustacheView.swift +++ b/MockGenerator/MustacheView.swift @@ -1,22 +1,55 @@ import UseCases import GRMustache import Foundation +import AST class MustacheView: NSObject, MockView { + typealias ModelDictionary = [String: Any] + private(set) var result = "" private let templateName: String + private let element: AST.TypeDeclaration - init(templateName: String) { + init(templateName: String, element: AST.TypeDeclaration) { self.templateName = templateName + self.element = element } func render(model: MockViewModel) { do { let template = try GRMustacheTemplate(fromResource: templateName, bundle: Bundle(for: MustacheView.self)) - result = try template.renderObject(model.toDictionary()) + let dict = injectAdditionalValues(model.toDictionary() as! ModelDictionary) + result = try template.renderObject(dict) } catch { } // ignored } + + private func injectAdditionalValues(_ dict: ModelDictionary) -> ModelDictionary { + let scope: String? = { + if element.hasOpenModifier { + return "open" + } else if element.hasPublicModifier { + return "public" + } else if element.hasInternalModifier { + return "internal" + } else if element.hasPrivateModifier || element.hasFilePrivateModifier { + return "fileprivate" + } else { + return nil + } + }() + + var result = dict + result["scope"] = scope + + let initializers = (dict["initializer"] as? [Any]) ?? [] + + if (scope == "open" || scope == "public") && initializers.isEmpty { + result["publicInitializer"] = NSNumber(true) + } + + return result + } } extension MockViewModel { @@ -27,9 +60,6 @@ extension MockViewModel { dictionary["property"] = property.map { $0.toDictionary() } dictionary["method"] = method.map { $0.toDictionary() } dictionary["subscript"] = `subscript`.map { $0.toDictionary() } - if let scope = scope { - dictionary["scope"] = scope - } return dictionary } } diff --git a/MockGenerator/dummy.mustache b/MockGenerator/dummy.mustache index c775b2c..799c1d6 100644 --- a/MockGenerator/dummy.mustache +++ b/MockGenerator/dummy.mustache @@ -1,15 +1,21 @@ {{#initializer}} - {{{declarationText}}} { + {{scope}} {{{declarationText}}} { {{{initializerCall}}} } {{/initializer}} +{{#publicInitializer}} + {{scope}} init() { + fatalError("Not implemented") + } +{{/publicInitializer}} + {{#property}} {{#hasSetter}} - {{{declarationText}}} { + {{scope}} {{{declarationText}}} { set { } get { @@ -21,7 +27,7 @@ {{/hasSetter}} {{^hasSetter}} - {{{declarationText}}} { + {{scope}} {{{declarationText}}} { {{#defaultValue}}return {{{defaultValue}}}{{/defaultValue}} {{^defaultValue}}fatalError("Dummy implementation"){{/defaultValue}} } @@ -32,7 +38,7 @@ {{#subscript}} - {{{declarationText}}} { + {{scope}} {{{declarationText}}} { {{#hasSetter}} set { @@ -51,7 +57,7 @@ {{#method}} - {{{declarationText}}} { + {{scope}} {{{declarationText}}} { {{#resultType}} {{#defaultValue}}return {{{defaultValue}}}{{/defaultValue}} diff --git a/MockGenerator/partial.mustache b/MockGenerator/partial.mustache index 1bf4dd0..dbff340 100644 --- a/MockGenerator/partial.mustache +++ b/MockGenerator/partial.mustache @@ -1,9 +1,15 @@ {{#initializer}} - {{{declarationText}}} { + {{scope}} {{{declarationText}}} { {{{initializerCall}}} } {{/initializer}} +{{#publicInitializer}} + {{scope}} init() { + fatalError("Not implemented") + } +{{/publicInitializer}} + {{#property}} {{#hasSetter}} @@ -20,7 +26,7 @@ {{scope}} var forwardToOriginal{{capitalizedUniqueName}} = false {{/isImplemented}} - {{{declarationText}}} { + {{scope}} {{{declarationText}}} { {{#hasSetter}} set { @@ -85,7 +91,7 @@ {{scope}} var forwardToOriginal{{capitalizedUniqueName}} = false {{/isImplemented}} - {{{declarationText}}} { + {{scope}} {{{declarationText}}} { {{#hasSetter}} set { @@ -167,7 +173,7 @@ {{scope}} var forwardToOriginal{{capitalizedUniqueName}} = false {{/isImplemented}} - {{{declarationText}}} { + {{scope}} {{{declarationText}}} { invoked{{capitalizedUniqueName}} = true invoked{{capitalizedUniqueName}}Count += 1 diff --git a/MockGenerator/spy.mustache b/MockGenerator/spy.mustache index 38fa938..7cebcc7 100644 --- a/MockGenerator/spy.mustache +++ b/MockGenerator/spy.mustache @@ -1,9 +1,15 @@ {{#initializer}} - {{{declarationText}}} { + {{scope}} {{{declarationText}}} { {{{initializerCall}}} } {{/initializer}} +{{#publicInitializer}} + {{scope}} init() { + fatalError("Not implemented") + } +{{/publicInitializer}} + {{#property}} {{#hasSetter}} @@ -16,7 +22,7 @@ {{scope}} var invoked{{capitalizedUniqueName}}GetterCount = 0 {{scope}} var stubbed{{capitalizedUniqueName}}: {{{iuoType}}} {{{defaultValueAssignment}}} - {{{declarationText}}} { + {{scope}} {{{declarationText}}} { {{#hasSetter}} set { @@ -61,7 +67,7 @@ {{scope}} var invoked{{capitalizedUniqueName}}List = [{{{resultType.type}}}]() {{/hasSetter}} - {{{declarationText}}} { + {{scope}} {{{declarationText}}} { {{#hasSetter}} set { @@ -124,7 +130,7 @@ {{scope}} var stubbed{{capitalizedUniqueName}}Result: {{{iuoType}}} {{{defaultValueAssignment}}} {{/resultType}} - {{{declarationText}}} { + {{scope}} {{{declarationText}}} { invoked{{capitalizedUniqueName}} = true invoked{{capitalizedUniqueName}}Count += 1 diff --git a/MockGenerator/stub.mustache b/MockGenerator/stub.mustache index 296283b..6d5813a 100644 --- a/MockGenerator/stub.mustache +++ b/MockGenerator/stub.mustache @@ -1,14 +1,20 @@ {{#initializer}} - {{{declarationText}}} { + {{scope}} {{{declarationText}}} { {{{initializerCall}}} } {{/initializer}} +{{#publicInitializer}} + {{scope}} init() { + fatalError("Not implemented") + } +{{/publicInitializer}} + {{#property}} {{scope}} var stubbed{{capitalizedUniqueName}}: {{{iuoType}}} {{{defaultValueAssignment}}} - {{{declarationText}}} { + {{scope}} {{{declarationText}}} { {{#hasSetter}} set { @@ -29,7 +35,7 @@ {{scope}} var stubbed{{capitalizedUniqueName}}Result: {{{resultType.iuoType}}} {{{resultType.defaultValueAssignment}}} - {{{declarationText}}} { + {{scope}} {{{declarationText}}} { {{#hasSetter}} set { @@ -51,7 +57,7 @@ {{scope}} var stubbed{{capitalizedUniqueName}}Result: {{{iuoType}}} {{{defaultValueAssignment}}} {{/resultType}} - {{{declarationText}}} { + {{scope}} {{{declarationText}}} { {{#resultType}} return stubbed{{capitalizedUniqueName}}Result{{returnCastStatement}}