Skip to content
This repository has been archived by the owner on Jan 5, 2024. It is now read-only.

Commit

Permalink
Used XCTUnwrap instead of try! and updated Readme
Browse files Browse the repository at this point in the history
  • Loading branch information
saniazafar committed Jan 10, 2022
1 parent 4c1bfa9 commit 8ede397
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 47 deletions.
6 changes: 3 additions & 3 deletions Example/JustTweak/Accessors/TweakAccessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,17 @@ class TweakAccessor {

var canShowYellowView: Bool {
return (try? tweakManager.tweakWith(feature: Features.uiCustomization,
variable: Variables.displayYellowView))?.boolValue ?? false
variable: Variables.displayYellowView))?.boolValue ?? false
}

var redViewAlpha: Float {
return (try? tweakManager.tweakWith(feature: Features.uiCustomization,
variable: Variables.redViewAlpha))?.floatValue ?? 0.0
variable: Variables.redViewAlpha))?.floatValue ?? 0.0
}

var isTapGestureToChangeColorEnabled: Bool {
return (try? tweakManager.tweakWith(feature: Features.general,
variable: Variables.tapToChangeViewColor))?.boolValue ?? false
variable: Variables.tapToChangeViewColor))?.boolValue ?? false
}
}

6 changes: 3 additions & 3 deletions Example/Tests/Core/LocalTweakProviderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class LocalTweakProviderTests: XCTestCase {
title: "Display Red View",
group: "UI Customization")
XCTAssertEqual(redViewTweak, try tweakProvider.tweakWith(feature: Features.uiCustomization,
variable: Variables.displayRedView))
variable: Variables.displayRedView))
}

func testParsesFloatTweak() {
Expand All @@ -43,7 +43,7 @@ class LocalTweakProviderTests: XCTestCase {
title: "Red View Alpha Component",
group: "UI Customization")
XCTAssertEqual(redViewAlphaTweak, try tweakProvider.tweakWith(feature: Features.uiCustomization,
variable: Variables.redViewAlpha))
variable: Variables.redViewAlpha))
}

func testParsesStringTweak() {
Expand All @@ -52,7 +52,7 @@ class LocalTweakProviderTests: XCTestCase {
value: "Test value",
title: "Label Text", group: "UI Customization")
XCTAssertEqual(buttonLabelTweak, try tweakProvider.tweakWith(feature: Features.uiCustomization,
variable: Variables.labelText))
variable: Variables.labelText))
}

