-
Notifications
You must be signed in to change notification settings - Fork 258
Expand file tree
/
Copy pathchord_sim_tests.rs
More file actions
515 lines (479 loc) · 12 KB
/
chord_sim_tests.rs
File metadata and controls
515 lines (479 loc) · 12 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
use super::*;
static SIMPLE_NONOVERLAPPING_CHORD_CFG: &str = "\
(defcfg process-unmapped-keys yes concurrent-tap-hold yes) \
(defsrc) \
(defalias c c)
(defvar d d)
(deflayer base) \
(defchordsv2 \
(a b) @c 200 all-released () \
(b z) $d 200 first-release () \
)";
#[test]
fn sim_chord_basic_repeated_last_release() {
let result = simulate(
SIMPLE_NONOVERLAPPING_CHORD_CFG,
"d:a t:50 d:b t:50 u:a t:50 u:b t:50 \
d:b t:50 d:a t:50 u:b t:50 u:a t:50 ",
);
assert_eq!(
"t:50ms\nout:↓C\nt:102ms\nout:↑C\nt:98ms\nout:↓C\nt:102ms\nout:↑C",
result
);
}
#[test]
fn sim_chord_min_idle_takes_effect() {
let result = simulate(
SIMPLE_NONOVERLAPPING_CHORD_CFG,
"d:z t:20 d:a t:20 d:b t:20 d:d t:20",
);
assert_eq!(
"t:21ms
out:↓Z
t:1ms
out:↓A
t:39ms
out:↓B
t:1ms
out:↓D",
result
);
}
#[test]
fn sim_timeout_hold_key() {
let result = simulate(SIMPLE_NONOVERLAPPING_CHORD_CFG, "d:z t:201 d:b t:200");
assert_eq!(
"t:201ms
out:↓Z
t:1ms
out:↓B",
result
);
}
#[test]
fn sim_chord_basic_repeated_first_release() {
let result = simulate(
SIMPLE_NONOVERLAPPING_CHORD_CFG,
"d:z t:50 d:b t:50 u:z t:50 u:b t:50 \
d:z t:50 d:b t:50 u:z t:50 u:b t:50 ",
);
assert_eq!(
"t:50ms\nout:↓D\nt:52ms\nout:↑D\nt:148ms\nout:↓D\nt:52ms\nout:↑D",
result
);
}
static SIMPLE_OVERLAPPING_CHORD_CFG: &str = "\
(defcfg process-unmapped-keys yes concurrent-tap-hold yes
chords-v2-min-idle-experimental 5)
(defsrc)
(deflayer base)
(defchordsv2-experimental
(a b) c 200 all-released ()
(a b z) d 250 first-release ()
(a b z y) e 400 first-release ()
)";
#[test]
fn sim_chord_overlapping_timeout() {
let result = simulate(SIMPLE_OVERLAPPING_CHORD_CFG, "d:a d:b t:201 d:z t:500").to_ascii();
assert_eq!("t:400ms dn:D", result);
}
#[test]
fn sim_chord_overlapping_timeout_v2() {
let result = simulate(
"
(defcfg process-unmapped-keys yes
concurrent-tap-hold yes)
(defsrc e f j)
(deflayermap (layer1))
(defchordsv2
(e f) lctl 5 all-released ()
(f j) ret 10 first-release ()
)
",
"d:f t:7 d:j t:100",
)
.to_ascii();
assert_eq!("t:7ms dn:Enter", result);
}
#[test]
fn sim_chord_overlapping_release() {
let result = simulate(
SIMPLE_OVERLAPPING_CHORD_CFG,
"d:a d:b t:100 u:a d:z t:300 u:b t:300",
)
.to_ascii();
assert_eq!("t:100ms dn:C t:401ms dn:Z t:3ms up:C", result);
}
#[test]
fn sim_presses_for_old_chord_repress_into_new_chord() {
let result = simulate(
SIMPLE_OVERLAPPING_CHORD_CFG,
"d:a d:b t:50 u:a t:50 d:z t:50 u:b t:50 d:a d:b t:50 u:a t:50",
)
.to_ascii();
assert_eq!("t:50ms dn:C t:200ms dn:D t:11ms up:C t:1ms up:D", result);
}
#[test]
fn sim_chord_activate_largest_overlapping() {
let result = simulate(
SIMPLE_OVERLAPPING_CHORD_CFG,
"d:a t:50 d:b t:50 d:z t:50 d:y t:50 u:b t:50",
);
assert_eq!("t:150ms\nout:↓E\nt:52ms\nout:↑E", result);
}
static SIMPLE_DISABLED_LAYER_CHORD_CFG: &str = "\
(defcfg process-unmapped-keys yes concurrent-tap-hold yes)
(defsrc)
(deflayermap (1) 2 (layer-switch 2)
3 (layer-switch 3))
(deflayermap (2) 3 (layer-while-held 3)
1 (layer-while-held 1))
(deflayermap (3) 2 (layer-while-held 2)
1 (layer-while-held 1))
(defchordsv2
(a b) x 200 all-released (1)
(c d) y 200 all-released (2)
(e f) z 200 all-released (3)
)";
#[test]
fn sim_chord_layer_1_switch_disabled() {
let result = simulate(
SIMPLE_DISABLED_LAYER_CHORD_CFG,
"d:a t:50 d:b t:50 d:c t:50 d:d t:50 d:e t:50 d:f t:50",
);
assert_eq!(
"t:1ms\nout:↓A\nt:50ms\nout:↓B\nt:99ms\nout:↓Y\nt:100ms\nout:↓Z",
result
);
}
#[test]
fn sim_chord_layer_2_switch_disabled() {
let result = simulate(
SIMPLE_DISABLED_LAYER_CHORD_CFG,
"d:2 t:50 d:a t:50 d:b t:50 d:c t:50 d:d t:50 d:e t:50 d:f t:50",
);
assert_eq!(
"t:100ms\nout:↓X\nt:51ms\nout:↓C\nt:50ms\nout:↓D\nt:99ms\nout:↓Z",
result
);
}
#[test]
fn sim_chord_layer_3_switch_disabled() {
let result = simulate(
SIMPLE_DISABLED_LAYER_CHORD_CFG,
"d:3 t:50 d:a t:50 d:b t:50 d:c t:50 d:d t:50 d:e t:50 d:f t:50",
);
assert_eq!(
"t:100ms\nout:↓X\nt:100ms\nout:↓Y\nt:51ms\nout:↓E\nt:50ms\nout:↓F",
result
);
}
#[test]
fn sim_chord_layer_1_held_disabled() {
let result = simulate(
SIMPLE_DISABLED_LAYER_CHORD_CFG,
"d:3 t:50 d:1 t:50 d:a t:50 d:b t:50 d:c t:50 d:d t:50 d:e t:50 d:f t:50",
);
assert_eq!(
"t:101ms\nout:↓A\nt:50ms\nout:↓B\nt:99ms\nout:↓Y\nt:100ms\nout:↓Z",
result
);
}
#[test]
fn sim_chord_layer_2_held_disabled() {
let result = simulate(
SIMPLE_DISABLED_LAYER_CHORD_CFG,
"d:3 t:50 d:2 t:50 d:a t:50 d:b t:50 d:c t:50 d:d t:50 d:e t:50 d:f t:50",
);
assert_eq!(
"t:150ms\nout:↓X\nt:51ms\nout:↓C\nt:50ms\nout:↓D\nt:99ms\nout:↓Z",
result
);
}
#[test]
fn sim_chord_layer_3_held_disabled() {
let result = simulate(
SIMPLE_DISABLED_LAYER_CHORD_CFG,
"d:2 t:50 d:3 t:50 d:a t:50 d:b t:50 d:c t:50 d:d t:50 d:e t:50 d:f t:50",
);
assert_eq!(
"t:150ms\nout:↓X\nt:100ms\nout:↓Y\nt:51ms\nout:↓E\nt:50ms\nout:↓F",
result
);
}
#[test]
fn sim_chord_layer_3_repeat() {
let result = simulate(
SIMPLE_DISABLED_LAYER_CHORD_CFG,
"d:3 t:50 d:a t:50 d:b t:50 r:b t:50 r:b t:50\n\
d:d t:50 d:c t:50 r:c t:50 r:d t:50",
);
assert_eq!(
"t:100ms\nout:↓X\nt:50ms\nout:↓X\nt:50ms\nout:↓X\n\
t:100ms\nout:↓Y\nt:50ms\nout:↓Y\nt:50ms\nout:↓Y",
result
);
}
static CHORD_INTO_TAP_HOLD_CFG: &str = "\
(defcfg process-unmapped-keys yes concurrent-tap-hold yes)
(defsrc)
(deflayer base)
(defchordsv2
(a b) (tap-hold 200 200 x y) 200 all-released ()
)";
#[test]
fn sim_chord_into_tap_hold() {
let result = simulate(
CHORD_INTO_TAP_HOLD_CFG,
"d:a t:50 d:b t:150 u:a u:b t:5 \
d:a t:50 d:b t:149 u:a u:b t:1000",
);
assert_eq!(
"t:199ms\nout:↓Y\nt:10ms\nout:↑Y\nt:195ms\nout:↓X\nt:10ms\nout:↑X",
result
);
}
static CHORD_WITH_PENDING_UNDERLYING_TAP_HOLD: &str = "\
(defcfg process-unmapped-keys yes concurrent-tap-hold yes)
(defsrc)
(deflayermap (base) a (tap-hold 200 200 a b))
(defchordsv2
(b c) d 100 all-released ()
)";
#[test]
fn sim_chord_pending_tap_hold() {
let result = simulate(
CHORD_WITH_PENDING_UNDERLYING_TAP_HOLD,
"d:a t:10 d:b t:10 d:c t:300",
);
// unlike other actions, chordv2 activations
// are intentionally not delayed by waiting actions like tap-hold.
assert_eq!("t:20ms\nout:↓D\nt:179ms\nout:↓B", result);
}
static CHORD_WITH_TRANSPARENCY: &str = "\
(defcfg process-unmapped-keys yes concurrent-tap-hold yes)
(defsrc)
(deflayer base)
(defchordsv2
(a b) _ 100 all-released ()
)";
#[test]
#[should_panic]
fn sim_denies_transparent() {
simulate(CHORD_WITH_TRANSPARENCY, "");
}
#[test]
fn sim_chord_eager_tapholdpress_activation() {
let result = simulate(
"
(defcfg concurrent-tap-hold yes)
(defsrc caps j k bspc)
(deflayer one (tap-hold-press 0 200 esc lctl) j k bspc)
(defvirtualkeys bspc bspc)
(defchordsv2
(j k) (multi
(on-press press-vkey bspc)
(on-release release-vkey bspc)
(fork XX bspc (nop9))) 75 first-release ()
)
",
"d:caps t:10 d:j d:k t:100 r:bspc t:10 r:bspc t:10 u:j u:k t:100 u:caps t:1000",
)
.to_ascii();
assert_eq!(
"t:12ms dn:LCtrl t:7ms dn:BSpace t:91ms dn:BSpace t:10ms dn:BSpace t:15ms up:BSpace t:95ms up:LCtrl",
result
);
}
#[test]
fn sim_chord_eager_tapholdrelease_activation() {
let result = simulate(
"
(defcfg concurrent-tap-hold yes)
(defsrc)
(deflayermap (base)
caps (tap-hold-release 0 200 esc lctl))
;; defines a vkey named v-$key, example v-bspc
;; and an alias @v-bspc that press and and releases the v-key
;; within on-press and on-release respectively.
(deftemplate v- (key)
(defvirtualkeys (concat v- $key) $key)
(defalias (concat v- $key)
(multi (on-press press-vkey (concat v- $key)) (on-release release-vkey (concat v- $key))))
)
(t! v- bspc)
(t! v- del)
(defchordsv2
(j k) @v-bspc 75 first-release ()
(s d) @v-del 75 first-release ()
)
",
"d:caps t:10 d:j d:k t:10 u:j u:k t:100 u:caps t:1000",
)
.to_ascii();
assert_eq!(
"t:20ms dn:LCtrl t:7ms dn:BSpace t:6ms up:BSpace t:87ms up:LCtrl",
result
);
}
#[test]
fn sim_chord_release_nonchord_key_has_correct_order() {
let result = simulate(
"
(defcfg concurrent-tap-hold yes)
(defsrc ralt j k)
(deflayer base _ _ _)
(defchordsv2
(j k) l 75 first-release ()
)
",
"d:ralt t:1000 d:j t:1 u:ralt t:100 u:j t:100",
)
.to_ascii();
assert_eq!(
"t:1ms dn:RAlt t:1075ms dn:J t:1ms up:RAlt t:24ms up:J",
result
);
}
#[test]
fn sim_chord_simultaneous_macro() {
let result = simulate(
"
(defsrc a b o)
(deflayer default
(chord base a)
(chord base b)
(chord base o)
)
(defchords base 500
(a) (macro a z)
(b) (macro b)
(o) o
(a o) o
)
",
"d:a t:10 d:b t:500",
)
.to_ascii();
assert_eq!(
"t:502ms dn:A dn:B t:1ms up:A up:B t:1ms dn:Z t:1ms up:Z",
result
);
}
#[test]
#[should_panic]
fn sim_chord_error_on_duplicate_keyset() {
simulate(
"
(defcfg concurrent-tap-hold yes)
(defsrc)
(deflayer base)
(defchordsv2
(1 2) (one-shot 2000 lsft) 20 all-released ()
(2 1) (one-shot 2000 lctl) 20 all-released ()
)
",
"",
);
}
#[test]
fn sim_chord_oneshot() {
let result = simulate(
"
(defcfg concurrent-tap-hold yes)
(defsrc)(deflayer base)
(defchordsv2
(a b) (one-shot 2500 rsft) 35 first-release ()
)
",
"d:a t:10 d:b t:10 u:a t:10 u:b t:3000 \
d:a t:10 d:b t:10 u:a t:10 u:b t:500 d:c u:c t:3000",
)
.to_ascii();
assert_eq!(
"t:10ms dn:RShift t:2500ms up:RShift t:530ms \
dn:RShift t:521ms dn:C t:5ms up:RShift up:C",
result
);
}
#[test]
fn sim_chord_timeout_events() {
let result = simulate(
"
(defcfg
concurrent-tap-hold yes
process-unmapped-keys yes
)
(defvirtualkeys
v-macro-word-end (macro spc)
)
(defsrc a b c)
(defchordsv2-experimental
(a b c) (macro x y z (on-press tap-vkey v-macro-word-end)) 200 all-released ()
(a b) (macro x y (on-press tap-vkey v-macro-word-end)) 200 all-released ()
)
(deflayer base a b c)
",
"d:a t:10 d:b t:3000 u:a u:b t:100",
)
.to_ascii();
assert_eq!(
"t:201ms dn:X t:1ms up:X t:1ms dn:Y t:1ms up:Y t:4ms dn:Space t:1ms up:Space",
result
);
}
#[test]
fn sim_chordsv2_quick_release() {
let result = simulate(
"
(defsrc)
(defcfg concurrent-tap-hold yes)
(deflayermap layer1)
(defchordsv2
(k l) rsft 75 all-released ()
)
",
"
d:k d:l t:10
d:t t:1
u:k t:1 u:l t:10
u:t t:10
",
)
.to_ascii();
assert_eq!("dn:RShift t:11ms dn:T t:4ms up:RShift t:7ms up:T", result);
}
#[test]
fn sim_chordsv1_key_history_updates_once() {
let result = simulate(
"
(defsrc)
(defchords iobspc 50
(i o) bspc
)
(deflayermap (base)
i (chord iobspc i)
o (chord iobspc o)
t (switch
((key-history bspc 3)) a fallthrough
((key-history bspc 2)) b fallthrough
((key-history bspc 1)) c fallthrough
)
)
",
"
d:i d:o t:10
u:i u:o t:10
d:t u:t t:10
d:t u:t t:10
",
)
.to_ascii();
// For first press of `t`, only position 1 should be filled.
// The key history should unpause, so on pressing `t` again,
// position 2 is now where bspc is because C was sent.
assert_eq!(
"t:1ms dn:BSpace t:10ms up:BSpace t:9ms dn:C t:1ms up:C t:9ms dn:B t:1ms up:B",
result
);
}