File: widgetadaptor.test

package info (click to toggle)
itcl4 4.3.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,512 kB
  • sloc: ansic: 25,739; tcl: 1,705; sh: 452; makefile: 68
file content (825 lines) | stat: -rw-r--r-- 19,485 bytes parent folder | download
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
#---------------------------------------------------------------------
# TITLE:
#       typefunction.test
#
# AUTHOR:
#       Arnulf Wiedemann with a lot of code form the snit tests by
#       Will Duquette
#
# DESCRIPTION:
#       Test cases for ::itcl::type proc, method, typemethod commands.
#       Uses the ::tcltest:: harness.
#
#       There is at least Tcl 8.6a3 needed
#
#    The tests assume tcltest 2.2
#-----------------------------------------------------------------------

# ### ### ### ######### ######### #########
## Declare the minimal version of Tcl required to run the package
## tested by this testsuite, and its dependencies.

proc testsNeedTcl {version} {
    # This command ensures that a minimum version of Tcl is used to
    # run the tests in the calling testsuite. If the minimum is not
    # met by the active interpreter we forcibly bail out of the
    # testsuite calling the command. The command has to be called
    # immediately after loading the utilities.

    if {[package vsatisfies [package provide Tcl] ${version}-]} return

    puts "    Aborting the tests found in \"[file tail [info script]]\""
    puts "    Requiring at least Tcl $version, have [package provide Tcl]."

    # This causes a 'return' in the calling scope.
    return -code return
}

# ### ### ### ######### ######### #########
## Declare the minimum version of Tcltest required to run the
## testsuite.

proc testsNeedTcltest {version} {
    # This command ensure that a minimum version of the Tcltest
    # support package is used to run the tests in the calling
    # testsuite. If the minimum is not met by the loaded package we
    # forcibly bail out of the testsuite calling the command. The
    # command has to be called after loading the utilities. The only
    # command allowed to come before it is 'textNeedTcl' above.

    # Note that this command will try to load a suitable version of
    # Tcltest if the package has not been loaded yet.

    if {[lsearch [namespace children] ::tcltest] == -1} {
	if {![catch {
	    package require tcltest $version
	}]} {
	    namespace import -force ::tcltest::*
	    return
	}
    } elseif {[package vcompare [package present tcltest] $version] >= 0} {
	namespace import -force ::tcltest::*
	return
    }

    puts "    Aborting the tests found in [file tail [info script]]."
    puts "    Requiring at least tcltest $version, have [package present tcltest]"

    # This causes a 'return' in the calling scope.
    return -code return
}

# Set up for Tk tests: enter the event loop long enough to catch
# any bgerrors.
proc tkbide {{msg "tkbide"} {msec 500}} {
    set ::bideVar 0
    set ::bideError ""
    set ::bideErrorInfo ""
    # It looks like update idletasks does the job.
    if {0} {
	after $msec {set ::bideVar 1}
	tkwait variable ::bideVar
    }
    update idletasks
    if {"" != $::bideError} {
	error "$msg: $::bideError" $::bideErrorInfo
    }
}

testsNeedTcl     8.6
testsNeedTcltest 2.2

interp alias {} type {} ::itcl::type
interp alias {} widgetadaptor {} ::itcl::widgetadaptor

# Marks tests which are only for Tk.
tcltest::testConstraint tk [expr {![catch {package require Tk}]}]

::tcltest::loadTestedCommands
package require itcl

#-----------------------------------------------------------------------
# Widgetadaptors

test widgetadaptor-1.1 {creating a widget: hull hijacking
} -constraints {
    tk
} -body {
    widgetadaptor mylabel {
	constructor {args} {
	    installhull [label $self]
	    $self configure {*}$args
	}

	delegate method * to itcl_hull
	delegate option * to itcl_hull
    }

    set xx [mylabel create .label -text "My Label"]

    set a [.label cget -text]
    set b [::itcl::internal::widgets::hull1.label cget -text]

    destroy .label
    tkbide
    list $a $b
} -cleanup {
    mylabel destroy
} -result {{My Label} {My Label}}

