From 3495f3941bc1f3d420c852f087a86eb5a41af501 Mon Sep 17 00:00:00 2001 From: Yakov Manshin Date: Sun, 28 Apr 2024 02:51:32 +0200 Subject: [PATCH] [#127] Refactored Tests (#128) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Refactored and extended `FeatureFlagValueTransformerTests` * Removed `MutableStoreTests` (they’re not really helpful) --- .../FeatureFlagValueTransformerTests.swift | 110 +++++++++++------- .../Stores/MutableFeatureFlagStoreMock.swift | 57 --------- .../YMFFTests/Stores/MutableStoreTests.swift | 80 ------------- 3 files changed, 66 insertions(+), 181 deletions(-) delete mode 100644 Tests/YMFFTests/Stores/MutableFeatureFlagStoreMock.swift delete mode 100644 Tests/YMFFTests/Stores/MutableStoreTests.swift diff --git a/Tests/YMFFTests/FeatureFlagValueTransformerTests.swift b/Tests/YMFFTests/FeatureFlagValueTransformerTests.swift index f01567e..7abd34b 100644 --- a/Tests/YMFFTests/FeatureFlagValueTransformerTests.swift +++ b/Tests/YMFFTests/FeatureFlagValueTransformerTests.swift @@ -14,71 +14,71 @@ import XCTest final class FeatureFlagValueTransformerTests: XCTestCase { - func testIdentityTransformer() { - let transformer: FeatureFlagValueTransformer = .identity + func test_valueFromRawValue_identityTransformer() { + let transformer = FeatureFlagValueTransformer.identity - let intValue = 123 + let value = transformer.valueFromRawValue(123) - XCTAssertEqual(transformer.valueFromRawValue(intValue), intValue) - XCTAssertEqual(transformer.rawValueFromValue(intValue), intValue) + XCTAssertEqual(value, 123) } - func testSameTypeTransformation() { - let transformer = FeatureFlagValueTransformer { string in - String(string.dropFirst(4)) - } rawValueFromValue: { string in - "RAW_\(string)" - } + func test_rawValueFromValue_identityTransformer() { + let transformer = FeatureFlagValueTransformer.identity - let stringValue = "some_value" - let stringRawValue = "RAW_some_value" + let rawValue = transformer.rawValueFromValue("TEST_value") - XCTAssertEqual(transformer.valueFromRawValue(stringRawValue), stringValue) - XCTAssertEqual(transformer.rawValueFromValue(stringValue), stringRawValue) + XCTAssertEqual(rawValue, "TEST_value") } - func testStringToBoolTransformation() { - let transformer = FeatureFlagValueTransformer { string in - string == "true" - } rawValueFromValue: { bool in - bool ? "true" : "false" + func test_valueFromRawValue_sameType() { + let transformer = FeatureFlagValueTransformer { rawValue in + String(rawValue.dropFirst(4)) + } rawValueFromValue: { value in + "RAW_\(value)" } - let stringRawValueTrue = "true" - let stringRawValueFalse = "false" - let stringRawValueOther = "OTHER" + let value = transformer.valueFromRawValue("RAW_some_value") - XCTAssertTrue(transformer.valueFromRawValue(stringRawValueTrue) == true) - XCTAssertTrue(transformer.valueFromRawValue(stringRawValueFalse) == false) - XCTAssertTrue(transformer.valueFromRawValue(stringRawValueOther) == false) + XCTAssertEqual(value, "some_value") + } + + func test_rawValueFromValue_sameType() { + let transformer = FeatureFlagValueTransformer { rawValue in + rawValue / 2 + } rawValueFromValue: { value in + value * 2 + } - XCTAssertEqual(transformer.rawValueFromValue(true), stringRawValueTrue) - XCTAssertEqual(transformer.rawValueFromValue(false), stringRawValueFalse) + let rawValue = transformer.rawValueFromValue(123) + + XCTAssertEqual(rawValue, 246) } - func testStringToEnumWithRawValueTransformation() { + func test_valueFromRawValue_intFromString() { let transformer = FeatureFlagValueTransformer { string in - AdType(rawValue: string) - } rawValueFromValue: { type in - type.rawValue + Int(string) + } rawValueFromValue: { int in + "\(int)" } - let stringRawValueNone = "none" - let stringRawValueBanner = "banner" - let stringRawValueVideo = "video" - let stringRawValueOther = "image" + let value = transformer.valueFromRawValue("12345") - XCTAssertEqual(transformer.valueFromRawValue(stringRawValueNone), AdType.none) - XCTAssertEqual(transformer.valueFromRawValue(stringRawValueBanner), .banner) - XCTAssertEqual(transformer.valueFromRawValue(stringRawValueVideo), .video) - XCTAssertEqual(transformer.valueFromRawValue(stringRawValueOther), nil) + XCTAssertEqual(value, 12345) + } + + func test_rawValueFromValue_stringFromBool() { + let transformer = FeatureFlagValueTransformer { string in + string == "YES" + } rawValueFromValue: { bool in + bool ? "YES" : "NO" + } - XCTAssertEqual(transformer.rawValueFromValue(.none), stringRawValueNone) - XCTAssertEqual(transformer.rawValueFromValue(.banner), stringRawValueBanner) - XCTAssertEqual(transformer.rawValueFromValue(.video), stringRawValueVideo) + let rawValue = transformer.rawValueFromValue(true) + + XCTAssertEqual(rawValue, "YES") } - func testIntToEnumWithCustomInitializationTransformation() { + func test_valueFromRawValue_enumFromInt() { let transformer = FeatureFlagValueTransformer { (age: Int) -> AgeGroup in switch age { case ..<13: @@ -114,6 +114,28 @@ final class FeatureFlagValueTransformerTests: XCTestCase { XCTAssertEqual(transformer.rawValueFromValue(.over17), 18) } + func test_rawValueFromValue_stringFromEnum() { + let transformer = FeatureFlagValueTransformer { string in + AdType(rawValue: string) + } rawValueFromValue: { type in + type.rawValue + } + + let stringRawValueNone = "none" + let stringRawValueBanner = "banner" + let stringRawValueVideo = "video" + let stringRawValueOther = "image" + + XCTAssertEqual(transformer.valueFromRawValue(stringRawValueNone), AdType.none) + XCTAssertEqual(transformer.valueFromRawValue(stringRawValueBanner), .banner) + XCTAssertEqual(transformer.valueFromRawValue(stringRawValueVideo), .video) + XCTAssertEqual(transformer.valueFromRawValue(stringRawValueOther), nil) + + XCTAssertEqual(transformer.rawValueFromValue(.none), stringRawValueNone) + XCTAssertEqual(transformer.rawValueFromValue(.banner), stringRawValueBanner) + XCTAssertEqual(transformer.rawValueFromValue(.video), stringRawValueVideo) + } + } // MARK: - Support Types diff --git a/Tests/YMFFTests/Stores/MutableFeatureFlagStoreMock.swift b/Tests/YMFFTests/Stores/MutableFeatureFlagStoreMock.swift deleted file mode 100644 index b441e57..0000000 --- a/Tests/YMFFTests/Stores/MutableFeatureFlagStoreMock.swift +++ /dev/null @@ -1,57 +0,0 @@ -// -// MutableFeatureFlagStoreMock.swift -// YMFF -// -// Created by Yakov Manshin on 5/30/21. -// Copyright © 2021 Yakov Manshin. See the LICENSE file for license info. -// - -import YMFF -#if !COCOAPODS -import YMFFProtocols -#endif - -final class MutableFeatureFlagStoreMock: MutableFeatureFlagStoreProtocol { - - private var store: TransparentFeatureFlagStore - private var onSaveChangesClosure: () -> Void - - init(store: TransparentFeatureFlagStore, onSaveChanges: @escaping () -> Void = { }) { - self.store = store - self.onSaveChangesClosure = onSaveChanges - } - - func containsValue(forKey key: String) -> Bool { - store[key] != nil - } - - func value(forKey key: String) -> Value? { - let expectedValueType = Value.self - - switch expectedValueType { - case is Bool.Type, - is Int.Type, - is String.Type, - is Optional.Type, - is Optional.Type, - is Optional.Type: - return store[key] as? Value - default: - assertionFailure("The expected feature flag value type (\(expectedValueType)) is not supported") - return nil - } - } - - func setValue(_ value: Value, forKey key: String) { - store[key] = value - } - - func removeValue(forKey key: String) { - store[key] = nil - } - - func saveChanges() { - onSaveChangesClosure() - } - -} diff --git a/Tests/YMFFTests/Stores/MutableStoreTests.swift b/Tests/YMFFTests/Stores/MutableStoreTests.swift deleted file mode 100644 index 7086d37..0000000 --- a/Tests/YMFFTests/Stores/MutableStoreTests.swift +++ /dev/null @@ -1,80 +0,0 @@ -// -// MutableStoreTests.swift -// YMFF -// -// Created by Yakov Manshin on 4/17/21. -// Copyright © 2021 Yakov Manshin. See the LICENSE file for license info. -// - -@testable import YMFF - -import XCTest -#if !COCOAPODS -import YMFFProtocols -#endif - -final class MutableStoreTests: XCTestCase { - - private var mutableStore: MutableFeatureFlagStoreProtocol! - private var resolver: FeatureFlagResolverProtocol! - - override func setUp() { - super.setUp() - - mutableStore = MutableFeatureFlagStoreMock(store: .init()) - resolver = FeatureFlagResolver(stores: [.mutable(mutableStore)]) - } - - func testOverride() { - let overrideKey = "OVERRIDE_KEY" - let overrideValue = 123 - - let initialValueFromResolver = try? resolver.value(for: overrideKey) as Int - let initialValueFromStore: Int? = mutableStore.value(forKey: overrideKey) - - XCTAssertNil(initialValueFromResolver) - XCTAssertNil(initialValueFromStore) - - try? resolver.setValue(overrideValue, toMutableStoreUsing: overrideKey) - let overrideValueFromResolver = try? resolver.value(for: overrideKey) as Int - let overrideValueFromStore: Int? = mutableStore.value(forKey: overrideKey) - - XCTAssertEqual(overrideValueFromResolver, overrideValue) - XCTAssertEqual(overrideValueFromStore, overrideValue) - } - - func testOverrideRemoval() { - let overrideKey = "OVERRIDE_KEY" - let overrideValue = 123 - - mutableStore.setValue(overrideValue, forKey: overrideKey) - - let overrideValueFromResolver = try? resolver.value(for: overrideKey) as Int - let overrideValueFromStore: Int? = mutableStore.value(forKey: overrideKey) - - XCTAssertEqual(overrideValueFromResolver, overrideValue) - XCTAssertEqual(overrideValueFromStore, overrideValue) - - XCTAssertNoThrow(try resolver.removeValueFromMutableStore(using: overrideKey)) - - let removedValueFromResolver = try? resolver.value(for: overrideKey) as Int - let removedValueFromStore: Int? = mutableStore.value(forKey: overrideKey) - - XCTAssertNil(removedValueFromResolver) - XCTAssertNil(removedValueFromStore) - } - - func testChangeSaving() { - var saveChangesCount = 0 - - mutableStore = MutableFeatureFlagStoreMock(store: .init()) { - saveChangesCount += 1 - } - resolver = FeatureFlagResolver(stores: [.mutable(mutableStore)]) - - resolver = nil - - XCTAssertEqual(saveChangesCount, 1) - } - -}