Skip to content

Commit

Permalink
[#127] Refactored Tests (#128)
Browse files Browse the repository at this point in the history
* Refactored and extended `FeatureFlagValueTransformerTests`
* Removed `MutableStoreTests` (they’re not really helpful)
  • Loading branch information
yakovmanshin authored Apr 28, 2024
1 parent a246b13 commit 3495f39
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 181 deletions.
110 changes: 66 additions & 44 deletions Tests/YMFFTests/FeatureFlagValueTransformerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,71 +14,71 @@ import XCTest

final class FeatureFlagValueTransformerTests: XCTestCase {

func testIdentityTransformer() {
let transformer: FeatureFlagValueTransformer<Int, Int> = .identity
func test_valueFromRawValue_identityTransformer() {
let transformer = FeatureFlagValueTransformer<Int, Int>.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<String, String>.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:
Expand Down Expand Up @@ -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
Expand Down
57 changes: 0 additions & 57 deletions Tests/YMFFTests/Stores/MutableFeatureFlagStoreMock.swift

This file was deleted.

80 changes: 0 additions & 80 deletions Tests/YMFFTests/Stores/MutableStoreTests.swift

This file was deleted.

0 comments on commit 3495f39

Please sign in to comment.