test widgetadaptor-1.2 {destroying a widget with destroy
} -constraints {
    tk
} -body {
    widgetadaptor mylabel {
	constructor {} {
	    installhull [label $self]
	}
    }

    mylabel create .label
    set a [lsort [namespace children ::itcl::internal::variables]]
    destroy .label
    set b [lsort [namespace children ::itcl::internal::variables]]
    tkbide
    list $a $b
} -cleanup {
    mylabel destroy
} -result {{::itcl::internal::variables::mylabel ::itcl::internal::variables::oo} {::itcl::internal::variables::mylabel ::itcl::internal::variables::oo}}

test widgetadaptor-1.3 {destroying two widgets of the same type with destroy
} -constraints {
    tk
} -body {
    widgetadaptor mylabel {
	constructor {} {
	    installhull [label $self]
	}
    }

    mylabel create .lab1
    mylabel create .lab2
    set a [lsort [namespace children ::itcl::internal::variables]]
    destroy .lab1
    destroy .lab2
    set b [lsort [namespace children ::itcl::internal::variables]]
    tkbide
    list $a $b
} -cleanup {
    mylabel destroy
} -result {{::itcl::internal::variables::mylabel ::itcl::internal::variables::oo} {::itcl::internal::variables::mylabel ::itcl::internal::variables::oo}}

test widgetadaptor-1.4 {destroying a widget with rename, then destroy type
} -constraints {
    tk
} -body {
    widgetadaptor mylabel {
	constructor {} {
	    installhull [label $self]
	}
    }

    mylabel create .label
    set a [lsort [namespace children ::itcl::internal::variables]]
    rename .label ""
    set b [lsort [namespace children ::itcl::internal::variables]]

    mylabel destroy
    tkbide
    list $a $b
} -result {{::itcl::internal::variables::mylabel ::itcl::internal::variables::oo} {::itcl::internal::variables::mylabel ::itcl::internal::variables::oo}}

test widgetadaptor-1.5 {destroying two widgets of the same type with rename
} -constraints {
    tk
} -body {
    widgetadaptor mylabel {
	constructor {} {
	    installhull [label $self]
	}
    }

    mylabel create .lab1
    mylabel create .lab2
    set a [lsort [namespace children ::itcl::internal::variables]]
    rename .lab1 ""
    rename .lab2 ""
    set b [lsort [namespace children ::itcl::internal::variables]]
    mylabel destroy
    tkbide
    list $a $b
} -result {{::itcl::internal::variables::mylabel ::itcl::internal::variables::oo} {::itcl::internal::variables::mylabel ::itcl::internal::variables::oo}}

test widgetadaptor-1.6 {create/destroy twice, with destroy
} -constraints {
    tk
} -body {
    widgetadaptor mylabel {
	constructor {} {
	    installhull [label $self]
	}
    }

    mylabel create .lab1
    set a [namespace children ::itcl::internal::variables]
    destroy .lab1

    mylabel create .lab1
    set b [namespace children ::itcl::internal::variables]
    destroy .lab1

    set c [namespace children ::itcl::internal::variables]
    mylabel destroy
    tkbide
    list $a $b $c
} -result {{::itcl::internal::variables::mylabel ::itcl::internal::variables::oo} {::itcl::internal::variables::mylabel ::itcl::internal::variables::oo} {::itcl::internal::variables::mylabel ::itcl::internal::variables::oo}}

test widgetadaptor-1.7 {create/destroy twice, with rename
} -constraints {
    tk
} -body {
    widgetadaptor mylabel {
	constructor {} {
	    installhull [label $self]
	}
    }

    mylabel create .lab1
    set a [namespace children ::itcl::internal::variables]
    rename .lab1 ""

    mylabel create .lab1
    set b [namespace children ::itcl::internal::variables]
    rename .lab1 ""

    set c [namespace children ::itcl::internal::variables]
    mylabel destroy
    tkbide
    list $a $b $c
} -result {{::itcl::internal::variables::mylabel ::itcl::internal::variables::oo} {::itcl::internal::variables::mylabel ::itcl::internal::variables::oo} {::itcl::internal::variables::mylabel ::itcl::internal::variables::oo}}

