Skip to content

Commit

Permalink
fix resolve by tag while use DelayMaker (Lazy or Provider)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivlevAstef committed Sep 17, 2019
1 parent 6fea882 commit af61f4c
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 5 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# v3.8.2
* Support Lazy with tags, many and other combinations. for example: `let services: [Lazy<ServiceProtocol>] = many(by(tag: FooService.self, on: *container))`

# v3.8.1
* Support Many<Lazy<Type>>. For example: let objects: [Lazy<Service>] = many(*container)
* Support Many<Lazy<Type>>. For example: `let objects: [Lazy<Service>] = many(*container)`

# v3.8.0
* remove bundle from DIFramework - now support found dependencies for static libralies
Expand Down
2 changes: 1 addition & 1 deletion DITranquillity.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = 'DITranquillity'
s.version = '3.8.1'
s.version = '3.8.2'
s.summary = 'DITranquillity - Dependency injection for iOS/macOS/tvOS (Swift) '

s.description = <<-DESC
Expand Down
8 changes: 6 additions & 2 deletions Sources/DITranquillity/Private/Resolver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,12 @@ class Resolver {
filterByFramework = filterByFramework && sType.inFramework /// filter
} else if sType.tag {
currentComponents = container.componentContainer[TypeKey(by: simpleType.type, tag: sType.tagType)]
} else if sType.delayed {
// ignore - delayed type don't change components list
type = parent.firstNotSwiftType
continue
} else {
currentComponents = container.componentContainer[TypeKey(by: simpleType.type)]
currentComponents = container.componentContainer[TypeKey(by: simpleType.type)]
}

type = parent.firstNotSwiftType
Expand All @@ -135,7 +139,7 @@ class Resolver {
components = first ? currentComponents : components.intersection(currentComponents)
first = false

} while type != simpleType
} while type != simpleType || first /*check on first need only for delayed types*/

if filterByFramework {
return filter(by: framework, Components(components))
Expand Down
2 changes: 1 addition & 1 deletion Sources/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>3.8.1</string>
<string>3.8.2</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
72 changes: 72 additions & 0 deletions Tests/DITranquillityTest/DITranquillityTests_Resolve.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,26 @@ class DITranquillityTests_Resolve: XCTestCase {

XCTAssertNotEqual(Unmanaged.passUnretained(serviceFoo as AnyObject).toOpaque(), Unmanaged.passUnretained(serviceBar as AnyObject).toOpaque())
}

func test10_ResolveMultiplyLazyProviderByTag() {
let container = DIContainer()

container.register(FooService.init)
.as(check: ServiceProtocol.self, tag: FooService.self){$0}
.lifetime(.single)

container.register(BarService.init)
.as(check: ServiceProtocol.self, tag: BarService.self){$0}
.lifetime(.single)

let serviceFoo: Lazy<ServiceProtocol> = container.resolve(tag: FooService.self)
XCTAssertEqual(serviceFoo.value.foo(), "foo")

let serviceBar: Provider<ServiceProtocol> = container.resolve(tag: BarService.self)
XCTAssertEqual(serviceBar.value.foo(), "bar")

XCTAssertNotEqual(Unmanaged.passUnretained(serviceFoo.value as AnyObject).toOpaque(), Unmanaged.passUnretained(serviceBar.value as AnyObject).toOpaque())
}

func test11_ResolveMultiplySingleByName() {
let container = DIContainer()
Expand Down Expand Up @@ -552,6 +572,32 @@ class DITranquillityTests_Resolve: XCTestCase {
let serviceFooTag2: ServiceProtocol? = by(tag: FooService.self, on: by(tag: BarService.self, on: *container2))
XCTAssertEqual(serviceFooTag2?.foo() ?? "", "")
}

func test20_ResolveLazyByTagAndTag() {
let container1 = DIContainer()

container1.register(FooService.init)
.as(check: ServiceProtocol.self, tag: FooService.self){$0}
.as(check: ServiceProtocol.self, tag: BarService.self){$0}

let container2 = DIContainer()
container2.register(FooService.init)
.as(check: ServiceProtocol.self, tag: FooService.self){$0}


let serviceFoo1: Lazy<ServiceProtocol?> = container1.resolve(tag: FooService.self)
XCTAssertEqual(serviceFoo1.value?.foo() ?? "", "foo")

let serviceFoo2: Lazy<ServiceProtocol?> = container2.resolve(tag: FooService.self)
XCTAssertEqual(serviceFoo2.value?.foo() ?? "", "foo")


let serviceFooTag1: Lazy<ServiceProtocol?> = by(tag: FooService.self, on: by(tag: BarService.self, on: *container1))
XCTAssertEqual(serviceFooTag1.value?.foo() ?? "", "foo")

let serviceFooTag2: Lazy<ServiceProtocol?> = by(tag: FooService.self, on: by(tag: BarService.self, on: *container2))
XCTAssertEqual(serviceFooTag2.value?.foo() ?? "", "")
}

func test20_ResolveByTagAndTagShort() {
let container1 = DIContainer()
Expand Down Expand Up @@ -602,6 +648,32 @@ class DITranquillityTests_Resolve: XCTestCase {

XCTAssertEqual(servicesByTag1.count, 2)
}

func test21_ResolveLazyByTagAndMany() {
let container = DIContainer()

container.register(FooService.init)
.as(check: ServiceProtocol.self, tag: FooService.self){$0}

container.register(BarService.init)
.as(check: ServiceProtocol.self, tag: FooService.self){$0}

container.register(FooService.init)
.as(check: ServiceProtocol.self){$0}

container.register(BarService.init)
.as(check: ServiceProtocol.self){$0}


let services: [Lazy<ServiceProtocol>] = container.resolveMany()
XCTAssertEqual(services.count, 4)
XCTAssertEqual(services.first?.value.foo() ?? "", "foo")

let servicesByTag1: [Lazy<ServiceProtocol>] = many(by(tag: FooService.self, on: *container))

XCTAssertEqual(servicesByTag1.count, 2)
XCTAssertEqual(servicesByTag1.last?.value.foo() ?? "", "bar")
}

func test22_ResolveByTagTagAndMany() {
let container = DIContainer()
Expand Down

0 comments on commit af61f4c

Please sign in to comment.