From f5371f0a5c77dc4299ad0fce822ae588bcb793aa Mon Sep 17 00:00:00 2001 From: Anton Pratsko Date: Tue, 20 Sep 2022 13:38:38 +0300 Subject: [PATCH 1/2] Add scope --- MockGenerator/MockGenerator.swift | 6 +++--- MockGenerator/MustacheView.swift | 26 +++++++++++++++++++++----- MockGenerator/dummy.mustache | 10 +++++----- MockGenerator/partial.mustache | 8 ++++---- MockGenerator/spy.mustache | 8 ++++---- MockGenerator/stub.mustache | 8 ++++---- 6 files changed, 41 insertions(+), 25 deletions(-) diff --git a/MockGenerator/MockGenerator.swift b/MockGenerator/MockGenerator.swift index f4537fe6..14bec622 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 76d4e864..77fc2a3d 100644 --- a/MockGenerator/MustacheView.swift +++ b/MockGenerator/MustacheView.swift @@ -1,22 +1,41 @@ import UseCases import GRMustache import Foundation +import AST class MustacheView: NSObject, MockView { 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()) + var dict = model.toDictionary().mutableCopy() as! NSMutableDictionary + dict["scope"] = getScope() + result = try template.renderObject(dict) } catch { } // ignored } + + private func getScope() -> 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 + } + } } extension MockViewModel { @@ -27,9 +46,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 c775b2cb..fc0f1869 100644 --- a/MockGenerator/dummy.mustache +++ b/MockGenerator/dummy.mustache @@ -1,5 +1,5 @@ {{#initializer}} - {{{declarationText}}} { + {{scope}} {{{declarationText}}} { {{{initializerCall}}} } {{/initializer}} @@ -9,7 +9,7 @@ {{#hasSetter}} - {{{declarationText}}} { + {{scope}} {{{declarationText}}} { set { } get { @@ -21,7 +21,7 @@ {{/hasSetter}} {{^hasSetter}} - {{{declarationText}}} { + {{scope}} {{{declarationText}}} { {{#defaultValue}}return {{{defaultValue}}}{{/defaultValue}} {{^defaultValue}}fatalError("Dummy implementation"){{/defaultValue}} } @@ -32,7 +32,7 @@ {{#subscript}} - {{{declarationText}}} { + {{scope}} {{{declarationText}}} { {{#hasSetter}} set { @@ -51,7 +51,7 @@ {{#method}} - {{{declarationText}}} { + {{scope}} {{{declarationText}}} { {{#resultType}} {{#defaultValue}}return {{{defaultValue}}}{{/defaultValue}} diff --git a/MockGenerator/partial.mustache b/MockGenerator/partial.mustache index 1bf4dd02..7e4fa788 100644 --- a/MockGenerator/partial.mustache +++ b/MockGenerator/partial.mustache @@ -1,5 +1,5 @@ {{#initializer}} - {{{declarationText}}} { + {{scope}} {{{declarationText}}} { {{{initializerCall}}} } {{/initializer}} @@ -20,7 +20,7 @@ {{scope}} var forwardToOriginal{{capitalizedUniqueName}} = false {{/isImplemented}} - {{{declarationText}}} { + {{scope}} {{{declarationText}}} { {{#hasSetter}} set { @@ -85,7 +85,7 @@ {{scope}} var forwardToOriginal{{capitalizedUniqueName}} = false {{/isImplemented}} - {{{declarationText}}} { + {{scope}} {{{declarationText}}} { {{#hasSetter}} set { @@ -167,7 +167,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 38fa9386..24eb9b82 100644 --- a/MockGenerator/spy.mustache +++ b/MockGenerator/spy.mustache @@ -1,5 +1,5 @@ {{#initializer}} - {{{declarationText}}} { + {{scope}} {{{declarationText}}} { {{{initializerCall}}} } {{/initializer}} @@ -16,7 +16,7 @@ {{scope}} var invoked{{capitalizedUniqueName}}GetterCount = 0 {{scope}} var stubbed{{capitalizedUniqueName}}: {{{iuoType}}} {{{defaultValueAssignment}}} - {{{declarationText}}} { + {{scope}} {{{declarationText}}} { {{#hasSetter}} set { @@ -61,7 +61,7 @@ {{scope}} var invoked{{capitalizedUniqueName}}List = [{{{resultType.type}}}]() {{/hasSetter}} - {{{declarationText}}} { + {{scope}} {{{declarationText}}} { {{#hasSetter}} set { @@ -124,7 +124,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 296283b5..0f03a60f 100644 --- a/MockGenerator/stub.mustache +++ b/MockGenerator/stub.mustache @@ -1,5 +1,5 @@ {{#initializer}} - {{{declarationText}}} { + {{scope}} {{{declarationText}}} { {{{initializerCall}}} } {{/initializer}} @@ -8,7 +8,7 @@ {{scope}} var stubbed{{capitalizedUniqueName}}: {{{iuoType}}} {{{defaultValueAssignment}}} - {{{declarationText}}} { + {{scope}} {{{declarationText}}} { {{#hasSetter}} set { @@ -29,7 +29,7 @@ {{scope}} var stubbed{{capitalizedUniqueName}}Result: {{{resultType.iuoType}}} {{{resultType.defaultValueAssignment}}} - {{{declarationText}}} { + {{scope}} {{{declarationText}}} { {{#hasSetter}} set { @@ -51,7 +51,7 @@ {{scope}} var stubbed{{capitalizedUniqueName}}Result: {{{iuoType}}} {{{defaultValueAssignment}}} {{/resultType}} - {{{declarationText}}} { + {{scope}} {{{declarationText}}} { {{#resultType}} return stubbed{{capitalizedUniqueName}}Result{{returnCastStatement}} From a0e7a15ed9672777298ea967e97341164051e6ee Mon Sep 17 00:00:00 2001 From: Anton Pratsko Date: Tue, 20 Sep 2022 17:55:35 +0300 Subject: [PATCH 2/2] Add init --- MockGenerator/MustacheView.swift | 40 +++++++++++++++++++++----------- MockGenerator/dummy.mustache | 6 +++++ MockGenerator/partial.mustache | 6 +++++ MockGenerator/spy.mustache | 6 +++++ MockGenerator/stub.mustache | 6 +++++ 5 files changed, 51 insertions(+), 13 deletions(-) diff --git a/MockGenerator/MustacheView.swift b/MockGenerator/MustacheView.swift index 77fc2a3d..3c0122c0 100644 --- a/MockGenerator/MustacheView.swift +++ b/MockGenerator/MustacheView.swift @@ -5,6 +5,8 @@ import AST class MustacheView: NSObject, MockView { + typealias ModelDictionary = [String: Any] + private(set) var result = "" private let templateName: String private let element: AST.TypeDeclaration @@ -17,24 +19,36 @@ class MustacheView: NSObject, MockView { func render(model: MockViewModel) { do { let template = try GRMustacheTemplate(fromResource: templateName, bundle: Bundle(for: MustacheView.self)) - var dict = model.toDictionary().mutableCopy() as! NSMutableDictionary - dict["scope"] = getScope() + let dict = injectAdditionalValues(model.toDictionary() as! ModelDictionary) result = try template.renderObject(dict) } catch { } // ignored } - private func getScope() -> 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 + 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 } } diff --git a/MockGenerator/dummy.mustache b/MockGenerator/dummy.mustache index fc0f1869..799c1d6e 100644 --- a/MockGenerator/dummy.mustache +++ b/MockGenerator/dummy.mustache @@ -4,6 +4,12 @@ } {{/initializer}} +{{#publicInitializer}} + {{scope}} init() { + fatalError("Not implemented") + } +{{/publicInitializer}} + {{#property}} diff --git a/MockGenerator/partial.mustache b/MockGenerator/partial.mustache index 7e4fa788..dbff340e 100644 --- a/MockGenerator/partial.mustache +++ b/MockGenerator/partial.mustache @@ -4,6 +4,12 @@ } {{/initializer}} +{{#publicInitializer}} + {{scope}} init() { + fatalError("Not implemented") + } +{{/publicInitializer}} + {{#property}} {{#hasSetter}} diff --git a/MockGenerator/spy.mustache b/MockGenerator/spy.mustache index 24eb9b82..7cebcc72 100644 --- a/MockGenerator/spy.mustache +++ b/MockGenerator/spy.mustache @@ -4,6 +4,12 @@ } {{/initializer}} +{{#publicInitializer}} + {{scope}} init() { + fatalError("Not implemented") + } +{{/publicInitializer}} + {{#property}} {{#hasSetter}} diff --git a/MockGenerator/stub.mustache b/MockGenerator/stub.mustache index 0f03a60f..6d5813ac 100644 --- a/MockGenerator/stub.mustache +++ b/MockGenerator/stub.mustache @@ -4,6 +4,12 @@ } {{/initializer}} +{{#publicInitializer}} + {{scope}} init() { + fatalError("Not implemented") + } +{{/publicInitializer}} + {{#property}} {{scope}} var stubbed{{capitalizedUniqueName}}: {{{iuoType}}} {{{defaultValueAssignment}}}