1 # This file is a Tcl script to test the code in the file tkTextIndex.c.
2 # This file is organized in the standard fashion for Tcl tests.
4 # Copyright (c) 1994 The Regents of the University of California.
5 # Copyright (c) 1994 Sun Microsystems, Inc.
6 # Copyright (c) 1998-1999 by Scriptics Corporation.
11 package require tcltest 2.1
12 namespace import -force tcltest::configure
13 namespace import -force tcltest::testsDirectory
14 configure -testdir [file join [pwd] [file dirname [info script]]]
15 configure -loadfile [file join [testsDirectory] constraints.tcl]
16 tcltest::loadTestedCommands
18 # Some tests require the testtext command
19 testConstraint testtext [llength [info commands testtext]]
22 text .t -font {Courier -12} -width 20 -height 10
23 pack append . .t {top expand fill}
28 # The statements below reset the main window; it's needed if the window
29 # manager is mwm to make mwm forget about a previous minimum size setting.
33 wm positionfrom . user
40 b\u4e4fy GIrl .#@? x_yz
44 image create photo textimage -width 10 -height 10
45 textimage put red -to 0 0 9 9
47 test textIndex-1.1 {TkTextMakeByteIndex} {testtext} {
49 testtext .t byteindex -1 3
51 test textIndex-1.2 {TkTextMakeByteIndex} {testtext} {
52 # (lineIndex < 0), because lineIndex == strtol(argv[2]) - 1
53 testtext .t byteindex 0 3
55 test textIndex-1.3 {TkTextMakeByteIndex} {testtext} {
57 testtext .t byteindex 1 3
59 test textIndex-1.4 {TkTextMakeByteIndex} {testtext} {
61 testtext .t byteindex 3 -1
63 test textIndex-1.5 {TkTextMakeByteIndex} {testtext} {
65 testtext .t byteindex 3 3
67 test textIndex-1.6 {TkTextMakeByteIndex} {testtext} {
68 # (indexPtr->linePtr == NULL)
69 testtext .t byteindex 9 2
71 test textIndex-1.7 {TkTextMakeByteIndex} {testtext} {
72 # not (indexPtr->linePtr == NULL)
73 testtext .t byteindex 7 2
75 test textIndex-1.8 {TkTextMakeByteIndex: shortcut for 0} {testtext} {
77 testtext .t byteindex 1 0
79 test textIndex-1.9 {TkTextMakeByteIndex: shortcut for 0} {testtext} {
80 # not (byteIndex == 0)
81 testtext .t byteindex 3 80
83 test textIndex-1.10 {TkTextMakeByteIndex: verify index is in range} {testtext} {
84 # for (segPtr = indexPtr->linePtr->segPtr; ; segPtr = segPtr->nextPtr)
87 testtext .t byteindex 3 5
89 test textIndex-1.11 {TkTextMakeByteIndex: verify index is in range} {testtext} {
90 # for (segPtr = indexPtr->linePtr->segPtr; ; segPtr = segPtr->nextPtr)
91 # index += segPtr->size
92 # Multiple segments, make sure add segment size to index.
95 set x [testtext .t byteindex 3 7]
99 test textIndex-1.12 {TkTextMakeByteIndex: verify index is in range} {testtext} {
101 testtext .t byteindex 3 7
103 test textIndex-1.13 {TkTextMakeByteIndex: verify index is in range} {testtext} {
104 # not (segPtr == NULL)
105 testtext .t byteindex 3 4
107 test textIndex-1.14 {TkTextMakeByteIndex: verify index is in range} {testtext} {
108 # (index + segPtr->size > byteIndex)
111 testtext .t byteindex 3 4
113 test textIndex-1.15 {TkTextMakeByteIndex: verify index is in range} {testtext} {
114 # (index + segPtr->size > byteIndex), index != 0
118 set x [testtext .t byteindex 3 4]
122 test textIndex-1.16 {TkTextMakeByteIndex: UTF-8 characters} {testtext} {
123 testtext .t byteindex 5 100
125 test textIndex-1.17 {TkTextMakeByteIndex: prevent splitting UTF-8 character} \
127 # ((byteIndex > index) && (segPtr->typePtr == &tkTextCharType))
128 # Wrong answer would be \xb9 (the 2nd byte of UTF rep of 0x4e4f).
130 set x [testtext .t byteindex 5 2]
131 list $x [.t get insert]
133 test textIndex-1.18 {TkTextMakeByteIndex: prevent splitting UTF-8 character} \
135 # ((byteIndex > index) && (segPtr->typePtr == &tkTextCharType))
136 testtext .t byteindex 5 1
140 test textIndex-2.1 {TkTextMakeCharIndex} {
144 test textIndex-2.2 {TkTextMakeCharIndex} {
145 # (lineIndex < 0), because lineIndex == strtol(argv[2]) - 1
148 test textIndex-2.3 {TkTextMakeCharIndex} {
149 # not (lineIndex < 0)
152 test textIndex-2.4 {TkTextMakeCharIndex} {
156 test textIndex-2.5 {TkTextMakeCharIndex} {
160 test textIndex-2.6 {TkTextMakeCharIndex} {
161 # (indexPtr->linePtr == NULL)
164 test textIndex-2.7 {TkTextMakeCharIndex} {
165 # not (indexPtr->linePtr == NULL)
168 test textIndex-2.8 {TkTextMakeCharIndex: verify index is in range} {
169 # for (segPtr = indexPtr->linePtr->segPtr; ; segPtr = segPtr->nextPtr)
174 test textIndex-2.9 {TkTextMakeCharIndex: verify index is in range} {
175 # for (segPtr = indexPtr->linePtr->segPtr; ; segPtr = segPtr->nextPtr)
176 # Multiple segments, make sure add segment size to index.
183 test textIndex-2.10 {TkTextMakeCharIndex: verify index is in range} {
187 test textIndex-2.11 {TkTextMakeCharIndex: verify index is in range} {
188 # not (segPtr == NULL)
191 test textIndex-2.12 {TkTextMakeCharIndex: verify index is in range} {
192 # (segPtr->typePtr == &tkTextCharType)
193 # Wrong answer would be \xb9 (the 2nd byte of UTF rep of 0x4e4f).
195 .t mark set insert 5.2
198 test textIndex-2.13 {TkTextMakeCharIndex: verify index is in range} {
199 # not (segPtr->typePtr == &tkTextCharType)
201 .t image create 5.2 -image textimage
202 .t mark set insert 5.5
203 set x [.t get insert]
207 test textIndex-2.14 {TkTextMakeCharIndex: verify index is in range} {
208 # (charIndex < segPtr->size)
210 .t image create 5.0 -image textimage
217 .t tag add x 2.8 2.11
219 set weirdTag "funny . +- 22.1\n\t{"
220 .t tag add $weirdTag 2.1 2.6
221 set weirdMark "asdf \n{-+ 66.2\t"
222 .t mark set $weirdMark 4.0
223 .t tag config y -relief raised
224 test textIndex-3.1 {TkTextGetIndex, weird mark names} {
225 list [catch {.t index $weirdMark} msg] $msg
228 test textIndex-4.1 {TkTextGetIndex, tags} {
229 list [catch {.t index x.first} msg] $msg
231 test textIndex-4.2 {TkTextGetIndex, tags} {
232 list [catch {.t index x.last} msg] $msg
234 test textIndex-4.3 {TkTextGetIndex, weird tags} {
235 list [.t index $weirdTag.first+1c] [.t index $weirdTag.last+2c]
237 test textIndex-4.4 {TkTextGetIndex, tags} {
238 list [catch {.t index x.gorp} msg] $msg
239 } {1 {bad text index "x.gorp"}}
240 test textIndex-4.5 {TkTextGetIndex, tags} {
241 list [catch {.t index foo.last} msg] $msg
242 } {1 {bad text index "foo.last"}}
243 test textIndex-4.6 {TkTextGetIndex, tags} {
244 list [catch {.t index y.first} msg] $msg
245 } {1 {text doesn't contain any characters tagged with "y"}}
246 test textIndex-4.7 {TkTextGetIndex, tags} {
247 list [catch {.t index x.last,} msg] $msg
248 } {1 {bad text index "x.last,"}}
249 test textIndex-4.8 {TkTextGetIndex, tags} {
251 set result [list [.t index z.first] [.t index z.last]]
256 test textIndex-5.1 {TkTextGetIndex, "@"} {nonPortable fonts} {
259 test textIndex-5.2 {TkTextGetIndex, "@"} {fonts} {
262 test textIndex-5.3 {TkTextGetIndex, "@"} {fonts} {
265 test textIndex-5.4 {TkTextGetIndex, "@"} {fonts} {
266 list [catch {.t index @x} msg] $msg
267 } {1 {bad text index "@x"}}
268 test textIndex-5.5 {TkTextGetIndex, "@"} {fonts} {
269 list [catch {.t index @10q} msg] $msg
270 } {1 {bad text index "@10q"}}
271 test textIndex-5.6 {TkTextGetIndex, "@"} {fonts} {
272 list [catch {.t index @10,} msg] $msg
273 } {1 {bad text index "@10,"}}
274 test textIndex-5.7 {TkTextGetIndex, "@"} {fonts} {
275 list [catch {.t index @10,a} msg] $msg
276 } {1 {bad text index "@10,a"}}
277 test textIndex-5.8 {TkTextGetIndex, "@"} {fonts} {
278 list [catch {.t index @10,9,} msg] $msg
279 } {1 {bad text index "@10,9,"}}
281 test textIndex-6.1 {TkTextGetIndex, numeric} {
282 list [catch {.t index 2.3} msg] $msg
284 test textIndex-6.2 {TkTextGetIndex, numeric} {
285 list [catch {.t index -} msg] $msg
286 } {1 {bad text index "-"}}
287 test textIndex-6.3 {TkTextGetIndex, numeric} {
288 list [catch {.t index 2.end} msg] $msg
290 test textIndex-6.4 {TkTextGetIndex, numeric} {
291 list [catch {.t index 2.x} msg] $msg
292 } {1 {bad text index "2.x"}}
293 test textIndex-6.5 {TkTextGetIndex, numeric} {
294 list [catch {.t index 2.3x} msg] $msg
295 } {1 {bad text index "2.3x"}}
297 test textIndex-7.1 {TkTextGetIndex, miscellaneous other bases} {
298 list [catch {.t index end} msg] $msg
300 test textIndex-7.2 {TkTextGetIndex, miscellaneous other bases} {
301 list [catch {.t index foo} msg] $msg
303 test textIndex-7.3 {TkTextGetIndex, miscellaneous other bases} {
304 list [catch {.t index foo+1c} msg] $msg
307 test textIndex-8.1 {TkTextGetIndex, modifiers} {
308 list [catch {.t index 2.1+1char} msg] $msg
310 test textIndex-8.2 {TkTextGetIndex, modifiers} {
311 list [catch {.t index "2.1 +1char"} msg] $msg
313 test textIndex-8.3 {TkTextGetIndex, modifiers} {
314 list [catch {.t index 2.1-1char} msg] $msg
316 test textIndex-8.4 {TkTextGetIndex, modifiers} {
317 list [catch {.t index {2.1 }} msg] $msg
319 test textIndex-8.5 {TkTextGetIndex, modifiers} {
320 list [catch {.t index {2.1+foo bar}} msg] $msg
321 } {1 {bad text index "2.1+foo bar"}}
322 test textIndex-8.6 {TkTextGetIndex, modifiers} {
323 list [catch {.t index {2.1 foo bar}} msg] $msg
324 } {1 {bad text index "2.1 foo bar"}}
326 test textIndex-9.1 {TkTextIndexCmp} {
327 list [.t compare 3.1 < 3.2] [.t compare 3.1 == 3.2]
329 test textIndex-9.2 {TkTextIndexCmp} {
330 list [.t compare 3.2 < 3.2] [.t compare 3.2 == 3.2]
332 test textIndex-9.3 {TkTextIndexCmp} {
333 list [.t compare 3.3 < 3.2] [.t compare 3.3 == 3.2]
335 test textIndex-9.4 {TkTextIndexCmp} {
336 list [.t compare 2.1 < 3.2] [.t compare 2.1 == 3.2]
338 test textIndex-9.5 {TkTextIndexCmp} {
339 list [.t compare 4.1 < 3.2] [.t compare 4.1 == 3.2]
342 test textIndex-10.1 {ForwBack} {
343 list [catch {.t index {2.3 + x}} msg] $msg
344 } {1 {bad text index "2.3 + x"}}
345 test textIndex-10.2 {ForwBack} {
346 list [catch {.t index {2.3 + 2 chars}} msg] $msg
348 test textIndex-10.3 {ForwBack} {
349 list [catch {.t index {2.3 + 2c}} msg] $msg
351 test textIndex-10.4 {ForwBack} {
352 list [catch {.t index {2.3 - 3ch}} msg] $msg
354 test textIndex-10.5 {ForwBack} {
355 list [catch {.t index {1.3 + 3 lines}} msg] $msg
357 test textIndex-10.6 {ForwBack} {
358 list [catch {.t index {2.3 -1l}} msg] $msg
360 test textIndex-10.7 {ForwBack} {
361 list [catch {.t index {2.3 -1 gorp}} msg] $msg
362 } {1 {bad text index "2.3 -1 gorp"}}
363 test textIndex-10.8 {ForwBack} {
364 list [catch {.t index {2.3 - 4 lines}} msg] $msg
366 test textIndex-10.9 {ForwBack} {
367 .t mark set insert 2.0
368 list [catch {.t index {insert -0 chars}} msg] $msg
370 test textIndex-10.10 {ForwBack} {
371 .t mark set insert 2.end
372 list [catch {.t index {insert +0 chars}} msg] $msg
375 test textIndex-11.1 {TkTextIndexForwBytes} {testtext} {
376 testtext .t forwbytes 2.3 -7
378 test textIndex-11.2 {TkTextIndexForwBytes} {testtext} {
379 testtext .t forwbytes 2.3 5
381 test textIndex-11.3 {TkTextIndexForwBytes} {testtext} {
382 testtext .t forwbytes 2.3 10
384 test textIndex-11.4 {TkTextIndexForwBytes} {testtext} {
385 testtext .t forwbytes 2.3 11
387 test textIndex-11.5 {TkTextIndexForwBytes} {testtext} {
388 testtext .t forwbytes 2.3 57
390 test textIndex-11.6 {TkTextIndexForwBytes} {testtext} {
391 testtext .t forwbytes 2.3 58
393 test textIndex-11.7 {TkTextIndexForwBytes} {testtext} {
394 testtext .t forwbytes 2.3 59
397 test textIndex-12.1 {TkTextIndexForwChars} {
399 .t index {2.3 + -7 chars}
401 test textIndex-12.2 {TkTextIndexForwChars} {
402 # not (charCount < 0)
403 .t index {2.3 + 5 chars}
405 test textIndex-12.3 {TkTextIndexForwChars: find index} {
406 # for ( ; segPtr != NULL; segPtr = segPtr->nextPtr)
408 .t index {2.3 + 9 chars}
410 test textIndex-12.4 {TkTextIndexForwChars: find index} {
411 # for ( ; segPtr != NULL; segPtr = segPtr->nextPtr)
414 set x [.t index {2.3 + 9 chars}]
418 test textIndex-12.5 {TkTextIndexForwChars: find index} {
419 # for ( ; segPtr != NULL; segPtr = segPtr->nextPtr)
420 # border condition: last char
422 .t index {2.3 + 10 chars}
424 test textIndex-12.6 {TkTextIndexForwChars: find index} {
425 # for ( ; segPtr != NULL; segPtr = segPtr->nextPtr)
426 # border condition: segPtr == NULL -> beginning of next line
428 .t index {2.3 + 11 chars}
430 test textIndex-12.7 {TkTextIndexForwChars: find index} {
431 # (segPtr->typePtr == &tkTextCharType)
432 .t index {2.3 + 2 chars}
434 test textIndex-12.8 {TkTextIndexForwChars: find index} {
436 # No more chars, so we found byte offset.
438 .t index {2.3 + 2 chars}
440 test textIndex-12.9 {TkTextIndexForwChars: find index} {
441 # not (segPtr->typePtr == &tkTextCharType)
443 .t image create 2.4 -image textimage
444 set x [.t get {2.3 + 3 chars}]
448 test textIndex-12.10 {TkTextIndexForwChars: find index} {
449 # dstPtr->byteIndex += segPtr->size - byteOffset
450 # When moving to next segment, account for bytes in last segment.
451 # Wrong answer would be 2.4
454 set x [.t index {2.3 + 5 chars}]
458 test textIndex-12.11 {TkTextIndexForwChars: go to next line} {
460 .t index {7.6 + 3 chars}
462 test textIndex-12.12 {TkTextIndexForwChars: go to next line} {
463 # Reset byteIndex to 0 now that we are on a new line.
464 # Wrong answer would be 2.9
465 .t index {1.3 + 6 chars}
467 test textIndex-12.13 {TkTextIndexForwChars} {
469 .t index {2.3 + 56 chars}
471 test textIndex-12.14 {TkTextIndexForwChars} {
473 .t index {2.3 + 57 chars}
476 test textIndex-13.1 {TkTextIndexBackBytes} {testtext} {
477 testtext .t backbytes 3.2 -10
479 test textIndex-13.2 {TkTextIndexBackBytes} {testtext} {
480 testtext .t backbytes 3.2 2
482 test textIndex-13.3 {TkTextIndexBackBytes} {testtext} {
483 testtext .t backbytes 3.2 3
485 test textIndex-13.4 {TkTextIndexBackBytes} {testtext} {
486 testtext .t backbytes 3.2 22
488 test textIndex-13.5 {TkTextIndexBackBytes} {testtext} {
489 testtext .t backbytes 3.2 23
491 test textIndex-13.6 {TkTextIndexBackBytes} {testtext} {
492 testtext .t backbytes 3.2 24
495 test textIndex-14.1 {TkTextIndexBackChars} {
497 .t index {3.2 - -10 chars}
499 test textIndex-14.2 {TkTextIndexBackChars} {
500 # not (charCount < 0)
501 .t index {3.2 - 2 chars}
503 test textIndex-14.3 {TkTextIndexBackChars: find starting segment} {
504 # for (segPtr = dstPtr->linePtr->segPtr; ; segPtr = segPtr->nextPtr)
507 .t index {3.2 - 3 chars}
509 test textIndex-14.4 {TkTextIndexBackChars: find starting segment} {
510 # for (segPtr = dstPtr->linePtr->segPtr; ; segPtr = segPtr->nextPtr)
515 .t mark set foo3 2.10
516 set x [.t index {2.9 - 1 chars}]
517 .t mark unset foo1 foo2 foo3
520 test textIndex-14.5 {TkTextIndexBackChars: find starting seg and offset} {
521 # for (segPtr = dstPtr->linePtr->segPtr; ; segPtr = segPtr->nextPtr)
522 # Make sure segSize was decremented. Wrong answer would be 2.10
525 set x [.t index {2.9 - 1 char}]
529 test textIndex-14.6 {TkTextIndexBackChars: back over characters} {
530 # (segPtr->typePtr == &tkTextCharType)
532 .t index {3.2 - 22 chars}
534 test textIndex-14.7 {TkTextIndexBackChars: loop backwards over chars} {
536 # No more chars, so we found byte offset.
538 .t index {3.4 - 2 chars}
540 test textIndex-14.8 {TkTextIndexBackChars: loop backwards over chars} {
542 # Still more chars, but we reached beginning of segment
544 .t image create 5.6 -image textimage
545 set x [.t index {5.8 - 3 chars}]
549 test textIndex-14.9 {TkTextIndexBackChars: back over image} {
550 # not (segPtr->typePtr == &tkTextCharType)
552 .t image create 5.6 -image textimage
553 set x [.t get {5.8 - 4 chars}]
557 test textIndex-14.10 {TkTextIndexBackChars: move to previous segment} {
559 # More segments to go
562 set x [.t index {3.5 - 2 chars}]
566 test textIndex-14.11 {TkTextIndexBackChars: move to previous segment} {
567 # not (segPtr != oldPtr)
568 # At beginning of line.
571 set x [.t index {3.5 - 10 chars}]
575 test textIndex-14.12 {TkTextIndexBackChars: move to previous line} {
577 .t index {1.5 - 10 chars}
579 test textIndex-14.13 {TkTextIndexBackChars: move to previous line} {
580 # not (lineIndex == 0)
581 .t index {2.5 - 10 chars}
583 test textIndex-14.14 {TkTextIndexBackChars: move to previous line} {
584 # for (segPtr = oldPtr; segPtr != NULL; segPtr = segPtr->nextPtr)
585 # Set byteIndex to end of previous line so we can subtract more
586 # bytes from it. Otherwise we get an TkTextIndex with a negative
589 .t index {2.5 - 6 chars}
591 test textIndex-14.15 {TkTextIndexBackChars: UTF} {
592 .t get {5.3 - 1 chars}
594 test textIndex-14.16 {TkTextIndexBackChars: UTF} {
595 .t get {5.3 - 2 chars}
597 test textIndex-14.17 {TkTextIndexBackChars: UTF} {
598 .t get {5.3 - 3 chars}
602 .t get [.t index "$index wordstart"] [.t index "$index wordend"]
604 test textIndex-15.1 {StartEnd} {
605 list [catch {.t index {2.3 lineend}} msg] $msg
607 test textIndex-15.2 {StartEnd} {
608 list [catch {.t index {2.3 linee}} msg] $msg
610 test textIndex-15.3 {StartEnd} {
611 list [catch {.t index {2.3 line}} msg] $msg
612 } {1 {bad text index "2.3 line"}}
613 test textIndex-15.4 {StartEnd} {
614 list [catch {.t index {2.3 linestart}} msg] $msg
616 test textIndex-15.5 {StartEnd} {
617 list [catch {.t index {2.3 lines}} msg] $msg
619 test textIndex-15.6 {StartEnd} {
622 test textIndex-15.7 {StartEnd} {
625 test textIndex-15.8 {StartEnd} {
628 test textIndex-15.9 {StartEnd} {
631 test textIndex-15.10 {StartEnd} {
634 test textIndex-15.11 {StartEnd} {
637 test textIndex-15.12 {StartEnd} {
640 .t tag add x 2.8 2.11
641 test textIndex-15.13 {StartEnd} {
642 list [catch {.t index {2.2 worde}} msg] $msg
644 test textIndex-15.14 {StartEnd} {
645 list [catch {.t index {2.12 words}} msg] $msg
647 test textIndex-15.15 {StartEnd} {
648 list [catch {.t index {2.12 word}} msg] $msg
649 } {1 {bad text index "2.12 word"}}
651 test testIndex-16.1 {TkTextPrintIndex} {
654 $t window create end -window [button $t.b]
655 set result [$t index end-2c]
661 test testIndex-16.2 {TkTextPrintIndex} {
664 $t window create end -window [button $t.b]
665 set result [$t tag add {} end-2c]
673 ::tcltest::cleanupTests