Skip to content

Add access modifiers and init #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions MockGenerator/MockGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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
}
Expand Down
40 changes: 35 additions & 5 deletions MockGenerator/MustacheView.swift
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
}
}
Expand Down
16 changes: 11 additions & 5 deletions MockGenerator/dummy.mustache
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
{{#initializer}}
{{{declarationText}}} {
{{scope}} {{{declarationText}}} {
{{{initializerCall}}}
}
{{/initializer}}

{{#publicInitializer}}
{{scope}} init() {
fatalError("Not implemented")
}
{{/publicInitializer}}

{{#property}}


{{#hasSetter}}

{{{declarationText}}} {
{{scope}} {{{declarationText}}} {
set {
}
get {
Expand All @@ -21,7 +27,7 @@
{{/hasSetter}}
{{^hasSetter}}

{{{declarationText}}} {
{{scope}} {{{declarationText}}} {
{{#defaultValue}}return {{{defaultValue}}}{{/defaultValue}}
{{^defaultValue}}fatalError("Dummy implementation"){{/defaultValue}}
}
Expand All @@ -32,7 +38,7 @@

{{#subscript}}

{{{declarationText}}} {
{{scope}} {{{declarationText}}} {

{{#hasSetter}}
set {
Expand All @@ -51,7 +57,7 @@

{{#method}}

{{{declarationText}}} {
{{scope}} {{{declarationText}}} {

{{#resultType}}
{{#defaultValue}}return {{{defaultValue}}}{{/defaultValue}}
Expand Down
14 changes: 10 additions & 4 deletions MockGenerator/partial.mustache
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
{{#initializer}}
{{{declarationText}}} {
{{scope}} {{{declarationText}}} {
{{{initializerCall}}}
}
{{/initializer}}

{{#publicInitializer}}
{{scope}} init() {
fatalError("Not implemented")
}
{{/publicInitializer}}

{{#property}}

{{#hasSetter}}
Expand All @@ -20,7 +26,7 @@
{{scope}} var forwardToOriginal{{capitalizedUniqueName}} = false
{{/isImplemented}}

{{{declarationText}}} {
{{scope}} {{{declarationText}}} {

{{#hasSetter}}
set {
Expand Down Expand Up @@ -85,7 +91,7 @@
{{scope}} var forwardToOriginal{{capitalizedUniqueName}} = false
{{/isImplemented}}

{{{declarationText}}} {
{{scope}} {{{declarationText}}} {

{{#hasSetter}}
set {
Expand Down Expand Up @@ -167,7 +173,7 @@
{{scope}} var forwardToOriginal{{capitalizedUniqueName}} = false
{{/isImplemented}}

{{{declarationText}}} {
{{scope}} {{{declarationText}}} {

invoked{{capitalizedUniqueName}} = true
invoked{{capitalizedUniqueName}}Count += 1
Expand Down
14 changes: 10 additions & 4 deletions MockGenerator/spy.mustache
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
{{#initializer}}
{{{declarationText}}} {
{{scope}} {{{declarationText}}} {
{{{initializerCall}}}
}
{{/initializer}}

{{#publicInitializer}}
{{scope}} init() {
fatalError("Not implemented")
}
{{/publicInitializer}}

{{#property}}

{{#hasSetter}}
Expand All @@ -16,7 +22,7 @@
{{scope}} var invoked{{capitalizedUniqueName}}GetterCount = 0
{{scope}} var stubbed{{capitalizedUniqueName}}: {{{iuoType}}} {{{defaultValueAssignment}}}

{{{declarationText}}} {
{{scope}} {{{declarationText}}} {

{{#hasSetter}}
set {
Expand Down Expand Up @@ -61,7 +67,7 @@
{{scope}} var invoked{{capitalizedUniqueName}}List = [{{{resultType.type}}}]()
{{/hasSetter}}

{{{declarationText}}} {
{{scope}} {{{declarationText}}} {

{{#hasSetter}}
set {
Expand Down Expand Up @@ -124,7 +130,7 @@
{{scope}} var stubbed{{capitalizedUniqueName}}Result: {{{iuoType}}} {{{defaultValueAssignment}}}
{{/resultType}}

{{{declarationText}}} {
{{scope}} {{{declarationText}}} {

invoked{{capitalizedUniqueName}} = true
invoked{{capitalizedUniqueName}}Count += 1
Expand Down
14 changes: 10 additions & 4 deletions MockGenerator/stub.mustache
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -29,7 +35,7 @@

{{scope}} var stubbed{{capitalizedUniqueName}}Result: {{{resultType.iuoType}}} {{{resultType.defaultValueAssignment}}}

{{{declarationText}}} {
{{scope}} {{{declarationText}}} {

{{#hasSetter}}
set {
Expand All @@ -51,7 +57,7 @@
{{scope}} var stubbed{{capitalizedUniqueName}}Result: {{{iuoType}}} {{{defaultValueAssignment}}}
{{/resultType}}

{{{declarationText}}} {
{{scope}} {{{declarationText}}} {

{{#resultType}}
return stubbed{{capitalizedUniqueName}}Result{{returnCastStatement}}
Expand Down