Skip to content
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

Add an ability to generate multiple functions with escaping character… #58

Merged
merged 1 commit into from
Sep 27, 2023
Merged
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
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
Loading