diff --git a/README.md b/README.md index 44db118..65a86f5 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Grove is available through the Swift Package Manager. To install it, simply add ```swift dependencies: [ - .package(url: "https://github.com/willowtreeapps/grove.git", from: "1.1.0") + .package(url: "https://github.com/willowtreeapps/grove.git", from: "1.1.1") ] ``` diff --git a/Sources/Grove/PropertyWrapper.swift b/Sources/Grove/PropertyWrapper.swift index 2344180..b6b8b3f 100644 --- a/Sources/Grove/PropertyWrapper.swift +++ b/Sources/Grove/PropertyWrapper.swift @@ -31,20 +31,26 @@ public class Resolve: @unchecked Sendable { } public var wrappedValue: Dependency { - switch container.scope(for: Dependency.self) { - case .singleton: - return container.resolve() - case .transient: - transientInstanceLock.lock() - defer { - transientInstanceLock.unlock() - } - guard let transientInstance else { - let transientInstance = (container.resolve() as Dependency) - self.transientInstance = transientInstance + get { + switch container.scope(for: Dependency.self) { + case .singleton: + return container.resolve() + case .transient: + transientInstanceLock.lock() + defer { + transientInstanceLock.unlock() + } + guard let transientInstance else { + let transientInstance = (container.resolve() as Dependency) + self.transientInstance = transientInstance + return transientInstance + } return transientInstance } - return transientInstance + } + + set { + /* No-Op */ } } } diff --git a/Tests/GroveTests/GrovePropertyWrapperTests.swift b/Tests/GroveTests/GrovePropertyWrapperTests.swift index 518ffdd..22b1c47 100644 --- a/Tests/GroveTests/GrovePropertyWrapperTests.swift +++ b/Tests/GroveTests/GrovePropertyWrapperTests.swift @@ -8,15 +8,27 @@ import XCTest @testable import Grove -private final class TestClass: TestProtocol { +fileprivate struct TestStruct { var value: Int +} + +fileprivate protocol TestProtocol { + var type: TestStruct { get set } + var value: Int { get } + func increment() +} + +private final class TestClass: TestProtocol { + var type: TestStruct + + var value: Int { type.value } init(value: Int = 0) { - self.value = value + self.type = TestStruct(value: value) } func increment() { - value += 1 + type.value += 1 } } @@ -49,10 +61,22 @@ final class GrovePropertyWrapperTests: XCTestCase { XCTAssertEqual(testClass.value, 103) XCTAssertEqual(testClass2.value, 100) } + + func testWritingToPropertyDirectly() { + // Given + Grove.defaultContainer.register(as: TestProtocol.self, scope: .singleton, TestClass(value: 10)) + @Resolve(TestProtocol.self) var testClass + + // When + testClass.type.value = 20 + + // Then + XCTAssertEqual(testClass.value, 20) + } } final class GrovePropertyWrapperAsClassTests: XCTestCase { - @Resolve(TestProtocol.self) var testClass + @Resolve(TestProtocol.self) private var testClass func testUpdatedRegistration() { // Given diff --git a/Tests/GroveTests/GroveTests.swift b/Tests/GroveTests/GroveTests.swift index 7bd4a1d..87d72d1 100644 --- a/Tests/GroveTests/GroveTests.swift +++ b/Tests/GroveTests/GroveTests.swift @@ -1,7 +1,7 @@ import XCTest @testable import Grove -protocol TestProtocol { +private protocol TestProtocol { var value: Int { get } func increment() }