-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial support for container in dependency module
- Loading branch information
Showing
5 changed files
with
156 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import XCTest | ||
@testable import WhoopDIKit | ||
|
||
class ContainerTests: XCTestCase { | ||
private let container = Container() | ||
|
||
func test_inject() { | ||
container.registerModules(modules: [GoodTestModule()]) | ||
let dependency: Dependency = container.inject("C_Factory", "param") | ||
XCTAssertTrue(dependency is DependencyC) | ||
} | ||
|
||
func test_inject_generic_integer() { | ||
container.registerModules(modules: [GoodTestModule()]) | ||
let dependency: GenericDependency<Int> = container.inject() | ||
XCTAssertEqual(42, dependency.value) | ||
} | ||
|
||
func test_inject_generic_string() { | ||
container.registerModules(modules: [GoodTestModule()]) | ||
let dependency: GenericDependency<String> = container.inject() | ||
XCTAssertEqual("string", dependency.value) | ||
} | ||
|
||
func test_inject_localDefinition() { | ||
container.registerModules(modules: [GoodTestModule()]) | ||
let dependency: Dependency = container.inject("C_Factory") { module in | ||
// Typically you'd override or provide a transient dependency. I'm using the top level dependency here | ||
// for the sake of simplicity. | ||
module.factory(name: "C_Factory") { DependencyA() as Dependency } | ||
} | ||
XCTAssertTrue(dependency is DependencyA) | ||
} | ||
|
||
func test_inject_localDefinition_noOverride() { | ||
container.registerModules(modules: [GoodTestModule()]) | ||
let dependency: Dependency = container.inject("C_Factory", params: "params") { _ in } | ||
XCTAssertTrue(dependency is DependencyC) | ||
} | ||
|
||
func test_inject_localDefinition_withParams() { | ||
container.registerModules(modules: [GoodTestModule()]) | ||
let dependency: Dependency = container.inject("C_Factory", params: "params") { module in | ||
module.factoryWithParams(name: "C_Factory") { params in DependencyB(params) as Dependency } | ||
} | ||
XCTAssertTrue(dependency is DependencyB) | ||
} | ||
|
||
func test_injecting() throws { | ||
throw XCTSkip("TODO: implement once WhoopDI uses a DI container") | ||
container.registerModules(modules: [FakeTestModuleForInjecting()]) | ||
let testInjecting: TestInjectingThing = container.inject() | ||
XCTAssertEqual(testInjecting, TestInjectingThing(name: 1)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import Foundation | ||
@testable import WhoopDIKit | ||
|
||
class GoodTestModule: DependencyModule { | ||
override func defineDependencies() { | ||
factory { DependencyA() } | ||
singleton { DependencyD() } | ||
factory(name: "A_Factory") { DependencyA() as Dependency } | ||
singleton(name: "A_Single") { DependencyA() as Dependency } | ||
|
||
factory { GenericDependency("string") } | ||
factory { GenericDependency(42) } | ||
|
||
factoryWithParams(name: "B_Factory") { params in DependencyB(params) as Dependency } | ||
factoryWithParams { params in DependencyB(params) } | ||
singletonWithParams(name: "B_Single") { params in DependencyB(params) as Dependency } | ||
|
||
factoryWithParams { params in | ||
DependencyC(proto: try self.get("A_Factory"), | ||
concrete: try self.get(params: params)) | ||
} | ||
factoryWithParams(name: "C_Factory") { params in | ||
DependencyC(proto: try self.get("A_Factory"), | ||
concrete: try self.get(params: params)) as Dependency | ||
} | ||
} | ||
} | ||
|
||
class BadTestModule: DependencyModule { | ||
override func defineDependencies() { | ||
factoryWithParams { params in | ||
DependencyC(proto: try self.get("A_Factory"), | ||
concrete: try self.get(params: params)) | ||
} | ||
} | ||
} | ||
|
||
class NilFactoryModule: DependencyModule { | ||
override func defineDependencies() { | ||
factory { nil as Dependency? } | ||
} | ||
} | ||
|
||
class NilSingletonModule: DependencyModule { | ||
override func defineDependencies() { | ||
singleton { nil as Dependency? } | ||
} | ||
} | ||
|
||
protocol Dependency { } | ||
|
||
class DependencyA: Dependency { } | ||
|
||
class DependencyB: Dependency { | ||
private let param: String | ||
|
||
internal init(_ param: String) { | ||
self.param = param | ||
} | ||
} | ||
|
||
class DependencyC: Dependency { | ||
private let proto: Dependency | ||
private let concrete: DependencyB | ||
|
||
internal init(proto: Dependency, concrete: DependencyB) { | ||
self.proto = proto | ||
self.concrete = concrete | ||
} | ||
} | ||
|
||
class DependencyD: Dependency { } | ||
|
||
struct GenericDependency<T>: Dependency { | ||
let value: T | ||
|
||
init(_ value: T) { | ||
self.value = value | ||
} | ||
} | ||
|
||
class FakeTestModuleForInjecting: DependencyModule { | ||
override func defineDependencies() { | ||
factory(name: "FakeName", factory: { 1 }) | ||
} | ||
} | ||
|
||
@Injectable | ||
struct TestInjectingThing: Equatable { | ||
@InjectableName(name: "FakeName") | ||
let name: Int | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters