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 test doubles for functions with escaping c… #57

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
6 changes: 3 additions & 3 deletions Sources/Rubicon/Generator/FunctionNameGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ final class FunctionNameGeneratorImpl: FunctionNameGenerator {
}

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

Expand All @@ -41,5 +42,4 @@ final class FunctionNameGeneratorImpl: FunctionNameGenerator {
func makeStructUniqueName(for function: FunctionDeclaration, in functions: [FunctionDeclaration]) -> String {
return makeFirstLetterCapitalized(in: makeUniqueName(for: function, in: functions))
}

}
8 changes: 8 additions & 0 deletions Tests/RubiconTests/Generator/FunctionNameGeneratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,12 @@ final class FunctionNameGeneratorTests: XCTestCase {

XCTAssertEqual(name, "Name")
}

func test_givenFunctionWithSwiftKeywordEscapingCharacters_whenMakeStructUniqueName_thenReturnsNameWithoutEscapedCharacters() {
let function = FunctionDeclaration.makeStub(name: "`continue`")

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

XCTAssertEqual(name, "Continue")
}
}
5 changes: 5 additions & 0 deletions Tests/RubiconTests/Integration/DummyIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ final class DummyIntegrationTests: XCTestCase {
func load(with stuff: Int, label: String) throws -> Int
func isFull(validate: @escaping () -> Void) -> Bool
func download() async throws -> [String]
func `continue`(from screenId: String)
}
"""
let sut = Rubicon()
Expand Down Expand Up @@ -52,6 +53,10 @@ final class DummyIntegrationTests: XCTestCase {
"-public func download() async throws -> [String] {",
"--fatalError()",
"-}",
"",
"-public func `continue`(from screenId: String) {",
"--fatalError()",
"-}",
"}",
"",
])
Expand Down
13 changes: 12 additions & 1 deletion Tests/RubiconTests/Integration/SpyIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Rubicon
import XCTest

final class SpyIntegrationTests: XCTestCase {
func test_givenProtocol_whenMakeDummy_thenReturnStub() {
func test_givenProtocol_whenMakeSpy_thenReturnSpy() {
let code = """
protocol Car: Vehicle {
var name: String? { get }
Expand All @@ -13,6 +13,7 @@ final class SpyIntegrationTests: XCTestCase {
func load(with stuff: Int, label: String) throws -> Int
func isFull(_ validate: @escaping () -> Void) -> Bool
func download() async throws -> [String]
func `continue`(from screenId: String)
}
"""
let sut = Rubicon()
Expand All @@ -30,6 +31,10 @@ final class SpyIntegrationTests: XCTestCase {
"--let validate: () -> Void",
"-}",
"",
"-struct Continue {",
"--let screenId: String",
"-}",
"",
"-var name: String?",
"-var color: Int",
"",
Expand All @@ -42,6 +47,7 @@ final class SpyIntegrationTests: XCTestCase {
"-var load = [Load]()",
"-var isFull = [IsFull]()",
"-var downloadCount = 0",
"-var `continue` = [Continue]()",
"",
"-init(color: Int, loadReturn: Int, isFullReturn: Bool, downloadReturn: [String]) {",
"--self.color = color",
Expand Down Expand Up @@ -72,6 +78,11 @@ final class SpyIntegrationTests: XCTestCase {
"--try downloadThrowBlock?()",
"--return downloadReturn",
"-}",
"",
"-func `continue`(from screenId: String) {",
"--let item = Continue(screenId: screenId)",
"--`continue`.append(item)",
"-}",
"}",
""
])
Expand Down
6 changes: 5 additions & 1 deletion Tests/RubiconTests/Integration/StubIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Rubicon
import XCTest

final class StubIntegrationTests: XCTestCase {
func test_givenProtocol_whenMakeDummy_thenReturnStub() {
func test_givenProtocol_whenMakeStub_thenReturnStub() {
let code = """
protocol Car {
var name: String? { get }
Expand All @@ -12,6 +12,7 @@ final class StubIntegrationTests: XCTestCase {
func load(with stuff: Int, label: String) throws -> Int
func isFull(validate: @escaping () -> Void) -> Bool
func download() async throws -> [String]
func `continue`(from screenId: String)
}
"""
let sut = Rubicon()
Expand Down Expand Up @@ -51,6 +52,9 @@ final class StubIntegrationTests: XCTestCase {
"--try downloadThrowBlock?()",
"--return downloadReturn",
"-}",
"",
"-func `continue`(from screenId: String) {",
"-}",
"}",
""
])
Expand Down
Loading