summaryrefslogtreecommitdiff
path: root/tix/tests/Driver.tcl
blob: 5f5372a138e9dc80eaadd9509b9f8bb5d77e0b22 (plain)
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
# This is the "Test Driver" program that sources in each test script. It
# must be invoked by the test/Test.tcl program (in Unix) or by a properly
# configured wish.exe program (in Wondows).
#

catch {
    cd [file dirname [info script]]
}

set oldglobals {}
set oldglobals [info globals]

# Some parts of the test execute tests for a specific platform. The variable
# tixPriv(test:platform) controls the tests for which platform should
# be executed. This can be controlled by the TEST_PLATFORM environment
# variable 

set tixPriv(test:platform) unix
if [info exists tcl_platform(platform)] {
    if {$tcl_platform(platform) == "windows"} {
	set tixPriv(test:platform) windows
    }
}

if [info exists env(TEST_PLATFORM)] {
    set tixPriv(test:platform) $env(TEST_PLATFORM)
}

global testConfig
if {![info exists tix]} {
    if ![info exists tcl_platform(platform)] {
	puts "ERROR: this version of wish doesn't support dynamic loading"
	exit -1
    }

    # This must have been executed by a plain wish expecting to
    # dynamic load Tix.

    puts -nonewline "trying to dynamically load Tix ... "

    global tk_version
    if {$tcl_platform(platform) == "unix"} {
	case $tk_version {
	    4.1 {
		set testConfig(dynlib) \
		    ../unix/tk4.1/libtix4.1.4.1[info sharedlibextension]
	    }
	    4.2 {
		set testConfig(dynlib) \
		    ../unix/tk4.2/libtix4.1.4.2[info sharedlibextension]
	    }
	}
    } else {
	case $tk_version {
	    4.1 {
		set testConfig(dynlib) ..\\win\\tix41.dll
	    }
	    4.2 {
		set testConfig(dynlib) ..\\win\\tix41.dll
	    }
	}
    }

    if [info exists testConfig(dynlib)] {
	load $testConfig(dynlib) Tix
    }

    if {[info exists tix]} {
	puts succeeded
    } else {
	puts failed
	exit
    }
} else {
    set testConfig(dynlib) ""
}

proc Driver:Test {name f} {
    global oldglobals errorInfo testConfig

    foreach w [winfo children .] {
	if [string comp .__top $w] {
	    destroy $w
	}
    }
	
    foreach g [info globals] {
	if {[lsearch $oldglobals $g] == -1} {
#	    uplevel #0 unset $g
	}
    }

    if {$testConfig(VERBOSE) >= 20} {
	puts ------------------------------------------------------------
	puts "Loading script $name"
    } else {
	puts $name
    }

    update
    uplevel #0 source $f
    Event-Initialize
    catch {
	wm title . [About]
	if {$testConfig(VERBOSE) >= 20} {
	    puts "  [About]"
	    puts "---------------------starting-------------------------------"
	}
    }

    set code [catch {
	Test
    } error]

    if $code {
	if {$code == 1234} {
	    puts -nonewline "Test $f is aborted"
	} else {
	    puts -nonewline "Test $f is aborted unexpectedly"
	}
	if {[info exists errorInfo] && ![tixStrEq $errorInfo ""]} {
	    puts " by the following error\n$errorInfo"
	} else {
	    puts "."
	}
    }
    Done
}

# fileList: name of the file that contains a list of test targets
# type: "dir" or "script"
#
proc Driver:GetTargets {fileList type} {
    set fd [open $fileList {RDONLY}]
    set data {}

    while {![eof $fd]} {
	set line [string trim [gets $fd]]
	if [regexp ^# $line] {
	    continue
	}
	append data $line\n
    }

    close $fd
    set files {}

    foreach item $data {
	set takeit 1

	foreach cond [lrange $item 1 end] {
	    set inverse 0
	    set cond [string trim $cond]
	    if {[string index $cond 0] == "!"} {
		set cond [string range $cond 1 end]
		set inverse 1
	    }

	    set true 1
	    case [lindex $cond 0] {
		c {
		    set cmd [lindex $cond 1]
		    if {[info command $cmd] != $cmd} {
			if ![auto_load $cmd] {
			    set true 0
			}
		    }
		}
		i {
		    if {[lsearch [image types] [lindex $cond 1]] == -1} {
			set true 0
		    }
		}
		v {
		    set var [lindex $cond 1]
		    if ![uplevel #0 info exists [list $var]] {
			set true 0
		    }
		}
		default {
		    # must be an expression
		    #
		    if ![uplevel #0 expr [list $cond]] {
			set true 0
		    }
		}
	    }

	    if {$inverse} {
		set true [expr !$true]
	    }
	    if {!$true} {
		set takeit 0
		break
	    }
	}

	if {$takeit} {
	    lappend files [lindex $item 0]
	}
    }
    return $files
}