func testDecryptionClosure() {
Expand Down
18 changes: 9 additions & 9 deletions Example/Tests/Core/TweakManagerCacheTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,28 @@ class TweakManagerCacheTests: XCTestCase {

// MARK: - tweakWith(feature:variable:)

func testTweakFetch_CacheDisabled() {
tweakFetch(useCache: false)
func testTweakFetch_CacheDisabled() throws {
try tweakFetch(useCache: false)
}

func testTweakFetch_CacheEnabled() {
tweakFetch(useCache: true)
func testTweakFetch_CacheEnabled() throws {
try tweakFetch(useCache: true)
}

private func tweakFetch(useCache: Bool) {
private func tweakFetch(useCache: Bool) throws {
tweakManager.useCache = useCache
XCTAssertEqual(mockTweakProvider.tweakWithFeatureVariableCallsCounter, 0)
let value = true
tweakManager.set(value, feature: Constants.feature, variable: Constants.variable)
XCTAssertEqual(try! tweakManager.tweakWith(feature: Constants.feature, variable: Constants.variable).value as! Bool, value)
XCTAssertEqual(try XCTUnwrap(tweakManager.tweakWith(feature: Constants.feature, variable: Constants.variable)).value as! Bool, value)
XCTAssertEqual(mockTweakProvider.tweakWithFeatureVariableCallsCounter, 1)
XCTAssertEqual(try! tweakManager.tweakWith(feature: Constants.feature, variable: Constants.variable).value as! Bool, value)
XCTAssertEqual(try XCTUnwrap(tweakManager.tweakWith(feature: Constants.feature, variable: Constants.variable)).value as! Bool, value)
XCTAssertEqual(mockTweakProvider.tweakWithFeatureVariableCallsCounter, useCache ? 1 : 2)
tweakManager.set(value, feature: Constants.feature, variable: Constants.variable)
XCTAssertEqual(try! tweakManager.tweakWith(feature: Constants.feature, variable: Constants.variable).value as! Bool, value)
XCTAssertEqual(try XCTUnwrap(tweakManager.tweakWith(feature: Constants.feature, variable: Constants.variable)).value as! Bool, value)
XCTAssertEqual(mockTweakProvider.tweakWithFeatureVariableCallsCounter, useCache ? 2 : 3)
tweakManager.resetCache()
XCTAssertEqual(try! tweakManager.tweakWith(feature: Constants.feature, variable: Constants.variable).value as! Bool, value)
XCTAssertEqual(try XCTUnwrap(tweakManager.tweakWith(feature: Constants.feature, variable: Constants.variable)).value as! Bool, value)
XCTAssertEqual(mockTweakProvider.tweakWithFeatureVariableCallsCounter, useCache ? 3 : 4)
}
}
Expand Down
24 changes: 12 additions & 12 deletions Example/Tests/Core/TweakManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,30 @@ class TweakManagerTests: XCTestCase {
XCTAssertNil(try? tweakManager.tweakWith(feature: Features.uiCustomization, variable: "some_undefined_tweak"))
}

func testReturnsRemoteConfigValue_ForDisplayRedViewTweak() {
XCTAssertTrue(try! tweakManager.tweakWith(feature: Features.uiCustomization, variable: Variables.displayRedView).boolValue)
func testReturnsRemoteConfigValue_ForDisplayRedViewTweak() throws {
XCTAssertTrue(try XCTUnwrap(tweakManager.tweakWith(feature: Features.uiCustomization, variable: Variables.displayRedView)).boolValue)
}

func testReturnsRemoteConfigValue_ForDisplayYellowViewTweak() {
XCTAssertFalse(try! tweakManager.tweakWith(feature: Features.uiCustomization, variable: Variables.displayYellowView).boolValue)
func testReturnsRemoteConfigValue_ForDisplayYellowViewTweak() throws {
XCTAssertFalse(try XCTUnwrap(tweakManager.tweakWith(feature: Features.uiCustomization, variable: Variables.displayYellowView)).boolValue)
}

func testReturnsRemoteConfigValue_ForDisplayGreenViewTweak() {
XCTAssertFalse(try! tweakManager.tweakWith(feature: Features.uiCustomization, variable: Variables.displayGreenView).boolValue)
func testReturnsRemoteConfigValue_ForDisplayGreenViewTweak() throws {
XCTAssertFalse(try XCTUnwrap(tweakManager.tweakWith(feature: Features.uiCustomization, variable: Variables.displayGreenView)).boolValue)
}

func testReturnsRemoteConfigValue_ForGreetOnAppDidBecomeActiveTweak() {
XCTAssertTrue(try! tweakManager.tweakWith(feature: Features.uiCustomization, variable: Variables.greetOnAppDidBecomeActive).boolValue)
func testReturnsRemoteConfigValue_ForGreetOnAppDidBecomeActiveTweak() throws {
XCTAssertTrue(try XCTUnwrap(tweakManager.tweakWith(feature: Features.uiCustomization, variable: Variables.greetOnAppDidBecomeActive)).boolValue)
}

func testReturnsJSONConfigValue_ForTapToChangeViewColorTweak_AsYetUnkown() {
XCTAssertTrue(try! tweakManager.tweakWith(feature: Features.general, variable: Variables.tapToChangeViewColor).boolValue)
func testReturnsJSONConfigValue_ForTapToChangeViewColorTweak_AsYetUnkown() throws {
XCTAssertTrue(try XCTUnwrap(tweakManager.tweakWith(feature: Features.general, variable: Variables.tapToChangeViewColor)).boolValue)
}

func testReturnsUserSetValue_ForGreetOnAppDidBecomeActiveTweak_AfterUpdatingUserDefaultsTweakProvider() {
func testReturnsUserSetValue_ForGreetOnAppDidBecomeActiveTweak_AfterUpdatingUserDefaultsTweakProvider() throws {
let mutableTweakProvider = tweakManager.mutableTweakProvider!
mutableTweakProvider.set(false, feature: Features.uiCustomization, variable: Variables.greetOnAppDidBecomeActive)
XCTAssertFalse(try! tweakManager.tweakWith(feature: Features.uiCustomization, variable: Variables.greetOnAppDidBecomeActive).boolValue)
XCTAssertFalse(try XCTUnwrap(tweakManager.tweakWith(feature: Features.uiCustomization, variable: Variables.greetOnAppDidBecomeActive)).boolValue)
}

func testCallsClosureForRegisteredObserverWhenAnyConfigurationChanges() {
Expand Down
32 changes: 16 additions & 16 deletions Example/Tests/Core/UserDefaultsTweakProviderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,51 +24,51 @@ class UserDefaultsTweakProviderTests: XCTestCase {
super.tearDown()
}

func testReturnsCorrectTweaksIdentifiersWhenInitializedAndTweaksHaveBeenSet() {
func testReturnsCorrectTweaksIdentifiersWhenInitializedAndTweaksHaveBeenSet() throws {
let anotherConfiguration = UserDefaultsTweakProvider(userDefaults: userDefaults)
anotherConfiguration.set("hello", feature: "feature_1", variable: "variable_4")
anotherConfiguration.set(true, feature: "feature_1", variable: "variable_3")
anotherConfiguration.set(12.34, feature: "feature_1", variable: "variable_2")
anotherConfiguration.set(42, feature: "feature_1", variable: "variable_1")

XCTAssertTrue(try! anotherConfiguration.tweakWith(feature: "feature_1", variable: "variable_1").value == 42)
XCTAssertTrue(try! anotherConfiguration.tweakWith(feature: "feature_1", variable: "variable_2").value == 12.34)
XCTAssertTrue(try! anotherConfiguration.tweakWith(feature: "feature_1", variable: "variable_3").value == true)
XCTAssertTrue(try! anotherConfiguration.tweakWith(feature: "feature_1", variable: "variable_4").value == "hello")
XCTAssertTrue(try XCTUnwrap(anotherConfiguration.tweakWith(feature: "feature_1", variable: "variable_1")).value == 42)
XCTAssertTrue(try XCTUnwrap(anotherConfiguration.tweakWith(feature: "feature_1", variable: "variable_2")).value == 12.34)
XCTAssertTrue(try XCTUnwrap(anotherConfiguration.tweakWith(feature: "feature_1", variable: "variable_3")).value == true)
XCTAssertTrue(try XCTUnwrap(anotherConfiguration.tweakWith(feature: "feature_1", variable: "variable_4")).value == "hello")
}

func testReturnsNilForTweaksThatHaveNoUserDefaultValue() {
let tweak = try? userDefaultsTweakProvider.tweakWith(feature: Features.uiCustomization, variable: Variables.displayRedView)
XCTAssertNil(tweak)
}

func testUpdatesValueForTweak_withBool() {
func testUpdatesValueForTweak_withBool() throws {
userDefaultsTweakProvider.set(true, feature: "feature_1", variable: "variable_1")
let tweak = try! userDefaultsTweakProvider.tweakWith(feature: "feature_1", variable: "variable_1")
XCTAssertTrue(tweak.value == true)
let tweak = try XCTUnwrap(userDefaultsTweakProvider.tweakWith(feature: "feature_1", variable: "variable_1"))
XCTAssertTrue(tweak.value.boolValue)
}

func testUpdatesValueForTweak_withInteger() {
func testUpdatesValueForTweak_withInteger() throws {
userDefaultsTweakProvider.set(42, feature: "feature_1", variable: "variable_1")
let tweak = try! userDefaultsTweakProvider.tweakWith(feature: "feature_1", variable: "variable_1")
let tweak = try XCTUnwrap(userDefaultsTweakProvider.tweakWith(feature: "feature_1", variable: "variable_1"))
XCTAssertTrue(tweak.value == 42)
}

func testUpdatesValueForTweak_withFloat() {
func testUpdatesValueForTweak_withFloat() throws {
userDefaultsTweakProvider.set(Float(12.34), feature: "feature_1", variable: "variable_1")
let tweak = try! userDefaultsTweakProvider.tweakWith(feature: "feature_1", variable: "variable_1")
let tweak = try XCTUnwrap(userDefaultsTweakProvider.tweakWith(feature: "feature_1", variable: "variable_1"))
XCTAssertTrue(tweak.value == Float(12.34))
}

func testUpdatesValueForTweak_withDouble() {
func testUpdatesValueForTweak_withDouble() throws {
userDefaultsTweakProvider.set(Double(23.45), feature: "feature_1", variable: "variable_1")
let tweak = try! userDefaultsTweakProvider.tweakWith(feature: "feature_1", variable: "variable_1")
let tweak = try XCTUnwrap(userDefaultsTweakProvider.tweakWith(feature: "feature_1", variable: "variable_1"))
XCTAssertTrue(tweak.value == Double(23.45))
}

func testUpdatesValueForTweak_withString() {
func testUpdatesValueForTweak_withString() throws {
userDefaultsTweakProvider.set("Hello", feature: "feature_1", variable: "variable_1")
let tweak = try! userDefaultsTweakProvider.tweakWith(feature: "feature_1", variable: "variable_1")
let tweak = try XCTUnwrap(userDefaultsTweakProvider.tweakWith(feature: "feature_1", variable: "variable_1"))
XCTAssertTrue(tweak.value == "Hello")
}

Expand Down
4 changes: 2 additions & 2 deletions Example/Tests/UI/TweakViewControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,15 @@ class TweakViewControllerTests: XCTestCase {

// MARK: Cells Actions

func testUpdatesValueOfTweak_WhenUserTooglesSwitchOnBooleanCell() {
func testUpdatesValueOfTweak_WhenUserTooglesSwitchOnBooleanCell() throws {
viewController.beginAppearanceTransition(true, animated: false)
viewController.endAppearanceTransition()

let indexPath = viewController.indexPathForTweak(with: Features.uiCustomization, variable: Variables.displayYellowView)!
let cell = viewController.tableView.cellForRow(at: indexPath) as! BooleanTweakTableViewCell
cell.switchControl.isOn = true
cell.switchControl.sendActions(for: .valueChanged)
XCTAssertTrue(try! tweakManager.tweakWith(feature: Features.uiCustomization, variable: Variables.displayYellowView).boolValue)
XCTAssertTrue(try XCTUnwrap(tweakManager.tweakWith(feature: Features.uiCustomization, variable: Variables.displayYellowView)).boolValue)
}

// MARK: Other Actions
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,13 @@ do {
// tweak was found in some tweak provider, use tweak.value
return tweak
} catch let error as TweakError {
// tweak was not found in any tweak provider
//handle error
switch error {
case .notFound: () // "Feature or variable is not found"
case .notSupported: () // "Variable type is not supported"
case .decryptionClosureNotProvided: () // "Value is encrypted but there's no decryption closure provided"
}
} catch let error { // add a default catch to satisfy the compiler
print(error.localizedDescription)
}
```

Expand Down

0 comments on commit 8ede397

Please sign in to comment.