-
-
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.
* [#38, #44] Added support for feature flag values in `UserDefaults` (#42, #46) * [#37] Updated Copyright Year in `LICENSE` * [#33, #35] Updated CI configuration (#34, #36)
- Loading branch information
Showing
7 changed files
with
162 additions
and
7 deletions.
There are no files selected for viewing
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
48 changes: 48 additions & 0 deletions
48
Sources/YMFF/FeatureFlagResolverImplementation/Store/UserDefaultsStore.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,48 @@ | ||
// | ||
// UserDefaultsStore.swift | ||
// YMFF | ||
// | ||
// Created by Yakov Manshin on 3/25/21. | ||
// Copyright © 2021 Yakov Manshin. See the LICENSE file for license info. | ||
// | ||
|
||
#if canImport(Foundation) | ||
|
||
import Foundation | ||
|
||
// MARK: - UserDefaultsStore | ||
|
||
/// An object that provides read and write access to feature flag values store in `UserDefaults`. | ||
final public class UserDefaultsStore { | ||
|
||
private let userDefaults: UserDefaults | ||
|
||
/// Initializes a new `UserDefaultsStore`. | ||
/// | ||
/// - Parameter userDefaults: *Optional.* The `UserDefaults` object used to read and write values. | ||
/// `UserDefaults.standard` is used by default. | ||
public init(userDefaults: UserDefaults = .standard) { | ||
self.userDefaults = userDefaults | ||
} | ||
|
||
} | ||
|
||
// MARK: - MutableFeatureFlagStoreProtocol | ||
|
||
extension UserDefaultsStore: MutableFeatureFlagStoreProtocol { | ||
|
||
public func value<Value>(forKey key: String) -> Value? { | ||
userDefaults.value(forKey: key) as? Value | ||
} | ||
|
||
public func setValue<Value>(_ value: Value, forKey key: String) { | ||
userDefaults.setValue(value, forKey: key) | ||
} | ||
|
||
public func removeValue(forKey key: String) { | ||
userDefaults.removeObject(forKey: key) | ||
} | ||
|
||
} | ||
|
||
#endif |
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,66 @@ | ||
// | ||
// UserDefaultsStoreTests.swift | ||
// YMFF | ||
// | ||
// Created by Yakov Manshin on 3/21/21. | ||
// Copyright © 2021 Yakov Manshin. See the LICENSE file for license info. | ||
// | ||
|
||
import XCTest | ||
@testable import YMFF | ||
|
||
final class UserDefaultsStoreTests: XCTestCase { | ||
|
||
private var resolver: FeatureFlagResolver! | ||
|
||
private lazy var userDefaults = UserDefaults() | ||
|
||
override func setUp() { | ||
super.setUp() | ||
|
||
resolver = FeatureFlagResolver(configuration: .init( | ||
persistentStores: [.userDefaults(userDefaults)], | ||
runtimeStore: UserDefaultsStore(userDefaults: userDefaults) | ||
)) | ||
} | ||
|
||
} | ||
|
||
extension UserDefaultsStoreTests { | ||
|
||
func testReadValueWithResolver() { | ||
let key = "TEST_UserDefaults_key_123" | ||
let value = 123 | ||
|
||
userDefaults.setValue(value, forKey: key) | ||
|
||
// FIXME: [#40] Can't use `retrievedValue: Int?` here | ||
let retrievedValue = try? resolver.value(for: key) as Int | ||
|
||
XCTAssertEqual(retrievedValue, value) | ||
} | ||
|
||
func testWriteValueWithResolver() { | ||
let key = "TEST_UserDefaults_key_456" | ||
let value = 456 | ||
|
||
try? resolver.overrideInRuntime(key, with: value) | ||
|
||
let retrievedValue = userDefaults.value(forKey: key) as? Int | ||
|
||
XCTAssertEqual(retrievedValue, value) | ||
} | ||
|
||
func testWriteAndReadValueWithResolver() { | ||
let key = "TEST_UserDefaults_key_789" | ||
let value = 789 | ||
|
||
try? resolver.overrideInRuntime(key, with: value) | ||
|
||
// FIXME: [#40] Can't use `retrievedValue: Int?` here | ||
let retrievedValue = try? resolver.value(for: key) as Int | ||
|
||
XCTAssertEqual(retrievedValue, value) | ||
} | ||
|
||
} |
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