Skip to content

Commit

Permalink
Fix escaping in init
Browse files Browse the repository at this point in the history
  • Loading branch information
Kryštof Matěj committed May 10, 2024
1 parent 9a02cd3 commit ff108a9
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
4 changes: 3 additions & 1 deletion Sources/Rubicon/Generator/InitGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ final class InitGeneratorImpl: InitGenerator {

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)
let prefix = variable.type.isOptional ? [] : variable.type.prefix
let type = TypeDeclaration(name: variable.type.name, isOptional: variable.type.isOptional, prefix: prefix)
let declaration = ArgumentDeclaration(name: variable.identifier, type: type)
return argumentGenerator.makeCode(from: declaration) + defaultValue
}
}
8 changes: 6 additions & 2 deletions Tests/RubiconTests/Generator/InitGeneratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ final class InitGeneratorTests: XCTestCase {
initialize(accessLevel: .public)

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

Expand All @@ -72,7 +75,8 @@ final class InitGeneratorTests: XCTestCase {
"}",
])
XCTAssertEqual(argumentGeneratorSpy.makeCode.count, 2)
XCTAssertEqual(argumentGeneratorSpy.makeCode.first?.declaration, .makeStub(label: nil, name: "identifier", type: .makeStub()))
XCTAssertEqual(argumentGeneratorSpy.makeCode.first?.declaration, .makeStub(label: nil, name: "identifier", type: .makeStub(prefix: [.escaping])))
XCTAssertEqual(argumentGeneratorSpy.makeCode.last?.declaration, .makeStub(label: nil, name: "identifier", type: .makeStub(isOptional: true, prefix: [])))
}

func test_givenOptionalVariableAndIsAddingDefaultValueToOptionalsEnabled_whenMakeCode_thenMakeInit() {
Expand Down
2 changes: 1 addition & 1 deletion Tests/RubiconTests/Integration/SpyIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ final class SpyIntegrationTests: XCTestCase {
"-var continueFromScreenId = [ContinueFromScreenId]()",
"-var continueFromId = [ContinueFromId]()",
"",
"-init(name: String? = nil, color: Int, loadThrowBlock: @escaping (() throws -> Void)? = nil, loadReturn: Int, isFullReturn: Bool, downloadThrowBlock: @escaping (() throws -> Void)? = nil, downloadReturn: [String]) {",
"-init(name: String? = nil, color: Int, loadThrowBlock: (() throws -> Void)? = nil, loadReturn: Int, isFullReturn: Bool, downloadThrowBlock: (() throws -> Void)? = nil, downloadReturn: [String]) {",
"--self.name = name",
"--self.color = color",
"--self.loadThrowBlock = loadThrowBlock",
Expand Down
2 changes: 1 addition & 1 deletion Tests/RubiconTests/Integration/StubIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ final class StubIntegrationTests: XCTestCase {
"-var downloadThrowBlock: (() throws -> Void)?",
"-var downloadReturn: [String]",
"",
"-init(name: String? = nil, color: Int, loadThrowBlock: @escaping (() throws -> Void)? = nil, loadReturn: Int, isFullReturn: Bool, downloadThrowBlock: @escaping (() throws -> Void)? = nil, downloadReturn: [String]) {",
"-init(name: String? = nil, color: Int, loadThrowBlock: (() throws -> Void)? = nil, loadReturn: Int, isFullReturn: Bool, downloadThrowBlock: (() throws -> Void)? = nil, downloadReturn: [String]) {",
"--self.name = name",
"--self.color = color",
"--self.loadThrowBlock = loadThrowBlock",
Expand Down

0 comments on commit ff108a9

Please sign in to comment.