-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy path_input_unicode_test.v
More file actions
524 lines (476 loc) · 11.8 KB
/
_input_unicode_test.v
File metadata and controls
524 lines (476 loc) · 11.8 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
module gui
// Tests for multi-byte text handling: cursor movement, insert, delete,
// copy, word boundaries, IME commit, and edge cases.
fn make_text_cursor_shape(text string) Shape {
return Shape{
shape_type: .text
tc: &ShapeTextConfig{
text: text
}
}
}
// ------------------------------------
// ## A. cursor_right / cursor_end
// ------------------------------------
fn test_cursor_right_clamp_3byte() {
// '€' is 3 bytes, 1 rune
shape := make_text_cursor_shape('€')
assert cursor_right(shape, 0) == 1
assert cursor_right(shape, 1) == 1 // clamped at rune count
}
fn test_cursor_right_clamp_4byte() {
// '𝐀' (U+1D400) is 4 bytes, 1 rune
shape := make_text_cursor_shape('𝐀')
assert cursor_right(shape, 0) == 1
assert cursor_right(shape, 1) == 1
}
fn test_cursor_right_cjk_string() {
// '日本語' = 3 runes, 9 bytes
shape := make_text_cursor_shape('日本語')
assert cursor_right(shape, 2) == 3
assert cursor_right(shape, 3) == 3
}
fn test_cursor_end_3byte() {
shape := make_text_cursor_shape('€€€')
assert cursor_end(shape) == 3
}
fn test_cursor_end_4byte() {
shape := make_text_cursor_shape('𝐀𝐁𝐂')
assert cursor_end(shape) == 3
}
fn test_cursor_end_emoji_only() {
// '😀😀' = 2 runes, 8 bytes
shape := make_text_cursor_shape('😀😀')
assert cursor_end(shape) == 2
}
fn test_cursor_end_empty() {
shape := make_text_cursor_shape('')
assert cursor_end(shape) == 0
}
fn test_cursor_end_combining_char() {
// 'é' as e + combining acute (U+0301) = 2 runes, 3 bytes
shape := make_text_cursor_shape('e\u0301')
assert cursor_end(shape) == 2
}
// ------------------------------------
// ## B. cursor_left sanity
// ------------------------------------
fn test_cursor_left_at_zero() {
assert cursor_left(0) == 0
}
fn test_cursor_left_from_one() {
assert cursor_left(1) == 0
}
// ------------------------------------
// ## C. cursor_end_of_line no-layout
// ------------------------------------
fn test_cursor_end_of_line_no_layout_3byte() {
// No vglyph_layout → fallback path
shape := make_text_cursor_shape('日本語')
assert cursor_end_of_line(shape, 0) == 3
}
fn test_cursor_end_of_line_no_layout_4byte() {
shape := make_text_cursor_shape('𝐀𝐁')
assert cursor_end_of_line(shape, 0) == 2
}
// ------------------------------------
// ## D. Insert with multi-byte text
// ------------------------------------
fn test_insert_emoji_at_start() {
id_focus := u32(10001)
mut w := Window{}
mut imap := state_map[u32, InputState](mut w, ns_input, cap_many)
imap.set(id_focus, InputState{ cursor_pos: 0 })
cfg := InputCfg{
id_focus: id_focus
text: 'abc'
}
got := cfg.insert('😀', mut w) or {
assert false
return
}
assert got == '😀abc'
}
fn test_insert_emoji_at_middle() {
id_focus := u32(10002)
mut w := Window{}
mut imap := state_map[u32, InputState](mut w, ns_input, cap_many)
imap.set(id_focus, InputState{ cursor_pos: 1 })
cfg := InputCfg{
id_focus: id_focus
text: 'ab'
}
got := cfg.insert('😀', mut w) or {
assert false
return
}
assert got == 'a😀b'
}
fn test_insert_cjk_string() {
id_focus := u32(10003)
mut w := Window{}
mut imap := state_map[u32, InputState](mut w, ns_input, cap_many)
imap.set(id_focus, InputState{ cursor_pos: 0 })
cfg := InputCfg{
id_focus: id_focus
text: ''
}
got := cfg.insert('日本語', mut w) or {
assert false
return
}
assert got == '日本語'
state := imap.get(id_focus) or { InputState{} }
assert state.cursor_pos == 3
}
fn test_insert_combining_char() {
// Insert combining acute after 'e'
id_focus := u32(10004)
mut w := Window{}
mut imap := state_map[u32, InputState](mut w, ns_input, cap_many)
imap.set(id_focus, InputState{ cursor_pos: 1 })
cfg := InputCfg{
id_focus: id_focus
text: 'e'
}
got := cfg.insert('\u0301', mut w) or {
assert false
return
}
assert got == 'e\u0301'
}
fn test_insert_ascii_into_multibyte() {
id_focus := u32(10005)
mut w := Window{}
mut imap := state_map[u32, InputState](mut w, ns_input, cap_many)
// Cursor at rune pos 1 in '日本' (between 日 and 本)
imap.set(id_focus, InputState{ cursor_pos: 1 })
cfg := InputCfg{
id_focus: id_focus
text: '日本'
}
got := cfg.insert('x', mut w) or {
assert false
return
}
assert got == '日x本'
}
// ------------------------------------
// ## E. Delete with multi-byte text
// ------------------------------------
fn test_backspace_after_emoji() {
id_focus := u32(10010)
mut w := Window{}
mut imap := state_map[u32, InputState](mut w, ns_input, cap_many)
// Cursor after the emoji (rune pos 1)
imap.set(id_focus, InputState{ cursor_pos: 1 })
cfg := InputCfg{
id_focus: id_focus
text: '😀x'
}
got := cfg.delete(mut w, false) or {
assert false
return
}
assert got == 'x'
}
fn test_backspace_after_3byte() {
id_focus := u32(10011)
mut w := Window{}
mut imap := state_map[u32, InputState](mut w, ns_input, cap_many)
imap.set(id_focus, InputState{ cursor_pos: 1 })
cfg := InputCfg{
id_focus: id_focus
text: '€x'
}
got := cfg.delete(mut w, false) or {
assert false
return
}
assert got == 'x'
}
fn test_forward_delete_on_emoji() {
id_focus := u32(10012)
mut w := Window{}
mut imap := state_map[u32, InputState](mut w, ns_input, cap_many)
// Cursor before the emoji (rune pos 0), forward delete
imap.set(id_focus, InputState{ cursor_pos: 0 })
cfg := InputCfg{
id_focus: id_focus
text: '😀x'
}
got := cfg.delete(mut w, true) or {
assert false
return
}
assert got == 'x'
}
fn test_backspace_combining_char() {
id_focus := u32(10013)
mut w := Window{}
mut imap := state_map[u32, InputState](mut w, ns_input, cap_many)
// 'e' + combining acute = 2 runes; cursor at 2, backspace removes
// the combining char
imap.set(id_focus, InputState{ cursor_pos: 2 })
cfg := InputCfg{
id_focus: id_focus
text: 'e\u0301'
}
got := cfg.delete(mut w, false) or {
assert false
return
}
assert got == 'e'
}
// ------------------------------------
// ## F. Selection + copy
// ------------------------------------
fn test_copy_single_multibyte_char() {
id_focus := u32(10020)
mut w := Window{}
mut imap := state_map[u32, InputState](mut w, ns_input, cap_many)
imap.set(id_focus, InputState{
select_beg: 0
select_end: 1
})
cfg := InputCfg{
id_focus: id_focus
text: '€ab'
}
got := cfg.copy(&w) or {
assert false
return
}
assert got == '€'
}
fn test_copy_span_across_multibyte() {
id_focus := u32(10021)
mut w := Window{}
mut imap := state_map[u32, InputState](mut w, ns_input, cap_many)
// Select runes 1..3 in 'a€bé' → '€b'
imap.set(id_focus, InputState{
select_beg: 1
select_end: 3
})
cfg := InputCfg{
id_focus: id_focus
text: 'a€b\u00e9'
}
got := cfg.copy(&w) or {
assert false
return
}
assert got == '€b'
}
fn test_copy_emoji() {
id_focus := u32(10022)
mut w := Window{}
mut imap := state_map[u32, InputState](mut w, ns_input, cap_many)
imap.set(id_focus, InputState{
select_beg: 1
select_end: 2
})
cfg := InputCfg{
id_focus: id_focus
text: 'a😀b'
}
got := cfg.copy(&w) or {
assert false
return
}
assert got == '😀'
}
// ------------------------------------
// ## G. Insert replacing selection
// ------------------------------------
fn test_replace_multibyte_selection_with_ascii() {
id_focus := u32(10030)
mut w := Window{}
mut imap := state_map[u32, InputState](mut w, ns_input, cap_many)
// Select the emoji at rune 1..2 in 'a😀b'
imap.set(id_focus, InputState{
cursor_pos: 1
select_beg: 1
select_end: 2
})
cfg := InputCfg{
id_focus: id_focus
text: 'a😀b'
}
got := cfg.insert('x', mut w) or {
assert false
return
}
assert got == 'axb'
}
fn test_replace_ascii_selection_with_emoji() {
id_focus := u32(10031)
mut w := Window{}
mut imap := state_map[u32, InputState](mut w, ns_input, cap_many)
// Select 'bc' (runes 1..3) in 'abcd'
imap.set(id_focus, InputState{
cursor_pos: 1
select_beg: 1
select_end: 3
})
cfg := InputCfg{
id_focus: id_focus
text: 'abcd'
}
got := cfg.insert('😀', mut w) or {
assert false
return
}
assert got == 'a😀d'
}
// ------------------------------------
// ## H. Word boundary with multi-byte
// ------------------------------------
fn test_cursor_end_of_word_cjk_mixed() {
// 'hello 日本語 world'
// Rune positions: h=0 e=1 l=2 l=3 o=4 ' '=5 日=6 本=7 語=8
// ' '=9 w=10 o=11 r=12 l=13 d=14
shape := make_text_cursor_shape('hello 日本語 world')
// From rune 5 (space), skip space then skip non-spaces → end of '日本語'
assert cursor_end_of_word(shape, 5) == 9
}
fn test_cursor_start_of_word_cjk_mixed() {
shape := make_text_cursor_shape('hello 日本語 world')
// From rune 9 (space after 語), skip space back, then skip non-spaces
// back → start of '日本語' at rune 6
assert cursor_start_of_word(shape, 9) == 6
}
// ------------------------------------
// ## I. IME commit simulation
// ------------------------------------
fn test_ime_commit_cjk_into_empty() {
id_focus := u32(10040)
mut w := Window{}
mut imap := state_map[u32, InputState](mut w, ns_input, cap_many)
imap.set(id_focus, InputState{ cursor_pos: 0 })
cfg := InputCfg{
id_focus: id_focus
text: ''
}
got := cfg.insert('中文', mut w) or {
assert false
return
}
assert got == '中文'
state := imap.get(id_focus) or { InputState{} }
assert state.cursor_pos == 2
}
fn test_ime_commit_cjk_at_cursor() {
id_focus := u32(10041)
mut w := Window{}
mut imap := state_map[u32, InputState](mut w, ns_input, cap_many)
imap.set(id_focus, InputState{ cursor_pos: 2 })
cfg := InputCfg{
id_focus: id_focus
text: 'abcd'
}
got := cfg.insert('漢字', mut w) or {
assert false
return
}
assert got == 'ab漢字cd'
state := imap.get(id_focus) or { InputState{} }
assert state.cursor_pos == 4
}
fn test_ime_commit_replacing_selection() {
id_focus := u32(10042)
mut w := Window{}
mut imap := state_map[u32, InputState](mut w, ns_input, cap_many)
imap.set(id_focus, InputState{
cursor_pos: 1
select_beg: 1
select_end: 3
})
cfg := InputCfg{
id_focus: id_focus
text: 'abcd'
}
got := cfg.insert('日', mut w) or {
assert false
return
}
assert got == 'a日d'
state := imap.get(id_focus) or { InputState{} }
assert state.cursor_pos == 2
}
// ------------------------------------
// ## J. Edge cases
// ------------------------------------
fn test_cursor_right_empty_text() {
shape := make_text_cursor_shape('')
assert cursor_right(shape, 0) == 0
}
fn test_cursor_end_single_4byte() {
shape := make_text_cursor_shape('😀')
assert cursor_end(shape) == 1
}
fn test_insert_empty_string() {
id_focus := u32(10050)
mut w := Window{}
mut imap := state_map[u32, InputState](mut w, ns_input, cap_many)
imap.set(id_focus, InputState{ cursor_pos: 1 })
cfg := InputCfg{
id_focus: id_focus
text: '日本'
}
got := cfg.insert('', mut w) or {
assert false
return
}
assert got == '日本'
}
fn test_delete_empty_text() {
id_focus := u32(10051)
mut w := Window{}
mut imap := state_map[u32, InputState](mut w, ns_input, cap_many)
imap.set(id_focus, InputState{ cursor_pos: 0 })
cfg := InputCfg{
id_focus: id_focus
text: ''
}
got := cfg.delete(mut w, false) or {
assert false
return
}
assert got == ''
}
fn test_mixed_script_sequential_insert() {
id_focus := u32(10052)
mut w := Window{}
mut imap := state_map[u32, InputState](mut w, ns_input, cap_many)
imap.set(id_focus, InputState{ cursor_pos: 0 })
cfg1 := InputCfg{
id_focus: id_focus
text: ''
}
text1 := cfg1.insert('abc', mut w) or {
assert false
return
}
assert text1 == 'abc'
// Cursor now at 3
cfg2 := InputCfg{
id_focus: id_focus
text: text1
}
text2 := cfg2.insert('日本', mut w) or {
assert false
return
}
assert text2 == 'abc日本'
// Cursor now at 5
cfg3 := InputCfg{
id_focus: id_focus
text: text2
}
text3 := cfg3.insert('😀', mut w) or {
assert false
return
}
assert text3 == 'abc日本😀'
}