]>
Commit | Line | Data |
---|---|---|
6f8a32d5 PN |
1 | set ::num_tests 0 |
2 | set ::num_passed 0 | |
3 | set ::num_failed 0 | |
4 | set ::tests_failed {} | |
98578b57 | 5 | |
c2ff0e90 PN |
6 | proc assert {condition} { |
7 | if {![uplevel 1 expr $condition]} { | |
6f8a32d5 | 8 | error "assertion:Expected '$value' to be true" |
c2ff0e90 PN |
9 | } |
10 | } | |
11 | ||
d4507ec6 PN |
12 | proc assert_match {pattern value} { |
13 | if {![string match $pattern $value]} { | |
6f8a32d5 | 14 | error "assertion:Expected '$value' to match '$pattern'" |
d4507ec6 PN |
15 | } |
16 | } | |
17 | ||
18 | proc assert_equal {expected value} { | |
19 | if {$expected ne $value} { | |
6f8a32d5 | 20 | error "assertion:Expected '$value' to be equal to '$expected'" |
d4507ec6 PN |
21 | } |
22 | } | |
23 | ||
24 | proc assert_error {pattern code} { | |
5eedc9c6 | 25 | if {[catch {uplevel 1 $code} error]} { |
d4507ec6 PN |
26 | assert_match $pattern $error |
27 | } else { | |
6f8a32d5 | 28 | error "assertion:Expected an error but nothing was catched" |
d4507ec6 PN |
29 | } |
30 | } | |
31 | ||
32 | proc assert_encoding {enc key} { | |
86d39249 PN |
33 | # Swapped out values don't have an encoding, so make sure that |
34 | # the value is swapped in before checking the encoding. | |
35 | set dbg [r debug object $key] | |
68254919 PN |
36 | while {[string match "* swapped at:*" $dbg]} { |
37 | r debug swapin $key | |
86d39249 PN |
38 | set dbg [r debug object $key] |
39 | } | |
40 | assert_match "* encoding:$enc *" $dbg | |
d4507ec6 PN |
41 | } |
42 | ||
43 | proc assert_type {type key} { | |
44 | assert_equal $type [r type $key] | |
45 | } | |
46 | ||
7982173f | 47 | # This is called before starting the test |
48 | proc announce_test {s} { | |
49 | if {[info exists ::env(TERM)] && [string match $::env(TERM) xterm]} { | |
50 | puts -nonewline "$s\033\[0K" | |
51 | flush stdout | |
52 | set ::backward_count [string length $s] | |
53 | } | |
54 | } | |
55 | ||
56 | # This is called after the test finished | |
eae9cce1 | 57 | proc colored_dot {tags passed} { |
58 | if {[info exists ::env(TERM)] && [string match $::env(TERM) xterm]} { | |
7982173f | 59 | # Go backward and delete what announc_test function printed. |
60 | puts -nonewline "\033\[${::backward_count}D\033\[0K\033\[J" | |
61 | ||
62 | # Print a coloured char, accordingly to test outcome and tags. | |
eae9cce1 | 63 | if {[lsearch $tags list] != -1} { |
64 | set colorcode {31} | |
65 | set ch L | |
66 | } elseif {[lsearch $tags hash] != -1} { | |
67 | set colorcode {32} | |
68 | set ch H | |
69 | } elseif {[lsearch $tags set] != -1} { | |
70 | set colorcode {33} | |
71 | set ch S | |
72 | } elseif {[lsearch $tags zset] != -1} { | |
73 | set colorcode {34} | |
74 | set ch Z | |
75 | } elseif {[lsearch $tags basic] != -1} { | |
76 | set colorcode {35} | |
77 | set ch B | |
78 | } else { | |
79 | set colorcode {37} | |
80 | set ch . | |
81 | } | |
82 | if {$colorcode ne {}} { | |
83 | if {$passed} { | |
84 | puts -nonewline "\033\[0;${colorcode};40m" | |
85 | } else { | |
180e07b8 | 86 | puts -nonewline "\033\[7;${colorcode};40m" |
eae9cce1 | 87 | } |
88 | puts -nonewline $ch | |
89 | puts -nonewline "\033\[0m" | |
90 | flush stdout | |
91 | } | |
92 | } else { | |
93 | if {$passed} { | |
94 | puts -nonewline . | |
95 | } else { | |
96 | puts -nonewline F | |
97 | } | |
98 | } | |
99 | } | |
100 | ||
6f8a32d5 | 101 | proc test {name code {okpattern undefined}} { |
6e0e5bed PN |
102 | # abort if tagged with a tag to deny |
103 | foreach tag $::denytags { | |
104 | if {[lsearch $::tags $tag] >= 0} { | |
105 | return | |
106 | } | |
107 | } | |
108 | ||
109 | # check if tagged with at least 1 tag to allow when there *is* a list | |
110 | # of tags to allow, because default policy is to run everything | |
111 | if {[llength $::allowtags] > 0} { | |
112 | set matched 0 | |
113 | foreach tag $::allowtags { | |
73bd6c58 | 114 | if {[lsearch $::tags $tag] >= 0} { |
6e0e5bed PN |
115 | incr matched |
116 | } | |
117 | } | |
118 | if {$matched < 1} { | |
119 | return | |
120 | } | |
121 | } | |
122 | ||
6f8a32d5 PN |
123 | incr ::num_tests |
124 | set details {} | |
125 | lappend details $::curfile | |
126 | lappend details $::tags | |
127 | lappend details $name | |
128 | ||
129 | if {$::verbose} { | |
130 | puts -nonewline [format "#%03d %-68s " $::num_tests $name] | |
131 | flush stdout | |
7982173f | 132 | } else { |
133 | announce_test $name | |
6f8a32d5 PN |
134 | } |
135 | ||
fdfb02e7 | 136 | if {[catch {set retval [uplevel 1 $code]} error]} { |
6f8a32d5 PN |
137 | if {[string match "assertion:*" $error]} { |
138 | set msg [string range $error 10 end] | |
139 | lappend details $msg | |
140 | lappend ::tests_failed $details | |
141 | ||
142 | incr ::num_failed | |
143 | if {$::verbose} { | |
144 | puts "FAILED" | |
145 | puts "$msg\n" | |
146 | } else { | |
eae9cce1 | 147 | colored_dot $::tags 0 |
6f8a32d5 | 148 | } |
d4507ec6 | 149 | } else { |
6f8a32d5 PN |
150 | # Re-raise, let handler up the stack take care of this. |
151 | error $error $::errorInfo | |
d4507ec6 | 152 | } |
98578b57 | 153 | } else { |
6f8a32d5 PN |
154 | if {$okpattern eq "undefined" || $okpattern eq $retval || [string match $okpattern $retval]} { |
155 | incr ::num_passed | |
156 | if {$::verbose} { | |
157 | puts "PASSED" | |
158 | } else { | |
eae9cce1 | 159 | colored_dot $::tags 1 |
6f8a32d5 | 160 | } |
d4507ec6 | 161 | } else { |
6f8a32d5 PN |
162 | set msg "Expected '$okpattern' to equal or match '$retval'" |
163 | lappend details $msg | |
164 | lappend ::tests_failed $details | |
165 | ||
166 | incr ::num_failed | |
167 | if {$::verbose} { | |
168 | puts "FAILED" | |
169 | puts "$msg\n" | |
170 | } else { | |
eae9cce1 | 171 | colored_dot $::tags 0 |
6f8a32d5 | 172 | } |
d4507ec6 | 173 | } |
98578b57 | 174 | } |
6f8a32d5 PN |
175 | flush stdout |
176 | ||
98578b57 | 177 | if {$::traceleaks} { |
5b12b47d PN |
178 | set output [exec leaks redis-server] |
179 | if {![string match {*0 leaks*} $output]} { | |
6f8a32d5 | 180 | puts "--- Test \"$name\" leaked! ---" |
5b12b47d | 181 | puts $output |
98578b57 PN |
182 | exit 1 |
183 | } | |
184 | } | |
185 | } |