test widgetadaptor-1.8 {"create" is optional
} -constraints {
    tk
} -body {
    widgetadaptor mylabel {
	constructor {args} {
	    installhull [label $self]
	}
	method howdy {} {return "Howdy!"}
    }

    mylabel .label
    set a [.label howdy]

    destroy .label
    tkbide
    set a
} -cleanup {
    mylabel destroy
} -result {Howdy!}

test widgetadaptor-1.10 {"create" is optional, but must be a valid name
} -constraints {
    tk
} -body {
    widgetadaptor mylabel {
	constructor {args} {
	    installhull [label $self]
	}
	method howdy {} {return "Howdy!"}
    }

    catch {mylabel foo} result
    tkbide
    set result
} -cleanup {
    mylabel destroy
} -result {bad window path name "foo"}

test widgetadaptor-1.11 {user-defined destructors are called
} -constraints {
    tk
} -body {
    widgetadaptor mylabel {
	typevariable flag ""

	constructor {args} {
	    installhull [label $self]
	    set flag "created $self"
	}

	destructor {
	    set flag "destroyed $self"
	}

	typemethod getflag {} {
	    return $flag
	}
    }

    mylabel .label
    set a [mylabel getflag]
    destroy .label
    tkbide
    list $a [mylabel getflag]
} -cleanup {
    mylabel destroy
} -result {{created ::itcl::internal::widgets::hull1.label} {destroyed ::itcl::internal::widgets::hull1.label}}

test widgetadaptor-1.12 {Constructor errors tolerated
} -constraints {
    tk
} -body {
    widgetadaptor mylabel {
	constructor {args} {error foo}
	destructor {}
    }

    # Without bug fix this will crash
    mylabel .label
} -cleanup {
    mylabel destroy
} -returnCodes error -result foo

test widgetadaptor-1.14 {hull can be repeatedly renamed
} -constraints {
    tk
} -body {
    widgetadaptor basetype {
	constructor {args} {
	    installhull [label $self]
	}

	method basemethod {} { return "basemethod" }
    }

    widgetadaptor w1 {
	constructor {args} {
	    installhull [basetype create $self]
	}
    }

    widgetadaptor w2 {
	constructor {args} {
	    installhull [w1 $self]
	}
    }

    set a [w2 .foo]
    destroy .foo
    tkbide
    set a
} -cleanup {
    w2 destroy
    w1 destroy
    basetype destroy
} -result {.foo}

