Skip to content

Commit e29a19c

Browse files
[swinject] - 'Tests and everything! - TT'
1 parent 1d0d74b commit e29a19c

File tree

7 files changed

+139
-29
lines changed

7 files changed

+139
-29
lines changed

DependencyInjection/DependencyInjected.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,23 @@ import Swinject
1212
@propertyWrapper
1313
public struct DependencyInjected<Value> {
1414
let name:String?
15+
let container:Container
16+
1517
public init(wrappedValue value: Value?) {
1618
self.name = nil
19+
container = Workflow.defaultContainer
1720
}
18-
public init(wrappedValue value: Value?, name:String?) {
21+
public init(wrappedValue value: Value? = nil, name:String) {
1922
self.name = name
23+
container = Workflow.defaultContainer
24+
}
25+
26+
public init(wrappedValue value: Value? = nil, container containerGetter:@autoclosure () -> Container, name:String? = nil) {
27+
self.name = name
28+
self.container = containerGetter()
2029
}
2130

2231
public lazy var wrappedValue: Value? = {
23-
Workflow.defaultContainer.resolve(Value.self, name: name)
32+
container.resolve(Value.self, name: name)
2433
}()
2534
}

DependencyInjectionTests/DependencyInjectionTests.swift

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,75 @@
77
//
88

99
import XCTest
10-
@testable import DependencyInjection
10+
import Swinject
1111

12-
class DependencyInjectionTests: XCTestCase {
12+
@testable import Workflow
1313

14-
override func setUp() {
15-
// Put setup code here. This method is called before the invocation of each test method in the class.
16-
}
14+
protocol Vehicle { }
15+
class Car:Vehicle { }
16+
class Motorcycle:Vehicle { }
17+
18+
class DependencyInjectionTests: XCTestCase {
1719

1820
override func tearDown() {
19-
// Put teardown code here. This method is called after the invocation of each test method in the class.
21+
Workflow.defaultContainer.removeAll()
2022
}
21-
22-
func testExample() {
23-
// This is an example of a functional test case.
24-
// Use XCTAssert and related functions to verify your tests produce the correct results.
23+
24+
func testPropertyWrapperInjectsFromDefaultContainer() {
25+
let car = Car()
26+
Workflow.defaultContainer.register(Vehicle.self) { _ in car }
27+
class Thing {
28+
@DependencyInjected var vehicle:Vehicle?
29+
}
30+
31+
XCTAssertNotNil(Thing().vehicle)
32+
XCTAssert(Thing().vehicle is Car)
33+
XCTAssert((Thing().vehicle as? Car) === car)
2534
}
2635

27-
func testPerformanceExample() {
28-
// This is an example of a performance test case.
29-
self.measure {
30-
// Put the code you want to measure the time of here.
36+
func testPropertyWrapperWithSpecificName() {
37+
let car = Car()
38+
Workflow.defaultContainer.register(Vehicle.self, name: "car1") { _ in car }
39+
Workflow.defaultContainer.register(Vehicle.self, name: "car2") { _ in Car() }
40+
41+
class Thing {
42+
@DependencyInjected(name: "car1") var vehicle:Vehicle?
3143
}
44+
45+
XCTAssertNotNil(Thing().vehicle)
46+
XCTAssert(Thing().vehicle is Car)
47+
XCTAssert((Thing().vehicle as? Car) === car)
48+
}
49+
50+
func testPropertyWrapperWithSpecificContainer() {
51+
Workflow.defaultContainer.register(Vehicle.self) { _ in Motorcycle() }
52+
Thing.container.register(Vehicle.self) { _ in Car() }
53+
54+
class Thing {
55+
static var container:Container = Container()
56+
57+
@DependencyInjected(container: Thing.container) var vehicle:Vehicle?
58+
}
59+
60+
XCTAssert(Thing().vehicle is Car)
3261
}
3362

63+
func testPropertyWrapperShouldBeLazy() {
64+
var callbackCalled = 0
65+
Workflow.defaultContainer.register(Vehicle.self) { _ in
66+
callbackCalled += 1
67+
return Car()
68+
}
69+
70+
class Thing {
71+
@DependencyInjected var vehicle:Vehicle?
72+
}
73+
74+
let thing = Thing()
75+
76+
XCTAssertNotNil(thing.vehicle)
77+
XCTAssert(thing.vehicle is Car)
78+
XCTAssertEqual(callbackCalled, 1)
79+
}
80+
3481
}

Podfile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ target 'Workflow' do
2020
pod 'CwlCatchException', :git => 'https://github.com/mattgallagher/CwlCatchException.git', :tag => '1.2.0'
2121
# Pods for testing
2222
end
23-
23+
24+
target 'DependencyInjectionTests' do
25+
shared_pods
26+
end
27+
2428
end
2529

2630
target 'WorkflowExample' do

Podfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@ SPEC CHECKSUMS:
4747
Swinject: ddf78b8486dd9b71a667b852cad919ab4484478e
4848
UIUTest: 842c642e5bec098b1e2c890cbe25aacab80f2481
4949

50-
PODFILE CHECKSUM: 8565ffc49ba5682f88adec67c1782af970d6f385
50+
PODFILE CHECKSUM: 9446e19cd304190fb0bff5ec8aefc4f0e183127b
5151

5252
COCOAPODS: 1.8.4

Workflow.xcodeproj/project.pbxproj

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
ADEE775E233BC901007E9FD2 /* PickupOrDeliveryViewControllerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADEE775D233BC901007E9FD2 /* PickupOrDeliveryViewControllerTests.swift */; };
7070
ADEE7760233BC960007E9FD2 /* Convenience.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADEE775F233BC960007E9FD2 /* Convenience.swift */; };
7171
ADEE7763233C2D5B007E9FD2 /* WorkflowNotifications.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADEE7762233C2D5B007E9FD2 /* WorkflowNotifications.swift */; };
72+
B2488BD170D0E683391A8759 /* Pods_Workflow_DependencyInjectionTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 849E05E774E42E2863BCC8D7 /* Pods_Workflow_DependencyInjectionTests.framework */; };
7273
F93CBFAA29F3056CFE892E13 /* Pods_WorkflowTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D2B5B41A4F86FDBF12D88F62 /* Pods_WorkflowTests.framework */; };
7374
FD64E72C1869DD8B92AC3061 /* Pods_Workflow.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 93153A9A1549D7ECC6FF5A30 /* Pods_Workflow.framework */; };
7475
/* End PBXBuildFile section */
@@ -99,6 +100,7 @@
99100

