diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f30332e..9d0a3da 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -12,10 +12,10 @@ jobs: # Set timeout to prevent overcharging timeout-minutes: 20 steps: - - uses: maxim-lobanov/setup-xcode@v1.2.3 + - uses: maxim-lobanov/setup-xcode@v1 with: - xcode-version: 12.4 + xcode-version: latest - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Test - run: swift test --parallel + run: swift test diff --git a/Sources/Biodag/Biodag.swift b/Sources/Biodag/Biodag.swift index f03a430..cb3038b 100644 --- a/Sources/Biodag/Biodag.swift +++ b/Sources/Biodag/Biodag.swift @@ -15,36 +15,40 @@ import Foundation /// A dependency collection that provides resolutions for object instances. final public class DependencyResolver { + private let moduleArray: [Module] /// Stored object instance factories. private var modules = [String: Module]() private var instances = [String: Any]() /// Construct dependency resolutions. public init(@ModuleBuilder _ modules: () -> [Module]) { - modules().forEach { add(module: $0) } + self.moduleArray = modules() } /// Construct dependency resolution. public init(@ModuleBuilder _ module: () -> Module) { - add(module: module()) + self.moduleArray = [module()] } /// Assigns the current container to the composition root. public func build() { - Self.root = self + for module in moduleArray { + Self.root.add(module: module) + } } - fileprivate init() {} - deinit { modules.removeAll() } + fileprivate init() { + self.moduleArray = [] + } } private extension DependencyResolver { /// Composition root container of dependencies. - static var root = DependencyResolver() + static let root = DependencyResolver() /// Registers a specific type and its instantiating factory. func add(module: Module) { - modules[module.name] = module + self.modules[module.name] = module } /// Resolves through inference and returns an instance of the given type from the current default container. diff --git a/Tests/BiodagTests/BiodagTests.swift b/Tests/BiodagTests/BiodagTests.swift index e3d36c8..61c2165 100644 --- a/Tests/BiodagTests/BiodagTests.swift +++ b/Tests/BiodagTests/BiodagTests.swift @@ -82,6 +82,24 @@ extension DependencyTests { // The singletons are the same instance XCTAssertTrue(singletonModule === helperSingleton) } + + func testAddingDependencies() { + struct Added { } + struct NotAdded { } + + let newResolver = DependencyResolver({ + Module { Added() as Added } + }) + newResolver.build() + + + @Inject var widgetModule: WidgetModuleType + @Inject var added: Added + @Inject var notAdded: NotAdded + + XCTAssertNotNil(widgetModule) + XCTAssertNotNil(added) + } } // MARK: - Subtypes