diff --git a/Sources/Injected/Injected.swift b/Sources/Injected/Injected.swift index fbef9e2..cbcd1ba 100644 --- a/Sources/Injected/Injected.swift +++ b/Sources/Injected/Injected.swift @@ -61,12 +61,20 @@ open class Dependencies: Sequence { return AnyIterator { iter.next()?.value } } - /// Return dependency by given camelCase name of the object type + /// Returns dependency by given camelCase name of the object type /// For example: if dependency registered as `MyService` name should be `myService` public subscript(dynamicMember name: String) -> T? { dependencies.first { $0.name == name.prefix(1).capitalized + name.dropFirst() }?.value as? T } + /// Returns resolved dependency + public func resolve() -> T { + guard let dependency = dependencies.first(where: { $0.value is T })?.value as? T else { + fatalError("Can't resolve \(T.self)") + } + return dependency + } + // MARK: - Private fileprivate init() { } @@ -79,13 +87,6 @@ open class Dependencies: Sequence { } dependencies.append(dependency) } - - fileprivate func resolve() -> T { - guard let dependency = dependencies.first(where: { $0.value is T })?.value as? T else { - fatalError("Can't resolve \(T.self)") - } - return dependency - } } @propertyWrapper diff --git a/Tests/InjectedTests/InjectedTests.swift b/Tests/InjectedTests/InjectedTests.swift index ce8f2b3..9b78af8 100644 --- a/Tests/InjectedTests/InjectedTests.swift +++ b/Tests/InjectedTests/InjectedTests.swift @@ -53,6 +53,13 @@ final class InjectedTests: XCTestCase { XCTAssertEqual(serviceB.fetch().first, "Foo") } + func testShared() { + let serviceA: ServiceA = Dependencies.shared.resolve() + XCTAssertTrue(serviceA.run()) + let serviceB: ServiceB = Dependencies.shared.resolve() + XCTAssertEqual(serviceB.fetch().first, "Foo") + } + static var allTests = [ ("testExample", testExample), ]