100101
/* Begin PBXFileReference section */
101102
09E02615312015EBAC53492E /* Pods_WorkflowExample_WorkflowExampleTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_WorkflowExample_WorkflowExampleTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
103+
107B0A4E5A2C4DAEEEAD875D /* Pods-Workflow-DependencyInjectionTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Workflow-DependencyInjectionTests.release.xcconfig"; path = "Target Support Files/Pods-Workflow-DependencyInjectionTests/Pods-Workflow-DependencyInjectionTests.release.xcconfig"; sourceTree = "<group>"; };
102104
1D08D8FF23A97CAA00EF05C1 /* DependencyInjected.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DependencyInjected.swift; sourceTree = "<group>"; };
103105
1D08D90123A97D3400EF05C1 /* DIContainer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DIContainer.swift; sourceTree = "<group>"; };
104106
1D08D90523A9919400EF05C1 /* NetworkManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkManager.swift; sourceTree = "<group>"; };
@@ -112,6 +114,8 @@
112114
4F790BC10E63BF4E7DABAD9D /* Pods-DependencyInjection.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-DependencyInjection.debug.xcconfig"; path = "Target Support Files/Pods-DependencyInjection/Pods-DependencyInjection.debug.xcconfig"; sourceTree = "<group>"; };
113115
619C275F7E04DADACCE5319E /* Pods_DependencyInjection.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_DependencyInjection.framework; sourceTree = BUILT_PRODUCTS_DIR; };
114116
844F9C41CF2BB78EBB56D9A0 /* Pods-WorkflowExample-WorkflowExampleTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-WorkflowExample-WorkflowExampleTests.debug.xcconfig"; path = "Target Support Files/Pods-WorkflowExample-WorkflowExampleTests/Pods-WorkflowExample-WorkflowExampleTests.debug.xcconfig"; sourceTree = "<group>"; };
117+
849E05E774E42E2863BCC8D7 /* Pods_Workflow_DependencyInjectionTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Workflow_DependencyInjectionTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
118+
86ECC9D9CB8F524572DE8C56 /* Pods-Workflow-DependencyInjectionTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Workflow-DependencyInjectionTests.debug.xcconfig"; path = "Target Support Files/Pods-Workflow-DependencyInjectionTests/Pods-Workflow-DependencyInjectionTests.debug.xcconfig"; sourceTree = "<group>"; };
115119
93153A9A1549D7ECC6FF5A30 /* Pods_Workflow.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Workflow.framework; sourceTree = BUILT_PRODUCTS_DIR; };
116120
94D06747513FD57DC845C020 /* Pods-Workflow.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Workflow.debug.xcconfig"; path = "Target Support Files/Pods-Workflow/Pods-Workflow.debug.xcconfig"; sourceTree = "<group>"; };
117121
A084DFDED9C9736BB28DAAC6 /* Pods-WorkflowExample.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-WorkflowExample.debug.xcconfig"; path = "Target Support Files/Pods-WorkflowExample/Pods-WorkflowExample.debug.xcconfig"; sourceTree = "<group>"; };
@@ -191,6 +195,7 @@
191195
buildActionMask = 2147483647;
192196
files = (
193197
1D08D92523A9C03E00EF05C1 /* Workflow.framework in Frameworks */,
198+
B2488BD170D0E683391A8759 /* Pods_Workflow_DependencyInjectionTests.framework in Frameworks */,
194199
);
195200
runOnlyForDeploymentPostprocessing = 0;
196201
};
@@ -258,6 +263,7 @@
258263
F410624365BB7807E12BEA08 /* Pods_WorkflowExample.framework */,
259264
09E02615312015EBAC53492E /* Pods_WorkflowExample_WorkflowExampleTests.framework */,
260265
619C275F7E04DADACCE5319E /* Pods_DependencyInjection.framework */,
266+
849E05E774E42E2863BCC8D7 /* Pods_Workflow_DependencyInjectionTests.framework */,
261267
);
262268
name = Frameworks;
263269
sourceTree = "<group>";
@@ -469,6 +475,8 @@
469475
CF2C1E796D1CCA3C0AC77D60 /* Pods-WorkflowExample-WorkflowExampleTests.release.xcconfig */,
470476
4F790BC10E63BF4E7DABAD9D /* Pods-DependencyInjection.debug.xcconfig */,
471477
DD1CAE72F7FEDD9196FE9748 /* Pods-DependencyInjection.release.xcconfig */,
478+
86ECC9D9CB8F524572DE8C56 /* Pods-Workflow-DependencyInjectionTests.debug.xcconfig */,
479+
107B0A4E5A2C4DAEEEAD875D /* Pods-Workflow-DependencyInjectionTests.release.xcconfig */,
472480
);
473481
path = Pods;
474482
sourceTree = "<group>";
@@ -491,9 +499,11 @@
491499
isa = PBXNativeTarget;
492500
buildConfigurationList = 1D08D92323A9C01500EF05C1 /* Build configuration list for PBXNativeTarget "DependencyInjectionTests" */;
493501
buildPhases = (
502+
7C04ADAD71819A4E11FECDA9 /* [CP] Check Pods Manifest.lock */,
494503
1D08D91123A9C01500EF05C1 /* Sources */,
495504
1D08D91223A9C01500EF05C1 /* Frameworks */,
496505
1D08D91323A9C01500EF05C1 /* Resources */,
506+
61CE1BF68C2538792DD7AD1E /* [CP] Embed Pods Frameworks */,
497507
);
498508
buildRules = (
499509
);
@@ -772,6 +782,45 @@
772782
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n";
773783
showEnvVarsInLog = 0;
774784
};
785+
61CE1BF68C2538792DD7AD1E /* [CP] Embed Pods Frameworks */ = {
786+
isa = PBXShellScriptBuildPhase;
787+
buildActionMask = 2147483647;
788+
files = (
789+
);
790+
inputFileListPaths = (
791+
"${PODS_ROOT}/Target Support Files/Pods-Workflow-DependencyInjectionTests/Pods-Workflow-DependencyInjectionTests-frameworks-${CONFIGURATION}-input-files.xcfilelist",
792+
);
793+
name = "[CP] Embed Pods Frameworks";
794+
outputFileListPaths = (
795+
"${PODS_ROOT}/Target Support Files/Pods-Workflow-DependencyInjectionTests/Pods-Workflow-DependencyInjectionTests-frameworks-${CONFIGURATION}-output-files.xcfilelist",
796+
);
797+
runOnlyForDeploymentPostprocessing = 0;
798+
shellPath = /bin/sh;
799+
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-Workflow-DependencyInjectionTests/Pods-Workflow-DependencyInjectionTests-frameworks.sh\"\n";
800+
showEnvVarsInLog = 0;
801+
};
802+
7C04ADAD71819A4E11FECDA9 /* [CP] Check Pods Manifest.lock */ = {
803+
isa = PBXShellScriptBuildPhase;
804+
buildActionMask = 2147483647;
805+
files = (
806+
);
807+
inputFileListPaths = (
808+
);
809+
inputPaths = (
810+
"${PODS_PODFILE_DIR_PATH}/Podfile.lock",
811+
"${PODS_ROOT}/Manifest.lock",
812+
);
813+
name = "[CP] Check Pods Manifest.lock";
814+
outputFileListPaths = (
815+
);
816+
outputPaths = (
817+
"$(DERIVED_FILE_DIR)/Pods-Workflow-DependencyInjectionTests-checkManifestLockResult.txt",
818+
);
819+
runOnlyForDeploymentPostprocessing = 0;
820+
shellPath = /bin/sh;
821+
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n";
822+
showEnvVarsInLog = 0;
823+
};
775824
AD661141C48932B2756F7C06 /* [CP] Check Pods Manifest.lock */ = {
776825
isa = PBXShellScriptBuildPhase;
777826
buildActionMask = 2147483647;
@@ -950,6 +999,7 @@
950999
/* Begin XCBuildConfiguration section */
9511000
1D08D92023A9C01500EF05C1 /* Debug */ = {
9521001
isa = XCBuildConfiguration;
1002+
baseConfigurationReference = 86ECC9D9CB8F524572DE8C56 /* Pods-Workflow-DependencyInjectionTests.debug.xcconfig */;
9531003
buildSettings = {
9541004
CODE_SIGN_STYLE = Automatic;
9551005
INFOPLIST_FILE = DependencyInjectionTests/Info.plist;
@@ -968,6 +1018,7 @@
9681018
};
9691019
1D08D92123A9C01500EF05C1 /* Release */ = {
9701020
isa = XCBuildConfiguration;
1021+
baseConfigurationReference = 107B0A4E5A2C4DAEEEAD875D /* Pods-Workflow-DependencyInjectionTests.release.xcconfig */;
9711022
buildSettings = {
9721023
CODE_SIGN_STYLE = Automatic;
9731024
INFOPLIST_FILE = DependencyInjectionTests/Info.plist;

Workflow.xcodeproj/xcshareddata/xcschemes/Workflow.xcscheme

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@
5858
ReferencedContainer = "container:Workflow.xcodeproj">
5959
</BuildableReference>
6060
</TestableReference>
61+
<TestableReference
62+
skipped = "NO">
63+
<BuildableReference
64+
BuildableIdentifier = "primary"
65+
BlueprintIdentifier = "1D08D91423A9C01500EF05C1"
66+
BuildableName = "DependencyInjectionTests.xctest"
67+
BlueprintName = "DependencyInjectionTests"
68+
ReferencedContainer = "container:Workflow.xcodeproj">
69+
</BuildableReference>
70+
</TestableReference>
6171
</Testables>
6272
</TestAction>
6373
<LaunchAction

WorkflowExample/Ordering/LocationsViewController.swift

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,6 @@ import DynamicWorkflow
1212
class LocationsViewController: UIWorkflowItem<[Location]>, StoryboardLoadable {
1313
@IBOutlet weak var tableView:UITableView!
1414

15-
@DependencyInjected var networkManager:NetworkManager?
16-
17-
override func viewDidLoad() {
18-
networkManager?.get(URL(string: "https://www.google.com")!, completion: {
19-
switch $0 {
20-
case .success(let body): print(body)
21-
case .failure(let error): print(error)
22-
}
23-
})
24-
}
25-
2615
var locations:[Location] = []
2716
}
2817

0 commit comments

Comments
 (0)