|
| 1 | +import Core |
| 2 | +import KanaKanjiConverterModule |
| 3 | +import Testing |
| 4 | + |
| 5 | +/// 絵文字入力モード (`.emojiInput`, `.emojiInputNested`) の遷移テスト |
| 6 | +private let zeroEvent = KeyEventCore( |
| 7 | + modifierFlags: [], |
| 8 | + characters: nil, |
| 9 | + charactersIgnoringModifiers: nil, |
| 10 | + keyCode: 0 |
| 11 | +) |
| 12 | + |
| 13 | +/// 日本語モードで全角コロン `:` を打ったのと同等の UserAction を作る |
| 14 | +private func colonInputAction() -> UserAction { |
| 15 | + .input([.key(intention: ":", input: ":", modifiers: [])]) |
| 16 | +} |
| 17 | + |
| 18 | +/// 任意の半角文字を日本語モードで打ったのと同等の UserAction を作る |
| 19 | +private func asciiInputAction(_ c: Character) -> UserAction { |
| 20 | + guard let fullwidth = Core.KeyMap.h2zMap(c) else { |
| 21 | + return .input([.character(c)]) |
| 22 | + } |
| 23 | + return .input([.key(intention: fullwidth, input: c, modifiers: [])]) |
| 24 | +} |
| 25 | + |
| 26 | +private func runEvent( |
| 27 | + state: InputState, |
| 28 | + userAction: UserAction, |
| 29 | + inputLanguage: InputLanguage = .japanese, |
| 30 | + emojiInputEnabled: Bool = true, |
| 31 | + emojiInputTrigger: String = ":" |
| 32 | +) -> (ClientAction, ClientActionCallback) { |
| 33 | + state.event( |
| 34 | + eventCore: zeroEvent, |
| 35 | + userAction: userAction, |
| 36 | + inputLanguage: inputLanguage, |
| 37 | + liveConversionEnabled: false, |
| 38 | + enableDebugWindow: false, |
| 39 | + enableSuggestion: false, |
| 40 | + emojiInputEnabled: emojiInputEnabled, |
| 41 | + emojiInputTrigger: emojiInputTrigger |
| 42 | + ) |
| 43 | +} |
| 44 | + |
| 45 | +// MARK: - トリガー発動 |
| 46 | + |
| 47 | +@Test func testColonFromNoneEntersEmojiInput() async throws { |
| 48 | + let (action, callback) = runEvent(state: .none, userAction: colonInputAction()) |
| 49 | + guard case .enterEmojiInputMode = action else { |
| 50 | + Issue.record("expected .enterEmojiInputMode, got \(action)") |
| 51 | + return |
| 52 | + } |
| 53 | + guard case .transition(.emojiInput("")) = callback else { |
| 54 | + Issue.record("expected .transition(.emojiInput(empty)), got \(callback)") |
| 55 | + return |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +@Test func testColonFromComposingEntersEmojiInputNested() async throws { |
| 60 | + let (action, callback) = runEvent(state: .composing, userAction: colonInputAction()) |
| 61 | + guard case .enterEmojiInputMode = action else { |
| 62 | + Issue.record("expected .enterEmojiInputMode, got \(action)") |
| 63 | + return |
| 64 | + } |
| 65 | + guard case .transition(.emojiInputNested("")) = callback else { |
| 66 | + Issue.record("expected .transition(.emojiInputNested(empty)), got \(callback)") |
| 67 | + return |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +@Test func testColonFromPreviewingEntersEmojiInputNested() async throws { |
| 72 | + let (_, callback) = runEvent(state: .previewing, userAction: colonInputAction()) |
| 73 | + guard case .transition(.emojiInputNested("")) = callback else { |
| 74 | + Issue.record("expected .transition(.emojiInputNested(empty)), got \(callback)") |
| 75 | + return |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +@Test func testColonFromSelectingEntersEmojiInputNested() async throws { |
| 80 | + let (_, callback) = runEvent(state: .selecting, userAction: colonInputAction()) |
| 81 | + guard case .transition(.emojiInputNested("")) = callback else { |
| 82 | + Issue.record("expected .transition(.emojiInputNested(empty)), got \(callback)") |
| 83 | + return |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +// MARK: - 無効化時は発動しない |
| 88 | + |
| 89 | +@Test func testColonDoesNotTriggerWhenDisabled() async throws { |
| 90 | + let (action, _) = runEvent(state: .none, userAction: colonInputAction(), emojiInputEnabled: false) |
| 91 | + // enterEmojiInputMode が返ってこないことを確認 |
| 92 | + if case .enterEmojiInputMode = action { |
| 93 | + Issue.record("emoji input should not trigger when disabled") |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +@Test func testEnglishModeColonDoesNotTriggerEmoji() async throws { |
| 98 | + // 英語モードでは :key(intention: nil) になり preferIntention:true でも ":" 一致しない |
| 99 | + let englishColon: UserAction = .input([.character(":")]) |
| 100 | + let (action, _) = runEvent(state: .none, userAction: englishColon, inputLanguage: .english) |
| 101 | + if case .enterEmojiInputMode = action { |
| 102 | + Issue.record("emoji input should not trigger in English mode") |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// MARK: - emojiInput 中のキー操作 |
| 107 | + |
| 108 | +@Test func testLetterAppendsToEmojiQuery() async throws { |
| 109 | + let (action, callback) = runEvent(state: .emojiInput("sm"), userAction: asciiInputAction("i")) |
| 110 | + guard case .appendToEmojiInput(let appended) = action else { |
| 111 | + Issue.record("expected .appendToEmojiInput, got \(action)") |
| 112 | + return |
| 113 | + } |
| 114 | + #expect(appended == "i") |
| 115 | + guard case .transition(.emojiInput("smi")) = callback else { |
| 116 | + Issue.record("expected .transition(.emojiInput(smi)), got \(callback)") |
| 117 | + return |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +@Test func testBackspaceShrinksEmojiQuery() async throws { |
| 122 | + let (action, callback) = runEvent(state: .emojiInput("smile"), userAction: .backspace) |
| 123 | + if case .removeLastEmojiInput = action {} else { |
| 124 | + Issue.record("expected .removeLastEmojiInput, got \(action)") |
| 125 | + } |
| 126 | + guard case .transition(.emojiInput("smil")) = callback else { |
| 127 | + Issue.record("expected .transition(.emojiInput(smil)), got \(callback)") |
| 128 | + return |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +@Test func testBackspaceOnEmptyQueryCancels() async throws { |
| 133 | + let (action, callback) = runEvent(state: .emojiInput(""), userAction: .backspace) |
| 134 | + if case .cancelEmojiInput = action {} else { |
| 135 | + Issue.record("expected .cancelEmojiInput, got \(action)") |
| 136 | + } |
| 137 | + guard case .transition(.none) = callback else { |
| 138 | + Issue.record("expected .transition(.none), got \(callback)") |
| 139 | + return |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +@Test func testBackspaceOnEmptyNestedQueryReturnsToComposing() async throws { |
| 144 | + let (_, callback) = runEvent(state: .emojiInputNested(""), userAction: .backspace) |
| 145 | + guard case .transition(.composing) = callback else { |
| 146 | + Issue.record("expected .transition(.composing), got \(callback)") |
| 147 | + return |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +@Test func testEnterSubmitsEmojiCandidate() async throws { |
| 152 | + let (action, callback) = runEvent(state: .emojiInput("smile"), userAction: .enter) |
| 153 | + if case .submitSelectedEmojiCandidate = action {} else { |
| 154 | + Issue.record("expected .submitSelectedEmojiCandidate, got \(action)") |
| 155 | + } |
| 156 | + guard case .transition(.none) = callback else { |
| 157 | + Issue.record("expected .transition(.none), got \(callback)") |
| 158 | + return |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +@Test func testNestedEnterReturnsToComposing() async throws { |
| 163 | + let (_, callback) = runEvent(state: .emojiInputNested("smile"), userAction: .enter) |
| 164 | + guard case .transition(.composing) = callback else { |
| 165 | + Issue.record("expected .transition(.composing), got \(callback)") |
| 166 | + return |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +@Test func testEscapeCancels() async throws { |
| 171 | + let (action, callback) = runEvent(state: .emojiInput("smile"), userAction: .escape) |
| 172 | + if case .cancelEmojiInput = action {} else { |
| 173 | + Issue.record("expected .cancelEmojiInput, got \(action)") |
| 174 | + } |
| 175 | + guard case .transition(.none) = callback else { |
| 176 | + Issue.record("expected .transition(.none), got \(callback)") |
| 177 | + return |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +@Test func testNavigationDownSelectsNext() async throws { |
| 182 | + let (action, _) = runEvent(state: .emojiInput("smile"), userAction: .navigation(.down)) |
| 183 | + if case .selectNextEmojiCandidate = action {} else { |
| 184 | + Issue.record("expected .selectNextEmojiCandidate, got \(action)") |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +@Test func testNavigationUpSelectsPrev() async throws { |
| 189 | + let (action, _) = runEvent(state: .emojiInput("smile"), userAction: .navigation(.up)) |
| 190 | + if case .selectPrevEmojiCandidate = action {} else { |
| 191 | + Issue.record("expected .selectPrevEmojiCandidate, got \(action)") |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +@Test func testSecondTriggerCharSubmitsSelected() async throws { |
| 196 | + // "smile" のクエリ中にもう一度「:」を打つと選択中候補を確定 |
| 197 | + let (action, callback) = runEvent(state: .emojiInput("smile"), userAction: colonInputAction()) |
| 198 | + if case .submitSelectedEmojiCandidate = action {} else { |
| 199 | + Issue.record("expected .submitSelectedEmojiCandidate, got \(action)") |
| 200 | + } |
| 201 | + guard case .transition(.none) = callback else { |
| 202 | + Issue.record("expected .transition(.none), got \(callback)") |
| 203 | + return |
| 204 | + } |
| 205 | +} |
| 206 | + |
| 207 | +// MARK: - カスタムトリガー文字 |
| 208 | + |
| 209 | +@Test func testCustomTriggerFiresOnly() async throws { |
| 210 | + // トリガーを ";" (全角セミコロン) に変更したら、コロンでは発動しない |
| 211 | + let customTrigger = ";" |
| 212 | + let (action, _) = runEvent( |
| 213 | + state: .none, |
| 214 | + userAction: colonInputAction(), |
| 215 | + emojiInputTrigger: customTrigger |
| 216 | + ) |
| 217 | + if case .enterEmojiInputMode = action { |
| 218 | + Issue.record("colon should not trigger when custom trigger is set to semicolon") |
| 219 | + } |
| 220 | +} |
0 commit comments