Skip to content

Commit

Permalink
Merge pull request #553 from zonble/associated_phrases
Browse files Browse the repository at this point in the history
Revise the associated phrases feature
  • Loading branch information
zonble authored Nov 5, 2024
2 parents aa41147 + 4b16bfa commit 8bb6e03
Show file tree
Hide file tree
Showing 13 changed files with 653 additions and 170 deletions.
4 changes: 4 additions & 0 deletions McBopomofo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
D427F7B4279086DC004A2160 /* InputSourceHelper in Frameworks */ = {isa = PBXBuildFile; productRef = D427F7B3279086DC004A2160 /* InputSourceHelper */; };
D427F7B6279086F6004A2160 /* InputSourceHelper in Frameworks */ = {isa = PBXBuildFile; productRef = D427F7B5279086F6004A2160 /* InputSourceHelper */; };
D427F7C127908EFC004A2160 /* OpenCCBridge in Frameworks */ = {isa = PBXBuildFile; productRef = D427F7C027908EFC004A2160 /* OpenCCBridge */; };
D436E23F2CD20BA700C6155D /* AssociatedPhrasesTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D436E23E2CD20B9E00C6155D /* AssociatedPhrasesTests.swift */; };
D43FC40B2B23788400ED5A1C /* InputMacro.swift in Sources */ = {isa = PBXBuildFile; fileRef = D43FC40A2B23788400ED5A1C /* InputMacro.swift */; };
D449AD5F2B393C00000C5812 /* InputMacroTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D449AD5E2B393C00000C5812 /* InputMacroTests.swift */; };
D449AD612B39506D000C5812 /* ServiceProviderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D449AD602B39506D000C5812 /* ServiceProviderTests.swift */; };
Expand Down Expand Up @@ -188,6 +189,7 @@
D427F7AC27907B7E004A2160 /* NotifierUI */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = NotifierUI; path = Packages/NotifierUI; sourceTree = "<group>"; };
D427F7B2279086B5004A2160 /* InputSourceHelper */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = InputSourceHelper; path = Packages/InputSourceHelper; sourceTree = "<group>"; };
D427F7BF27908EAC004A2160 /* OpenCCBridge */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = OpenCCBridge; path = Packages/OpenCCBridge; sourceTree = "<group>"; };
D436E23E2CD20B9E00C6155D /* AssociatedPhrasesTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AssociatedPhrasesTests.swift; sourceTree = "<group>"; };
D43FC40A2B23788400ED5A1C /* InputMacro.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InputMacro.swift; sourceTree = "<group>"; };
D449AD5E2B393C00000C5812 /* InputMacroTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InputMacroTests.swift; sourceTree = "<group>"; };
D449AD602B39506D000C5812 /* ServiceProviderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ServiceProviderTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -468,6 +470,7 @@
D485D3B72796A8A000657FF3 /* McBopomofoTests */ = {
isa = PBXGroup;
children = (
D436E23E2CD20B9E00C6155D /* AssociatedPhrasesTests.swift */,
D47D73A327A5D43900255A50 /* KeyHandlerBopomofoTests.swift */,
D4A8E43527A9E982002F7A07 /* KeyHandlerPlainBopomofoTests.swift */,
D485D3B82796A8A000657FF3 /* PreferencesTests.swift */,
Expand Down Expand Up @@ -744,6 +747,7 @@
buildActionMask = 2147483647;
files = (
D4A8E43627A9E982002F7A07 /* KeyHandlerPlainBopomofoTests.swift in Sources */,
D436E23F2CD20BA700C6155D /* AssociatedPhrasesTests.swift in Sources */,
D449AD5F2B393C00000C5812 /* InputMacroTests.swift in Sources */,
D4C2A9872B4309D700113711 /* UTF8HelperTest.mm in Sources */,
D47D73A427A5D43900255A50 /* KeyHandlerBopomofoTests.swift in Sources */,
Expand Down
145 changes: 145 additions & 0 deletions McBopomofoTests/AssociatedPhrasesTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
// Copyright (c) 2022 and onwards The McBopomofo Authors.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

import XCTest

@testable import McBopomofo

class AssociatedPhrasesTests: XCTestCase {

var handler = KeyHandler()
var chineseConversionEnabled: Bool = false

override func setUpWithError() throws {
chineseConversionEnabled = Preferences.chineseConversionEnabled
Preferences.chineseConversionEnabled = false
LanguageModelManager.loadDataModels()
handler = KeyHandler()
handler.inputMode = .bopomofo
}

override func tearDownWithError() throws {
Preferences.chineseConversionEnabled = chineseConversionEnabled
}

func testBuildingAssociatedPhrasesState() {
var state: InputState = InputState.Empty()
let keys = Array("u6").map {
String($0)
}
for key in keys {
let input = KeyHandlerInput(
inputText: key, keyCode: 0, charCode: charCode(key), flags: [],
isVerticalMode: false)
handler.handle(input: input, state: state) { newState in
state = newState
} errorCallback: {
}
}
guard
let associatedPhrases = handler.buildAssociatedPhraseState(
withPreviousState: state, prefixCursorAt: 1, reading: "", value: "",
selectedCandidateIndex: 0, useVerticalMode: false, useShiftKey: false)
as? InputState.AssociatedPhrases
else {
XCTFail("There should be an associated phrase state")
return
}
XCTAssert(associatedPhrases.candidates.count > 0)
}

func testAssociatedPhrasesStatePunctuation1() {
var state: InputState = InputState.Empty()
let keys = Array("{").map {
String($0)
}
for key in keys {
let input = KeyHandlerInput(
inputText: key, keyCode: 0, charCode: charCode(key), flags: [],
isVerticalMode: false)
handler.handle(input: input, state: state) { newState in
state = newState
} errorCallback: {
}
}
guard
let associatedPhrases = handler.buildAssociatedPhraseState(
withPreviousState: state, prefixCursorAt: 1, reading: "_punctuation_{", value: "",
selectedCandidateIndex: 0, useVerticalMode: false, useShiftKey: false)
as? InputState.AssociatedPhrases
else {
XCTFail("There should be an associated phrase state")
return
}
XCTAssert(associatedPhrases.candidates.count > 0)
let candidate = associatedPhrases.candidates[0]

handler.fixNodeForAssociatedPhraseWithPrefix(
at: associatedPhrases.prefixCursorIndex, prefixReading: associatedPhrases.prefixReading,
prefixValue: associatedPhrases.prefixValue, associatedPhraseReading: candidate.reading,
associatedPhraseValue: candidate.value)
guard let inputting = handler.buildInputtingState() as? InputState.Inputting else {
XCTFail("There should be an inputting state")
return
}
XCTAssertTrue(inputting.composingBuffer == "『』")
}

func testAssociatedPhrasesStatePunctuation2() {
var state: InputState = InputState.Empty()
let keys = Array("{").map {
String($0)
}
for key in keys {
let input = KeyHandlerInput(
inputText: key, keyCode: 0, charCode: charCode(key), flags: [],
isVerticalMode: false)
handler.handle(input: input, state: state) { newState in
state = newState
} errorCallback: {
}
}
guard
let associatedPhrases = handler.buildAssociatedPhraseState(
withPreviousState: state, prefixCursorAt: 1, reading: "_punctuation_{", value: "",
selectedCandidateIndex: 0, useVerticalMode: false, useShiftKey: false)
as? InputState.AssociatedPhrases
else {
XCTFail("There should be an associated phrase state")
return
}
XCTAssert(associatedPhrases.candidates.count > 0)
let candidate = associatedPhrases.candidates[0]

handler.fixNodeForAssociatedPhraseWithPrefix(
at: associatedPhrases.prefixCursorIndex, prefixReading: associatedPhrases.prefixReading,
prefixValue: associatedPhrases.prefixValue, associatedPhraseReading: candidate.reading,
associatedPhraseValue: candidate.value)
guard let inputting = handler.buildInputtingState() as? InputState.Inputting else {
XCTFail("There should be an inputting state")
return
}
XCTAssertTrue(inputting.composingBuffer == "《》")
}

}
Loading

0 comments on commit 8bb6e03

Please sign in to comment.