forked from autokey/autokey
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_phrase.py
More file actions
555 lines (471 loc) · 25.7 KB
/
Copy pathtest_phrase.py
File metadata and controls
555 lines (471 loc) · 25.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
# Copyright (C) 2018 Thomas Hess <thomas.hess@udo.edu>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import typing
from unittest.mock import MagicMock
import pytest
from hamcrest import *
from autokey.model.triggermode import TriggerMode
import autokey.model.phrase
from autokey.interface import WindowInfo
ABBR_ONLY = [TriggerMode.ABBREVIATION]
AbbreviationType = typing.Union[str, typing.List[str]]
# Used as parameter containers to cut down argument count.
# These make reading output of failed test cases much easier, because when pytest prints it, it annotates each value
# (e.g. "True") with it’s intended meaning (e.g. "ignore_case=True")
PhraseData = typing.NamedTuple("PhraseData", [
("name", str),
("abbreviation", AbbreviationType),
("content", str),
("trigger_modes", typing.List[TriggerMode]),
("ignore_case", bool),
("match_case", bool),
("trigger_immediately", bool)
])
PhraseResult = typing.NamedTuple("PhraseResult", [
("expansion", typing.Optional[str]),
("abbreviation_length", typing.Optional[int]),
("backspace_count", int),
("triggered_on_input", bool)
])
def create_phrase(
name: str = "phrase",
abbreviation: AbbreviationType = "xp@",
content: str = "expansion@autokey.com", # Values taken from original test code
trigger_modes: typing.List[TriggerMode] = None,
ignore_case: bool = False,
match_case: bool = False,
trigger_immediately: bool = False) -> autokey.model.phrase.Phrase:
"""Save typing by wrapping the Phrase constructor, attribute setters and attributes into a single call."""
if trigger_modes is None:
trigger_modes = [TriggerMode.ABBREVIATION]
phrase = autokey.model.phrase.Phrase(name, content)
if isinstance(abbreviation, str):
phrase.add_abbreviation(abbreviation)
else:
for abbr in abbreviation:
phrase.add_abbreviation(abbr)
phrase.set_modes(trigger_modes)
phrase.ignoreCase = ignore_case
phrase.matchCase = match_case
phrase.immediate = trigger_immediately
# Expansion triggers usage count increase in the parent Folder. Prevent crashes because of a missing parent
phrase.parent = MagicMock()
return phrase
def generate_test_cases_for_ignore_case():
"""Yields PhraseData, typed_input, PhraseResult"""
# Test format is effectively:
# yield phrase_data("abbreviation that triggers phrase", is_case_insensitive_trigger, match_phrase_case_to_typed_phrase_case), "typed text containing abbreviation", phrase_result("Phrase Content", should_phrase_have_triggered)
def phrase_data(abbreviation: str, phrase_content: str, ignore_case: bool) -> PhraseData:
"""Local helper function to save typing constant data"""
return PhraseData(
name="name", abbreviation=abbreviation, content=phrase_content,
trigger_modes=[TriggerMode.ABBREVIATION], ignore_case=ignore_case, match_case=False,
trigger_immediately=False)
def phrase_result(expansion_result: str, triggered: bool) -> PhraseResult:
"""Local helper function to save typing constant data"""
return PhraseResult(
expansion=expansion_result, abbreviation_length=None,
backspace_count=len(expansion_result), triggered_on_input=triggered)
yield phrase_data("ab@", "abbr", False), "AB@ ", phrase_result("", False)
yield phrase_data("AB@", "abbr", False), "ab@ ", phrase_result("", False)
# Don’t match case
yield phrase_data("ab@", "abbr", True), "AB@ ", phrase_result("abbr ", True)
yield phrase_data("AB@", "abbr", True), "ab@ ", phrase_result("abbr ", True)
yield phrase_data("tri", "ab br", True), "TRI ", phrase_result("ab br ", True)
yield phrase_data("TRI", "ab br", True), "tri ", phrase_result("ab br ", True)
yield phrase_data("Tri", "ab br", True), "tri ", phrase_result("ab br ", True)
@pytest.mark.parametrize("phrase_data, trigger_str, phrase_result", generate_test_cases_for_ignore_case())
def test_ignore_case(phrase_data: PhraseData, trigger_str: str, phrase_result: PhraseResult):
phrase = create_phrase(*phrase_data)
# Verify trigger behaviour
assert_that(
phrase.check_input(trigger_str, WindowInfo("", "")),
is_(equal_to(phrase_result.triggered_on_input)),
"Phrase expansion should trigger"
)
if phrase_result.triggered_on_input:
# If the phrase should trigger, also verify the expansion correctness
assert_that(
phrase.build_phrase(trigger_str).string,
is_(equal_to(phrase_result.expansion)),
"Invalid Phrase expansion result string"
)
def generate_test_cases_for_match_case():
"""Yields PhraseData, typed_input, PhraseResult"""
# "tri" in test data is short for "trigger", "ab br" is a short phrase that can show all behaviour variants
def phrase_data(abbreviation: str, phrase_content: str) -> PhraseData:
"""Local helper function to save typing constant data"""
return PhraseData(
name="name", abbreviation=abbreviation, content=phrase_content,
trigger_modes=[TriggerMode.ABBREVIATION], ignore_case=True, match_case=True,
trigger_immediately=False)
def phrase_result(expansion_result: str) -> PhraseResult:
"""Local helper function to save typing constant data"""
return PhraseResult(
expansion=expansion_result, abbreviation_length=None,
backspace_count=len(expansion_result), triggered_on_input=True)
# Match case for lower case content and a lower case abbreviation
yield phrase_data("tri", "ab br"), "tri ", phrase_result("ab br ")
yield phrase_data("tri", "ab br"), "Tri ", phrase_result("Ab br ")
yield phrase_data("tri", "ab br"), "TRI ", phrase_result("AB BR ")
yield phrase_data("tri", "ab br"), "TRi ", phrase_result("ab br ") # Case as defined in the Phrase
# Match case for UPPER CASE content and a lower case abbreviation
yield phrase_data("tri", "AB BR"), "tri ", phrase_result("ab br ")
yield phrase_data("tri", "AB BR"), "Tri ", phrase_result("Ab br ")
yield phrase_data("tri", "AB BR"), "TRI ", phrase_result("AB BR ")
yield phrase_data("tri", "AB BR"), "TRi ", phrase_result("AB BR ") # Case as defined in the Phrase
# Match case for Title Case content and a lower case abbreviation
yield phrase_data("tri", "Ab Br"), "tri ", phrase_result("ab br ")
yield phrase_data("tri", "Ab Br"), "Tri ", phrase_result("Ab br ")
yield phrase_data("tri", "Ab Br"), "TRI ", phrase_result("AB BR ")
yield phrase_data("tri", "Ab Br"), "TRi ", phrase_result("Ab Br ") # Case as defined in the Phrase
# Match case for lower case content and an UPPER CASE abbreviation
yield phrase_data("TRI", "ab br"), "tri ", phrase_result("ab br ")
yield phrase_data("TRI", "ab br"), "Tri ", phrase_result("Ab br ")
yield phrase_data("TRI", "ab br"), "TRI ", phrase_result("AB BR ")
yield phrase_data("TRI", "ab br"), "TRi ", phrase_result("ab br ") # Case as defined in the Phrase
# Match case for UPPER CASE content and an UPPER CASE abbreviation
yield phrase_data("TRI", "AB BR"), "tri ", phrase_result("ab br ")
yield phrase_data("TRI", "AB BR"), "Tri ", phrase_result("Ab br ")
yield phrase_data("TRI", "AB BR"), "TRI ", phrase_result("AB BR ")
yield phrase_data("TRI", "AB BR"), "TRi ", phrase_result("AB BR ") # Case as defined in the Phrase
# Match case for Title Case content and an UPPER CASE abbreviation
yield phrase_data("TRI", "Ab Br"), "tri ", phrase_result("ab br ")
yield phrase_data("TRI", "Ab Br"), "Tri ", phrase_result("Ab br ")
yield phrase_data("TRI", "Ab Br"), "TRI ", phrase_result("AB BR ")
yield phrase_data("TRI", "Ab Br"), "TRi ", phrase_result("Ab Br ") # Case as defined in the Phrase
# Match case for lower case content and a Title Case abbreviation
yield phrase_data("Tri", "ab br"), "tri ", phrase_result("ab br ")
yield phrase_data("Tri", "ab br"), "Tri ", phrase_result("Ab br ")
yield phrase_data("Tri", "ab br"), "TRI ", phrase_result("AB BR ")
yield phrase_data("Tri", "ab br"), "TRi ", phrase_result("ab br ") # Case as defined in the Phrase
# Match case for UPPER CASE content and a Title Case abbreviation
yield phrase_data("Tri", "AB BR"), "tri ", phrase_result("ab br ")
yield phrase_data("Tri", "AB BR"), "Tri ", phrase_result("Ab br ")
yield phrase_data("Tri", "AB BR"), "TRI ", phrase_result("AB BR ")
yield phrase_data("Tri", "AB BR"), "TRi ", phrase_result("AB BR ") # Case as defined in the Phrase
# Match case for Title Case content and a Title Case abbreviation
yield phrase_data("Tri", "Ab Br"), "tri ", phrase_result("ab br ")
yield phrase_data("Tri", "Ab Br"), "Tri ", phrase_result("Ab br ")
yield phrase_data("Tri", "Ab Br"), "TRI ", phrase_result("AB BR ")
yield phrase_data("Tri", "Ab Br"), "TRi ", phrase_result("Ab Br ") # Case as defined in the Phrase
@pytest.mark.parametrize("phrase_data, trigger_str, phrase_result", generate_test_cases_for_match_case())
def test_match_case(phrase_data: PhraseData, trigger_str: str, phrase_result: PhraseResult):
phrase = create_phrase(*phrase_data)
# Expansion should always trigger
assert_that(
phrase.check_input(trigger_str, WindowInfo("", "")),
is_(equal_to(phrase_result.triggered_on_input)),
"Phrase expansion should trigger:"
)
# Verify that the case is matched correctly
result = phrase.build_phrase(trigger_str)
assert_that(
result.string,
is_(equal_to(phrase_result.expansion)),
"Invalid Phrase expansion result string"
)
assert_that(
result.lefts,
is_(equal_to(0)),
)
def generate_test_cases_for_trigger_immediately():
"""Yields PhraseData, typed_input, PhraseResult"""
def phrase_data(abbreviation: str, ignore_case: bool, match_case: bool) -> PhraseData:
"""Local helper function to save typing constant data"""
return PhraseData(
name="name", abbreviation=abbreviation, content="Phrase Content.",
trigger_modes=[TriggerMode.ABBREVIATION], ignore_case=ignore_case, match_case=match_case,
trigger_immediately=True)
def phrase_result(expansion_result: str, triggered: bool) -> PhraseResult:
"""Local helper function to save typing constant data"""
return PhraseResult(
expansion=expansion_result, abbreviation_length=None,
backspace_count=len(expansion_result), triggered_on_input=triggered)
# Positive test
yield phrase_data("ueue", False, False), "ueue", phrase_result("Phrase Content.", True)
yield phrase_data("Ueue", False, False), "Ueue", phrase_result("Phrase Content.", True)
yield phrase_data("UeUe", False, False), "UeUe", phrase_result("Phrase Content.", True)
yield phrase_data("UEUE", False, False), "UEUE", phrase_result("Phrase Content.", True)
# Tests for issue https://github.com/autokey/autokey/issues/254
yield phrase_data("ueue", True, False), "a", phrase_result("", False)
yield phrase_data("ueue", True, True), "b", phrase_result("", False)
yield phrase_data("UeUe", True, False), "a", phrase_result("", False)
yield phrase_data("UeUe", True, True), "a", phrase_result("", False)
yield phrase_data("ueue", True, False), "A", phrase_result("", False)
yield phrase_data("ueue", True, True), "B", phrase_result("", False)
yield phrase_data("UeUe", True, False), "A", phrase_result("", False)
yield phrase_data("UeUe", True, True), "A", phrase_result("", False)
# Test that the fix for #254 does not break things.
# lower case
yield phrase_data("ueue", True, False), "ueue", phrase_result("Phrase Content.", True)
yield phrase_data("ueue", True, True), "ueue", phrase_result("phrase content.", True)
yield phrase_data("UeUe", True, False), "ueue", phrase_result("Phrase Content.", True)
yield phrase_data("UeUe", True, True), "ueue", phrase_result("phrase content.", True)
# mixed case
yield phrase_data("ueue", True, False), "UeUe", phrase_result("Phrase Content.", True)
yield phrase_data("ueue", True, True), "UeUe", phrase_result("Phrase Content.", True)
yield phrase_data("UeUe", True, False), "UeUe", phrase_result("Phrase Content.", True)
yield phrase_data("UeUe", True, True), "UeUe", phrase_result("Phrase Content.", True)
# title case
yield phrase_data("ueue", True, False), "Ueue", phrase_result("Phrase Content.", True)
yield phrase_data("ueue", True, True), "Ueue", phrase_result("Phrase content.", True)
yield phrase_data("UeUe", True, False), "Ueue", phrase_result("Phrase Content.", True)
yield phrase_data("UeUe", True, True), "Ueue", phrase_result("Phrase content.", True)
# upper case
yield phrase_data("ueue", True, False), "UEUE", phrase_result("Phrase Content.", True)
yield phrase_data("ueue", True, True), "UEUE", phrase_result("PHRASE CONTENT.", True)
yield phrase_data("UeUe", True, False), "UEUE", phrase_result("Phrase Content.", True)
yield phrase_data("UeUe", True, True), "UEUE", phrase_result("PHRASE CONTENT.", True)
# Issue from gitter
yield phrase_data("---", False, False), "---", phrase_result("Phrase Content.", True)
@pytest.mark.parametrize("phrase_data, trigger_str, phrase_result", generate_test_cases_for_trigger_immediately())
def test_trigger_immediately(phrase_data: PhraseData, trigger_str: str, phrase_result: PhraseResult):
window_info = WindowInfo("", "")
phrase = create_phrase(*phrase_data)
assert_that(
phrase.check_input(trigger_str, window_info),
is_(equal_to(phrase_result.triggered_on_input)),
"""
Unexpected Phrase trigger.
Does trigger '{}' triggered on input '{}':
""".format(phrase_data.abbreviation, trigger_str)
)
# Generally don’t trigger if the trigger immediately option is on and there is a space after the typed abbreviation.
assert_that(phrase.check_input(phrase_data.abbreviation + " ", window_info), is_(equal_to(False)))
if phrase_result.triggered_on_input:
# Verify the result, if the expansion should trigger
abbreviation_length = len(phrase_data.abbreviation)
result = phrase.build_phrase(trigger_str)
assert_that(result.string, is_(equal_to(phrase_result.expansion)), "Case matching broken.")
assert_that(result.backspaces, is_(equal_to(abbreviation_length)), "Result length computation broken.")
def generate_test_cases_for_case_insensitive_rpartition():
"""
Yields tuples to test the custom case insensitive str.rpartition
input, separator, (left, match, right)
"""
yield "a", "ue", ("", "", "a")
yield "a", "a", ("", "a", "")
yield "ab", "a", ("", "a", "b")
yield "abc", "b", ("a", "b", "c")
yield "ab", "b", ("a", "b", "")
# Upper case match
yield "a", "Ue", ("", "", "a")
yield "a", "A", ("", "a", "")
yield "ab", "A", ("", "a", "b")
yield "abc", "B", ("a", "b", "c")
yield "ab", "B", ("a", "b", "")
# Upper case buffer
yield "A", "ue", ("", "", "A")
yield "A", "a", ("", "A", "")
yield "AB", "a", ("", "A", "B")
yield "ABC", "b", ("A", "B", "C")
yield "AB", "b", ("A", "B", "")
@pytest.mark.parametrize("input_str, match, expected", generate_test_cases_for_case_insensitive_rpartition())
def test_case_insensitive_rpartition(input_str: str, match: str, expected:typing.Tuple[str, str, str]):
assert_that(autokey.model.phrase.Phrase._case_insensitive_rpartition(input_str, match), is_(equal_to(expected)))
def generate_test_cases_for_undo_on_backspace():
"""Yields PhraseData, typed_input, undo_enabled, PhraseResult"""
def phrase_data(trigger_immediately: bool) -> PhraseData:
"""Local helper function to save typing constant data"""
return PhraseData(
name="name", abbreviation="tri", content="ab br",
trigger_modes=[TriggerMode.ABBREVIATION], ignore_case=False, match_case=False,
trigger_immediately=trigger_immediately)
def phrase_result(backspace_count: int) -> PhraseResult:
"""Local helper function to save typing constant data"""
return PhraseResult(
expansion="ab br ", abbreviation_length=None,
backspace_count=backspace_count, triggered_on_input=True)
# Undo disabled: Only one backspace, regardless of the trigger character
yield phrase_data(False), "tri ", False, phrase_result(1)
yield phrase_data(False), "tri\t", False, phrase_result(1)
yield phrase_data(False), "tri\n", False, phrase_result(1)
yield phrase_data(True), "tri", False, phrase_result(0) # Now trigger immediately
# Undo enabled: Remove the trigger character and phrase content
yield phrase_data(False), "tri ", True, phrase_result(4)
yield phrase_data(False), "tri\t", True, phrase_result(4)
yield phrase_data(False), "tri\n", True, phrase_result(4)
yield phrase_data(True), "tri", True, phrase_result(3) # Now trigger immediately
@pytest.mark.parametrize("phrase_data, trigger_str, undo_enabled, phrase_result",
generate_test_cases_for_undo_on_backspace())
def test_undo_on_backspace(phrase_data: PhraseData, trigger_str: str, undo_enabled: bool, phrase_result: PhraseResult):
phrase = create_phrase(*phrase_data)
phrase.backspace = undo_enabled
# Expansion should always trigger
assert_that(
phrase.check_input(trigger_str, WindowInfo("", "")),
is_(equal_to(phrase_result.triggered_on_input)),
"Phrase expansion should trigger:"
)
result = phrase.build_phrase(trigger_str)
assert_that(
result.backspaces,
is_(equal_to(phrase_result.backspace_count)),
"Wrong backspace key count"
)
assert_that(
result.lefts,
is_(equal_to(0)),
)
def generate_test_cases_for_omit_trigger():
"""Yields PhraseData, trigger_str, omit_trigger, PhraseResults"""
def phrase_data(trigger_immediately: bool) -> PhraseData:
"""Local helper function to save typing constant data"""
return PhraseData(
name="name", abbreviation="tri", content="ab br",
trigger_modes=[TriggerMode.ABBREVIATION], ignore_case=False, match_case=False,
trigger_immediately=trigger_immediately)
def phrase_result(expansion: str, backspace_count: int) -> PhraseResult:
"""Local helper function to save typing constant data"""
return PhraseResult(
expansion=expansion, abbreviation_length=None,
backspace_count=backspace_count, triggered_on_input=True)
yield phrase_data(False), "tri ", False, phrase_result("ab br ", 4)
yield phrase_data(False), "tri\t", False, phrase_result("ab br\t", 4)
yield phrase_data(False), "tri\n", False, phrase_result("ab br\n", 4)
yield phrase_data(False), "tri ", True, phrase_result("ab br", 4)
yield phrase_data(False), "tri\t", True, phrase_result("ab br", 4)
yield phrase_data(False), "tri\n", True, phrase_result("ab br", 4)
# Now trigger immediately
yield phrase_data(True), "tri", False, phrase_result("ab br", 3)
yield phrase_data(True), "tri", True, phrase_result("ab br", 3)
@pytest.mark.parametrize("phrase_data, trigger_str, omit_trigger, phrase_result",
generate_test_cases_for_omit_trigger())
def test_omit_trigger(phrase_data: PhraseData, trigger_str: str, omit_trigger: bool, phrase_result: PhraseResult):
"""
omitTrigger set to True causes the trigger character to be not re-typed during Phrase expansion
"""
phrase = create_phrase(*phrase_data)
phrase.omitTrigger = omit_trigger
# Expansion should always trigger
assert_that(
phrase.check_input(trigger_str, WindowInfo("", "")),
is_(equal_to(phrase_result.triggered_on_input)),
"Phrase expansion should trigger:"
)
result = phrase.build_phrase(trigger_str)
assert_that(
result.string,
is_(equal_to(phrase_result.expansion)),
"Wrong trigger character in expansion result"
)
assert_that(
result.backspaces,
is_(equal_to(phrase_result.backspace_count)),
"Wrong backspace character count"
)
assert_that(
result.lefts,
is_(equal_to(0)),
)
def generate_test_cases_for_trigger_phrase_inside_word():
"""Yields PhraseData, trigger_str, PhraseResults"""
def phrase_data(trigger_immediately: bool) -> PhraseData:
"""Local helper function to save typing constant data"""
return PhraseData(
name="name", abbreviation="tri", content="ab br",
trigger_modes=[TriggerMode.ABBREVIATION], ignore_case=False, match_case=False,
trigger_immediately=trigger_immediately)
def phrase_result(expansion: str, backspace_count: int) -> PhraseResult:
"""Local helper function to save typing constant data"""
return PhraseResult(
expansion=expansion, abbreviation_length=None,
backspace_count=backspace_count, triggered_on_input=True)
yield phrase_data(False), "tri\n", phrase_result("ab br\n", 4)
yield phrase_data(False), "abctri ", phrase_result("ab br ", 4)
yield phrase_data(False), "ZQtri.", phrase_result("ab br.", 4)
# Now trigger immediately
yield phrase_data(True), "tri", phrase_result("ab br", 3)
yield phrase_data(True), "abctri", phrase_result("ab br", 3)
yield phrase_data(True), "ZQtri", phrase_result("ab br", 3)
@pytest.mark.parametrize("phrase_data, trigger_str, phrase_result",
generate_test_cases_for_trigger_phrase_inside_word())
def test_trigger_phrase_inside_word(phrase_data: PhraseData, trigger_str: str, phrase_result: PhraseResult):
phrase = create_phrase(*phrase_data)
phrase.triggerInside = True
# Expansion should always trigger
assert_that(
phrase.check_input(trigger_str, WindowInfo("", "")),
is_(equal_to(phrase_result.triggered_on_input)),
"Phrase expansion should trigger:"
)
result = phrase.build_phrase(trigger_str)
assert_that(
result.string,
is_(equal_to(phrase_result.expansion)),
"Wrong expansion result"
)
assert_that(
result.backspaces,
is_(equal_to(phrase_result.backspace_count)),
"Wrong backspace character count"
)
assert_that(
result.lefts,
is_(equal_to(0)),
)
def generate_test_cases_for_count_lefts_for_cursor_macro():
"""Yields PhraseData, trigger_str, expected_lefts, PhraseResults"""
def phrase_data(content: str, trigger_immediately: bool) -> PhraseData:
"""Local helper function to save typing constant data"""
return PhraseData(
name="name", abbreviation="tri", content=content,
trigger_modes=[TriggerMode.ABBREVIATION], ignore_case=False, match_case=False,
trigger_immediately=trigger_immediately)
def phrase_result(expansion: str, backspace_count: int) -> PhraseResult:
"""Local helper function to save typing constant data"""
return PhraseResult(
expansion=expansion, abbreviation_length=None,
backspace_count=backspace_count, triggered_on_input=True)
# Trigger on trigger character
yield phrase_data("ab<cursor> br", False), "tri ", 4, phrase_result("ab br ", 4)
yield phrase_data("ab<cursor> br", False), "tri\n", 4, phrase_result("ab br\n", 4)
yield phrase_data("ab<cursor> br", False), "tri\t", 4, phrase_result("ab br\t", 4)
yield phrase_data("ab<cursor> br", False), "tri.", 4, phrase_result("ab br.", 4)
yield phrase_data("<cursor>ab br", False), "tri ", 6, phrase_result("ab br ", 4)
yield phrase_data("<cursor>ab br", False), "tri\n", 6, phrase_result("ab br\n", 4)
yield phrase_data("ab br<cursor>", False), "tri\t", 1, phrase_result("ab br\t", 4)
yield phrase_data("ab b<cursor>r", False), "tri.", 2, phrase_result("ab br.", 4)
# Trigger immediately
yield phrase_data("<cursor>ab br", True), "tri", 5, phrase_result("ab br", 3)
yield phrase_data("a<cursor>b br", True), "tri", 4, phrase_result("ab br", 3)
yield phrase_data("ab<cursor> br", True), "tri", 3, phrase_result("ab br", 3)
yield phrase_data("ab b<cursor>r", True), "tri", 1, phrase_result("ab br", 3)
yield phrase_data("ab br<cursor>", True), "tri", 0, phrase_result("ab br", 3)
@pytest.mark.parametrize("phrase_data, trigger_str, expected_lefts, phrase_result",
generate_test_cases_for_count_lefts_for_cursor_macro())
def test_count_lefts_for_cursor_macro(phrase_data: PhraseData, trigger_str: str,
expected_lefts: int, phrase_result: PhraseResult):
phrase = create_phrase(*phrase_data)
# Expansion should always trigger
assert_that(
phrase.check_input(trigger_str, WindowInfo("", "")),
is_(equal_to(phrase_result.triggered_on_input)),
"Phrase expansion should trigger:"
)
pytest.xfail("Counting lefts in expansion result seems to be broken legacy code?")
result = phrase.build_phrase(trigger_str)
assert_that(
result.string,
is_(equal_to(phrase_result.expansion)),
"Wrong expansion result"
)
assert_that(
result.backspaces,
is_(equal_to(phrase_result.backspace_count)),
"Wrong backspace character count"
)
assert_that(
result.lefts,
is_(equal_to(expected_lefts)),
)