Skip to content

Commit

Permalink
Merge pull request #227 from cashapp/bradfol/sanitize-args
Browse files Browse the repository at this point in the history
Sanitize the Service name used for Argument unit tests
bradfol authored Jan 21, 2025
2 parents ec2fb8b + c1fd063 commit 4bd3362
Showing 2 changed files with 6 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Sources/KnitCodeGen/UnitTestSourceFile.swift
Original file line number Diff line number Diff line change
@@ -245,7 +245,8 @@ private extension Registration {
/// Argument identifiers prefixed with the service name. Provides additional collision safety.
func serviceIdentifiedArguments() -> [Argument] {
return uniquelyIdentifiedArguments().map { arg in
let serviceName = self.service.prefix(1).lowercased() + self.service.dropFirst()
let sanitizedServiceName = TypeNamer.sanitizeType(type: service, keepGenerics: true)
let serviceName = sanitizedServiceName.prefix(1).lowercased() + sanitizedServiceName.dropFirst()
let capitalizedName = arg.resolvedIdentifier().prefix(1).uppercased() + arg.resolvedIdentifier().dropFirst()
// When generating the test arguments struct, the `@escaping` attribute should always be removed from the
// argument type, as assigning a closure type to a property is inherently escaping
4 changes: 4 additions & 0 deletions Tests/KnitCodeGenTests/UnitTestSourceFileTests.swift
Original file line number Diff line number Diff line change
@@ -252,6 +252,8 @@ final class UnitTestSourceFileTests: XCTestCase {
Registration(service: "B", accessLevel: .public, arguments: [.init(identifier: "field", type: "String"), .init(type: "String")]),
Registration(service: "A", accessLevel: .public, arguments: [.init(type: "Int"), .init(type: "String")]),
Registration(service: "C", accessLevel: .public, arguments: [.init(type: "@escaping () -> Void")]),
Registration(service: "D?", accessLevel: .public, arguments: [.init(type: "String")]),
Registration(service: "Optional<E>", accessLevel: .public, arguments: [.init(type: "String")]),
]
let result = try UnitTestSourceFile.makeArgumentStruct(registrations: registrations, assemblyName: "MyModule")

@@ -264,6 +266,8 @@ final class UnitTestSourceFileTests: XCTestCase {
let bString: String
let aInt: Int
let cClosure: () -> Void
let dString: String
let optional_EString: String
}
"""

0 comments on commit 4bd3362

Please sign in to comment.