Skip to content

Commit 413d068

Browse files
committed
feat: unmatchingArgumentType tests
1 parent a282b3e commit 413d068

File tree

4 files changed

+91
-26
lines changed

4 files changed

+91
-26
lines changed

Sources/Container/Async/AsyncContainer.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,20 @@ public actor AsyncContainer: AsyncDependencyResolving, AsyncDependencyRegisterin
190190
// MARK: Private methods
191191
private extension AsyncContainer {
192192
func getRegistration(with identifier: RegistrationIdentifier) throws -> AsyncRegistration {
193-
guard let registration = registrations[identifier] else {
194-
throw ResolutionError.dependencyNotRegistered(
195-
message: "Dependency of type \(identifier.description) wasn't registered in container \(self)"
193+
if let registration = registrations[identifier] {
194+
return registration
195+
}
196+
197+
if let matchingIdentifier = registrations.keys.first(where: { $0.typeIdentifier == identifier.typeIdentifier }) {
198+
throw ResolutionError.unmatchingArgumentType(
199+
message: "Registration of type \(matchingIdentifier.description) doesn't accept arguments of type \(identifier.description)"
196200
)
197201
}
198202

199-
return registration
203+
throw ResolutionError.dependencyNotRegistered(
204+
message: "Dependency of type \(identifier.description) wasn't registered in container \(self)"
205+
)
206+
200207
}
201208

202209
func getDependency<Dependency: Sendable>(from registration: AsyncRegistration, with argument: Any? = nil) async throws -> Dependency {

Sources/Container/Sync/Container.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,20 @@ open class Container: DependencyAutoregistering, DependencyResolving, Dependency
186186
// MARK: Private methods
187187
private extension Container {
188188
func getRegistration(with identifier: RegistrationIdentifier) throws -> Registration {
189-
guard let registration = registrations[identifier] else {
190-
throw ResolutionError.dependencyNotRegistered(
191-
message: "Dependency of type \(identifier.description) wasn't registered in container \(self)"
189+
if let registration = registrations[identifier] {
190+
return registration
191+
}
192+
193+
if let matchingIdentifier = registrations.keys.first(where: { $0.typeIdentifier == identifier.typeIdentifier }) {
194+
throw ResolutionError.unmatchingArgumentType(
195+
message: "Registration of type \(matchingIdentifier.description) doesn't accept arguments of type \(identifier.description)"
192196
)
193197
}
194198

195-
return registration
199+
throw ResolutionError.dependencyNotRegistered(
200+
message: "Dependency of type \(identifier.description) wasn't registered in container \(self)"
201+
)
202+
196203
}
197204

198205
func getDependency<Dependency>(from registration: Registration, with argument: Any? = nil) throws -> Dependency {

Tests/Container/Async/AsyncArgumentTests.swift

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,33 @@ final class AsyncContainerArgumentTests: AsyncDITestCase {
3131
XCTAssertEqual(argument, resolvedDependency.subDependency, "Container returned dependency with different argument")
3232
}
3333

34-
func testUnmatchingArgumentType() async {
34+
func testUnmatchingArgumentType_ZeroArguments() async {
35+
await container.register { _ -> SimpleDependency in
36+
SimpleDependency()
37+
}
38+
39+
let argument = 48
40+
41+
do {
42+
_ = try await container.tryResolve(type: SimpleDependency.self, argument: argument)
43+
44+
XCTFail("Expected to throw error")
45+
} catch {
46+
guard let resolutionError = error as? ResolutionError else {
47+
XCTFail("Incorrect error type")
48+
return
49+
}
50+
51+
switch resolutionError {
52+
case .unmatchingArgumentType:
53+
XCTAssertNotEqual(resolutionError.localizedDescription, "", "Error description is empty")
54+
default:
55+
XCTFail("Incorrect resolution error: \(resolutionError)")
56+
}
57+
}
58+
}
59+
60+
func testUnmatchingArgumentType_OneArgument() async {
3561
await container.register { _, argument -> DependencyWithValueTypeParameter in
3662
DependencyWithValueTypeParameter(subDependency: argument)
3763
}
@@ -49,10 +75,10 @@ final class AsyncContainerArgumentTests: AsyncDITestCase {
4975
}
5076

5177
switch resolutionError {
52-
case .dependencyNotRegistered:
78+
case .unmatchingArgumentType:
5379
XCTAssertNotEqual(resolutionError.localizedDescription, "", "Error description is empty")
5480
default:
55-
XCTFail("Incorrect resolution error")
81+
XCTFail("Incorrect resolution error: \(resolutionError)")
5682
}
5783
}
5884
}
@@ -94,7 +120,7 @@ final class AsyncContainerArgumentTests: AsyncDITestCase {
94120
XCTAssertEqual(argument2, resolvedDependency.argument2, "Container returned dependency with different second argument")
95121
}
96122

97-
func testUnmatchingTwoArgumentsType() async {
123+
func testUnmatchingArgumentType_TwoArguments() async {
98124
await container.register { _, argument1, argument2 -> DependencyWithTwoArguments in
99125
DependencyWithTwoArguments(argument1: argument1, argument2: argument2)
100126
}
@@ -113,10 +139,10 @@ final class AsyncContainerArgumentTests: AsyncDITestCase {
113139
}
114140

115141
switch resolutionError {
116-
case .dependencyNotRegistered:
142+
case .unmatchingArgumentType:
117143
XCTAssertNotEqual(resolutionError.localizedDescription, "", "Error description is empty")
118144
default:
119-
XCTFail("Incorrect resolution error")
145+
XCTFail("Incorrect resolution error: \(resolutionError)")
120146
}
121147
}
122148
}
@@ -151,7 +177,7 @@ final class AsyncContainerArgumentTests: AsyncDITestCase {
151177
XCTAssertEqual(argument3, resolvedDependency.argument3, "Container returned dependency with different third argument")
152178
}
153179

154-
func testUnmatchingThreeArgumentsType() async {
180+
func testUnmatchingArgumentType_ThreeArguments() async {
155181
await container.register { _, argument1, argument2, argument3 -> DependencyWithThreeArguments in
156182
DependencyWithThreeArguments(argument1: argument1, argument2: argument2, argument3: argument3)
157183
}
@@ -171,10 +197,10 @@ final class AsyncContainerArgumentTests: AsyncDITestCase {
171197
}
172198

173199
switch resolutionError {
174-
case .dependencyNotRegistered:
200+
case .unmatchingArgumentType:
175201
XCTAssertNotEqual(resolutionError.localizedDescription, "", "Error description is empty")
176202
default:
177-
XCTFail("Incorrect resolution error")
203+
XCTFail("Incorrect resolution error: \(resolutionError)")
178204
}
179205
}
180206
}

Tests/Container/Sync/ArgumentTests.swift

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,32 @@ final class ContainerArgumentTests: DITestCase {
3131
XCTAssertEqual(argument, resolvedDependency.subDependency, "Container returned dependency with different argument")
3232
}
3333

34-
func testUnmatchingArgumentType() {
34+
func testUnmatchingArgumentType_ZeroArguments() {
35+
container.register { _ -> SimpleDependency in
36+
SimpleDependency()
37+
}
38+
39+
let argument = 48
40+
41+
XCTAssertThrowsError(
42+
try container.tryResolve(type: SimpleDependency.self, argument: argument),
43+
"Resolver didn't throw an error"
44+
) { error in
45+
guard let resolutionError = error as? ResolutionError else {
46+
XCTFail("Incorrect error type")
47+
return
48+
}
49+
50+
switch resolutionError {
51+
case .unmatchingArgumentType:
52+
XCTAssertNotEqual(resolutionError.localizedDescription, "", "Error description is empty")
53+
default:
54+
XCTFail("Incorrect resolution error: \(resolutionError)")
55+
}
56+
}
57+
}
58+
59+
func testUnmatchingArgumentType_OneArgument() {
3560
container.register { _, argument -> DependencyWithValueTypeParameter in
3661
DependencyWithValueTypeParameter(subDependency: argument)
3762
}
@@ -48,10 +73,10 @@ final class ContainerArgumentTests: DITestCase {
4873
}
4974

5075
switch resolutionError {
51-
case .dependencyNotRegistered:
76+
case .unmatchingArgumentType:
5277
XCTAssertNotEqual(resolutionError.localizedDescription, "", "Error description is empty")
5378
default:
54-
XCTFail("Incorrect resolution error")
79+
XCTFail("Incorrect resolution error: \(resolutionError)")
5580
}
5681
}
5782
}
@@ -82,7 +107,7 @@ final class ContainerArgumentTests: DITestCase {
82107
XCTAssertEqual(argument2, resolvedDependency.argument2, "Container returned dependency with different second argument")
83108
}
84109

85-
func testUnmatchingTwoArgumentsType() {
110+
func testUnmatchingArgumentType_TwoArguments() {
86111
container.register { _, argument1, argument2 -> DependencyWithTwoArguments in
87112
DependencyWithTwoArguments(argument1: argument1, argument2: argument2)
88113
}
@@ -100,10 +125,10 @@ final class ContainerArgumentTests: DITestCase {
100125
}
101126

102127
switch resolutionError {
103-
case .dependencyNotRegistered:
128+
case .unmatchingArgumentType:
104129
XCTAssertNotEqual(resolutionError.localizedDescription, "", "Error description is empty")
105130
default:
106-
XCTFail("Incorrect resolution error")
131+
XCTFail("Incorrect resolution error: \(resolutionError)")
107132
}
108133
}
109134
}
@@ -138,7 +163,7 @@ final class ContainerArgumentTests: DITestCase {
138163
XCTAssertEqual(argument3, resolvedDependency.argument3, "Container returned dependency with different third argument")
139164
}
140165

141-
func testUnmatchingThreeArgumentsType() {
166+
func testUnmatchingArgumentType_ThreeArguments() {
142167
container.register { _, argument1, argument2, argument3 -> DependencyWithThreeArguments in
143168
DependencyWithThreeArguments(argument1: argument1, argument2: argument2, argument3: argument3)
144169
}
@@ -157,10 +182,10 @@ final class ContainerArgumentTests: DITestCase {
157182
}
158183

159184
switch resolutionError {
160-
case .dependencyNotRegistered:
185+
case .unmatchingArgumentType:
161186
XCTAssertNotEqual(resolutionError.localizedDescription, "", "Error description is empty")
162187
default:
163-
XCTFail("Incorrect resolution error")
188+
XCTFail("Incorrect resolution error: \(resolutionError)")
164189
}
165190
}
166191
}

0 commit comments

Comments
 (0)