Skip to content

Commit

Permalink
Reset selectCandidateWithNumericKeypad for testing
Browse files Browse the repository at this point in the history
This issue was discovered after #381 if the tests were run locally when
`selectCandidateWithNumericKeypad` is set to true. This commit makes the
test reset and restore the setting during testing.

Ideally we shouldn't let Preferences affect tests, since they are
supposed to be hermetic. We have discussed the issue in #294. Until the
code is properly refactored, though, this will make the tests pass
locally.

Also updated the reset-restore code elsewhere in the tests so that the
restore always takes place before the assertions.
  • Loading branch information
lukhnos committed Nov 23, 2023
1 parent f2b45c8 commit a079112
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions McBopomofoTests/KeyHandlerBopomofoTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@ class KeyHandlerBopomofoTests: XCTestCase {
}

func testisNumericPad() {
let current = Preferences.selectCandidateWithNumericKeypad
Preferences.selectCandidateWithNumericKeypad = false

var input = KeyHandlerInput(inputText: "b", keyCode: 0, charCode: charCode("b"), flags: [], isVerticalMode: false)
var state: InputState = InputState.Empty()
handler.handle(input: input, state: state) { newState in
Expand All @@ -257,6 +260,9 @@ class KeyHandlerBopomofoTests: XCTestCase {

} errorCallback: {
}

Preferences.selectCandidateWithNumericKeypad = current

XCTAssertEqual(count, 3)
XCTAssertTrue(state is InputState.Empty, "\(state)")
XCTAssertTrue(empty is InputState.Empty, "\(empty)")
Expand All @@ -276,8 +282,8 @@ class KeyHandlerBopomofoTests: XCTestCase {
state = newState
} errorCallback: {
}
XCTAssertFalse(result)
Preferences.letterBehavior = current
XCTAssertFalse(result)
}

func testUppercaseLetterWhenEmpty2() {
Expand All @@ -289,12 +295,12 @@ class KeyHandlerBopomofoTests: XCTestCase {
state = newState
} errorCallback: {
}
Preferences.letterBehavior = current

XCTAssertTrue(state is InputState.Inputting, "\(state)")
if let state = state as? InputState.Inputting {
XCTAssertEqual(state.composingBuffer, "a")
}
Preferences.letterBehavior = current
}

// Regression test for #292.
Expand All @@ -319,8 +325,8 @@ class KeyHandlerBopomofoTests: XCTestCase {
} errorCallback: {
}

XCTAssertFalse(result)
Preferences.letterBehavior = current
XCTAssertFalse(result)
}

// Regression test for #292.
Expand All @@ -345,11 +351,12 @@ class KeyHandlerBopomofoTests: XCTestCase {
} errorCallback: {
}

Preferences.letterBehavior = current

XCTAssertTrue(state is InputState.Inputting, "\(state)")
if let state = state as? InputState.Inputting {
XCTAssertEqual(state.composingBuffer, "一a")
}
Preferences.letterBehavior = current
}

func testPunctuationTable() {
Expand Down

0 comments on commit a079112

Please sign in to comment.