diff --git a/McBopomofo.xcodeproj/project.pbxproj b/McBopomofo.xcodeproj/project.pbxproj index 0e608666..c4ae5ad4 100644 --- a/McBopomofo.xcodeproj/project.pbxproj +++ b/McBopomofo.xcodeproj/project.pbxproj @@ -60,7 +60,7 @@ D485D3B92796A8A000657FF3 /* PreferencesTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D485D3B82796A8A000657FF3 /* PreferencesTests.swift */; }; D485D3C02796CE3200657FF3 /* VersionUpdateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D485D3BF2796CE3200657FF3 /* VersionUpdateTests.swift */; }; D4E569DC27A34D0E00AC2CEF /* KeyHandler.mm in Sources */ = {isa = PBXBuildFile; fileRef = D4E569DB27A34CC100AC2CEF /* KeyHandler.mm */; }; - D4E569DF27A40F1400AC2CEF /* KeyHandlerTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = D4E569DE27A40F1400AC2CEF /* KeyHandlerTests.mm */; }; + D4E569DF27A40F1400AC2CEF /* KeyHandlerBopomofoTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = D4E569DE27A40F1400AC2CEF /* KeyHandlerBopomofoTests.mm */; }; D4E569E027A4123200AC2CEF /* KeyHandlerInput.swift in Sources */ = {isa = PBXBuildFile; fileRef = D456576D279E4F7B00DF6BC9 /* KeyHandlerInput.swift */; }; D4E569E127A4128300AC2CEF /* InputState.swift in Sources */ = {isa = PBXBuildFile; fileRef = D461B791279DAC010070E734 /* InputState.swift */; }; D4E569E227A412E700AC2CEF /* Preferences.swift in Sources */ = {isa = PBXBuildFile; fileRef = D44FB74427915555003C80A6 /* Preferences.swift */; }; @@ -224,7 +224,7 @@ D4E569DA27A34CC100AC2CEF /* KeyHandler.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = KeyHandler.h; sourceTree = ""; }; D4E569DB27A34CC100AC2CEF /* KeyHandler.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = KeyHandler.mm; sourceTree = ""; }; D4E569DD27A40F1300AC2CEF /* McBopomofoTests-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "McBopomofoTests-Bridging-Header.h"; sourceTree = ""; }; - D4E569DE27A40F1400AC2CEF /* KeyHandlerTests.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = KeyHandlerTests.mm; sourceTree = ""; }; + D4E569DE27A40F1400AC2CEF /* KeyHandlerBopomofoTests.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = KeyHandlerBopomofoTests.mm; sourceTree = ""; }; D4F0BBDE279AF1AF0071253C /* ArchiveUtil.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArchiveUtil.swift; sourceTree = ""; }; D4F0BBE0279AF8B30071253C /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; D4F0BBE2279B08900071253C /* BundleTranslocate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BundleTranslocate.h; sourceTree = ""; }; @@ -502,7 +502,7 @@ children = ( D485D3B82796A8A000657FF3 /* PreferencesTests.swift */, D485D3BF2796CE3200657FF3 /* VersionUpdateTests.swift */, - D4E569DE27A40F1400AC2CEF /* KeyHandlerTests.mm */, + D4E569DE27A40F1400AC2CEF /* KeyHandlerBopomofoTests.mm */, D4E569DD27A40F1300AC2CEF /* McBopomofoTests-Bridging-Header.h */, ); path = McBopomofoTests; @@ -750,7 +750,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - D4E569DF27A40F1400AC2CEF /* KeyHandlerTests.mm in Sources */, + D4E569DF27A40F1400AC2CEF /* KeyHandlerBopomofoTests.mm in Sources */, D485D3B92796A8A000657FF3 /* PreferencesTests.swift in Sources */, D4E569E227A412E700AC2CEF /* Preferences.swift in Sources */, D4E569E127A4128300AC2CEF /* InputState.swift in Sources */, diff --git a/McBopomofoTests/KeyHandlerBopomofoTests.mm b/McBopomofoTests/KeyHandlerBopomofoTests.mm new file mode 100644 index 00000000..7c2589cf --- /dev/null +++ b/McBopomofoTests/KeyHandlerBopomofoTests.mm @@ -0,0 +1,655 @@ +#import +#import "KeyHandler.h" +#import "LanguageModelManager.h" +#import "McBopomofoTests-Swift.h" + +@interface KeyHandlerBopomofoTests : XCTestCase + +@end + +@implementation KeyHandlerBopomofoTests + +- (void)setUp +{ + [LanguageModelManager loadDataModels]; +} + +- (void)tearDown +{ +} + +- (void)testPunctuationComma +{ + KeyHandler *handler = [[KeyHandler alloc] init]; + handler.inputMode = kBopomofoModeIdentifier; + + KeyHandlerInput *input; + __block InputState *state; + state = [[InputStateEmpty alloc] init]; + + input = [[KeyHandlerInput alloc] initWithInputText:@"<" keyCode:0 charCode:'<' flags:NSEventModifierFlagShift isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + XCTAssertTrue([state isKindOfClass:NSClassFromString(@"McBopomofo.InputStateInputting")], @"It should be an inputting state %@.", NSStringFromClass([state class])); + NSString *composingBuffer = [(InputStateInputting *)state composingBuffer]; + XCTAssertTrue([composingBuffer isEqualToString:@","], @"It should be , but %@", composingBuffer); +} + +- (void)testPunctuationPeriod +{ + KeyHandler *handler = [[KeyHandler alloc] init]; + handler.inputMode = kBopomofoModeIdentifier; + + KeyHandlerInput *input; + __block InputState *state; + state = [[InputStateEmpty alloc] init]; + + input = [[KeyHandlerInput alloc] initWithInputText:@">" keyCode:0 charCode:'>' flags:NSEventModifierFlagShift isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + XCTAssertTrue([state isKindOfClass:NSClassFromString(@"McBopomofo.InputStateInputting")], @"It should be an inputting state %@.", NSStringFromClass([state class])); + NSString *composingBuffer = [(InputStateInputting *)state composingBuffer]; + XCTAssertTrue([composingBuffer isEqualToString:@"。"], @"It should be 。 but %@", composingBuffer); +} + +- (void)testInputtingNihao +{ + KeyHandler *handler = [[KeyHandler alloc] init]; + handler.inputMode = kBopomofoModeIdentifier; + + KeyHandlerInput *input; + __block InputState *state; + state = [[InputStateEmpty alloc] init]; + + input = [[KeyHandlerInput alloc] initWithInputText:@"s" keyCode:0 charCode:'s' flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + input = [[KeyHandlerInput alloc] initWithInputText:@"u" keyCode:0 charCode:'u' flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + input = [[KeyHandlerInput alloc] initWithInputText:@"3" keyCode:0 charCode:'3' flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + input = [[KeyHandlerInput alloc] initWithInputText:@"c" keyCode:0 charCode:'c' flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + input = [[KeyHandlerInput alloc] initWithInputText:@"l" keyCode:0 charCode:'l' flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + input = [[KeyHandlerInput alloc] initWithInputText:@"3" keyCode:0 charCode:'3' flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + XCTAssertTrue([state isKindOfClass:NSClassFromString(@"McBopomofo.InputStateInputting")], @"It should be an inputting state %@.", NSStringFromClass([state class])); + NSString *composingBuffer = [(InputStateInputting *)state composingBuffer]; + XCTAssertTrue([composingBuffer isEqualToString:@"你好"], @"It should be 你好 but %@", composingBuffer); +} + +- (void)testCommittingNihao +{ + KeyHandler *handler = [[KeyHandler alloc] init]; + handler.inputMode = kBopomofoModeIdentifier; + + KeyHandlerInput *input; + __block InputState *state; + state = [[InputStateEmpty alloc] init]; + + input = [[KeyHandlerInput alloc] initWithInputText:@"s" keyCode:0 charCode:'s' flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + input = [[KeyHandlerInput alloc] initWithInputText:@"u" keyCode:0 charCode:'u' flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + input = [[KeyHandlerInput alloc] initWithInputText:@"3" keyCode:0 charCode:'3' flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + input = [[KeyHandlerInput alloc] initWithInputText:@"c" keyCode:0 charCode:'c' flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + input = [[KeyHandlerInput alloc] initWithInputText:@"l" keyCode:0 charCode:'l' flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + input = [[KeyHandlerInput alloc] initWithInputText:@"3" keyCode:0 charCode:'3' flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + __block NSInteger count = 0; + + __block InputState *empty; + + input = [[KeyHandlerInput alloc] initWithInputText:@" " keyCode:0 charCode:13 flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + if (!count) { + state = inState; + } + empty = inState; + count++; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + XCTAssertTrue([state isKindOfClass:NSClassFromString(@"McBopomofo.InputStateCommitting")], @"It should be a committing state %@.", NSStringFromClass([state class])); + NSString *poppedText = [(InputStateCommitting *)state poppedText]; + XCTAssertTrue([poppedText isEqualToString:@"你好"], @"It should be 你好 but %@", poppedText); + + XCTAssertTrue([empty isKindOfClass:NSClassFromString(@"McBopomofo.InputStateEmpty")], @"It should be an empty state %@.", NSStringFromClass([state class])); +} + +- (void)testDelete +{ + KeyHandler *handler = [[KeyHandler alloc] init]; + handler.inputMode = kBopomofoModeIdentifier; + + KeyHandlerInput *input; + __block InputState *state; + state = [[InputStateEmpty alloc] init]; + + input = [[KeyHandlerInput alloc] initWithInputText:@"s" keyCode:0 charCode:'s' flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + input = [[KeyHandlerInput alloc] initWithInputText:@"u" keyCode:0 charCode:'u' flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + input = [[KeyHandlerInput alloc] initWithInputText:@"3" keyCode:0 charCode:'3' flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + input = [[KeyHandlerInput alloc] initWithInputText:@"c" keyCode:0 charCode:'c' flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + input = [[KeyHandlerInput alloc] initWithInputText:@"l" keyCode:0 charCode:'l' flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + input = [[KeyHandlerInput alloc] initWithInputText:@"3" keyCode:0 charCode:'3' flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + XCTAssertTrue([state isKindOfClass:NSClassFromString(@"McBopomofo.InputStateInputting")], @"It should be an inputting state %@.", NSStringFromClass([state class])); + NSString *composingBuffer = [(InputStateInputting *)state composingBuffer]; + XCTAssertTrue([composingBuffer isEqualToString:@"你好"], @"It should be 你好 but %@", composingBuffer); + XCTAssertEqual([(InputStateInputting *)state cursorIndex], 2); + + input = [[KeyHandlerInput alloc] initWithInputText:@" " keyCode:123 charCode:0 flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = (InputStateInputting *)inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + XCTAssertTrue([state isKindOfClass:NSClassFromString(@"McBopomofo.InputStateInputting")], @"It should be a inputting state %@.", NSStringFromClass([state class])); + composingBuffer = [(InputStateInputting *)state composingBuffer]; + XCTAssertTrue([composingBuffer isEqualToString:@"你好"], @"It should be 你好 but %@", composingBuffer); + XCTAssertEqual([(InputStateInputting *)state cursorIndex], 1); + + input = [[KeyHandlerInput alloc] initWithInputText:@" " keyCode:117 charCode:0 flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = (InputStateInputting *)inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + XCTAssertTrue([state isKindOfClass:NSClassFromString(@"McBopomofo.InputStateInputting")], @"It should be a inputting state %@.", NSStringFromClass([state class])); + composingBuffer = [(InputStateInputting *)state composingBuffer]; + XCTAssertTrue([composingBuffer isEqualToString:@"你"], @"It should be 你 but %@", composingBuffer); + XCTAssertEqual([(InputStateInputting *)state cursorIndex], 1); + + __block BOOL errorCalled = NO; + + input = [[KeyHandlerInput alloc] initWithInputText:@" " keyCode:117 charCode:0 flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = (InputStateInputting *)inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + errorCalled = YES; + }]; + + XCTAssertTrue([state isKindOfClass:NSClassFromString(@"McBopomofo.InputStateInputting")], @"It should be a inputting state %@.", NSStringFromClass([state class])); + composingBuffer = [(InputStateInputting *)state composingBuffer]; + XCTAssertTrue([composingBuffer isEqualToString:@"你"], @"It should be 你 but %@", composingBuffer); + XCTAssertEqual([(InputStateInputting *)state cursorIndex], 1); + XCTAssertTrue(errorCalled); + + input = [[KeyHandlerInput alloc] initWithInputText:@" " keyCode:123 charCode:0 flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = (InputStateInputting *)inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + XCTAssertTrue([state isKindOfClass:NSClassFromString(@"McBopomofo.InputStateInputting")], @"It should be a inputting state %@.", NSStringFromClass([state class])); + composingBuffer = [(InputStateInputting *)state composingBuffer]; + XCTAssertTrue([composingBuffer isEqualToString:@"你"], @"It should be 你 but %@", composingBuffer); + XCTAssertEqual([(InputStateInputting *)state cursorIndex], 0); + + errorCalled = NO; + + input = [[KeyHandlerInput alloc] initWithInputText:@" " keyCode:117 charCode:0 flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = (InputStateInputting *)inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + errorCalled = YES; + }]; + + XCTAssertTrue([state isKindOfClass:NSClassFromString(@"McBopomofo.InputStateEmptyIgnoringPreviousState")], @"It should be a inputting state %@.", NSStringFromClass([state class])); + XCTAssertFalse(errorCalled); +} + +- (void)testBackspace +{ + KeyHandler *handler = [[KeyHandler alloc] init]; + handler.inputMode = kBopomofoModeIdentifier; + + KeyHandlerInput *input; + __block InputState *state; + state = [[InputStateEmpty alloc] init]; + + input = [[KeyHandlerInput alloc] initWithInputText:@"s" keyCode:0 charCode:'s' flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + input = [[KeyHandlerInput alloc] initWithInputText:@"u" keyCode:0 charCode:'u' flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + input = [[KeyHandlerInput alloc] initWithInputText:@"3" keyCode:0 charCode:'3' flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + input = [[KeyHandlerInput alloc] initWithInputText:@"c" keyCode:0 charCode:'c' flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + input = [[KeyHandlerInput alloc] initWithInputText:@"l" keyCode:0 charCode:'l' flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + input = [[KeyHandlerInput alloc] initWithInputText:@"3" keyCode:0 charCode:'3' flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + XCTAssertTrue([state isKindOfClass:NSClassFromString(@"McBopomofo.InputStateInputting")], @"It should be an inputting state %@.", NSStringFromClass([state class])); + NSString *composingBuffer = [(InputStateInputting *)state composingBuffer]; + XCTAssertTrue([composingBuffer isEqualToString:@"你好"], @"It should be 你好 but %@", composingBuffer); + + input = [[KeyHandlerInput alloc] initWithInputText:@" " keyCode:0 charCode:8 flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = (InputStateInputting *)inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + XCTAssertTrue([state isKindOfClass:NSClassFromString(@"McBopomofo.InputStateInputting")], @"It should be a inputting state %@.", NSStringFromClass([state class])); + composingBuffer = [(InputStateInputting *)state composingBuffer]; + XCTAssertTrue([composingBuffer isEqualToString:@"你"], @"It should be 你 but %@", composingBuffer); + + __block InputStateEmpty *empty; + + input = [[KeyHandlerInput alloc] initWithInputText:@" " keyCode:0 charCode:8 flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + empty = (InputStateEmpty *)inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + XCTAssertTrue([empty isKindOfClass:NSClassFromString(@"McBopomofo.InputStateEmptyIgnoringPreviousState")], @"It should be a inputting state %@.", NSStringFromClass([state class])); +} + +- (void)testCursor +{ + KeyHandler *handler = [[KeyHandler alloc] init]; + handler.inputMode = kBopomofoModeIdentifier; + + KeyHandlerInput *input; + __block InputState *state; + state = [[InputStateEmpty alloc] init]; + + input = [[KeyHandlerInput alloc] initWithInputText:@"s" keyCode:0 charCode:'s' flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + input = [[KeyHandlerInput alloc] initWithInputText:@"u" keyCode:0 charCode:'u' flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + input = [[KeyHandlerInput alloc] initWithInputText:@"3" keyCode:0 charCode:'3' flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + input = [[KeyHandlerInput alloc] initWithInputText:@"c" keyCode:0 charCode:'c' flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + input = [[KeyHandlerInput alloc] initWithInputText:@"l" keyCode:0 charCode:'l' flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + input = [[KeyHandlerInput alloc] initWithInputText:@"3" keyCode:0 charCode:'3' flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + XCTAssertTrue([state isKindOfClass:NSClassFromString(@"McBopomofo.InputStateInputting")], @"It should be an inputting state %@.", NSStringFromClass([state class])); + NSString *composingBuffer = [(InputStateInputting *)state composingBuffer]; + XCTAssertTrue([composingBuffer isEqualToString:@"你好"], @"It should be 你好 but %@", composingBuffer); + XCTAssertEqual([(InputStateInputting *)state cursorIndex], 2); + + input = [[KeyHandlerInput alloc] initWithInputText:@" " keyCode:123 charCode:0 flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = (InputStateInputting *)inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + XCTAssertTrue([state isKindOfClass:NSClassFromString(@"McBopomofo.InputStateInputting")], @"It should be a inputting state %@.", NSStringFromClass([state class])); + composingBuffer = [(InputStateInputting *)state composingBuffer]; + XCTAssertTrue([composingBuffer isEqualToString:@"你好"], @"It should be 你好 but %@", composingBuffer); + XCTAssertEqual([(InputStateInputting *)state cursorIndex], 1); + + input = [[KeyHandlerInput alloc] initWithInputText:@" " keyCode:123 charCode:0 flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = (InputStateInputting *)inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + XCTAssertTrue([state isKindOfClass:NSClassFromString(@"McBopomofo.InputStateInputting")], @"It should be a inputting state %@.", NSStringFromClass([state class])); + composingBuffer = [(InputStateInputting *)state composingBuffer]; + XCTAssertTrue([composingBuffer isEqualToString:@"你好"], @"It should be 你好 but %@", composingBuffer); + XCTAssertEqual([(InputStateInputting *)state cursorIndex], 0); + + __block BOOL errorCalled = NO; + + input = [[KeyHandlerInput alloc] initWithInputText:@" " keyCode:123 charCode:0 flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = (InputStateInputting *)inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + errorCalled = YES; + }]; + + XCTAssertTrue([state isKindOfClass:NSClassFromString(@"McBopomofo.InputStateInputting")], @"It should be a inputting state %@.", NSStringFromClass([state class])); + composingBuffer = [(InputStateInputting *)state composingBuffer]; + XCTAssertTrue([composingBuffer isEqualToString:@"你好"], @"It should be 你好 but %@", composingBuffer); + XCTAssertEqual([(InputStateInputting *)state cursorIndex], 0); + XCTAssertTrue(errorCalled); + + input = [[KeyHandlerInput alloc] initWithInputText:@" " keyCode:124 charCode:0 flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = (InputStateInputting *)inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + XCTAssertTrue([state isKindOfClass:NSClassFromString(@"McBopomofo.InputStateInputting")], @"It should be a inputting state %@.", NSStringFromClass([state class])); + composingBuffer = [(InputStateInputting *)state composingBuffer]; + XCTAssertTrue([composingBuffer isEqualToString:@"你好"], @"It should be 你好 but %@", composingBuffer); + XCTAssertEqual([(InputStateInputting *)state cursorIndex], 1); + + input = [[KeyHandlerInput alloc] initWithInputText:@" " keyCode:124 charCode:0 flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = (InputStateInputting *)inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + XCTAssertTrue([state isKindOfClass:NSClassFromString(@"McBopomofo.InputStateInputting")], @"It should be a inputting state %@.", NSStringFromClass([state class])); + composingBuffer = [(InputStateInputting *)state composingBuffer]; + XCTAssertTrue([composingBuffer isEqualToString:@"你好"], @"It should be 你好 but %@", composingBuffer); + XCTAssertEqual([(InputStateInputting *)state cursorIndex], 2); + + errorCalled = NO; + + input = [[KeyHandlerInput alloc] initWithInputText:@" " keyCode:124 charCode:0 flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = (InputStateInputting *)inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + errorCalled = YES; + }]; + + XCTAssertTrue([state isKindOfClass:NSClassFromString(@"McBopomofo.InputStateInputting")], @"It should be a inputting state %@.", NSStringFromClass([state class])); + composingBuffer = [(InputStateInputting *)state composingBuffer]; + XCTAssertTrue([composingBuffer isEqualToString:@"你好"], @"It should be 你好 but %@", composingBuffer); + XCTAssertEqual([(InputStateInputting *)state cursorIndex], 2); + XCTAssertTrue(errorCalled); +} + +- (void)testCandidateWithDown +{ + KeyHandler *handler = [[KeyHandler alloc] init]; + handler.inputMode = kBopomofoModeIdentifier; + + KeyHandlerInput *input; + __block InputState *state; + state = [[InputStateEmpty alloc] init]; + + input = [[KeyHandlerInput alloc] initWithInputText:@"s" keyCode:0 charCode:'s' flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + input = [[KeyHandlerInput alloc] initWithInputText:@"u" keyCode:0 charCode:'u' flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + input = [[KeyHandlerInput alloc] initWithInputText:@"3" keyCode:0 charCode:'3' flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + input = [[KeyHandlerInput alloc] initWithInputText:@" " keyCode:125 charCode:0 flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + XCTAssertTrue([state isKindOfClass:NSClassFromString(@"McBopomofo.InputStateChoosingCandidate")], @"It should be a inputting state %@.", NSStringFromClass([state class])); + NSArray *candidates = [(InputStateChoosingCandidate *)state candidates]; + XCTAssertTrue([candidates containsObject:@"你"]); + +} + +- (void)testHomeAndEnd +{ + KeyHandler *handler = [[KeyHandler alloc] init]; + handler.inputMode = kBopomofoModeIdentifier; + + KeyHandlerInput *input; + __block InputState *state; + state = [[InputStateEmpty alloc] init]; + + input = [[KeyHandlerInput alloc] initWithInputText:@"s" keyCode:0 charCode:'s' flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + input = [[KeyHandlerInput alloc] initWithInputText:@"u" keyCode:0 charCode:'u' flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + input = [[KeyHandlerInput alloc] initWithInputText:@"3" keyCode:0 charCode:'3' flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + input = [[KeyHandlerInput alloc] initWithInputText:@"c" keyCode:0 charCode:'c' flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + input = [[KeyHandlerInput alloc] initWithInputText:@"l" keyCode:0 charCode:'l' flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + input = [[KeyHandlerInput alloc] initWithInputText:@"3" keyCode:0 charCode:'3' flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + XCTAssertTrue([state isKindOfClass:NSClassFromString(@"McBopomofo.InputStateInputting")], @"It should be an inputting state %@.", NSStringFromClass([state class])); + NSString *composingBuffer = [(InputStateInputting *)state composingBuffer]; + XCTAssertTrue([composingBuffer isEqualToString:@"你好"], @"It should be 你好 but %@", composingBuffer); + XCTAssertEqual([(InputStateInputting *)state cursorIndex], 2); + + input = [[KeyHandlerInput alloc] initWithInputText:@" " keyCode:115 charCode:0 flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + XCTAssertTrue([state isKindOfClass:NSClassFromString(@"McBopomofo.InputStateInputting")], @"It should be an inputting state %@.", NSStringFromClass([state class])); + composingBuffer = [(InputStateInputting *)state composingBuffer]; + XCTAssertTrue([composingBuffer isEqualToString:@"你好"], @"It should be 你好 but %@", composingBuffer); + XCTAssertEqual([(InputStateInputting *)state cursorIndex], 0); + + input = [[KeyHandlerInput alloc] initWithInputText:@" " keyCode:119 charCode:0 flags:0 isVerticalMode:0]; + [handler handleInput:input state:state stateCallback:^(InputState * inState) { + state = inState; + } candidateSelectionCallback:^{ + } errorCallback:^{ + }]; + + XCTAssertTrue([state isKindOfClass:NSClassFromString(@"McBopomofo.InputStateInputting")], @"It should be an inputting state %@.", NSStringFromClass([state class])); + composingBuffer = [(InputStateInputting *)state composingBuffer]; + XCTAssertTrue([composingBuffer isEqualToString:@"你好"], @"It should be 你好 but %@", composingBuffer); + XCTAssertEqual([(InputStateInputting *)state cursorIndex], 2); + +} + +@end + diff --git a/McBopomofoTests/KeyHandlerTests.mm b/McBopomofoTests/KeyHandlerTests.mm deleted file mode 100644 index 696e79b1..00000000 --- a/McBopomofoTests/KeyHandlerTests.mm +++ /dev/null @@ -1,230 +0,0 @@ -#import -#import "KeyHandler.h" -#import "LanguageModelManager.h" -#import "McBopomofoTests-Swift.h" - -@interface KeyHandlerTests : XCTestCase - -@end - -@implementation KeyHandlerTests - -- (void)setUp -{ - [LanguageModelManager loadDataModels]; -} - -- (void)tearDown -{ -} - -- (void)testInputtingNihaoUsingMcBopomofo -{ - KeyHandler *handler = [[KeyHandler alloc] init]; - handler.inputMode = kBopomofoModeIdentifier; - - KeyHandlerInput *input; - __block InputState *state; - state = [[InputStateEmpty alloc] init]; - - input = [[KeyHandlerInput alloc] initWithInputText:@"s" keyCode:0 charCode:'s' flags:0 isVerticalMode:0]; - [handler handleInput:input state:state stateCallback:^(InputState * inState) { - state = inState; - } candidateSelectionCallback:^{ - } errorCallback:^{ - }]; - - input = [[KeyHandlerInput alloc] initWithInputText:@"u" keyCode:0 charCode:'u' flags:0 isVerticalMode:0]; - [handler handleInput:input state:state stateCallback:^(InputState * inState) { - state = inState; - } candidateSelectionCallback:^{ - } errorCallback:^{ - }]; - - input = [[KeyHandlerInput alloc] initWithInputText:@"3" keyCode:0 charCode:'3' flags:0 isVerticalMode:0]; - [handler handleInput:input state:state stateCallback:^(InputState * inState) { - state = inState; - } candidateSelectionCallback:^{ - } errorCallback:^{ - }]; - - input = [[KeyHandlerInput alloc] initWithInputText:@"c" keyCode:0 charCode:'c' flags:0 isVerticalMode:0]; - [handler handleInput:input state:state stateCallback:^(InputState * inState) { - state = inState; - } candidateSelectionCallback:^{ - } errorCallback:^{ - }]; - - input = [[KeyHandlerInput alloc] initWithInputText:@"l" keyCode:0 charCode:'l' flags:0 isVerticalMode:0]; - [handler handleInput:input state:state stateCallback:^(InputState * inState) { - state = inState; - } candidateSelectionCallback:^{ - } errorCallback:^{ - }]; - - input = [[KeyHandlerInput alloc] initWithInputText:@"3" keyCode:0 charCode:'3' flags:0 isVerticalMode:0]; - [handler handleInput:input state:state stateCallback:^(InputState * inState) { - state = inState; - } candidateSelectionCallback:^{ - } errorCallback:^{ - }]; - - XCTAssertTrue([state isKindOfClass:NSClassFromString(@"McBopomofo.InputStateInputting")], @"It should be an inputting state %@.", NSStringFromClass([state class])); - NSString *composingBuffer = [(InputStateInputting *)state composingBuffer]; - XCTAssertTrue([composingBuffer isEqualToString:@"你好"], @"It should be 你好 but %@", composingBuffer); -} - -- (void)testComittingNihaoUsingMcBopomofo -{ - KeyHandler *handler = [[KeyHandler alloc] init]; - handler.inputMode = kBopomofoModeIdentifier; - - KeyHandlerInput *input; - __block InputState *state; - state = [[InputStateEmpty alloc] init]; - - input = [[KeyHandlerInput alloc] initWithInputText:@"s" keyCode:0 charCode:'s' flags:0 isVerticalMode:0]; - [handler handleInput:input state:state stateCallback:^(InputState * inState) { - state = inState; - } candidateSelectionCallback:^{ - } errorCallback:^{ - }]; - - input = [[KeyHandlerInput alloc] initWithInputText:@"u" keyCode:0 charCode:'u' flags:0 isVerticalMode:0]; - [handler handleInput:input state:state stateCallback:^(InputState * inState) { - state = inState; - } candidateSelectionCallback:^{ - } errorCallback:^{ - }]; - - input = [[KeyHandlerInput alloc] initWithInputText:@"3" keyCode:0 charCode:'3' flags:0 isVerticalMode:0]; - [handler handleInput:input state:state stateCallback:^(InputState * inState) { - state = inState; - } candidateSelectionCallback:^{ - } errorCallback:^{ - }]; - - input = [[KeyHandlerInput alloc] initWithInputText:@"c" keyCode:0 charCode:'c' flags:0 isVerticalMode:0]; - [handler handleInput:input state:state stateCallback:^(InputState * inState) { - state = inState; - } candidateSelectionCallback:^{ - } errorCallback:^{ - }]; - - input = [[KeyHandlerInput alloc] initWithInputText:@"l" keyCode:0 charCode:'l' flags:0 isVerticalMode:0]; - [handler handleInput:input state:state stateCallback:^(InputState * inState) { - state = inState; - } candidateSelectionCallback:^{ - } errorCallback:^{ - }]; - - input = [[KeyHandlerInput alloc] initWithInputText:@"3" keyCode:0 charCode:'3' flags:0 isVerticalMode:0]; - [handler handleInput:input state:state stateCallback:^(InputState * inState) { - state = inState; - } candidateSelectionCallback:^{ - } errorCallback:^{ - }]; - - __block NSInteger count = 0; - - __block InputState *empty; - - input = [[KeyHandlerInput alloc] initWithInputText:@" " keyCode:0 charCode:13 flags:0 isVerticalMode:0]; - [handler handleInput:input state:state stateCallback:^(InputState * inState) { - if (!count) { - state = inState; - } - empty = inState; - count++; - } candidateSelectionCallback:^{ - } errorCallback:^{ - }]; - - XCTAssertTrue([state isKindOfClass:NSClassFromString(@"McBopomofo.InputStateCommitting")], @"It should be a committing state %@.", NSStringFromClass([state class])); - NSString *poppedText = [(InputStateCommitting *)state poppedText]; - XCTAssertTrue([poppedText isEqualToString:@"你好"], @"It should be 你好 but %@", poppedText); - - XCTAssertTrue([empty isKindOfClass:NSClassFromString(@"McBopomofo.InputStateEmpty")], @"It should be an empty state %@.", NSStringFromClass([state class])); -} - - -- (void)testBackspaceUsingMcBopomofo -{ - KeyHandler *handler = [[KeyHandler alloc] init]; - handler.inputMode = kBopomofoModeIdentifier; - - KeyHandlerInput *input; - __block InputState *state; - state = [[InputStateEmpty alloc] init]; - - input = [[KeyHandlerInput alloc] initWithInputText:@"s" keyCode:0 charCode:'s' flags:0 isVerticalMode:0]; - [handler handleInput:input state:state stateCallback:^(InputState * inState) { - state = inState; - } candidateSelectionCallback:^{ - } errorCallback:^{ - }]; - - input = [[KeyHandlerInput alloc] initWithInputText:@"u" keyCode:0 charCode:'u' flags:0 isVerticalMode:0]; - [handler handleInput:input state:state stateCallback:^(InputState * inState) { - state = inState; - } candidateSelectionCallback:^{ - } errorCallback:^{ - }]; - - input = [[KeyHandlerInput alloc] initWithInputText:@"3" keyCode:0 charCode:'3' flags:0 isVerticalMode:0]; - [handler handleInput:input state:state stateCallback:^(InputState * inState) { - state = inState; - } candidateSelectionCallback:^{ - } errorCallback:^{ - }]; - - input = [[KeyHandlerInput alloc] initWithInputText:@"c" keyCode:0 charCode:'c' flags:0 isVerticalMode:0]; - [handler handleInput:input state:state stateCallback:^(InputState * inState) { - state = inState; - } candidateSelectionCallback:^{ - } errorCallback:^{ - }]; - - input = [[KeyHandlerInput alloc] initWithInputText:@"l" keyCode:0 charCode:'l' flags:0 isVerticalMode:0]; - [handler handleInput:input state:state stateCallback:^(InputState * inState) { - state = inState; - } candidateSelectionCallback:^{ - } errorCallback:^{ - }]; - - input = [[KeyHandlerInput alloc] initWithInputText:@"3" keyCode:0 charCode:'3' flags:0 isVerticalMode:0]; - [handler handleInput:input state:state stateCallback:^(InputState * inState) { - state = inState; - } candidateSelectionCallback:^{ - } errorCallback:^{ - }]; - - XCTAssertTrue([state isKindOfClass:NSClassFromString(@"McBopomofo.InputStateInputting")], @"It should be an inputting state %@.", NSStringFromClass([state class])); - NSString *composingBuffer = [(InputStateInputting *)state composingBuffer]; - XCTAssertTrue([composingBuffer isEqualToString:@"你好"], @"It should be 你好 but %@", composingBuffer); - - input = [[KeyHandlerInput alloc] initWithInputText:@" " keyCode:0 charCode:8 flags:0 isVerticalMode:0]; - [handler handleInput:input state:state stateCallback:^(InputState * inState) { - state = (InputStateInputting *)inState; - } candidateSelectionCallback:^{ - } errorCallback:^{ - }]; - - XCTAssertTrue([state isKindOfClass:NSClassFromString(@"McBopomofo.InputStateInputting")], @"It should be a inputting state %@.", NSStringFromClass([state class])); - composingBuffer = [(InputStateInputting *)state composingBuffer]; - XCTAssertTrue([composingBuffer isEqualToString:@"你"], @"It should be 你 but %@", composingBuffer); - - __block InputStateEmpty *empty; - - input = [[KeyHandlerInput alloc] initWithInputText:@" " keyCode:0 charCode:8 flags:0 isVerticalMode:0]; - [handler handleInput:input state:state stateCallback:^(InputState * inState) { - empty = (InputStateEmpty *)inState; - } candidateSelectionCallback:^{ - } errorCallback:^{ - }]; - - XCTAssertTrue([empty isKindOfClass:NSClassFromString(@"McBopomofo.InputStateEmptyIgnoringPreviousState")], @"It should be a inputting state %@.", NSStringFromClass([state class])); -} - -@end -