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

Propery wrapper #14

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: objective-c
osx_image: xcode10.2
osx_image: xcode11.6

script:
- swift build
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
46 changes: 46 additions & 0 deletions Sources/SwiftDI/Inject.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// Inject.swift
// SwiftDI
//
// Created by Andrey Chernoprudov on 25.04.2020.
//

#if swift(>=5.1)

@propertyWrapper
public struct Inject<Value> {
// 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
}
}

@propertyWrapper
public struct OptionalInject<Value> {
// 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
4 changes: 4 additions & 0 deletions Sources/SwiftDI/Injector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
//

public class Injector {
// MARK: - Static

public static var `default` = Injector()

// MARK: - Instance variables

let config: Config
Expand Down
14 changes: 12 additions & 2 deletions SwiftDI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -185,6 +187,8 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
552197D1245480170003C013 /* Inject.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Inject.swift; sourceTree = "<group>"; };
552197D3245482130003C013 /* InjectViaWrapperTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InjectViaWrapperTest.swift; sourceTree = "<group>"; };
556EED3722BCF407004195B8 /* InjectorConcurrencyTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InjectorConcurrencyTest.swift; sourceTree = "<group>"; };
556EED3A22BD05EA004195B8 /* SimpleDependencyStorage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SimpleDependencyStorage.swift; sourceTree = "<group>"; };
55FA370A2431C6F20091CF06 /* ThreadLock.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThreadLock.swift; sourceTree = "<group>"; };
Expand All @@ -201,7 +205,7 @@
55FA37252431EDD50091CF06 /* SpinThreadLock.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SpinThreadLock.swift; sourceTree = "<group>"; };
55FA37272431EDF00091CF06 /* ReadWriteLock.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReadWriteLock.swift; sourceTree = "<group>"; };
55FEE0C52431B692008818A0 /* InjectorKeysTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InjectorKeysTest.swift; sourceTree = "<group>"; };
"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 = "<group>"; };
OBJ_100 /* Contain.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Contain.swift; sourceTree = "<group>"; };
OBJ_101 /* ContainElementSatisfying.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContainElementSatisfying.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -445,14 +449,15 @@
children = (
OBJ_23 /* Info.plist */,
55FA371C2431D6080091CF06 /* Storage */,
OBJ_29 /* Utils */,
OBJ_24 /* ConstructorInjectionTest.swift */,
OBJ_25 /* InjectorInheritanceTest.swift */,
OBJ_26 /* InjectorLeakingTest.swift */,
556EED3722BCF407004195B8 /* InjectorConcurrencyTest.swift */,
OBJ_27 /* InjectorPerformanceTest.swift */,
55FEE0C52431B692008818A0 /* InjectorKeysTest.swift */,
OBJ_28 /* InjectorTest.swift */,
OBJ_29 /* Utils */,
552197D3245482130003C013 /* InjectViaWrapperTest.swift */,
);
name = SwiftDITests;
path = Tests/SwiftDITests;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -1091,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;
Expand Down Expand Up @@ -1119,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;
Expand Down
142 changes: 142 additions & 0 deletions Tests/SwiftDITests/InjectViaWrapperTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
//
// InjectViaWrapperTest.swift
// SwiftDITests
//
// Created by Andrey Chernoprudov on 25.04.2020.
//

import Nimble
import Quick

@testable import SwiftDI

#if swift(>=5.1) && compiler(>=5.1)

class InjectViaWrapperTest: QuickSpec {
private static var customInjector: Injector!

override func spec() {
beforeEach {
Injector.default = Injector()
}

afterEach {
Injector.default = Injector()
Self.customInjector = nil
}

describe("inject") {
it("with default injector and tag") {
class TestObject {
@Inject
var value: String
}

// prepare
let expectedValue = "foo"
Injector.default.bind(String.self).with(expectedValue)

// actions
let testObject = TestObject()

// assertions
expect(testObject.value) == expectedValue
}

it("with default injector and custom tag tag") {
// prepare
let expectedValue = "foo"

class TestObject {
@Inject(tag: "bar")
var value: String
}

Injector.default.bind(String.self)
.tag("bar")
.with(expectedValue)

// actions
let testObject = TestObject()

// assertions
expect(testObject.value) == expectedValue
}

it("with custom injector") {
// prepare
Self.customInjector = Injector()
let expectedValue = "foo"

Self.customInjector.bind(String.self)
.with(expectedValue)

class TestObject {
@Inject(injector: InjectViaWrapperTest.customInjector)
var value: String
}

// actions
let testObject = TestObject()

// assertions
expect(testObject.value) == expectedValue
}

it("with custom injector") {
// prepare
Self.customInjector = Injector()
let expectedValue = "foo"

Self.customInjector.bind(String.self)
.with(expectedValue)

class TestObject {
@Inject(injector: InjectViaWrapperTest.customInjector)
var value: String
}

// 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