test widgetadaptor-1.15 {widget names can be generated
} -constraints {
    tk
} -body {
    widgetadaptor unique {
	constructor {args} {
	    installhull [label $self]
	}
    }

    set w [unique .#auto]
    destroy $w
    tkbide
    set w
} -cleanup {
    unique destroy
} -result {.unique0}

test widgetadaptor-1.16 {snit::widgetadaptor as hull
} -constraints {
    tk
} -body {
    widgetadaptor mylabel {
	constructor {args} {
	    installhull [label $self]
	    if {[llength $args]} {
		$self configure {*}$args
	    }
	}
	method method1 {} {
	    return "method1"
	}
	delegate option * to itcl_hull
    }

    widgetadaptor mylabel2 {
	constructor {args} {
	    installhull [mylabel $self]
		$self configure {*}$args
	}
	method method2 {} {
	    return "method2: [$itcl_hull method1]"
	}
	delegate option * to itcl_hull
    }

    mylabel2 .label -text "Some Text"
    set a [.label method2]
    set b [.label cget -text]
    .label configure -text "More Text"
    set c [.label cget -text]
    set d [lsort [namespace children ::itcl::internal::variables]]

    destroy .label

    set e [lsort [namespace children ::itcl::internal::variables]]

    mylabel2 destroy
    mylabel destroy

    tkbide
    list $a $b $c $d $e
} -result {{method2: method1} {Some Text} {More Text} {::itcl::internal::variables::mylabel ::itcl::internal::variables::mylabel2 ::itcl::internal::variables::oo} {::itcl::internal::variables::mylabel ::itcl::internal::variables::mylabel2 ::itcl::internal::variables::oo}}

test widgetadaptor-1.17 {snit::widgetadaptor as hull; use rename
} -constraints {
    tk
} -body {
    widgetadaptor mylabel {
	constructor {args} {
	    installhull [label $self]
	    $self configure {*}$args
	}
	method method1 {} {
	    return "method1"
	}
	delegate option * to itcl_hull
    }

    widgetadaptor mylabel2 {
	constructor {args} {
	    installhull [mylabel $self]
	    $self configure {*}$args
	}
	method method2 {} {
	    return "method2: [$itcl_hull method1]"
	}
	delegate option * to itcl_hull
    }

    mylabel2 .label -text "Some Text"
    set a [.label method2]
    set b [.label cget -text]
    .label configure -text "More Text"
    set c [.label cget -text]
    set d [lsort [namespace children ::itcl::internal::variables]]

    rename .label ""

    set e [lsort [namespace children ::itcl::internal::variables]]

    mylabel2 destroy
    mylabel destroy

    tkbide
    list $a $b $c $d $e
} -result {{method2: method1} {Some Text} {More Text} {::itcl::internal::variables::mylabel ::itcl::internal::variables::mylabel2 ::itcl::internal::variables::oo} {::itcl::internal::variables::mylabel ::itcl::internal::variables::mylabel2 ::itcl::internal::variables::oo}}

test widgetadaptor-1.19 {error in widgetadaptor constructor
} -constraints {
    tk
} -body {
    widgetadaptor mylabel {
	constructor {args} {
	    error "Simulated Error"
	}
    }

    mylabel .lab
} -returnCodes {
    error
} -cleanup {
    mylabel destroy
} -result {Simulated Error}


test install-1.3 {can't install until hull exists
} -constraints {
    tk
} -body {
    widgetadaptor myframe {
	# Delegate an option just to make sure the component variable
	# exists.
	delegate option -font to text

	constructor {args} {
	    installcomponent text using text $win.text -background green
	}
    }

    myframe .frm
} -returnCodes {
    error
} -cleanup {
    myframe destroy
} -result {cannot install "text" before "itcl_hull" exists}

test installhull-1.3 {
    options delegated to a widgetadaptor's itcl_hull frame
    with the same name are
    initialized from the option database.  Note that there's no
    explicit code in Snit to do this; there's no way to change the
    adapted hull widget's -class, so the widget is simply being
    initialized normally.
} -constraints {
    tk
} -body {
    widgetadaptor myframe {
	delegate option -background to itcl_hull

	typeconstructor {
	    option add *Frame.background red
	    option add *Frame.width 123
	}

	constructor {args} {
	    installhull using frame
	}

	method getwid {} {
	    $itcl_hull cget -width
	}
    }

    myframe .frm
    set a [.frm cget -background]
    set b [.frm getwid]
    destroy .frm
    tkbide
    list $a $b
} -cleanup {
    myframe destroy
} -result {red 123}

test installhull-1.4 {
    Options delegated to a widget's itcl_hull frame with a different name are
    initialized from the option database.
} -constraints {
    tk
} -body {
    widgetadaptor myframe {
	delegate option -mainbackground to itcl_hull as -background

	typeconstructor {
	    option add *Frame.mainbackground red
	}

	constructor {args} {
	    installhull using frame
	}
    }

    myframe .frm
    set a [.frm cget -mainbackground]
    destroy .frm
    tkbide
    set a
} -cleanup {
    myframe destroy
} -result {red}

test installhull-1.5 {
    Option values read from the option database are overridden by options
    explicitly passed, even if delegated under a different name.
} -constraints {
    tk
} -body {
    widgetadaptor myframe {
	delegate option -mainbackground to itcl_hull as -background

	typeconstructor {
	    option add *Frame.mainbackground red
	    option add *Frame.width 123
	}

	constructor {args} {
	    installhull using frame -background green -width 321
	}

	method getwid {} {
	    $itcl_hull cget -width
	}
    }

    myframe .frm
    set a [.frm cget -mainbackground]
    set b [.frm getwid]
    destroy .frm
    tkbide
    list $a $b
} -cleanup {
    myframe destroy
} -result {green 321}

test option-2.5 {configure returns info, unknown options
} -constraints {
    tk
} -body {
    widgetadaptor myframe {
	option -foo a
	delegate option -width to itcl_hull
	delegate option * to itcl_hull
	constructor {args} {
	    # need to reset because of test installhull-1.5
	    option add *Frame.width 0
	    installhull [frame $self]
	}
    }

    myframe .frm
    set a [.frm configure -foo]
    set b [.frm configure -width]
    set c [.frm configure -height]
    destroy .frm
    tkbide

    list $a $b $c

} -cleanup {
    myframe destroy
} -result {{-foo foo Foo a a} {-width width Width 0 0} {-height height Height 0 0}}

test option-2.6 {configure -opt unknown to implicit component
} -constraints {
    tk
} -body {
    widgetadaptor myframe {
	delegate option * to itcl_hull
	constructor {args} {
	    installhull [frame $self]
	}
    }
    myframe .frm
    catch {.frm configure -quux} result
    destroy .frm
    tkbide
    set result
} -cleanup {
    myframe destroy
} -result {unknown option "-quux"}

test iinfo-6.5 {info options with unknown delegated options
} -constraints {
    tk
} -body {
    widgetadaptor myframe {
	option -foo a
	delegate option * to itcl_hull
	constructor {args} {
	    installhull [frame $self]
	}
    }
    myframe .frm

    set a [lsort [.frm info options]]
    destroy .frm
    tkbide
    set a
} -cleanup {
    myframe destroy
} -match glob -result {-background *-bd -bg*-borderwidth -class -colormap -container -cursor -foo -height -highlightbackground -highlightcolor -highlightthickness -padx -pady -relief -takefocus*-visual -width}

test iinfo-6.7 {info options with exceptions
} -constraints {
    tk
} -body {
    widgetadaptor myframe {
	option -foo a
	delegate option * to itcl_hull except -background
	constructor {args} {
	    installhull [frame $self]
	}
    }
    myframe .frm

    set a [lsort [.frm info options]]
    destroy .frm
    tkbide
    set a
} -cleanup {
    myframe destroy
} -match glob -result {*-bd -bg*-borderwidth -class -colormap -container -cursor -foo -height -highlightbackground -highlightcolor -highlightthickness -padx -pady -relief -takefocus*-visual -width}

test iinfo-6.8 {info options with pattern
} -constraints {
    tk
} -body {
    widgetadaptor myframe {
	option -foo a
	delegate option * to itcl_hull
	constructor {args} {
	    installhull [frame $self]
	}
    }
    myframe .frm

    set a [lsort [.frm info options -c*]]
    destroy .frm
    tkbide
    set a
} -cleanup {
    myframe destroy
} -result {-class -colormap -container -cursor}

test tinfo-3.2 {widget info instances
} -constraints {
    tk
} -body {
    widgetadaptor mylabel {
	constructor {args} {
	    installhull [label $self]
	}
    }

    mylabel .lab1
    mylabel .lab2

    set result [mylabel info instances]

    destroy .lab1
    destroy .lab2
    tkbide

    lsort $result
} -cleanup {
    mylabel destroy
} -result {.lab1 .lab2}

test widgetclass-1.2 {can't set widgetclass for itcl::widgetadaptors
} -constraints {
    tk
} -body {
    widgetadaptor dog {
	widgetclass Dog
    }
} -returnCodes {
    error
} -result {can't set widgetclass for ::itcl::widgetadaptor}

test hulltype-1.2 {can't set hulltype for itcl::widgetadaptors
} -constraints {
    tk
} -body {
    widgetadaptor dog {
	hulltype Dog
    }
} -returnCodes {
    error
} -result {can't set hulltype for ::itcl::widgetadaptor}

test wainfo-10.1 {widgetadaptor info widgetadaptors
} -constraints {
    tk
} -body {
    widgetadaptor dog {
    }

    widgetadaptor cat {
    }

    lsort [dog info widgetadaptors]
} -cleanup {
    dog destroy
    cat destroy
} -result {cat dog}

test wainfo-10.2 {widgetadaptor info components
} -constraints {
    tk
} -body {
    widgetadaptor dog {
	component comp1
	component comp2
    }

    widgetadaptor cat {
	component comp1
	component comp1a
    }

    set a [lsort [dog info components]]
    set b [lsort [cat info components]]
    list $a $b
} -cleanup {
    dog destroy
    cat destroy
} -result {{comp1 comp2 itcl_hull} {comp1 comp1a itcl_hull}}





#---------------------------------------------------------------------
# Clean up

::tcltest::cleanupTests
return