1 # Copyright (C) 1996-2019, 2020 Free Software Foundation, Inc.
3 # This file is part of DejaGnu.
5 # DejaGnu is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # DejaGnu is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with DejaGnu. If not, see <http://www.gnu.org/licenses/>.
21 # this tests a proc for a returned pattern
22 proc lib_pat_test
{ cmd arglist pattern
} {
23 puts "CMD(lib_pat_test) is: $cmd $arglist"
24 if { [catch { eval [list $cmd] [lrange $arglist 0 end
] } result
] == 0 } {
25 puts "RESULT(lib_pat_test) was: \"${result}\"\
26 for pattern \"$pattern\"."
27 return [string match
$pattern $result]
29 puts "RESULT(lib_pat_test) was error \"${result}\""
34 # this tests a proc for a returned regexp
35 proc lib_regexp_test
{ cmd arglist
regexp } {
36 puts "CMD(lib_regexp_test) is: $cmd $arglist"
37 if { [catch { eval [list $cmd] [lrange $arglist 0 end
] } result
] == 0 } {
38 puts "RESULT(lib_regexp_test) was: \"${result}\"\
39 for regexp \"$regexp\"."
40 return [regexp -- $regexp $result]
42 puts "RESULT(lib_regexp_test) was error \"${result}\""
47 # this tests a proc for a returned value
48 proc lib_ret_test
{ cmd arglist val
} {
49 puts "CMD(lib_ret_test) is: $cmd $arglist"
50 if { [catch { eval [list $cmd] [lrange $arglist 0 end
] } result
] == 0 } {
51 puts "RESULT(lib_ret_test) was: $result"
52 return [string equal
$result $val]
54 puts "RESULT(lib_ret_test) was error \"${result}\""
59 # this tests a proc for an expected boolean result
60 proc lib_bool_test
{ cmd arglist val
} {
61 puts "CMD(lib_bool_test) is: $cmd $arglist"
62 if { [catch { eval [list $cmd] [lrange $arglist 0 end
] } result
] == 0 } {
63 puts "RESULT(lib_bool_test) was: \"$result\" expecting $val."
64 # the "odd" spacing is used to help make the operator grouping clear
65 return [expr { $val ?
$result ?
1 : 0 : $result ?
0 : 1 }]
67 puts "RESULT(lib_bool_test) was error \"${result}\""
72 # this tests that a proc raises an error matching a pattern
73 proc lib_errpat_test
{ cmd arglist pattern
} {
74 puts "CMD(lib_errpat_test) is: $cmd $arglist"
75 if { [catch { eval [list $cmd] [lrange $arglist 0 end
] } result
] == 1 } {
76 # caught exception code 1 (TCL_ERROR) as expected
77 puts "RESULT(lib_errpat_test) was error\
78 \"${result}\" for pattern \"$pattern\"."
79 if { [string match
$pattern $result] } {
88 puts "RESULT(lib_errpat_test) was: \"${result}\"\
89 without error; failing."
94 # this tests that a proc raises an error matching a regexp
95 proc lib_errregexp_test
{ cmd arglist
regexp } {
96 puts "CMD(lib_errregexp_test) is: $cmd $arglist"
97 if { [catch { eval [list $cmd] [lrange $arglist 0 end
] } result
] == 1 } {
98 # caught exception code 1 (TCL_ERROR) as expected
99 puts "RESULT(lib_errregexp_test) was error\
100 \"${result}\" for regexp \"$regexp\"."
101 if { [regexp -- $regexp $result] } {
105 # an unexpected error
110 puts "RESULT(lib_errregexp_test) was: \"${result}\"\
111 without error; failing."
116 # this tests that a proc raises an error matching an exact string
117 proc lib_err_test
{ cmd arglist val
} {
118 puts "CMD(lib_err_test) is: $cmd $arglist"
119 if { [catch { eval [list $cmd] [lrange $arglist 0 end
] } result
] == 1 } {
120 # caught exception code 1 (TCL_ERROR) as expected
121 puts "RESULT(lib_err_test) was error: $result"
122 if { $result eq
$val } {
126 # an unexpected error
131 puts "RESULT(lib_err_test) was: \"${result}\"\
132 without error; failing."
137 # support for testing output procs
138 proc clear_test_output
{} {
141 array unset test_output
142 array set test_output
{ error {} log
{} tty
{} user
{} }
145 proc store_test_output
{ dest argv
} {
148 set argc
[llength $argv]
149 for { set argi
0 } { $argi < $argc } { incr argi
} {
150 set arg
[lindex $argv $argi]
151 if { $arg eq
"--" } {
152 set stri
[expr $argi + 1]
154 } elseif
{ ![string match
"-*" $arg] } {
158 # the string must be the last argument
159 if { $stri != ($argc - 1) } {
160 error "bad call: send_${dest} $argv"
162 append test_output
($dest) [lindex $argv $stri]
164 foreach dest
{ error log tty user
} {
165 proc send_
${dest
} { args
} [concat store_test_output
$dest {$args}]
168 # this checks output against VAL, which is a list of key-value pairs
169 # each key specifies an output channel (from { error log tty user }) and a
170 # matching mode (from { "", pat, re }) separated by "_" unless mode is ""
171 proc lib_output_test
{ cmd arglist val
} {
174 puts "CMD(lib_output_test) is: $cmd $arglist"
176 if { ([llength $val] % 2) != 0 } {
177 puts "ERROR(lib_output_test): expected result is invalid"
180 if { [catch { eval [list $cmd] [lrange $arglist 0 end
] } result
] == 0 } {
181 puts "RESULT(lib_output_test) was: $result"
182 foreach dest
{ error log tty user
} {
183 puts "OUTPUT(lib_output_test/$dest) was: <<$test_output($dest)>>"
186 puts "RESULT(lib_output_test) was error \"${result}\""
189 foreach { check expected
} $val {
190 if { [regexp {(error|log|tty|user
)(?
:_
(pat|re
))?
} $check\
191 -> dest mode
] != 1 } {
192 puts "ERROR(lib_output_test): unknown check token: $check"
197 if { ![string equal
$expected $test_output($dest)] } {
202 if { ![string match
$expected $test_output($dest)] } {
207 if { ![regexp -- $expected $test_output($dest)] } {
213 # if we get here, all checks have passed
218 # This runs a standard test for a proc. The list is set up as:
219 # |test proc|proc being tested|args|pattern|message|
220 # test proc is something like lib_pat_test or lib_ret_test.
222 proc run_tests
{ tests
} {
223 foreach test
$tests {
224 # skip comments in test lists
225 if { [lindex $test 0] eq
"#" } { continue }
226 set result
[eval [lrange $test 0 3]]
229 puts "ERRORED: [lindex $test 4]"
232 puts "PASSED: [lindex $test 4]"
235 puts "FAILED: [lindex $test 4]"
238 puts "BAD VALUE: [lindex $test 4]"
252 proc perror
{ msg
} {
258 proc warning
{ msg
} {
264 proc untested
{ msg
} {
265 puts "NOTTESTED: $msg"
268 proc unsupported
{ msg
} {
269 puts "NOTSUPPORTED: $msg"
271 proc verbose
{ args
} {
272 puts [lindex $args 0]