Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional dip tag #5

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 41 additions & 4 deletions DipUI/DipUITests/DipUITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ import XCTest

#endif

class DipViewController: ViewController, StoryboardInstantiatable { }
class DipViewController: ViewController, StoryboardInstantiatable {}
class NilTagViewController: ViewController, StoryboardInstantiatable {}

class DipUITests: XCTestCase {

Expand Down Expand Up @@ -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() }
Expand All @@ -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 {
Expand Down
20 changes: 18 additions & 2 deletions DipUI/DipUITests/NSStoryboard.storyboard
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="9531" systemVersion="15C50" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="10117" systemVersion="15F34" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="9531"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="10117"/>
</dependencies>
<scenes>
<!--Dip View Controller-->
Expand All @@ -20,6 +20,22 @@
</objects>
<point key="canvasLocation" x="311" y="339"/>
</scene>
<!--Nil Tag View Controller-->
<scene sceneID="OhX-tC-zpS">
<objects>
<viewController storyboardIdentifier="NilTagViewController" id="35S-Ec-qEA" customClass="NilTagViewController" customModule="DipUI_OSXTests" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" id="y58-K4-cDZ">
<rect key="frame" x="0.0" y="0.0" width="450" height="300"/>
<autoresizingMask key="autoresizingMask"/>
</view>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="nil" keyPath="dipTag"/>
</userDefinedRuntimeAttributes>
</viewController>
<customObject id="pvf-jv-8Cj" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="874" y="339"/>
</scene>
<!--View Controller-->
<scene sceneID="Rwu-gt-fAa">
<objects>
Expand Down
27 changes: 25 additions & 2 deletions DipUI/DipUITests/UIStoryboard.storyboard
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="9532" systemVersion="15D21" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="5AO-J3-7R4">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10117" systemVersion="15F34" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="5AO-J3-7R4">
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9530"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
</dependencies>
<scenes>
<!--View Controller-->
Expand Down Expand Up @@ -48,5 +48,28 @@
</objects>
<point key="canvasLocation" x="646" y="200"/>
</scene>
<!--Dip View Controller-->
<scene sceneID="AUA-qF-7ky">
<objects>
<viewController storyboardIdentifier="NilTagViewController" id="fFP-hb-OdS" userLabel="Dip View Controller" customClass="NilTagViewController" customModule="DipUI_iOSTests" customModuleProvider="target" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="Sio-ii-jPl"/>
<viewControllerLayoutGuide type="bottom" id="bhs-zK-dln"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="4xq-UV-htt">
<rect key="frame" x="0.0" y="0.0" width="200" height="100"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</view>
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
<size key="freeformSize" width="200" height="100"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="nil" keyPath="dipTag"/>
</userDefinedRuntimeAttributes>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="xvW-k9-I9y" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="898" y="200"/>
</scene>
</scenes>
</document>
9 changes: 5 additions & 4 deletions Sources/StoryboardInstantiatable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
Expand Down