Skip to content

Commit

Permalink
Sanitize the Service name used for Argument unit tests
Browse files Browse the repository at this point in the history
The service name is used as part of naming the properties on the generated unit test struct, so it also needs to be sanitized.
  • Loading branch information
bradfol committed Jan 21, 2025
1 parent ec2fb8b commit c1fd063
Show file tree
Hide file tree
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
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions Tests/KnitCodeGenTests/UnitTestSourceFileTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")

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

Expand Down

0 comments on commit c1fd063

Please sign in to comment.