File: typeoption.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 (556 lines) | stat: -rw-r--r-- 11,288 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
#---------------------------------------------------------------------
# TITLE:
#       typeoption.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.
#
#    The tests assume tcltest 2.2
#-----------------------------------------------------------------------

package require tcltest 2.2
namespace import ::tcltest::*
::tcltest::loadTestedCommands
package require itcl

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

#-----------------------------------------------------------------------
# Options

test option-1.1 {options get default values} -body {
    type dog {
	option -color golden
    }

    dog create spot
    spot cget -color
} -cleanup {
    dog destroy
} -result {golden}

test option-1.2 {options can be set} -body {
    type dog {
	option -color golden
    }

    dog create spot
    spot configure -color black
    spot cget -color
} -cleanup {
    dog destroy
} -result {black}

test option-1.3 {multiple options can be set} -body {
    type dog {
	option -color golden
	option -akc 0
    }

    dog create spot
    spot configure -color brown -akc 1
    list [spot cget -color] [spot cget -akc]
} -cleanup {
    dog destroy
} -result {brown 1}

test option-1.4 {options can be retrieved as instance variable} -body {
    type dog {
	option -color golden
	option -akc 0

	method listopts {} {
	    list $itcl_options(-color) $itcl_options(-akc)
	}
    }

    dog create spot
    spot configure -color black -akc 1
    spot listopts
} -cleanup {
    dog destroy
} -result {black 1}

test option-1.5 {options can be set as an instance variable} -body {
    type dog {
	option -color golden
	option -akc 0

	method setopts {} {
	    set itcl_options(-color) black
	    set itcl_options(-akc) 1
	}
    }

    dog create spot
    spot setopts
    list [spot cget -color] [spot cget -akc]
} -cleanup {
    dog destroy
} -result {black 1}

test option-1.6 {options can be set at creation time} -body {
    type dog {
	option -color golden
	option -akc 0
    }

    dog create spot -color white -akc 1
    list [spot cget -color] [spot cget -akc]
} -cleanup {
    dog destroy
} -result {white 1}

test option-1.7 {undefined option: cget} -body {
    type dog {
	option -color golden
	option -akc 0
    }

    dog create spot
    spot cget -colour
} -returnCodes {
    error
} -cleanup {
    dog destroy
} -result {unknown option "-colour"}

test option-1.8 {undefined option: configure} -body {
    type dog {
	option -color golden
	option -akc 0
    }

    dog create spot
    spot configure -colour blue
} -returnCodes {
    error
} -cleanup {
    dog destroy
} -result {unknown option "-colour"}

test option-1.9 {options default to ""} -body {
    type dog {
	option -color
    }


    dog create spot
    spot cget -color
} -cleanup {
    dog destroy
} -result {<undefined>}

test option-1.10 {spaces allowed in option defaults} -body {
    type dog {
	option -breed "golden retriever"
    }
    dog fido
    fido cget -breed
} -cleanup {
    dog destroy
} -result {golden retriever}

test option-1.11 {brackets allowed in option defaults} -body {
    type dog {
	option -regexp {[a-z]+}
    }

    dog fido
    fido cget -regexp
} -cleanup {
    dog destroy
} -result {[a-z]+}

test option-2.1 {configure returns info, local options only} -body {
    type dog {
	option -color black
	option -akc 1
    }

    dog create spot
    spot configure -color red
    spot configure -akc 0
    lsort [spot configure]
} -cleanup {
    dog destroy
} -result {{-akc akc Akc 1 0} {-color color Color black red}}

test option-2.2 {configure -opt returns info, local options only} -body {
    type dog {
	option -color black
	option -akc 1
    }

    dog create spot
    spot configure -color red
    spot configure -color
} -cleanup {
    dog destroy
} -result {-color color Color black red}

test option-2.3 {configure -opt returns info, explicit options} -body {
    type papers {
	option -akcflag 1
    }

    type dog {
	option -color black
	delegate option -akc to papers as -akcflag
	constructor {args} {
	    set papers [papers create $self.papers]
	}

	destructor {
	    catch {$self.papers destroy}
	}
    }

    dog create spot
    spot configure -akc 0
    spot configure -akc
} -cleanup {
    dog destroy
    papers destroy
} -result {-akc akc Akc 1 0}

test option-2.4 {configure -unknownopt} -body {
    type papers {
	option -akcflag 1
    }

    type dog {
	option -color black
	delegate option -akc to papers as -akcflag
	constructor {args} {
	    set papers [papers create $self.papers]
	}

	destructor {
	    catch {$self.papers destroy}
	}
    }

    dog create spot
    spot configure -foo
} -returnCodes {
    error
} -cleanup {
    dog destroy
    papers destroy
} -result {unknown option "-foo"}

test option-3.1 {set option resource name explicitly} -body {
    type dog {
	option {-tailcolor tailColor} black
    }

    dog fido

    fido configure -tailcolor
} -cleanup {
    dog destroy
} -result {-tailcolor tailColor TailColor black black}

test option-3.2 {set option class name explicitly} -body {
    type dog {
	option {-tailcolor tailcolor TailColor} black
    }

    dog fido

    fido configure -tailcolor
} -cleanup {
    dog destroy
} -result {-tailcolor tailcolor TailColor black black}

test option-3.3 {delegated option's names come from owner} -body {
    type tail {
	option -color black
    }

    type dog {
	delegate option -tailcolor to tail as -color

	constructor {args} {
	    set tail [tail fidotail]
	}
    }

    dog fido

    fido configure -tailcolor
} -cleanup {
    dog destroy
    tail destroy
} -result {-tailcolor tailcolor Tailcolor black black}

