-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CAT-104] UserDefaultsClient 모듈 추가 (#16)
- Loading branch information
1 parent
ccc826b
commit 8e75679
Showing
17 changed files
with
260 additions
and
41 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 0 additions & 30 deletions
30
Projects/Core/KeychainClient/Testing/KeychainClientTesting.swift
This file was deleted.
Oops, something went wrong.
44 changes: 44 additions & 0 deletions
44
Projects/Core/UserDefaultsClient/Interface/UserDefaultsClientInterface.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// | ||
// UserDefaultsClientInterface.swift | ||
// UserDefaultsClient | ||
// | ||
// Created by devMinseok on 8/4/24. | ||
// | ||
|
||
import Foundation | ||
|
||
import Dependencies | ||
import DependenciesMacros | ||
|
||
@DependencyClient | ||
public struct UserDefaultsClient { | ||
// MARK: - Create, Update | ||
|
||
public var setString: @Sendable (String?, _ key: String) async -> Void | ||
public var setBool: @Sendable (Bool, _ key: String) async -> Void | ||
public var setData: @Sendable (Data?, _ key: String) async -> Void | ||
public var setDouble: @Sendable (Double, _ key: String) async -> Void | ||
public var setInteger: @Sendable (Int, _ key: String) async -> Void | ||
|
||
// MARK: - Read | ||
|
||
public var stringForKey: @Sendable (String) -> String? | ||
public var boolForKey: @Sendable (String) -> Bool = { _ in false } | ||
public var dataForKey: @Sendable (String) -> Data? | ||
public var doubleForKey: @Sendable (String) -> Double = { _ in 0.0 } | ||
public var integerForKey: @Sendable (String) -> Int = { _ in 0 } | ||
|
||
|
||
// MARK: - Delete | ||
|
||
public var remove: @Sendable (String) async -> Void | ||
|
||
// MARK: - Reset | ||
|
||
public var removePersistentDomain: @Sendable (_ bundleId: String) -> Void | ||
} | ||
|
||
extension UserDefaultsClient: TestDependencyKey { | ||
public static var testValue = Self() | ||
public static var previewValue = Self() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// UserDefaultsClientTesting.swift | ||
// UserDefaultsClientManifests | ||
// | ||
// Created by devMinseok on 8/4/24. | ||
// | ||
|
||
import ProjectDescription | ||
import ProjectDescriptionHelpers | ||
|
||
@_spi(Core) | ||
@_spi(Shared) | ||
import DependencyPlugin | ||
|
||
let project: Project = .makeTMABasedProject( | ||
module: Core.UserDefaultsClient, | ||
scripts: [], | ||
targets: [ | ||
.sources, | ||
.interface, | ||
.tests | ||
], | ||
dependencies: [ | ||
.interface: [ | ||
.dependency(rootModule: Shared.self) | ||
] | ||
] | ||
) |
34 changes: 34 additions & 0 deletions
34
Projects/Core/UserDefaultsClient/Sources/UserDefaultsClient.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// | ||
// UserDefaultsClient.swift | ||
// UserDefaultsClient | ||
// | ||
// Created by devMinseok on 8/4/24. | ||
// | ||
|
||
import Foundation | ||
|
||
import Dependencies | ||
|
||
import UserDefaultsClientInterface | ||
|
||
extension UserDefaultsClient: DependencyKey { | ||
public static let liveValue: UserDefaultsClient = .live() | ||
|
||
public static func live() -> UserDefaultsClient { | ||
let userDefaults = { UserDefaults.standard } | ||
return .init( | ||
setString: { userDefaults().set($0, forKey: $1) }, | ||
setBool: { userDefaults().set($0, forKey: $1) }, | ||
setData: { userDefaults().set($0, forKey: $1) }, | ||
setDouble: { userDefaults().set($0, forKey: $1) }, | ||
setInteger: { userDefaults().set($0, forKey: $1) }, | ||
stringForKey: { userDefaults().string(forKey: $0) }, | ||
boolForKey: { userDefaults().bool(forKey: $0) }, | ||
dataForKey: { userDefaults().data(forKey: $0) }, | ||
doubleForKey: { userDefaults().double(forKey: $0) }, | ||
integerForKey: { userDefaults().integer(forKey: $0) }, | ||
remove: { userDefaults().removeObject(forKey: $0) }, | ||
removePersistentDomain: { userDefaults().removePersistentDomain(forName: $0) } | ||
) | ||
} | ||
} |
129 changes: 129 additions & 0 deletions
129
Projects/Core/UserDefaultsClient/Tests/UserDefaultsClientTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
// | ||
// UserDefaultsClientTests.swift | ||
// UserDefaultsClient | ||
// | ||
// Created by devMinseok on 8/4/24. | ||
// | ||
|
||
import XCTest | ||
|
||
import UserDefaultsClient | ||
import UserDefaultsClientInterface | ||
|
||
import Dependencies | ||
|
||
final class UserDefaultsClientTests: XCTestCase { | ||
@Dependency(UserDefaultsClient.self) var userDefaultsClient | ||
|
||
override func tearDown() { | ||
withDependencies { | ||
$0[UserDefaultsClient.self] = UserDefaultsClient.live() | ||
} operation: { | ||
userDefaultsClient.removePersistentDomain(bundleId: Bundle.main.bundleIdentifier ?? "") | ||
} | ||
super.tearDown() | ||
} | ||
|
||
func testSetString() async { | ||
await withDependencies { | ||
$0[UserDefaultsClient.self] = UserDefaultsClient.live() | ||
} operation: { | ||
// given | ||
let testKey: String = "TestKey" | ||
let testValue: String = "TestString" | ||
|
||
// when | ||
await userDefaultsClient.setString(testValue, key: testKey) | ||
|
||
// then | ||
let result = userDefaultsClient.stringForKey(testKey) | ||
XCTAssertEqual(result, testValue) | ||
} | ||
} | ||
|
||
func testSetBool() async { | ||
await withDependencies { | ||
$0[UserDefaultsClient.self] = UserDefaultsClient.live() | ||
} operation: { | ||
// given | ||
let testKey: String = "TestKeyBool" | ||
let testValue: Bool = true | ||
|
||
// when | ||
await userDefaultsClient.setBool(testValue, testKey) | ||
|
||
// then | ||
let result = userDefaultsClient.boolForKey(testKey) | ||
XCTAssertEqual(result, testValue) | ||
} | ||
} | ||
|
||
func testSetData() async { | ||
await withDependencies { | ||
$0[UserDefaultsClient.self] = UserDefaultsClient.live() | ||
} operation: { | ||
// given | ||
let testKey: String = "TestKeyData" | ||
let testValue: Data = "TestData".data(using: .utf8)! | ||
|
||
// when | ||
await userDefaultsClient.setData(testValue, testKey) | ||
|
||
// then | ||
let result = userDefaultsClient.dataForKey(testKey) | ||
XCTAssertEqual(result, testValue) | ||
} | ||
} | ||
|
||
func testSetDouble() async { | ||
await withDependencies { | ||
$0[UserDefaultsClient.self] = UserDefaultsClient.live() | ||
} operation: { | ||
// given | ||
let testKey: String = "TestKeyDouble" | ||
let testValue: Double = 123.456 | ||
|
||
// when | ||
await userDefaultsClient.setDouble(testValue, testKey) | ||
|
||
// then | ||
let result = userDefaultsClient.doubleForKey(testKey) | ||
XCTAssertEqual(result, testValue) | ||
} | ||
} | ||
|
||
func testSetInteger() async { | ||
await withDependencies { | ||
$0[UserDefaultsClient.self] = UserDefaultsClient.live() | ||
} operation: { | ||
// given | ||
let testKey: String = "TestKeyInteger" | ||
let testValue: Int = 123 | ||
|
||
// when | ||
await userDefaultsClient.setInteger(testValue, testKey) | ||
|
||
// then | ||
let result = userDefaultsClient.integerForKey(testKey) | ||
XCTAssertEqual(result, testValue) | ||
} | ||
} | ||
|
||
func testRemove() async { | ||
await withDependencies { | ||
$0[UserDefaultsClient.self] = UserDefaultsClient.live() | ||
} operation: { | ||
// given | ||
let testKey: String = "TestKeyRemove" | ||
let testValue: String = "TestString" | ||
await userDefaultsClient.setString(testValue, testKey) | ||
|
||
// when | ||
await userDefaultsClient.remove(testKey) | ||
|
||
// then | ||
let result = userDefaultsClient.stringForKey(testKey) | ||
XCTAssertNil(result) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,4 @@ | |
// | ||
|
||
@_exported import AppService | ||
@_exported import AppServiceInterface | ||
@_exported import PushService |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters