Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Kryštof Matěj committed May 10, 2024
2 parents d1886df + 9a02cd3 commit 495bd5f
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 71 deletions.
12 changes: 0 additions & 12 deletions Sources/Rubicon/Domain/AccessLevel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,6 @@ public enum AccessLevel {
case `internal`
case `private`

// TODO: DELETE ME LATER!
func makeClassString() -> String {
switch self {
case .public:
return "public "
case .internal:
return ""
case .private:
return "private "
}
}

func makeContentString() -> String {
switch self {
case .public:
Expand Down
2 changes: 1 addition & 1 deletion Sources/Rubicon/Generator/DummyGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ final class DummyGenerator {
setContent: ["fatalError()"]
)
})
content.append(initGenerator.makeCode(with: []))
content.append(initGenerator.makeCode(with: [], isAddingDefaultValueToOptionalsEnabled: false))

content += protocolType.functions.map {
return functionGenerator.makeCode(from: $0, content: ["fatalError()"])
Expand Down
30 changes: 13 additions & 17 deletions Sources/Rubicon/Generator/InitGenerator.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
protocol InitGenerator {
func makeCode(with variables: [VarDeclaration]) -> [String]
func makeCode(with variables: [VarDeclaration], isAddingDefaultValueToOptionalsEnabled: Bool) -> [String]
}

final class InitGeneratorImpl: InitGenerator {
Expand All @@ -20,14 +20,17 @@ final class InitGeneratorImpl: InitGenerator {
self.argumentGenerator = argumentGenerator
}

func makeCode(with variables: [VarDeclaration]) -> [String] {
let assignments = variables.compactMap(makeAssigment(of:))
func makeCode(with variables: [VarDeclaration], isAddingDefaultValueToOptionalsEnabled: Bool) -> [String] {
let assignments = variables.map(makeAssigment(of:))

guard !assignments.isEmpty || accessLevel == .public else {
return []
}

let initArguments = variables.compactMap(makeInitArgument(from:)).joined(separator: ", ")
let initArguments = variables.map { makeInitArgument(
from: $0,
isAddingDefaultValueToOptionalsEnabled: isAddingDefaultValueToOptionalsEnabled
) } .joined(separator: ", ")

return [
"\(accessLevelGenerator.makeContentAccessLevel())init(\(initArguments)) {"
Expand All @@ -36,20 +39,13 @@ final class InitGeneratorImpl: InitGenerator {
]
}

private func makeAssigment(of variable: VarDeclaration) -> String? {
if variable.type.isOptional {
return nil
} else {
return "self.\(variable.identifier) = \(variable.identifier)"
}
private func makeAssigment(of variable: VarDeclaration) -> String {
return "self.\(variable.identifier) = \(variable.identifier)"
}

private func makeInitArgument(from variable: VarDeclaration) -> String? {
if variable.type.isOptional {
return nil
} else {
let declaration = ArgumentDeclaration(name: variable.identifier, type: variable.type)
return argumentGenerator.makeCode(from: declaration)
}
private func makeInitArgument(from variable: VarDeclaration, isAddingDefaultValueToOptionalsEnabled: Bool) -> String {
let defaultValue = isAddingDefaultValueToOptionalsEnabled && variable.type.isOptional ? " = nil" : ""
let declaration = ArgumentDeclaration(name: variable.identifier, type: variable.type)
return argumentGenerator.makeCode(from: declaration) + defaultValue
}
}
11 changes: 9 additions & 2 deletions Sources/Rubicon/Generator/SpyGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ final class SpyGenerator {
private let initGenerator: InitGenerator
private let structGenerator: StructGenerator
private let accessLevelGenerator: AccessLevelGenerator
private var isInitWithOptionalsEnabled: Bool = false

init(
protocolGenerator: ProtocolGenerator,
Expand All @@ -25,7 +26,8 @@ final class SpyGenerator {
self.accessLevelGenerator = accessLevelGenerator
}

func generate(from protocolType: ProtocolDeclaration) -> String {
func generate(from protocolType: ProtocolDeclaration, isInitWithOptionalsEnabled: Bool) -> String {
self.isInitWithOptionalsEnabled = isInitWithOptionalsEnabled
let content = generateBody(from: protocolType)
return protocolGenerator.makeProtocol(
from: protocolType,
Expand All @@ -43,7 +45,12 @@ final class SpyGenerator {

content.append(makeVariables(from: protocolType).map(variableGenerator.makeCode))
content.append(makeSpyVariables(from: protocolType))
content.append(initGenerator.makeCode(with: makeVariables(from: protocolType) + makeReturnVariables(from: protocolType)))
let initVariables = makeVariables(from: protocolType) + makeReturnVariables(from: protocolType)
let nonOptionalInitVariables = initVariables.filter { !$0.type.isOptional }
content.append(initGenerator.makeCode(
with: isInitWithOptionalsEnabled ? initVariables : nonOptionalInitVariables,
isAddingDefaultValueToOptionalsEnabled: isInitWithOptionalsEnabled
))

content += protocolType.functions.map {
makeFunction(from: $0, protocolDeclaration: protocolType)
Expand Down
11 changes: 9 additions & 2 deletions Sources/Rubicon/Generator/StubGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ final class StubGenerator {
private let functionGenerator: FunctionGenerator
private let functionNameGenerator: FunctionNameGenerator
private let initGenerator: InitGenerator
private var isInitWithOptionalsEnabled: Bool = false

init(
protocolGenerator: ProtocolGenerator,
Expand All @@ -19,7 +20,8 @@ final class StubGenerator {
self.initGenerator = initGenerator
}

func generate(from protocolType: ProtocolDeclaration, nameSuffix: String) -> String {
func generate(from protocolType: ProtocolDeclaration, nameSuffix: String, isInitWithOptionalsEnabled: Bool) -> String {
self.isInitWithOptionalsEnabled = isInitWithOptionalsEnabled
let content = generateBody(from: protocolType)
return protocolGenerator.makeProtocol(
from: protocolType,
Expand All @@ -32,7 +34,12 @@ final class StubGenerator {
var content = [[String]]()

content.append(makeVariables(from: protocolType).map(variableGenerator.makeCode))
content.append(initGenerator.makeCode(with: makeVariables(from: protocolType)))
let initVariables = makeVariables(from: protocolType)
let nonOptionalInitVariables = initVariables.filter { !$0.type.isOptional }
content.append(initGenerator.makeCode(
with: isInitWithOptionalsEnabled ? initVariables : nonOptionalInitVariables,
isAddingDefaultValueToOptionalsEnabled: isInitWithOptionalsEnabled
))

content += protocolType.functions.map {
makeFunction(from: $0, protocolDeclaration: protocolType)
Expand Down
64 changes: 58 additions & 6 deletions Sources/Rubicon/Integration/Rubicon.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,38 @@
public struct SpyConfiguration {
public let accessLevel: AccessLevel
public let indentStep: String
public let isInitWithOptionalsEnabled: Bool

public init(
accessLevel: AccessLevel,
indentStep: String,
isInitWithOptionalsEnabled: Bool
) {
self.accessLevel = accessLevel
self.indentStep = indentStep
self.isInitWithOptionalsEnabled = isInitWithOptionalsEnabled
}
}

public struct StubConfiguration {
public let accessLevel: AccessLevel
public let indentStep: String
public let nameSuffix: String
public let isInitWithOptionalsEnabled: Bool

public init(
accessLevel: AccessLevel,
indentStep: String,
nameSuffix: String = "Stub",
isInitWithOptionalsEnabled: Bool
) {
self.accessLevel = accessLevel
self.indentStep = indentStep
self.nameSuffix = nameSuffix
self.isInitWithOptionalsEnabled = isInitWithOptionalsEnabled
}
}

public final class Rubicon {
public init() {}

Expand Down Expand Up @@ -35,12 +70,21 @@ public final class Rubicon {
)
}

public func makeStub(code: String, accessLevel: AccessLevel, indentStep: String, nameSuffix: String = "Stub") -> [String] {
public func makeStub(code: String, configuration: StubConfiguration) -> [String] {
let parser = makeParser()
let generator = makeStubGenerator(accessLevel: accessLevel, indentStep: indentStep)
let generator = makeStubGenerator(
accessLevel: configuration.accessLevel,
indentStep: configuration.indentStep
)
do {
let protocols = try parser.parse(text: code)
return protocols.map { generator.generate(from: $0, nameSuffix: nameSuffix) }
return protocols.map {
generator.generate(
from: $0,
nameSuffix: configuration.nameSuffix,
isInitWithOptionalsEnabled: configuration.isInitWithOptionalsEnabled
)
}
} catch {
return []
}
Expand All @@ -57,12 +101,20 @@ public final class Rubicon {
)
}

public func makeSpy(code: String, accessLevel: AccessLevel, indentStep: String) -> [String] {
public func makeSpy(code: String, configuration: SpyConfiguration) -> [String] {
let parser = makeParser()
let generator = makeSpyGenerator(accessLevel: accessLevel, indentStep: indentStep)
let generator = makeSpyGenerator(
accessLevel: configuration.accessLevel,
indentStep: configuration.indentStep
)
do {
let protocols = try parser.parse(text: code)
return protocols.map(generator.generate(from:))
return protocols.map{
generator.generate(
from: $0,
isInitWithOptionalsEnabled: configuration.isInitWithOptionalsEnabled
)
}
} catch {
return []
}
Expand Down
34 changes: 28 additions & 6 deletions Tests/RubiconTests/Generator/InitGeneratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ final class InitGeneratorTests: XCTestCase {
}

func test_givenNoVariable_whenMakeCode_thenDoNotMakeInit() {
let result = sut.makeCode(with: [])
let result = sut.makeCode(with: [], isAddingDefaultValueToOptionalsEnabled: false)

XCTAssertEqual(result, [])
}

func test_givenPublicAccessLevelAndNoVariable_whenMakeCode_thenMakeInit() {
initialize(accessLevel: .public)

let result = sut.makeCode(with: [])
let result = sut.makeCode(with: [], isAddingDefaultValueToOptionalsEnabled: false)

equal(result, rows: [
"accessLevel init() {",
Expand All @@ -45,7 +45,7 @@ final class InitGeneratorTests: XCTestCase {
func test_givenSomeVariables_whenMakeCode_thenMakeInit() {
initialize(accessLevel: .public)

let result = sut.makeCode(with: [.makeStub(), .makeStub()])
let result = sut.makeCode(with: [.makeStub(), .makeStub()], isAddingDefaultValueToOptionalsEnabled: false)

equal(result, rows: [
"accessLevel init(argument, argument) {",
Expand All @@ -60,14 +60,36 @@ final class InitGeneratorTests: XCTestCase {
func test_givenOptionalVariable_whenMakeCode_thenMakeInit() {
initialize(accessLevel: .public)

let result = sut.makeCode(with: [.makeStub(), .makeStub(type: .makeStub(isOptional: true))])
let result = sut.makeCode(
with: [.makeStub(), .makeStub(type: .makeStub(isOptional: true))],
isAddingDefaultValueToOptionalsEnabled: false
)

equal(result, rows: [
"accessLevel init(argument) {",
"accessLevel init(argument, argument) {",
"-self.identifier = identifier",
"-self.identifier = identifier",
"}",
])
XCTAssertEqual(argumentGeneratorSpy.makeCode.count, 1)
XCTAssertEqual(argumentGeneratorSpy.makeCode.count, 2)
XCTAssertEqual(argumentGeneratorSpy.makeCode.first?.declaration, .makeStub(label: nil, name: "identifier", type: .makeStub()))
}

func test_givenOptionalVariableAndIsAddingDefaultValueToOptionalsEnabled_whenMakeCode_thenMakeInit() {
initialize(accessLevel: .public)

let result = sut.makeCode(
with: [.makeStub(), .makeStub(type: .makeStub(isOptional: true))],
isAddingDefaultValueToOptionalsEnabled: true
)

equal(result, rows: [
"accessLevel init(argument, argument = nil) {",
"-self.identifier = identifier",
"-self.identifier = identifier",
"}",
])
XCTAssertEqual(argumentGeneratorSpy.makeCode.count, 2)
XCTAssertEqual(argumentGeneratorSpy.makeCode.first?.declaration, .makeStub(label: nil, name: "identifier", type: .makeStub()))
}
}
Loading

0 comments on commit 495bd5f

Please sign in to comment.