test option-3.4 {delegated option's resource name set explicitly} -body {
    type tail {
	option -color black
    }

    type dog {
	delegate option {-tailcolor tailColor} to tail as -color

	constructor {args} {
	    set tail [tail fidotail]
	}
    }

    dog fido

    fido configure -tailcolor
} -cleanup {
    dog destroy
    tail destroy
} -result {-tailcolor tailColor TailColor black black}

test option-3.5 {delegated option's class name set explicitly} -body {
    type tail {
	option -color black
    }

    type dog {
	delegate option {-tailcolor tailcolor TailColor} to tail as -color

	constructor {args} {
	    set tail [tail fidotail]
	}
    }

    dog fido

    fido configure -tailcolor
} -cleanup {
    dog destroy
    tail destroy
} -result {-tailcolor tailcolor TailColor black black}

test option-3.6 {delegated option's default comes from component} -body {
    type tail {
	option -color black
    }

    type dog {
	delegate option -tailcolor to tail as -color

	constructor {args} {
	    set tail [tail fidotail -color red]
	}
    }

    dog fido

    fido configure -tailcolor
} -cleanup {
    dog destroy
    tail destroy
} -result {-tailcolor tailcolor Tailcolor black red}

test option-4.1 {local option name must begin with hyphen} -body {
    type dog {
	option nohyphen
    }
} -returnCodes {
    error
} -result {bad option name "nohyphen", options must start with a "-"}

test option-4.2 {local option name must be lower case} -body {
    type dog {
	option -Upper
    }
} -returnCodes {
    error
} -result {bad option name "-Upper" , options must not contain uppercase characters}

test option-4.3 {local option name may not contain spaces} -body {
    type dog {
	option {"-with space"}
    }
} -returnCodes {
    error
} -result {bad option name "-with space", option names must not contain " "}

test option-4.4 {delegated option name must begin with hyphen} -body {
    type dog {
	delegate option nohyphen to tail
    }
} -returnCodes {
    error
} -result {bad delegated option name "nohyphen", options must start with a "-"}

test option-4.5 {delegated option name must be lower case} -body {
    type dog {
	delegate option -Upper to tail
    }
} -returnCodes {
    error
} -result {bad option name "-Upper" , options must not contain uppercase characters}

test option-4.6 {delegated option name may not contain spaces} -body {
    type dog {
	delegate option {"-with space"} to tail
    }
} -returnCodes {
    error
} -result {bad option name "-with space", option names must not contain " "}

test option-6.1a {itcl_options variable is always there} -body {
    type dog {
	variable dummy
    }

    dog spot
    spot info vars itcl_options
} -cleanup {
    dog destroy
} -result {itcl_options}

test option-6.2 {if no options, no options methods} -body {
    type dog {
	variable dummy
    }

    dog spot
    spot info methods c*
} -cleanup {
    dog destroy
} -result {}

#-----------------------------------------------------------------------
# option -validatemethod

test validatemethod-1.1 {Validate method is called} -body {
    type dog {
	variable flag 0

	option -color \
	    -default black \
	    -validatemethod ValidateColor

	method ValidateColor {option value} {
	    set flag 1
	}

	method getflag {} {
	    return $flag
	}
    }

    dog fido -color brown
    fido getflag
} -cleanup {
    dog destroy
} -result {1}

test validatemethod-1.2 {Validate method gets correct arguments} -body {
    type dog {
	option -color \
	    -default black \
	    -validatemethod ValidateColor

	method ValidateColor {option value} {
	    if {![string equal $option "-color"] ||
		![string equal $value "brown"]} {
		error "Expected '-color brown'"
	    }
	}
    }

    dog fido -color brown
} -cleanup {
    dog destroy
} -result {::fido}

test validatemethod-1.4 {Invalid -validatemethod causes error} -body {
    type dog {
	option -foo -default bar -validatemethod bogus
    }

    dog fido
    fido configure -foo quux
} -returnCodes {
    error
} -cleanup {
    dog destroy
} -result {invalid command name "bogus"}

test validatemethod-1.5 {hierarchical -validatemethod} -body {
    type dog {
	option -foo -default bar -validatemethod {Val Opt}

	method {Val Opt} {option value} {
	    error "Dummy"
	}
    }

    dog fido -foo value
} -returnCodes {
    error
} -cleanup {
    dog destroy
} -result {Dummy}



#-----------------------------------------------------------------------
# option -readonly semantics

test optionreadonly-1.1 {Readonly options can be set at creation time} -body {
    type dog {
	option -color \
	    -default black \
	    -readonly true
    }

    dog fido -color brown

    fido cget -color
} -cleanup {
    dog destroy
} -result {brown}

test optionreadonly-1.2 {Readonly options can't be set after creation} -body {
    type dog {
	option -color \
	    -default black \
	    -readonly true
    }

    dog fido

    fido configure -color brown
} -returnCodes {
    error
} -cleanup {
    dog destroy
} -result {option "-color" can only be set at instance creation}

test optionreadonly-1.3 {Readonly options can't be set after creation} -body {
    type dog {
	option -color \
	    -default black \
	    -readonly true
    }

    dog fido -color yellow

    fido configure -color brown
} -returnCodes {
    error
} -cleanup {
    dog destroy
} -result {option "-color" can only be set at instance creation}


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

cleanupTests
return