From 58d977dc02b206bd3bb04f40e6e57dcfeddd9a58 Mon Sep 17 00:00:00 2001 From: Ilya Puchka Date: Sun, 3 Jul 2016 22:05:19 +0200 Subject: [PATCH] optional dipTag, calling dipTag getter in setter --- DipUI/DipUITests/DipUITests.swift | 45 +++++++++++++++++++++--- DipUI/DipUITests/NSStoryboard.storyboard | 20 +++++++++-- DipUI/DipUITests/UIStoryboard.storyboard | 27 ++++++++++++-- Sources/StoryboardInstantiatable.swift | 9 ++--- 4 files changed, 89 insertions(+), 12 deletions(-) diff --git a/DipUI/DipUITests/DipUITests.swift b/DipUI/DipUITests/DipUITests.swift index 7233e87..3f3338c 100644 --- a/DipUI/DipUITests/DipUITests.swift +++ b/DipUI/DipUITests/DipUITests.swift @@ -42,7 +42,8 @@ import XCTest #endif -class DipViewController: ViewController, StoryboardInstantiatable { } +class DipViewController: ViewController, StoryboardInstantiatable {} +class NilTagViewController: ViewController, StoryboardInstantiatable {} class DipUITests: XCTestCase { @@ -76,8 +77,8 @@ class DipUITests: XCTestCase { DependencyContainer.uiContainer = container storyboard.instantiateViewControllerWithIdentifier("ViewController") } - - func testThatItResolvesIfContainerAndTagAreSet() { + + func testThatItResolvesIfContainerAndStringTagAreSet() { var resolved = false let container = DependencyContainer() container.register(tag: "vc") { DipViewController() } @@ -87,9 +88,45 @@ class DipUITests: XCTestCase { DependencyContainer.uiContainer = container storyboard.instantiateViewControllerWithIdentifier("DipViewController") - XCTAssertTrue(resolved, "Should resolve when container is set.") + XCTAssertTrue(resolved, "Should resolve when container and tag are set.") + } + + func testThatItResolvesIfContainerAndNilTagAreSet() { + var resolved = false + let container = DependencyContainer() + container.register() { NilTagViewController() } + .resolveDependencies { _, _ in + resolved = true + } + + DependencyContainer.uiContainer = container + storyboard.instantiateViewControllerWithIdentifier("NilTagViewController") + XCTAssertTrue(resolved, "Should resolve when container and nil tag are set.") + } + + func testThatItDoesNotResolveIfTagDoesNotMatch() { + let container = DependencyContainer() + container.register(tag: "wrong tag") { DipViewController() } + .resolveDependencies { _, _ in + XCTFail("Should not resolve when container is not set.") + } + + DependencyContainer.uiContainer = container + storyboard.instantiateViewControllerWithIdentifier("DipViewController") } + func testThatItResolvesWithDefinitionWithNoTag() { + var resolved = false + let container = DependencyContainer() + container.register() { DipViewController() } + .resolveDependencies { _, _ in + resolved = true + } + + DependencyContainer.uiContainer = container + storyboard.instantiateViewControllerWithIdentifier("DipViewController") + XCTAssertTrue(resolved, "Should fallback to definition with no tag.") + } } protocol SomeService: class { diff --git a/DipUI/DipUITests/NSStoryboard.storyboard b/DipUI/DipUITests/NSStoryboard.storyboard index 145a3cb..9d46876 100644 --- a/DipUI/DipUITests/NSStoryboard.storyboard +++ b/DipUI/DipUITests/NSStoryboard.storyboard @@ -1,7 +1,7 @@ - + - + @@ -20,6 +20,22 @@ + + + + + + + + + + + + + + + + diff --git a/DipUI/DipUITests/UIStoryboard.storyboard b/DipUI/DipUITests/UIStoryboard.storyboard index f2264d9..26cefce 100644 --- a/DipUI/DipUITests/UIStoryboard.storyboard +++ b/DipUI/DipUITests/UIStoryboard.storyboard @@ -1,8 +1,8 @@ - + - + @@ -48,5 +48,28 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Sources/StoryboardInstantiatable.swift b/Sources/StoryboardInstantiatable.swift index deafcbe..9906fb6 100644 --- a/Sources/StoryboardInstantiatable.swift +++ b/Sources/StoryboardInstantiatable.swift @@ -118,11 +118,12 @@ public protocol StoryboardInstantiatable { ``` */ - func didInstantiateFromStoryboard(container: DependencyContainer, tag: DependencyContainer.Tag) + func didInstantiateFromStoryboard(container: DependencyContainer, tag: DependencyContainer.Tag?) + var dipTag: String? { get } } extension StoryboardInstantiatable { - public func didInstantiateFromStoryboard(container: DependencyContainer, tag: DependencyContainer.Tag) { + public func didInstantiateFromStoryboard(container: DependencyContainer, tag: DependencyContainer.Tag?) { do { try container.resolveDependenciesOf(self, tag: tag) } @@ -143,15 +144,15 @@ extension NSObject { ///A string tag that will be used to resolve dependencies of this instance ///if it implements `StoryboardInstantiatable` protocol. - @IBInspectable private(set) public var dipTag: String? { + private(set) public var dipTag: String? { get { return objc_getAssociatedObject(self, DipTagAssociatedObjectKey) as? String } set { objc_setAssociatedObject(self, DipTagAssociatedObjectKey, newValue, .OBJC_ASSOCIATION_COPY_NONATOMIC) + let tag = dipTag.map(DependencyContainer.Tag.String) if let - tag = newValue.map(DependencyContainer.Tag.String), container = DependencyContainer.uiContainer, instantiatable = self as? StoryboardInstantiatable { instantiatable.didInstantiateFromStoryboard(container, tag: tag)