Skip to content

Commit

Permalink
Add an ability to generate multiple functions with escaping character…
Browse files Browse the repository at this point in the history
…s in one protocol (#58)
  • Loading branch information
libec authored Sep 27, 2023
1 parent 0ffb2d6 commit 558bbf4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
8 changes: 6 additions & 2 deletions Sources/Rubicon/Generator/FunctionNameGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class FunctionNameGeneratorImpl: FunctionNameGenerator {
}

private func makeLongName(for function: FunctionDeclaration) -> String {
return ([function.name] + function.arguments.map(makeLongArgument(for:))).joined()
return ([stringReplacingEscapingCharacters(in: function.name)] + function.arguments.map(makeLongArgument(for:))).joined()
}

private func makeLongArgument(for argument: ArgumentDeclaration) -> String {
Expand All @@ -29,12 +29,16 @@ final class FunctionNameGeneratorImpl: FunctionNameGenerator {
}

private func makeFirstLetterCapitalized(in string: String) -> String {
let functionNameWithoutEscapingCharacters = string.replacingOccurrences(of: "`", with: "")
let functionNameWithoutEscapingCharacters = stringReplacingEscapingCharacters(in: string)
let first = String(functionNameWithoutEscapingCharacters.prefix(1)).capitalized
let other = String(functionNameWithoutEscapingCharacters.dropFirst())
return first + other
}

private func stringReplacingEscapingCharacters(in string: String) -> String {
string.replacingOccurrences(of: "`", with: "")
}

private func isFunctionNameUnique(_ function: FunctionDeclaration, in functions: [FunctionDeclaration]) -> Bool {
return !functions.contains(where: { $0.name == function.name && $0 != function })
}
Expand Down
9 changes: 9 additions & 0 deletions Tests/RubiconTests/Generator/FunctionNameGeneratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,13 @@ final class FunctionNameGeneratorTests: XCTestCase {

XCTAssertEqual(name, "Continue")
}

func test_givenMultipleFunctionWithSwiftKeywordEscapingCharacters_whenMakeStructUniqueName_thenReturnsFullNameWithoutEscapedCharacters() {
let function = FunctionDeclaration.makeStub(name: "`continue`", arguments: [.init(name: "fromScreenId", type: .makeStub())])
let otherFunction = FunctionDeclaration.makeStub(name: "`continue`", arguments: [.init(name: "id", type: .makeStub())])

let name = sut.makeStructUniqueName(for: function, in: [function, otherFunction])

XCTAssertEqual(name, "ContinueFromScreenId")
}
}
19 changes: 15 additions & 4 deletions Tests/RubiconTests/Integration/SpyIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ final class SpyIntegrationTests: XCTestCase {
func isFull(_ validate: @escaping () -> Void) -> Bool
func download() async throws -> [String]
func `continue`(from screenId: String)
func `continue`(from id: String)
}
"""
let sut = Rubicon()
Expand All @@ -31,10 +32,14 @@ final class SpyIntegrationTests: XCTestCase {
"--let validate: () -> Void",
"-}",
"",
"-struct Continue {",
"-struct ContinueFromScreenId {",
"--let screenId: String",
"-}",
"",
"-struct ContinueFromId {",
"--let id: String",
"-}",
"",
"-var name: String?",
"-var color: Int",
"",
Expand All @@ -47,7 +52,8 @@ final class SpyIntegrationTests: XCTestCase {
"-var load = [Load]()",
"-var isFull = [IsFull]()",
"-var downloadCount = 0",
"-var `continue` = [Continue]()",
"-var continueFromScreenId = [ContinueFromScreenId]()",
"-var continueFromId = [ContinueFromId]()",
"",
"-init(color: Int, loadReturn: Int, isFullReturn: Bool, downloadReturn: [String]) {",
"--self.color = color",
Expand Down Expand Up @@ -80,8 +86,13 @@ final class SpyIntegrationTests: XCTestCase {
"-}",
"",
"-func `continue`(from screenId: String) {",
"--let item = Continue(screenId: screenId)",
"--`continue`.append(item)",
"--let item = ContinueFromScreenId(screenId: screenId)",
"--continueFromScreenId.append(item)",
"-}",
"",
"-func `continue`(from id: String) {",
"--let item = ContinueFromId(id: id)",
"--continueFromId.append(item)",
"-}",
"}",
""
Expand Down

0 comments on commit 558bbf4

Please sign in to comment.