proc Driver:Main {} {
    global argv env

    if [tixStrEq $argv "dont"] {
	return
    }

    set argvfiles  $argv
    set env(WAITTIME) 200

    set errCount 0

    set PWD [pwd]
    if {$argvfiles == {}} {
	set argvfiles [Driver:GetTargets files dir]
    }

    foreach f $argvfiles {
	Driver:Execute $f
	cd $PWD
    }
}

proc Driver:Execute {f} {
    global testConfig

    if [file isdir $f] {
	raise .
	set dir $f

	if {$testConfig(VERBOSE) >= 20} {
	    puts "Entering directory $dir ..."
	}
	cd $dir

	if [file exists pkginit.tcl] {
	    # call the package initialization file, which is
	    # something specific to the files in this directory
	    #
	    source pkginit.tcl
	}
	foreach f [Driver:GetTargets files script] {
	    set _PWD [pwd]
	    Driver:Test $dir/$f $f
	    cd $_PWD
	}
	if {$testConfig(VERBOSE) >= 20} {
	    puts "Leaving directory $dir ..."
	}
    } else {
	set dir [file dirname $f]
	if {$dir != {}} {
	    if {$testConfig(VERBOSE) >= 20} {
		puts "Entering directory $dir ..."
	    }
	    cd $dir
	    if [file exists pkginit.tcl] {
		# call the package initialization file, which is
		# something specific to the files in this directory
		#
		source pkginit.tcl
	    }
	    set f [file tail $f]
	}
	set _PWD [pwd]
	Driver:Test $f $f
	cd $_PWD

	if {$testConfig(VERBOSE) >= 20} {
	    puts "Leaving directory $dir ..."
	}
    }
}

if [tixStrEq [tix platform] "windows"] {
    # The following are a bunch of useful functions to make it more convenient
    # to run the tests on Windows inside the Tix console window.
    #

    # do --
    #
    #	Execute a test.
    #
    proc do {f} {
	set PWD [pwd]
	Driver:Execute $f
	cd $PWD
	puts "% "
    }

    # rnew --
    #
    # 	Read in all the files in the Tix library path that has been modified.
    #
    proc rnew {} {
	global lastModified filesPatterns
	foreach file [eval glob $filesPatterns] {
	    set mtime [file mtime $file]
	    if {$lastModified < $mtime} {
		set lastModified $mtime
		puts "sourcing $file"
		uplevel #0 source [list $file]
	    }
	}
    }
    
    # pk --
    #
    #	pack widgets filled and expanded
    proc pk {args} {
	eval pack $args -expand yes -fill both
    }

    # Initialize the lastModified so that rnew only loads in newly modified
    # files
    #
    set filesPatterns {../library/*.tcl Driver.tcl library/*.tcl}
    set lastModified 0
    foreach file [eval glob $filesPatterns] {
	set mtime [file mtime $file]
	if {$lastModified < $mtime} {
	    set lastModified $mtime
	}
    }

    proc ei {} {
	global errorInfo
	puts $errorInfo
    }
}


uplevel #0 source library/TestLib.tcl
uplevel #0 source library/CaseData.tcl
wm title . "Test-driving Tix"
Driver:Main

puts "$testConfig(errCount) error(s) found"

if {[tix platform] != "windows"} {
    destroy .
    catch {
	update
    }
    exit 0
} else {
    puts -nonewline "type \"exit\" to quit the test\n% "
    proc q {} {
	exit
    }
}