From 8029654f39a798f29598f35de73cb6bd769f56eb Mon Sep 17 00:00:00 2001 From: Andrey Chernoprudov Date: Sat, 25 Apr 2020 21:03:14 +0500 Subject: [PATCH 01/12] property wrapper + tests --- Sources/SwiftDI/Inject.swift | 24 ++++ Tests/SwiftDITests/InjectViaWrapperTest.swift | 103 ++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 Sources/SwiftDI/Inject.swift create mode 100644 Tests/SwiftDITests/InjectViaWrapperTest.swift diff --git a/Sources/SwiftDI/Inject.swift b/Sources/SwiftDI/Inject.swift new file mode 100644 index 0000000..d0fe36f --- /dev/null +++ b/Sources/SwiftDI/Inject.swift @@ -0,0 +1,24 @@ +// +// Inject.swift +// SwiftDI +// +// Created by Andrey Chernoprudov on 25.04.2020. +// + +@propertyWrapper +public struct Inject { + // MARK: - Instance variables + + let dependency: Value + + // MARK: - Public + + public init(injector: Injector = .default, tag: String? = nil) { + let dependencyTag = tag ?? injector.config.tag + dependency = injector.resolve(Value.self, tag: dependencyTag) + } + + public var wrappedValue: Value { + return dependency + } +} diff --git a/Tests/SwiftDITests/InjectViaWrapperTest.swift b/Tests/SwiftDITests/InjectViaWrapperTest.swift new file mode 100644 index 0000000..d95bb16 --- /dev/null +++ b/Tests/SwiftDITests/InjectViaWrapperTest.swift @@ -0,0 +1,103 @@ +// +// InjectViaWrapperTest.swift +// SwiftDITests +// +// Created by Andrey Chernoprudov on 25.04.2020. +// + +import Nimble +import Quick + +@testable import SwiftDI + +class InjectViaWrapperTest: QuickSpec { + private static var customInjector: Injector! + + override func spec() { + beforeEach { + Injector.default = Injector() + } + + afterEach { + Injector.default = Injector() + Self.customInjector = nil + } + + it("inject with default injector and tag") { + struct TestStruct { + @Inject + var value: String + } + + // prepare + let expectedValue = "foo" + Injector.default.bind(String.self).with(expectedValue) + + // actions + let testStruct = TestStruct() + + // assertions + expect(testStruct.value) == expectedValue + } + + it("inject with default injector and custom tag tag") { + // prepare + let expectedValue = "foo" + + struct TestStruct { + @Inject(tag: "bar") + var value: String + } + + Injector.default.bind(String.self) + .tag("bar") + .with(expectedValue) + + // actions + let testStruct = TestStruct() + + // assertions + expect(testStruct.value) == expectedValue + } + + it("inject with custom injector") { + // prepare + Self.customInjector = Injector() + let expectedValue = "foo" + + Self.customInjector.bind(String.self) + .with(expectedValue) + + struct TestStruct { + @Inject(injector: InjectViaWrapperTest.customInjector) + var value: String + } + + // actions + let testStruct = TestStruct() + + // assertions + expect(testStruct.value) == expectedValue + } + + it("inject with custom injector") { + // prepare + Self.customInjector = Injector() + let expectedValue = "foo" + + Self.customInjector.bind(String.self) + .with(expectedValue) + + struct TestStruct { + @Inject(injector: InjectViaWrapperTest.customInjector) + var value: String + } + + // actions + let testStruct = TestStruct() + + // assertions + expect(testStruct.value) == expectedValue + } + } +} From 7d3fc6f700e9017746e55dfa59b84bccc05f655c Mon Sep 17 00:00:00 2001 From: Andrey Chernoprudov Date: Sat, 25 Apr 2020 21:03:32 +0500 Subject: [PATCH 02/12] static --- Sources/SwiftDI/Injector.swift | 4 ++++ SwiftDI.xcodeproj/project.pbxproj | 10 +++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Sources/SwiftDI/Injector.swift b/Sources/SwiftDI/Injector.swift index bb2b009..7b9396a 100644 --- a/Sources/SwiftDI/Injector.swift +++ b/Sources/SwiftDI/Injector.swift @@ -7,6 +7,10 @@ // public class Injector { + // MARK: - Static + + public static var `default` = Injector() + // MARK: - Instance variables let config: Config diff --git a/SwiftDI.xcodeproj/project.pbxproj b/SwiftDI.xcodeproj/project.pbxproj index 394178f..ac47622 100644 --- a/SwiftDI.xcodeproj/project.pbxproj +++ b/SwiftDI.xcodeproj/project.pbxproj @@ -21,6 +21,8 @@ /* End PBXAggregateTarget section */ /* Begin PBXBuildFile section */ + 552197D2245480170003C013 /* Inject.swift in Sources */ = {isa = PBXBuildFile; fileRef = 552197D1245480170003C013 /* Inject.swift */; }; + 552197D4245482130003C013 /* InjectViaWrapperTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 552197D3245482130003C013 /* InjectViaWrapperTest.swift */; }; 556EED3922BCF40E004195B8 /* InjectorConcurrencyTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 556EED3722BCF407004195B8 /* InjectorConcurrencyTest.swift */; }; 556EED3B22BD05EA004195B8 /* SimpleDependencyStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 556EED3A22BD05EA004195B8 /* SimpleDependencyStorage.swift */; }; 55FA370C2431C7DF0091CF06 /* ThreadLock.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55FA370A2431C6F20091CF06 /* ThreadLock.swift */; }; @@ -185,6 +187,8 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 552197D1245480170003C013 /* Inject.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Inject.swift; sourceTree = ""; }; + 552197D3245482130003C013 /* InjectViaWrapperTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InjectViaWrapperTest.swift; sourceTree = ""; }; 556EED3722BCF407004195B8 /* InjectorConcurrencyTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InjectorConcurrencyTest.swift; sourceTree = ""; }; 556EED3A22BD05EA004195B8 /* SimpleDependencyStorage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SimpleDependencyStorage.swift; sourceTree = ""; }; 55FA370A2431C6F20091CF06 /* ThreadLock.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThreadLock.swift; sourceTree = ""; }; @@ -445,6 +449,7 @@ children = ( OBJ_23 /* Info.plist */, 55FA371C2431D6080091CF06 /* Storage */, + OBJ_29 /* Utils */, OBJ_24 /* ConstructorInjectionTest.swift */, OBJ_25 /* InjectorInheritanceTest.swift */, OBJ_26 /* InjectorLeakingTest.swift */, @@ -452,7 +457,7 @@ OBJ_27 /* InjectorPerformanceTest.swift */, 55FEE0C52431B692008818A0 /* InjectorKeysTest.swift */, OBJ_28 /* InjectorTest.swift */, - OBJ_29 /* Utils */, + 552197D3245482130003C013 /* InjectViaWrapperTest.swift */, ); name = SwiftDITests; path = Tests/SwiftDITests; @@ -645,6 +650,7 @@ OBJ_15 /* DependencyScope.swift */, OBJ_16 /* Injector+Config.swift */, OBJ_17 /* Injector.swift */, + 552197D1245480170003C013 /* Inject.swift */, ); name = SwiftDI; path = Sources/SwiftDI; @@ -991,6 +997,7 @@ 55FA370C2431C7DF0091CF06 /* ThreadLock.swift in Sources */, 55FA37202431EC910091CF06 /* ReadWriteLockDependencyStorage.swift in Sources */, OBJ_253 /* DependencyKey.swift in Sources */, + 552197D2245480170003C013 /* Inject.swift in Sources */, 55FA37192431CB3B0091CF06 /* DependencyStorageFactory.swift in Sources */, 55FA37242431EDC40091CF06 /* NSThreadLock.swift in Sources */, 55FA37112431C9B80091CF06 /* DependencyStorageType.swift in Sources */, @@ -1030,6 +1037,7 @@ OBJ_280 /* InjectorTest.swift in Sources */, OBJ_281 /* LeakTest+Nimble.swift in Sources */, 55FA371E2431D6290091CF06 /* DependencyStorageTest.swift in Sources */, + 552197D4245482130003C013 /* InjectViaWrapperTest.swift in Sources */, OBJ_282 /* LeakTest.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; From a689814904a31b445af281ca18fb69913ec58e71 Mon Sep 17 00:00:00 2001 From: Andrey Chernoprudov Date: Sun, 26 Apr 2020 23:12:36 +0500 Subject: [PATCH 03/12] swift version check --- Tests/SwiftDITests/InjectViaWrapperTest.swift | 136 +++++++++--------- 1 file changed, 69 insertions(+), 67 deletions(-) diff --git a/Tests/SwiftDITests/InjectViaWrapperTest.swift b/Tests/SwiftDITests/InjectViaWrapperTest.swift index d95bb16..150341b 100644 --- a/Tests/SwiftDITests/InjectViaWrapperTest.swift +++ b/Tests/SwiftDITests/InjectViaWrapperTest.swift @@ -10,94 +10,96 @@ import Quick @testable import SwiftDI -class InjectViaWrapperTest: QuickSpec { - private static var customInjector: Injector! +#if swift(>=5.1) + class InjectViaWrapperTest: QuickSpec { + private static var customInjector: Injector! - override func spec() { - beforeEach { - Injector.default = Injector() - } - - afterEach { - Injector.default = Injector() - Self.customInjector = nil - } - - it("inject with default injector and tag") { - struct TestStruct { - @Inject - var value: String + override func spec() { + beforeEach { + Injector.default = Injector() } - // prepare - let expectedValue = "foo" - Injector.default.bind(String.self).with(expectedValue) + afterEach { + Injector.default = Injector() + Self.customInjector = nil + } - // actions - let testStruct = TestStruct() + it("inject with default injector and tag") { + struct TestStruct { + @Inject + var value: String + } - // assertions - expect(testStruct.value) == expectedValue - } + // prepare + let expectedValue = "foo" + Injector.default.bind(String.self).with(expectedValue) - it("inject with default injector and custom tag tag") { - // prepare - let expectedValue = "foo" + // actions + let testStruct = TestStruct() - struct TestStruct { - @Inject(tag: "bar") - var value: String + // assertions + expect(testStruct.value) == expectedValue } - Injector.default.bind(String.self) - .tag("bar") - .with(expectedValue) + it("inject with default injector and custom tag tag") { + // prepare + let expectedValue = "foo" - // actions - let testStruct = TestStruct() + struct TestStruct { + @Inject(tag: "bar") + var value: String + } - // assertions - expect(testStruct.value) == expectedValue - } + Injector.default.bind(String.self) + .tag("bar") + .with(expectedValue) - it("inject with custom injector") { - // prepare - Self.customInjector = Injector() - let expectedValue = "foo" + // actions + let testStruct = TestStruct() - Self.customInjector.bind(String.self) - .with(expectedValue) - - struct TestStruct { - @Inject(injector: InjectViaWrapperTest.customInjector) - var value: String + // assertions + expect(testStruct.value) == expectedValue } - // actions - let testStruct = TestStruct() + it("inject with custom injector") { + // prepare + Self.customInjector = Injector() + let expectedValue = "foo" - // assertions - expect(testStruct.value) == expectedValue - } + Self.customInjector.bind(String.self) + .with(expectedValue) - it("inject with custom injector") { - // prepare - Self.customInjector = Injector() - let expectedValue = "foo" + struct TestStruct { + @Inject(injector: InjectViaWrapperTest.customInjector) + var value: String + } - Self.customInjector.bind(String.self) - .with(expectedValue) + // actions + let testStruct = TestStruct() - struct TestStruct { - @Inject(injector: InjectViaWrapperTest.customInjector) - var value: String + // assertions + expect(testStruct.value) == expectedValue } - // actions - let testStruct = TestStruct() + it("inject with custom injector") { + // prepare + Self.customInjector = Injector() + let expectedValue = "foo" + + Self.customInjector.bind(String.self) + .with(expectedValue) - // assertions - expect(testStruct.value) == expectedValue + struct TestStruct { + @Inject(injector: InjectViaWrapperTest.customInjector) + var value: String + } + + // actions + let testStruct = TestStruct() + + // assertions + expect(testStruct.value) == expectedValue + } } } -} +#endif From db15ee12c11dc2f10d0c3a108ce473a292736d14 Mon Sep 17 00:00:00 2001 From: Andrey Chernoprudov Date: Sun, 26 Apr 2020 23:15:36 +0500 Subject: [PATCH 04/12] Revert "swift version check" This reverts commit a689814904a31b445af281ca18fb69913ec58e71. --- Tests/SwiftDITests/InjectViaWrapperTest.swift | 136 +++++++++--------- 1 file changed, 67 insertions(+), 69 deletions(-) diff --git a/Tests/SwiftDITests/InjectViaWrapperTest.swift b/Tests/SwiftDITests/InjectViaWrapperTest.swift index 150341b..d95bb16 100644 --- a/Tests/SwiftDITests/InjectViaWrapperTest.swift +++ b/Tests/SwiftDITests/InjectViaWrapperTest.swift @@ -10,96 +10,94 @@ import Quick @testable import SwiftDI -#if swift(>=5.1) - class InjectViaWrapperTest: QuickSpec { - private static var customInjector: Injector! +class InjectViaWrapperTest: QuickSpec { + private static var customInjector: Injector! - override func spec() { - beforeEach { - Injector.default = Injector() - } - - afterEach { - Injector.default = Injector() - Self.customInjector = nil - } - - it("inject with default injector and tag") { - struct TestStruct { - @Inject - var value: String - } - - // prepare - let expectedValue = "foo" - Injector.default.bind(String.self).with(expectedValue) + override func spec() { + beforeEach { + Injector.default = Injector() + } - // actions - let testStruct = TestStruct() + afterEach { + Injector.default = Injector() + Self.customInjector = nil + } - // assertions - expect(testStruct.value) == expectedValue + it("inject with default injector and tag") { + struct TestStruct { + @Inject + var value: String } - it("inject with default injector and custom tag tag") { - // prepare - let expectedValue = "foo" + // prepare + let expectedValue = "foo" + Injector.default.bind(String.self).with(expectedValue) - struct TestStruct { - @Inject(tag: "bar") - var value: String - } + // actions + let testStruct = TestStruct() - Injector.default.bind(String.self) - .tag("bar") - .with(expectedValue) + // assertions + expect(testStruct.value) == expectedValue + } - // actions - let testStruct = TestStruct() + it("inject with default injector and custom tag tag") { + // prepare + let expectedValue = "foo" - // assertions - expect(testStruct.value) == expectedValue + struct TestStruct { + @Inject(tag: "bar") + var value: String } - it("inject with custom injector") { - // prepare - Self.customInjector = Injector() - let expectedValue = "foo" + Injector.default.bind(String.self) + .tag("bar") + .with(expectedValue) + + // actions + let testStruct = TestStruct() - Self.customInjector.bind(String.self) - .with(expectedValue) + // assertions + expect(testStruct.value) == expectedValue + } - struct TestStruct { - @Inject(injector: InjectViaWrapperTest.customInjector) - var value: String - } + it("inject with custom injector") { + // prepare + Self.customInjector = Injector() + let expectedValue = "foo" - // actions - let testStruct = TestStruct() + Self.customInjector.bind(String.self) + .with(expectedValue) - // assertions - expect(testStruct.value) == expectedValue + struct TestStruct { + @Inject(injector: InjectViaWrapperTest.customInjector) + var value: String } - it("inject with custom injector") { - // prepare - Self.customInjector = Injector() - let expectedValue = "foo" + // actions + let testStruct = TestStruct() - Self.customInjector.bind(String.self) - .with(expectedValue) + // assertions + expect(testStruct.value) == expectedValue + } - struct TestStruct { - @Inject(injector: InjectViaWrapperTest.customInjector) - var value: String - } + it("inject with custom injector") { + // prepare + Self.customInjector = Injector() + let expectedValue = "foo" - // actions - let testStruct = TestStruct() + Self.customInjector.bind(String.self) + .with(expectedValue) - // assertions - expect(testStruct.value) == expectedValue + struct TestStruct { + @Inject(injector: InjectViaWrapperTest.customInjector) + var value: String } + + // actions + let testStruct = TestStruct() + + // assertions + expect(testStruct.value) == expectedValue } } -#endif +} From e9f9931985a3f8d3ee71998455a3e6fcc449ddc1 Mon Sep 17 00:00:00 2001 From: Andrey Chernoprudov Date: Sun, 26 Apr 2020 23:25:41 +0500 Subject: [PATCH 05/12] swift version --- Sources/SwiftDI/Inject.swift | 28 ++-- SwiftDI.xcodeproj/project.pbxproj | 4 +- Tests/SwiftDITests/InjectViaWrapperTest.swift | 136 +++++++++--------- 3 files changed, 89 insertions(+), 79 deletions(-) diff --git a/Sources/SwiftDI/Inject.swift b/Sources/SwiftDI/Inject.swift index d0fe36f..a7510a4 100644 --- a/Sources/SwiftDI/Inject.swift +++ b/Sources/SwiftDI/Inject.swift @@ -5,20 +5,24 @@ // Created by Andrey Chernoprudov on 25.04.2020. // -@propertyWrapper -public struct Inject { - // MARK: - Instance variables +#if swift(>=5.1) - let dependency: Value + @propertyWrapper + public struct Inject { + // MARK: - Instance variables - // MARK: - Public + let dependency: Value - public init(injector: Injector = .default, tag: String? = nil) { - let dependencyTag = tag ?? injector.config.tag - dependency = injector.resolve(Value.self, tag: dependencyTag) - } + // MARK: - Public + + public init(injector: Injector = .default, tag: String? = nil) { + let dependencyTag = tag ?? injector.config.tag + dependency = injector.resolve(Value.self, tag: dependencyTag) + } - public var wrappedValue: Value { - return dependency + public var wrappedValue: Value { + return dependency + } } -} + +#endif diff --git a/SwiftDI.xcodeproj/project.pbxproj b/SwiftDI.xcodeproj/project.pbxproj index ac47622..dcfbe10 100644 --- a/SwiftDI.xcodeproj/project.pbxproj +++ b/SwiftDI.xcodeproj/project.pbxproj @@ -205,7 +205,7 @@ 55FA37252431EDD50091CF06 /* SpinThreadLock.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SpinThreadLock.swift; sourceTree = ""; }; 55FA37272431EDF00091CF06 /* ReadWriteLock.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReadWriteLock.swift; sourceTree = ""; }; 55FEE0C52431B692008818A0 /* InjectorKeysTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InjectorKeysTest.swift; sourceTree = ""; }; - "Nimble::Nimble::Product" /* Nimble.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Nimble.framework; sourceTree = BUILT_PRODUCTS_DIR; }; + "Nimble::Nimble::Product" /* Nimble.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = Nimble.framework; sourceTree = BUILT_PRODUCTS_DIR; }; OBJ_10 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; OBJ_100 /* Contain.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Contain.swift; sourceTree = ""; }; OBJ_101 /* ContainElementSatisfying.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContainElementSatisfying.swift; sourceTree = ""; }; @@ -1099,6 +1099,7 @@ PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; SKIP_INSTALL = YES; SWIFT_ACTIVE_COMPILATION_CONDITIONS = "$(inherited)"; + SWIFT_SUPPRESS_WARNINGS = YES; SWIFT_VERSION = 5.0; TARGET_NAME = Nimble; TVOS_DEPLOYMENT_TARGET = 9.0; @@ -1127,6 +1128,7 @@ PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; SKIP_INSTALL = YES; SWIFT_ACTIVE_COMPILATION_CONDITIONS = "$(inherited)"; + SWIFT_SUPPRESS_WARNINGS = YES; SWIFT_VERSION = 5.0; TARGET_NAME = Nimble; TVOS_DEPLOYMENT_TARGET = 9.0; diff --git a/Tests/SwiftDITests/InjectViaWrapperTest.swift b/Tests/SwiftDITests/InjectViaWrapperTest.swift index d95bb16..d992a07 100644 --- a/Tests/SwiftDITests/InjectViaWrapperTest.swift +++ b/Tests/SwiftDITests/InjectViaWrapperTest.swift @@ -10,94 +10,98 @@ import Quick @testable import SwiftDI -class InjectViaWrapperTest: QuickSpec { - private static var customInjector: Injector! +#if swift(>=5.1) - override func spec() { - beforeEach { - Injector.default = Injector() - } + class InjectViaWrapperTest: QuickSpec { + private static var customInjector: Injector! - afterEach { - Injector.default = Injector() - Self.customInjector = nil - } + override func spec() { + beforeEach { + Injector.default = Injector() + } - it("inject with default injector and tag") { - struct TestStruct { - @Inject - var value: String + afterEach { + Injector.default = Injector() + Self.customInjector = nil } - // prepare - let expectedValue = "foo" - Injector.default.bind(String.self).with(expectedValue) + it("inject with default injector and tag") { + struct TestStruct { + @Inject + var value: String + } - // actions - let testStruct = TestStruct() + // prepare + let expectedValue = "foo" + Injector.default.bind(String.self).with(expectedValue) - // assertions - expect(testStruct.value) == expectedValue - } + // actions + let testStruct = TestStruct() - it("inject with default injector and custom tag tag") { - // prepare - let expectedValue = "foo" - - struct TestStruct { - @Inject(tag: "bar") - var value: String + // assertions + expect(testStruct.value) == expectedValue } - Injector.default.bind(String.self) - .tag("bar") - .with(expectedValue) - - // actions - let testStruct = TestStruct() + it("inject with default injector and custom tag tag") { + // prepare + let expectedValue = "foo" - // assertions - expect(testStruct.value) == expectedValue - } + struct TestStruct { + @Inject(tag: "bar") + var value: String + } - it("inject with custom injector") { - // prepare - Self.customInjector = Injector() - let expectedValue = "foo" + Injector.default.bind(String.self) + .tag("bar") + .with(expectedValue) - Self.customInjector.bind(String.self) - .with(expectedValue) + // actions + let testStruct = TestStruct() - struct TestStruct { - @Inject(injector: InjectViaWrapperTest.customInjector) - var value: String + // assertions + expect(testStruct.value) == expectedValue } - // actions - let testStruct = TestStruct() + it("inject with custom injector") { + // prepare + Self.customInjector = Injector() + let expectedValue = "foo" - // assertions - expect(testStruct.value) == expectedValue - } + Self.customInjector.bind(String.self) + .with(expectedValue) - it("inject with custom injector") { - // prepare - Self.customInjector = Injector() - let expectedValue = "foo" + struct TestStruct { + @Inject(injector: InjectViaWrapperTest.customInjector) + var value: String + } - Self.customInjector.bind(String.self) - .with(expectedValue) + // actions + let testStruct = TestStruct() - struct TestStruct { - @Inject(injector: InjectViaWrapperTest.customInjector) - var value: String + // assertions + expect(testStruct.value) == expectedValue } - // actions - let testStruct = TestStruct() + it("inject with custom injector") { + // prepare + Self.customInjector = Injector() + let expectedValue = "foo" + + Self.customInjector.bind(String.self) + .with(expectedValue) + + struct TestStruct { + @Inject(injector: InjectViaWrapperTest.customInjector) + var value: String + } - // assertions - expect(testStruct.value) == expectedValue + // actions + let testStruct = TestStruct() + + // assertions + expect(testStruct.value) == expectedValue + } } } -} + +#endif From 73ca942af31b1586922b1c809118e760f4cad3a8 Mon Sep 17 00:00:00 2001 From: Andrey Chernoprudov Date: Sun, 26 Apr 2020 23:33:15 +0500 Subject: [PATCH 06/12] test struct to class --- Tests/SwiftDITests/InjectViaWrapperTest.swift | 136 +++++++++--------- 1 file changed, 66 insertions(+), 70 deletions(-) diff --git a/Tests/SwiftDITests/InjectViaWrapperTest.swift b/Tests/SwiftDITests/InjectViaWrapperTest.swift index d992a07..a5bf5c7 100644 --- a/Tests/SwiftDITests/InjectViaWrapperTest.swift +++ b/Tests/SwiftDITests/InjectViaWrapperTest.swift @@ -10,98 +10,94 @@ import Quick @testable import SwiftDI -#if swift(>=5.1) +class InjectViaWrapperTest: QuickSpec { + private static var customInjector: Injector! - class InjectViaWrapperTest: QuickSpec { - private static var customInjector: Injector! + override func spec() { + beforeEach { + Injector.default = Injector() + } - override func spec() { - beforeEach { - Injector.default = Injector() - } + afterEach { + Injector.default = Injector() + Self.customInjector = nil + } - afterEach { - Injector.default = Injector() - Self.customInjector = nil + it("inject with default injector and tag") { + class TestObject { + @Inject + var value: String } - it("inject with default injector and tag") { - struct TestStruct { - @Inject - var value: String - } + // prepare + let expectedValue = "foo" + Injector.default.bind(String.self).with(expectedValue) - // prepare - let expectedValue = "foo" - Injector.default.bind(String.self).with(expectedValue) + // actions + let testObject = TestObject() - // actions - let testStruct = TestStruct() + // assertions + expect(testObject.value) == expectedValue + } - // assertions - expect(testStruct.value) == expectedValue + it("inject with default injector and custom tag tag") { + // prepare + let expectedValue = "foo" + + class TestObject { + @Inject(tag: "bar") + var value: String } - it("inject with default injector and custom tag tag") { - // prepare - let expectedValue = "foo" + Injector.default.bind(String.self) + .tag("bar") + .with(expectedValue) + + // actions + let testObject = TestObject() - struct TestStruct { - @Inject(tag: "bar") - var value: String - } + // assertions + expect(testObject.value) == expectedValue + } - Injector.default.bind(String.self) - .tag("bar") - .with(expectedValue) + it("inject with custom injector") { + // prepare + Self.customInjector = Injector() + let expectedValue = "foo" - // actions - let testStruct = TestStruct() + Self.customInjector.bind(String.self) + .with(expectedValue) - // assertions - expect(testStruct.value) == expectedValue + class TestObject { + @Inject(injector: InjectViaWrapperTest.customInjector) + var value: String } - it("inject with custom injector") { - // prepare - Self.customInjector = Injector() - let expectedValue = "foo" + // actions + let testObject = TestObject() - Self.customInjector.bind(String.self) - .with(expectedValue) + // assertions + expect(testObject.value) == expectedValue + } - struct TestStruct { - @Inject(injector: InjectViaWrapperTest.customInjector) - var value: String - } + it("inject with custom injector") { + // prepare + Self.customInjector = Injector() + let expectedValue = "foo" - // actions - let testStruct = TestStruct() + Self.customInjector.bind(String.self) + .with(expectedValue) - // assertions - expect(testStruct.value) == expectedValue + class TestObject { + @Inject(injector: InjectViaWrapperTest.customInjector) + var value: String } - it("inject with custom injector") { - // prepare - Self.customInjector = Injector() - let expectedValue = "foo" - - Self.customInjector.bind(String.self) - .with(expectedValue) - - struct TestStruct { - @Inject(injector: InjectViaWrapperTest.customInjector) - var value: String - } + // actions + let testObject = TestObject() - // actions - let testStruct = TestStruct() - - // assertions - expect(testStruct.value) == expectedValue - } + // assertions + expect(testObject.value) == expectedValue } } - -#endif +} From 0c604a1d7a22e6e5e6f36a6c4e40e0803e625577 Mon Sep 17 00:00:00 2001 From: Andrey Chernoprudov Date: Mon, 27 Apr 2020 00:16:05 +0500 Subject: [PATCH 07/12] optional inject --- Sources/SwiftDI/Inject.swift | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Sources/SwiftDI/Inject.swift b/Sources/SwiftDI/Inject.swift index a7510a4..e4e6fe7 100644 --- a/Sources/SwiftDI/Inject.swift +++ b/Sources/SwiftDI/Inject.swift @@ -25,4 +25,22 @@ } } + @propertyWrapper + public struct OptionalInject { + // MARK: - Instance variables + + let dependency: Value? + + // MARK: - Public + + public init(injector: Injector = .default, tag: String? = nil) { + let dependencyTag = tag ?? injector.config.tag + dependency = injector.resolveSafe(Value.self, tag: dependencyTag) + } + + public var wrappedValue: Value? { + return dependency + } + } + #endif From da88aa8f7ad3c7e4ad7ae40b97357338f66e2f1f Mon Sep 17 00:00:00 2001 From: Andrey Chernoprudov Date: Mon, 27 Apr 2020 00:16:14 +0500 Subject: [PATCH 08/12] optional inject tests --- README.md | 1 + Tests/SwiftDITests/InjectViaWrapperTest.swift | 173 +++++++++++------- 2 files changed, 107 insertions(+), 67 deletions(-) diff --git a/README.md b/README.md index 6f4aa2b..01bb896 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ # SwiftDI +![Github Actions](https://github.com/achernoprudov/SwiftDI/workflows/Swift/badge.svg) [![Travis CI](https://travis-ci.org/achernoprudov/SwiftDI.svg?branch=master)](https://travis-ci.org/achernoprudov/SwiftDI) [![CocoaPods Version](https://img.shields.io/cocoapods/v/SwiftDI.svg?style=flat)](http://cocoapods.org/pods/SwiftDI) [![License](https://img.shields.io/cocoapods/l/SwiftDI.svg?style=flat)](http://cocoapods.org/pods/SwiftDI) diff --git a/Tests/SwiftDITests/InjectViaWrapperTest.swift b/Tests/SwiftDITests/InjectViaWrapperTest.swift index a5bf5c7..64c655d 100644 --- a/Tests/SwiftDITests/InjectViaWrapperTest.swift +++ b/Tests/SwiftDITests/InjectViaWrapperTest.swift @@ -10,94 +10,133 @@ import Quick @testable import SwiftDI -class InjectViaWrapperTest: QuickSpec { - private static var customInjector: Injector! +#if swift(>=5.1) - override func spec() { - beforeEach { - Injector.default = Injector() - } + class InjectViaWrapperTest: QuickSpec { + private static var customInjector: Injector! - afterEach { - Injector.default = Injector() - Self.customInjector = nil - } + override func spec() { + beforeEach { + Injector.default = Injector() + } - it("inject with default injector and tag") { - class TestObject { - @Inject - var value: String + afterEach { + Injector.default = Injector() + Self.customInjector = nil } - // prepare - let expectedValue = "foo" - Injector.default.bind(String.self).with(expectedValue) + describe("inject") { + it("with default injector and tag") { + class TestObject { + @Inject + var value: String + } - // actions - let testObject = TestObject() + // prepare + let expectedValue = "foo" + Injector.default.bind(String.self).with(expectedValue) - // assertions - expect(testObject.value) == expectedValue - } + // actions + let testObject = TestObject() - it("inject with default injector and custom tag tag") { - // prepare - let expectedValue = "foo" + // assertions + expect(testObject.value) == expectedValue + } - class TestObject { - @Inject(tag: "bar") - var value: String - } + it("with default injector and custom tag tag") { + // prepare + let expectedValue = "foo" - Injector.default.bind(String.self) - .tag("bar") - .with(expectedValue) + class TestObject { + @Inject(tag: "bar") + var value: String + } - // actions - let testObject = TestObject() + Injector.default.bind(String.self) + .tag("bar") + .with(expectedValue) - // assertions - expect(testObject.value) == expectedValue - } + // actions + let testObject = TestObject() - it("inject with custom injector") { - // prepare - Self.customInjector = Injector() - let expectedValue = "foo" + // assertions + expect(testObject.value) == expectedValue + } - Self.customInjector.bind(String.self) - .with(expectedValue) + it("with custom injector") { + // prepare + Self.customInjector = Injector() + let expectedValue = "foo" - class TestObject { - @Inject(injector: InjectViaWrapperTest.customInjector) - var value: String - } + Self.customInjector.bind(String.self) + .with(expectedValue) - // actions - let testObject = TestObject() + class TestObject { + @Inject(injector: InjectViaWrapperTest.customInjector) + var value: String + } - // assertions - expect(testObject.value) == expectedValue - } + // actions + let testObject = TestObject() - it("inject with custom injector") { - // prepare - Self.customInjector = Injector() - let expectedValue = "foo" + // assertions + expect(testObject.value) == expectedValue + } - Self.customInjector.bind(String.self) - .with(expectedValue) + it("with custom injector") { + // prepare + Self.customInjector = Injector() + let expectedValue = "foo" - class TestObject { - @Inject(injector: InjectViaWrapperTest.customInjector) - var value: String - } + Self.customInjector.bind(String.self) + .with(expectedValue) - // actions - let testObject = TestObject() + class TestObject { + @Inject(injector: InjectViaWrapperTest.customInjector) + var value: String + } - // assertions - expect(testObject.value) == expectedValue + // actions + let testObject = TestObject() + + // assertions + expect(testObject.value) == expectedValue + } + } + + describe("optional inject") { + it("sets dependency") { + class TestObject { + @OptionalInject + var value: String? + } + + // prepare + let expectedValue = "foo" + Injector.default.bind(String.self).with(expectedValue) + + // actions + let testObject = TestObject() + + // assertions + expect(testObject.value) == expectedValue + } + + it("sets nil") { + // prepare + class TestObject { + @OptionalInject + var value: String? + } + + // actions + let testObject = TestObject() + + // assertions + expect(testObject.value).to(beNil()) + } + } } } -} + +#endif From 149fa5f977f91715d4d7894bf78a72c61160cccb Mon Sep 17 00:00:00 2001 From: Andrey Chernoprudov Date: Mon, 27 Apr 2020 00:22:08 +0500 Subject: [PATCH 09/12] add compiler if --- Tests/SwiftDITests/InjectViaWrapperTest.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/SwiftDITests/InjectViaWrapperTest.swift b/Tests/SwiftDITests/InjectViaWrapperTest.swift index 64c655d..e6eb326 100644 --- a/Tests/SwiftDITests/InjectViaWrapperTest.swift +++ b/Tests/SwiftDITests/InjectViaWrapperTest.swift @@ -10,7 +10,7 @@ import Quick @testable import SwiftDI -#if swift(>=5.1) +#if swift(>=4.1) && compiler(>=5.0) class InjectViaWrapperTest: QuickSpec { private static var customInjector: Injector! From 942531770c5b9288ad54b5d7e4b59769ef5d1d1a Mon Sep 17 00:00:00 2001 From: Andrey Chernoprudov Date: Mon, 27 Apr 2020 00:25:10 +0500 Subject: [PATCH 10/12] fix compiler check --- Tests/SwiftDITests/InjectViaWrapperTest.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/SwiftDITests/InjectViaWrapperTest.swift b/Tests/SwiftDITests/InjectViaWrapperTest.swift index e6eb326..b34b581 100644 --- a/Tests/SwiftDITests/InjectViaWrapperTest.swift +++ b/Tests/SwiftDITests/InjectViaWrapperTest.swift @@ -10,7 +10,7 @@ import Quick @testable import SwiftDI -#if swift(>=4.1) && compiler(>=5.0) +#if swift(>=5.1) && compiler(>=5.1) class InjectViaWrapperTest: QuickSpec { private static var customInjector: Injector! From 6cb52b82ce01579e3046e65c79db38f2f5edf6c5 Mon Sep 17 00:00:00 2001 From: Andrey Chernoprudov Date: Mon, 27 Apr 2020 00:31:56 +0500 Subject: [PATCH 11/12] update travis config --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 7f34abb..36a5a86 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,5 @@ language: objective-c -osx_image: xcode10.2 +osx_image: xcode11.3 script: - swift build From 5bd904b4685c18550f55bf9840a1f2d2ca3354f6 Mon Sep 17 00:00:00 2001 From: Andrey Chernoprudov Date: Wed, 22 Jul 2020 15:41:09 +0500 Subject: [PATCH 12/12] update travis image --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 36a5a86..3312a22 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,5 @@ language: objective-c -osx_image: xcode11.3 +osx_image: xcode11.6 script: - swift build