From 558bbf401e14cec8a232270dd2687aacac656e51 Mon Sep 17 00:00:00 2001 From: Libor Huspenina Date: Wed, 27 Sep 2023 13:06:16 +0200 Subject: [PATCH] Add an ability to generate multiple functions with escaping characters in one protocol (#58) --- .../Generator/FunctionNameGenerator.swift | 8 ++++++-- .../FunctionNameGeneratorTests.swift | 9 +++++++++ .../Integration/SpyIntegrationTests.swift | 19 +++++++++++++++---- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/Sources/Rubicon/Generator/FunctionNameGenerator.swift b/Sources/Rubicon/Generator/FunctionNameGenerator.swift index 238750a..5d66f40 100644 --- a/Sources/Rubicon/Generator/FunctionNameGenerator.swift +++ b/Sources/Rubicon/Generator/FunctionNameGenerator.swift @@ -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 { @@ -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 }) } diff --git a/Tests/RubiconTests/Generator/FunctionNameGeneratorTests.swift b/Tests/RubiconTests/Generator/FunctionNameGeneratorTests.swift index 8c6c118..f54fc1a 100644 --- a/Tests/RubiconTests/Generator/FunctionNameGeneratorTests.swift +++ b/Tests/RubiconTests/Generator/FunctionNameGeneratorTests.swift @@ -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") + } } diff --git a/Tests/RubiconTests/Integration/SpyIntegrationTests.swift b/Tests/RubiconTests/Integration/SpyIntegrationTests.swift index e20b417..45fa891 100644 --- a/Tests/RubiconTests/Integration/SpyIntegrationTests.swift +++ b/Tests/RubiconTests/Integration/SpyIntegrationTests.swift @@ -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() @@ -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", "", @@ -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", @@ -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)", "-}", "}", ""