forked from livecode/livecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchar.lcb
More file actions
495 lines (397 loc) · 15.6 KB
/
char.lcb
File metadata and controls
495 lines (397 loc) · 15.6 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
/*
Copyright (C) 2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode 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 LiveCode. If not see <http://www.gnu.org/licenses/>. */
module com.livecode.char.tests
use com.livecode.char
use com.livecode.__INTERNAL._testlib
public handler TestCount()
test "count" when the number of chars in "xxx" is 3
end handler
----------------------------------------------------------------
handler TestPutChar_Zero()
variable tString
put "xxx" into tString
put "." into char 0 of tString
end handler
handler TestPutChar_Before()
variable tString
put "xxx" into tString
put "." into char -4 of tString
end handler
handler TestPutChar_After()
variable tString
put "xxx" into tString
put "." into char 4 of tString
end handler
public handler TestPutChar()
variable tString
put "xxx" into tString
put "." into char 2 of tString
test "put char (+ve)" when tString is "x.x"
put "xxx" into tString
put "." into char -2 of tString
test "put char (-ve)" when tString is "x.x"
MCUnitTestHandlerThrows(TestPutChar_Zero, "put char (0)")
MCUnitTestHandlerThrows(TestPutChar_Before, "put char (before)")
MCUnitTestHandlerThrows(TestPutChar_After, "put char (after)")
end handler
handler TestGetChar_Zero()
variable tString
put "xxx" into tString
get char 0 of tString
end handler
handler TestGetChar_Before()
return char -4 of "xxx"
end handler
handler TestGetChar_After()
return char 4 of "xxx"
end handler
public handler TestGetChar()
variable tString
put char 2 of "x.x" into tString
test "get char (+ve)" when tString is "."
put char -2 of "x.x" into tString
test "get char (-ve)" when tString is "."
MCUnitTestHandlerThrows(TestGetChar_Zero, "get char (0)")
MCUnitTestHandlerThrows(TestGetChar_Before, "get char (before)")
MCUnitTestHandlerThrows(TestGetChar_After, "get char (after)")
end handler
----------------------------------------------------------------
public handler TestRange()
variable tString
-- Put into range
put "xxxx" into tString
put "." into char 2 to 2 of tString
test "range (put, +ve, equal)" when tString is "x.xx"
put "." into char 2 to 3 of tString
test "range (put, +ve)" when tString is "x.x"
put "xxxx" into tString
put "." into char -2 to -2 of tString
test "range (put, -ve, equal)" when tString is "xx.x"
put "." into char -3 to -2 of tString
test "range (put, -ve)" when tString is "x.x"
-- Get from range
put "xx.x" into tString
test "range (get, +ve, equal)" when char 3 to 3 of tString is "."
test "range (get, +ve)" when char 2 to 3 of tString is "x."
test "range (get, +ve, equal)" when char -2 to -2 of tString is "."
test "range (get, +ve)" when char -3 to -2 of tString is "x."
end handler
----------------------------------------------------------------
handler TestFirst_GetEmpty()
return the first char of ""
end handler
handler TestFirst_PutEmpty()
variable tString
put "" into tString
put "xxx" into the first char of tString
end handler
public handler TestFirst()
variable tString
put "xxx" into tString
put "." into the first char of tString
test "first (put single)" when tString is ".xx"
put "yz" into the first char of tString
test "first (put multiple)" when tString is "yzxx"
test "first (get)" when the first char of tString is "y"
put "xyz" into tString
put tString into the first char of tString
test "first (self)" when tString is "xyzyz"
MCUnitTestHandlerThrows(TestFirst_GetEmpty, "first (get empty)")
MCUnitTestHandlerThrows(TestFirst_PutEmpty, "first (put empty)")
end handler
handler TestLast_GetEmpty()
return the last char of ""
end handler
handler TestLast_PutEmpty()
variable tString
put "" into tString
put "xxx" into the last char of tString
end handler
public handler TestLast()
variable tString
put "xxx" into tString
put "." into the last char of tString
test "last (put)" when tString is "xx."
put "yz" into the last char of tString
test "last (put multiple)" when tString is "xxyz"
test "last (get)" when the last char of tString is "z"
put "xyz" into tString
put tString into the last char of tString
test "first (self)" when tString is "xyxyz"
MCUnitTestHandlerThrows(TestLast_GetEmpty, "last (get empty)")
MCUnitTestHandlerThrows(TestLast_PutEmpty, "last (put empty)")
end handler
----------------------------------------------------------------
handler TestDeleteSingle_Zero()
variable tString
put "x" into tString
delete char 0 of tString
end handler
handler TestDeleteSingle_Before()
variable tString
put "x" into tString
delete char -2 of tString
end handler
handler TestDeleteSingle_After()
variable tString
put "x" into tString
delete char 2 of tString
end handler
handler TestDeleteSingle_FirstEmpty()
variable tString
put "" into tString
delete the first char of tString
end handler
handler TestDeleteSingle_LastEmpty()
variable tString
put "" into tString
delete the last char of tString
end handler
public handler TestDeleteSingle()
variable tString
put "xyz" into tString
delete char 2 of tString
test "delete single (+ve)" when tString is "xz"
put "xyz" into tString
delete char -2 of tString
test "delete single (-ve)" when tString is "xz"
MCUnitTestHandlerThrows(TestDeleteSingle_Zero, "delete (single, 0)")
MCUnitTestHandlerThrows(TestDeleteSingle_Before, "delete (single, before)")
MCUnitTestHandlerThrows(TestDeleteSingle_After, "delete (single, after)")
put "xyz" into tString
delete the first char of tString
test "delete first" when tString is "yz"
put "xyz" into tString
delete the last char of tString
test "delete last" when tString is "xy"
MCUnitTestHandlerThrows(TestDeleteSingle_FirstEmpty, "delete first (empty)")
MCUnitTestHandlerThrows(TestDeleteSingle_LastEmpty, "delete last (empty)")
end handler
handler TestDeleteRange_Zero()
variable tString
put "xxx" into tString
delete char 0 to 0 of tString
end handler
handler TestDeleteRange_Reverse()
variable tString
put "xxx" into tString
delete char 3 to 2 of tString
end handler
handler TestDeleteRange_After()
variable tString
put "xyz" into tString
delete char 3 to 5 of tString
end handler
handler TestDeleteRange_Before()
variable tString
put "xyz" into tString
delete char -5 to -3 of tString
test diagnostic tString
end handler
public handler TestDeleteRange()
variable tString
put "xyz" into tString
delete char 2 to 2 of tString
test "delete range (+ve, equal)" when tString is "xz"
put "xyz" into tString
delete char -2 to -2 of tString
test "delete range (-ve, equal)" when tString is "xz"
put "wxyz" into tString
delete char 2 to 3 of tString
test "delete range (+ve)" when tString is "wz"
put "wxyz" into tString
delete char -3 to -2 of tString
test "delete range (+ve)" when tString is "wz"
MCUnitTestHandlerThrows(TestDeleteRange_Zero, "delete range (0)")
MCUnitTestHandlerThrows(TestDeleteRange_Before, "delete range (before)")
MCUnitTestHandlerThrows(TestDeleteRange_After, "delete range (after)")
MCUnitTestHandlerThrows(TestDeleteRange_Reverse, "delete range (reverse)")
end handler
----------------------------------------------------------------
public handler TestContainsChar()
test "char is in" when "y" is in "xyz"
test "char isn't in" when not "w" is in "xyz"
test "multi-codeunit char isn't in" when not "\u{1f499}" is in "\r\n"
test diagnostic "TODO 'y' is not in 'xyz'"
end handler
public handler TestContainsString()
test "contains" when "wxyz" contains "xy"
test "contains (missing)" when not "wxyz" contains ".xy"
end handler
public handler TestOffset()
-- Only test single-character needles for now
test "offset (single)" when the offset of "x" in ".xx." is 2
test "first offset (single)" when the first offset of "x" in ".xx." is 2
test "last offset (single)" when the last offset of "x" in ".xx." is 3
test "offset (missing)" when the offset of "xx" in ".x.x." is 0
test "offset (single, same)" when the offset of "x" in "x" is 1
test "first offset (single, same)" when the first offset of "x" in "x" is 1
-- bug 14677
test "last offset (single, same)" when the last offset of "x" in "x" is 1
test "offset (empty)" when the offset of "" in "x" is 0
test "first offset (empty)" when the first offset of "" in "x" is 0
test "last offset (empty)" when the last offset of "" in "x" is 0
test "offset (empty, empty)" when the offset of "" in "" is 0
-- For chars, "index" is synonymous with "offset"
test "index" when the index of "x" in ".xx." is 2
end handler
public handler TestOffsetUnicode()
test "offset (native, BMP)" when the index of " " in "\u{2192} right" is 2
test "offset (BMP, BMP)" when the index of "\u{2192}" in "\u{2192} right" is 1
end handler
public handler TestOffsetAfter()
-- Only test single-character needles for now
test "offset after (single, +ve)" when the offset of "x" after 1 in "x.xx." is 3
test "first offset after (single, +ve)" when the first offset of "x" after 1 in "x.xx." is 3
test "last offset after (single, +ve)" when the last offset of "x" after 1 in "x.xx." is 4
test "offset after (single, -ve)" when the offset of "x" after -5 in "x.xx." is 3
test "first offset after (single, -ve)" when the first offset of "x" after -5 in "x.xx." is 3
test "last offset after (single, -ve)" when the last offset of "x" after -5 in "x.xx." is 4
test "offset after (single, missing, +ve)" when the offset of "x" after 1 in "x.." is 0
test "first offset after (single, missing, +ve)" when the first offset of "x" after 1 in "x.." is 0
test "last offset after (single, missing, +ve)" when the last offset of "x" after 1 in "x.." is 0
test "offset after (single, missing, -ve)" when the offset of "x" after -3 in "x.." is 0
test "first offset after (single, missing, -ve)" when the first offset of "x" after -3 in "x.." is 0
test "last offset after (single, missing, -ve)" when the last offset of "x" after -3 in "x.." is 0
MCUnitTestHandlerThrows(TestOffsetAfter_SingleInvalidPositive, "offset after (single, invalid +ve)")
MCUnitTestHandlerThrows(TestOffsetAfter_SingleInvalidNegative, "offset after (single, invalid -ve)")
MCUnitTestHandlerThrows(TestFirstOffsetAfter_SingleInvalidPositive, "first offset after (single, invalid +ve)")
MCUnitTestHandlerThrows(TestFirstOffsetAfter_SingleInvalidNegative, "first offset after (single, invalid -ve)")
MCUnitTestHandlerThrows(TestLastOffsetAfter_SingleInvalidPositive, "last offset after (single, invalid +ve)")
MCUnitTestHandlerThrows(TestLastOffsetAfter_SingleInvalidNegative, "last offset after (single, invalid -ve)")
-- For chars, "index" is synonymous with "offset"
test "index after" when the index of "x" after 2 in ".xx." is 3
end handler
handler TestOffsetAfter_SingleInvalidPositive()
get the offset of "x" after 2 in "x"
end handler
handler TestOffsetAfter_SingleInvalidNegative()
get the offset of "x" after -3 in "x"
end handler
handler TestFirstOffsetAfter_SingleInvalidPositive()
get the first offset of "x" after 2 in "x"
end handler
handler TestFirstOffsetAfter_SingleInvalidNegative()
get the first offset of "x" after -3 in "x"
end handler
handler TestLastOffsetAfter_SingleInvalidPositive()
get the last offset of "x" after 2 in "x"
end handler
handler TestLastOffsetAfter_SingleInvalidNegative()
get the last offset of "x" after -3 in "x"
end handler
public handler TestOffsetAfterZero()
-- "offset of _ after 0 in _" should be equivalent to "offset of _ in _"
variable tNoAfter
put the offset of "x" in "x" into tNoAfter
test "offset after (single, 0)" when the offset of "x" after 0 in "x" is tNoAfter
put the first offset of "x" in "x" into tNoAfter
test "first offset after (single, 0)" when the first offset of "x" after 0 in "x" is tNoAfter
put the last offset of "x" in ".x." into tNoAfter
test "last offset after (single, 0)" when the last offset of "x" after 0 in ".x." is tNoAfter
-- bug 14677
put the last offset of "x" in "x" into tNoAfter
test "the last offset after (single, 0, same)" when the last offset of "x" after 0 in "x" is tNoAfter
end handler
public handler TestBeginsWith()
test "begins with" when "xx." begins with "xx"
test "begins with (missing)" when not ".xx" begins with "xx"
end handler
public handler TestEndsWith()
test "ends with" when ".xx" ends with "xx"
test "ends with (missing)" when not "xx." ends with "xx"
end handler
public handler TestNewline()
test "newline" when newline is "\n"
end handler
public handler TestRepeatChar()
variable tIter
variable tCount
put 0 into tCount
repeat for each char tIter in "xyz"
add 1 to tCount
test "repeatchar (iter)" when tIter is char tCount of "xyz"
end repeat
test "repeatchar (count)" when tCount is 3
put 0 into tCount
repeat for each char tIter in "a\u{1d11e}c"
add 1 to tCount
test "repeatchar SMP (iter)" when tIter is char tCount of "a\u{1d11e}c"
end repeat
test "repeatchar SMP (count)" when tCount is 3
put 0 into tCount
repeat for each char tIter in "av\u{fe0e}c"
add 1 to tCount
test "repeatchar VAR (iter)" when tIter is char tCount of "av\u{fe0e}c"
end repeat
test "repeatchar VAR (count)" when tCount is 3
end handler
handler TestReverse_Inplace(in pDesc as String, \
in pForward as String, in pReverse as String)
variable tReversed
put pForward into tReversed
reverse tReversed
test diagnostic tReversed
test "reverse in-place (" & pDesc & ")" when tReversed is pReverse
reverse tReversed
test diagnostic tReversed
test "rereverse in-place (" & pDesc & ")" when tReversed is pForward
end handler
public handler TestReverse()
TestReverse_Inplace("empty", "", "")
TestReverse_Inplace("native, odd", "abc", "cba")
TestReverse_Inplace("native, even", "abcd", "dcba")
TestReverse_Inplace("trivial, odd", \
"\u{039a}\u{03b1}\u{03bb}\u{03b7}\u{03bc}\u{03ad}\u{03c1}\u{03b1}!", \
"!\u{03b1}\u{03c1}\u{03ad}\u{03bc}\u{03b7}\u{03bb}\u{03b1}\u{039a}")
TestReverse_Inplace("trivial, even", \
"\u{039a}\u{03b1}\u{03bb}\u{03b7}\u{03bc}\u{03ad}\u{03c1}\u{03b1}", \
"\u{03b1}\u{03c1}\u{03ad}\u{03bc}\u{03b7}\u{03bb}\u{03b1}\u{039a}")
TestReverse_Inplace("BMP variation selector, 1", "a\u{fe0e}", "a\u{fe0e}")
TestReverse_Inplace("BMP variation selector, even", \
"\u{2b55}\u{fe0f}\u{25ef}", "\u{25ef}\u{2b55}\u{fe0f}")
TestReverse_Inplace("SMP, odd", "a\u{1d11e}c", "c\u{1d11e}a")
end handler
----------------------------------------------------------------
handler TestCharWithCode_Negative()
return the char with code -1
end handler
public handler TestCharWithCode()
variable tString
put the char with code 120 into tString
test "code->byte" when tString is "x"
put the char with code 8594 into tString
test "code->byte (BMP)" when tString is "\u{2192}"
put the char with code 119070 into tString
test "code->byte (SMP)" when tString is "\u{1d11e}"
MCUnitTestHandlerThrows(TestCharWithCode_Negative, "code->byte (-ve)")
end handler
handler TestCodeOfChar_Long()
return the code of "xx"
end handler
handler TestCodeOfChar_Empty()
return the code of ""
end handler
public handler TestCodeOfChar()
test "char->code" when the code of "x" is 120
test "char->code (BMP)" when the code of "\u{2192}" is 8594
test "char->code (SMP)" when the code of "\u{1d11e}" is 119070
MCUnitTestHandlerThrows(TestCodeOfChar_Long, "char->code (multibyte)")
MCUnitTestHandlerThrows(TestCodeOfChar_Empty, "char->code (empty)")
end handler
constant kVarSel is "a\u{fe0e}"
public handler TestVariationSelector()
test "variation selector is part of grapheme" when \
char 1 of kVarSel is kVarSel
end handler
end module