From 1dc3301b90dbac5ba39519b312369f8f7fd5394a Mon Sep 17 00:00:00 2001 From: Lukhnos Liu Date: Thu, 23 Nov 2023 07:03:12 -0800 Subject: [PATCH] Reset selectCandidateWithNumericKeypad for testing This issue was discovered after #381 if the tests were run locally when `selectCandidateWithNumericKeypad` is set to true. This 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. --- McBopomofoTests/KeyHandlerBopomofoTests.swift | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/McBopomofoTests/KeyHandlerBopomofoTests.swift b/McBopomofoTests/KeyHandlerBopomofoTests.swift index ef47d3e2..a5358787 100644 --- a/McBopomofoTests/KeyHandlerBopomofoTests.swift +++ b/McBopomofoTests/KeyHandlerBopomofoTests.swift @@ -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 @@ -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)") @@ -276,8 +282,8 @@ class KeyHandlerBopomofoTests: XCTestCase { state = newState } errorCallback: { } - XCTAssertFalse(result) Preferences.letterBehavior = current + XCTAssertFalse(result) } func testUppercaseLetterWhenEmpty2() { @@ -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. @@ -319,8 +325,8 @@ class KeyHandlerBopomofoTests: XCTestCase { } errorCallback: { } - XCTAssertFalse(result) Preferences.letterBehavior = current + XCTAssertFalse(result) } // Regression test for #292. @@ -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() {