From dd416a85a1db7015c5fe805dd93271a64bac0137 Mon Sep 17 00:00:00 2001 From: Javier Cicchelli Date: Sun, 16 Apr 2023 17:13:52 +0200 Subject: [PATCH] Implemented the DependencyService service --- .../Services/DependencyService.swift | 28 +++++++++ Tests/Dependencies/Helpers/TestServices.swift | 34 +++++++++++ .../Services/DependencyServiceTests.swift | 57 +++++++++++++++++++ 3 files changed, 119 insertions(+) create mode 100644 Sources/Dependencies/Services/DependencyService.swift create mode 100644 Tests/Dependencies/Helpers/TestServices.swift create mode 100644 Tests/Dependencies/Services/DependencyServiceTests.swift diff --git a/Sources/Dependencies/Services/DependencyService.swift b/Sources/Dependencies/Services/DependencyService.swift new file mode 100644 index 0000000..3915ce8 --- /dev/null +++ b/Sources/Dependencies/Services/DependencyService.swift @@ -0,0 +1,28 @@ +// +// DependencyService.swift +// Dependencies +// +// Created by Javier Cicchelli on 11/04/2023. +// Copyright © 2023 Röck+Cöde. All rights reserved. +// + +/// This service provide write/read access to the injected dependencies. +public struct DependencyService { + + // MARK: Properties + + private static var current = DependencyService() + + // MARK: Subscripts + + public static subscript(key: DK.Type) -> DK.Value { + get { key.currentValue } + set { key.currentValue = newValue } + } + + public static subscript(_ keyPath: WritableKeyPath) -> D { + get { current[keyPath: keyPath] } + set { current[keyPath: keyPath] = newValue } + } + +} diff --git a/Tests/Dependencies/Helpers/TestServices.swift b/Tests/Dependencies/Helpers/TestServices.swift new file mode 100644 index 0000000..c5d28ad --- /dev/null +++ b/Tests/Dependencies/Helpers/TestServices.swift @@ -0,0 +1,34 @@ +// +// TestServices.swift +// DependenciesTests +// +// Created by Javier Cicchelli on 11/04/2023. +// Copyright © 2023 Röck+Cöde. All rights reserved. +// + +import Dependencies + +// MARK: - Protocols + +protocol TestService {} + +// MARK: - Services + +struct SomeService: TestService, Equatable {} +struct SomeOtherService: TestService, Equatable {} + +// MARK: - DependencyKey + +struct TestServiceKey: DependencyKey { + static var currentValue: TestService = SomeService() +} + +// MARK: - DependencyService+Keys + +extension DependencyService { + var testService: TestService { + get { Self[TestServiceKey.self] } + set { Self[TestServiceKey.self] = newValue } + } +} + diff --git a/Tests/Dependencies/Services/DependencyServiceTests.swift b/Tests/Dependencies/Services/DependencyServiceTests.swift new file mode 100644 index 0000000..48bb5bc --- /dev/null +++ b/Tests/Dependencies/Services/DependencyServiceTests.swift @@ -0,0 +1,57 @@ +// +// DependencyServiceTests.swift +// DependenciesTests +// +// Created by Javier Cicchelli on 11/04/2023. +// Copyright © 2023 Röck+Cöde. All rights reserved. +// + +import Dependencies +import XCTest + +final class DependencyServiceTests: XCTestCase { + + // MARK: Setup + + override func setUp() { + DependencyService[\.testService] = SomeService() + } + + // MARK: Tests + + func test_readDependencyKey() async throws { + // GIVEN + // WHEN + let service = DependencyService[\.testService] + + // THEN + XCTAssertNotNil(service) + XCTAssert(service is SomeService) + } + + func test_writeDependencyKey() async throws { + // GIVEN + DependencyService[\.testService] = SomeOtherService() + + // WHEN + let service = DependencyService[\.testService] + + // THEN + XCTAssertNotNil(service) + XCTAssert(service is SomeOtherService) + } + + func test_writeDependencyKeyTwice() async throws { + // GIVEN + DependencyService[\.testService] = SomeOtherService() + DependencyService[\.testService] = SomeService() + + // WHEN + let service = DependencyService[\.testService] + + // THEN + XCTAssertNotNil(service) + XCTAssert(service is SomeService) + } + +}