aboutsummaryrefslogtreecommitdiff
path: root/testsuite/runtest.libs/default_procs.tcl
blob: 326b7891863c4c9eef6f7be7c34f7f6e994c217c (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
# Copyright (C) 1996-2019, 2020 Free Software Foundation, Inc.
#
# This file is part of DejaGnu.
#
# DejaGnu is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# DejaGnu 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 DejaGnu; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
set sum_file ""
set reboot 0
set errno ""

# this tests a proc for a returned pattern
proc lib_pat_test { cmd arglist pattern } {
    puts "CMD(lib_pat_test) is: $cmd $arglist"
    if { [catch { eval [list $cmd] [lrange $arglist 0 end] } result] == 0 } {
	puts "RESULT(lib_pat_test) was: \"${result}\"\
		for pattern \"$pattern\"."
	return [string match $pattern $result]
    } else {
	puts "RESULT(lib_pat_test) was error \"${result}\""
	return -1
    }
}

# this tests a proc for a returned regexp
proc lib_regexp_test { cmd arglist regexp } {
    puts "CMD(lib_regexp_test) is: $cmd $arglist"
    if { [catch { eval [list $cmd] [lrange $arglist 0 end] } result] == 0 } {
	puts "RESULT(lib_regexp_test) was: \"${result}\"\
		for regexp \"$regexp\"."
	return [regexp -- $regexp $result]
    } else {
	puts "RESULT(lib_regexp_test) was error \"${result}\""
	return -1
    }
}

# this tests a proc for a returned value
proc lib_ret_test { cmd arglist val } {
    puts "CMD(lib_ret_test) is: $cmd $arglist"
    if { [catch { eval [list $cmd] [lrange $arglist 0 end] } result] == 0 } {
	puts "RESULT(lib_ret_test) was: $result"
	return [string equal $result $val]
    } else {
	puts "RESULT(lib_ret_test) was error \"${result}\""
	return -1
    }
}

# this tests a proc for an expected boolean result
proc lib_bool_test { cmd arglist val } {
    puts "CMD(lib_bool_test) is: $cmd $arglist"
    if { [catch { eval [list $cmd] [lrange $arglist 0 end] } result] == 0 } {
	puts "RESULT(lib_bool_test) was: \"$result\" expecting $val."
	# the "odd" spacing is used to help make the operator grouping clear
	return [expr {  $val  ?   $result ? 1 : 0   :   $result ? 0 : 1   }]
    } else {
	puts "RESULT(lib_bool_test) was error \"${result}\""
	return -1
    }
}

# this tests that a proc raises an error matching a pattern
proc lib_errpat_test { cmd arglist pattern } {
    puts "CMD(lib_errpat_test) is: $cmd $arglist"
    if { [catch { eval [list $cmd] [lrange $arglist 0 end] } result] == 1 } {
	# caught exception code 1 (TCL_ERROR) as expected
	puts "RESULT(lib_errpat_test) was error\
		\"${result}\" for pattern \"$pattern\"."
	if { [string match $pattern $result] } {
	    # the expected error
	    return 1
	} else {
	    # an unexpected error
	    return -1
	}
    } else {
	# no error -> fail
	puts "RESULT(lib_errpat_test) was: \"${result}\"\
		without error; failing."
	return 0
    }
}

# this tests that a proc raises an error matching a regexp
proc lib_errregexp_test { cmd arglist regexp } {
    puts "CMD(lib_errregexp_test) is: $cmd $arglist"
    if { [catch { eval [list $cmd] [lrange $arglist 0 end] } result] == 1 } {
	# caught exception code 1 (TCL_ERROR) as expected
	puts "RESULT(lib_errregexp_test) was error\
		\"${result}\" for regexp \"$regexp\"."
	if { [regexp -- $regexp $result] } {
	    # the expected error
	    return 1
	} else {
	    # an unexpected error
	    return -1
	}
    } else {
	# no error -> fail
	puts "RESULT(lib_errregexp_test) was: \"${result}\"\
		without error; failing."
	return 0
    }
}

# this tests that a proc raises an error matching an exact string
proc lib_err_test { cmd arglist val } {
    puts "CMD(lib_err_test) is: $cmd $arglist"
    if { [catch { eval [list $cmd] [lrange $arglist 0 end] } result] == 1 } {
	# caught exception code 1 (TCL_ERROR) as expected
	puts "RESULT(lib_err_test) was error: $result"
	if { $result eq $val } {
	    # the expected error
	    return 1
	} else {
	    # an unexpected error
	    return -1
	}
    } else {
	# no error -> fail
	puts "RESULT(lib_err_test) was: \"${result}\"\
		without error; failing."
	return 0
    }
}

# support for testing output procs
proc clear_test_output {} {
    global test_output

    array unset test_output
    array set test_output { error {} log {} tty {} user {} }
}

proc store_test_output { dest argv } {
    global test_output

    set argc [llength $argv]
    for { set argi 0 } { $argi < $argc } { incr argi } {
	set arg [lindex $argv $argi]
	if { $arg eq "--" } {
	    set stri [expr $argi + 1]
	    break
	} elseif { ![string match "-*" $arg] } {
	    set stri $argi
	}
    }
    # the string must be the last argument
    if { $stri != ($argc - 1) } {
	error "bad call: send_${dest} $argv"
    }
    append test_output($dest) [lindex $argv $stri]
}
foreach dest { error log tty user } {
    proc send_${dest} { args } [concat store_test_output $dest {$args}]
}

# this checks output against VAL, which is a list of key-value pairs
#  each key specifies an output channel (from { error log tty user }) and a
#  matching mode (from { "", pat, re }) separated by "_" unless mode is ""
proc lib_output_test { cmd arglist val } {
    global test_output

    puts "CMD(lib_output_test) is: $cmd $arglist"
    clear_test_output
    if { ([llength $val] % 2) != 0 } {
	puts "ERROR(lib_output_test): expected result is invalid"
	return -1
    }
    if { [catch { eval [list $cmd] [lrange $arglist 0 end] } result] == 0 } {
	puts "RESULT(lib_output_test) was: $result"
	foreach dest { error log tty user } {
	    puts "OUTPUT(lib_output_test/$dest) was: <<$test_output($dest)>>"
	}
    } else {
	puts "RESULT(lib_output_test) was error \"${result}\""
	return -1
    }
    foreach { check expected } $val {
	if { [regexp {(error|log|tty|user)(?:_(pat|re))?} $check\
		  -> dest mode] != 1 } {
	    puts "ERROR(lib_output_test): unknown check token: $check"
	    return -1
	}
	switch -- $mode {
	    "" {
		if { ![string equal $expected $test_output($dest)] } {
		    return 0
		}
	    }
	    pat {
		if { ![string match $expected $test_output($dest)] } {
		    return 0
		}
	    }
	    re {
		if { ![regexp -- $expected $test_output($dest)] } {
		    return 0
		}
	    }
	}
    }
    # if we get here, all checks have passed
    return 1
}

#
# This runs a standard test for a proc. The list is set up as:
# |test proc|proc being tested|args|pattern|message|
# test proc is something like lib_pat_test or lib_ret_test.
#
proc run_tests { tests } {
    foreach test $tests {
	# skip comments in test lists
	if { [lindex $test 0] eq "#" } { continue }
	set result [eval [lrange $test 0 3]]
	switch -- $result {
	    "-1" {
		puts "ERRORED: [lindex $test 4]"
	    }
	    "1" {
		puts "PASSED: [lindex $test 4]"
	    }
	    "0" {
		puts "FAILED: [lindex $test 4]"
	    }
	    default {
		puts "BAD VALUE: [lindex $test 4]"
	    }
	}
    }
}

proc pass { msg } {
    puts "PASSED: $msg"
}

proc fail { msg } {
    puts "FAILED: $msg"
}

proc perror { msg } {
    global errno
    puts "ERRORED: $msg"
    set errno $msg
}

proc warning { msg } {
    global errno
    puts "WARNED: $msg"
    set errno $msg
}

proc untested { msg } {
    puts "NOTTESTED: $msg"
}

proc unsupported { msg } {
    puts "NOTSUPPORTED: $msg"
}
proc verbose { args } {
    puts [lindex $args 0]
}