Debian Codegraph @

tests/history.test

raw
1 # Commands covered: history
2 #
3 # This file contains a collection of tests for one or more of the Tcl built-in
4 # commands. Sourcing this file into Tcl runs the tests and generates output
5 # for errors. No output means no errors were found.
6 #
7 # Copyright (c) 1991-1993 The Regents of the University of California.
8 # Copyright (c) 1994 Sun Microsystems, Inc.
9 # Copyright (c) 1998-1999 by Scriptics Corporation.
10 #
11 # See the file "license.terms" for information on usage and redistribution of
12 # this file, and for a DISCLAIMER OF ALL WARRANTIES.
13
14 if {"::tcltest" ni [namespace children]} {
15 package require tcltest 2.5
16 namespace import -force ::tcltest::*
17 }
18
19 # The history command might be autoloaded...
20 if {[catch {history}]} {
21 testConstraint history 0
22 } else {
23 testConstraint history 1
24 }
25
26 if {[testConstraint history]} {
27 set num [history nextid]
28 history keep 3
29 history add {set a 12345}
30 history add {set b [format {A test %s} string]}
31 history add {Another test}
32 } else {
33 # Dummy value, must be numeric
34 set num 0
35 }
36
37 # "history event"
38
39 test history-1.1 {event option} history {history event -1} \
40 {set b [format {A test %s} string]}
41 test history-1.2 {event option} history {history event $num} \
42 {set a 12345}
43 test history-1.3 {event option} history {history event [expr {$num+2}]} \
44 {Another test}
45 test history-1.4 {event option} history {history event set} \
46 {set b [format {A test %s} string]}
47 test history-1.5 {event option} history {history e "* a*"} \
48 {set a 12345}
49 test history-1.6 {event option} history {catch {history event *gorp} msg} 1
50 test history-1.7 {event option} history {
51 catch {history event *gorp} msg
52 set msg
53 } {no event matches "*gorp"}
54 test history-1.8 {event option} history {history event} \
55 {set b [format {A test %s} string]}
56 test history-1.9 {event option} history {catch {history event 123 456} msg} 1
57 test history-1.10 {event option} -constraints history -body {
58 catch {history event 123 456} msg
59 set msg
60 } -match glob -result {wrong # args: should be "*history event \?event\?"}
61
62 # "history redo"
63
64 if {[testConstraint history]} {
65 set a 0
66 history redo -2
67 }
68 test history-2.1 {redo option} history {set a} 12345
69 if {[testConstraint history]} {
70 set b 0
71 history redo
72 }
73 test history-2.2 {redo option} history {set b} {A test string}
74 test history-2.3 {redo option} history {catch {history redo -3 -4}} 1
75 test history-2.4 {redo option} -constraints history -body {
76 catch {history redo -3 -4} msg
77 set msg
78 } -match glob -result {wrong # args: should be "*history redo \?event\?"}
79
80 # "history add"
81
82 if {[testConstraint history]} {
83 history add "set a 444" exec
84 }
85 test history-3.1 {add option} history {set a} 444
86 test history-3.2 {add option} history {catch {history add "set a 444" execGorp}} 1
87 test history-3.3 {add option} history {
88 catch {history add "set a 444" execGorp} msg
89 set msg
90 } {bad argument "execGorp": should be "exec"}
91 test history-3.4 {add option} history {catch {history add "set a 444" a} msg} 1
92 test history-3.5 {add option} history {
93 catch {history add "set a 444" a} msg
94 set msg
95 } {bad argument "a": should be "exec"}
96 if {[testConstraint history]} {
97 history add "set a 555" e
98 }
99 test history-3.6 {add option} history {set a} 555
100 if {[testConstraint history]} {
101 history add "set a 666"
102 }
103 test history-3.7 {add option} history {set a} 555
104 test history-3.8 {add option} history {catch {history add "set a 666" e f} msg} 1
105 test history-3.9 {add option} -constraints history -body {
106 catch {history add "set a 666" e f} msg
107 set msg
108 } -match glob -result {wrong # args: should be "*history add event \?exec\?"}
109
110 # "history change"
111
112 if {[testConstraint history]} {
113 history change "A test value"
114 }
115 test history-4.1 {change option} history {history event [expr {[history n]-1}]} \
116 "A test value"
117 if {[testConstraint history]} {
118 history ch "Another test" -1
119 }
120 test history-4.2 {change option} history {history e} "Another test"
121 test history-4.3 {change option} history {history event [expr {[history n]-1}]} \
122 "A test value"
123 test history-4.4 {change option} history {catch {history change Foo 4 10}} 1
124 test history-4.5 {change option} -constraints history -body {
125 catch {history change Foo 4 10} msg
126 set msg
127 } -match glob -result {wrong # args: should be "*history change newValue \?event\?"}
128 test history-4.6 {change option} history {
129 catch {history change Foo [expr {[history n]-4}]}
130 } 1
131 if {[testConstraint history]} {
132 set num [expr {[history n]-4}]
133 }
134 test history-4.7 {change option} history {
135 catch {history change Foo $num} msg
136 set msg
137 } "event \"$num\" is too far in the past"
138
139 # "history info"
140
141 if {[testConstraint history]} {
142 set num [history n]
143 history add set\ a\ {b\nc\ d\ e}
144 history add {set b 1234}
145 history add set\ c\ {a\nb\nc}
146 }
147 test history-5.1 {info option} history {history info} [format {%6d set a {b
148 c d e}
149 %6d set b 1234
150 %6d set c {a
151 b
152 c}} $num [expr {$num+1}] [expr {$num+2}]]
153 test history-5.2 {info option} history {history i 2} [format {%6d set b 1234
154 %6d set c {a
155 b
156 c}} [expr {$num+1}] [expr {$num+2}]]
157 test history-5.3 {info option} history {catch {history i 2 3}} 1
158 test history-5.4 {info option} -constraints history -body {
159 catch {history i 2 3} msg
160 set msg
161 } -match glob -result {wrong # args: should be "*history info \?count\?"}
162 test history-5.5 {info option} history {history} [format {%6d set a {b
163 c d e}
164 %6d set b 1234
165 %6d set c {a
166 b
167 c}} $num [expr {$num+1}] [expr {$num+2}]]
168
169 # "history keep"
170
171 if {[testConstraint history]} {
172 history add "foo1"
173 history add "foo2"
174 history add "foo3"
175 history keep 2
176 }
177 test history-6.1 {keep option} history {
178 history event [expr {[history n]-1}]
179 } foo3
180 test history-6.2 {keep option} history {history event -1} foo2
181 test history-6.3 {keep option} history {catch {history event -3}} 1
182 test history-6.4 {keep option} history {
183 catch {history event -3} msg
184 set msg
185 } {event "-3" is too far in the past}
186 if {[testConstraint history]} {
187 history k 5
188 }
189 test history-6.5 {keep option} history {history event -1} foo2
190 test history-6.6 {keep option} history {history event -2} {}
191 test history-6.7 {keep option} history {history event -3} {}
192 test history-6.8 {keep option} history {history event -4} {}
193 test history-6.9 {keep option} history {catch {history event -5}} 1
194 test history-6.10 {keep option} history {catch {history keep 4 6}} 1
195 test history-6.11 {keep option} -constraints history -body {
196 catch {history keep 4 6} msg
197 set msg
198 } -match glob -result {wrong # args: should be "*history keep \?count\?"}
199 test history-6.12 {keep option} history {catch {history keep}} 0
200 test history-6.13 {keep option} history {
201 history keep
202 } {5}
203 test history-6.14 {keep option} history {catch {history keep -3}} 1
204 test history-6.15 {keep option} history {
205 catch {history keep -3} msg
206 set msg
207 } {illegal keep count "-3"}
208 test history-6.16 {keep option} history {
209 catch {history keep butter} msg
210 set msg
211 } {illegal keep count "butter"}
212
213 # "history nextid"
214
215 if {[testConstraint history]} {
216 set num [history n]
217 history add "Testing"
218 history add "Testing2"
219 }
220 test history-7.1 {nextid option} history {history event} "Testing"
221 test history-7.2 {nextid option} history {history next} [expr {$num+2}]
222 test history-7.3 {nextid option} history {catch {history nextid garbage}} 1
223 test history-7.4 {nextid option} -constraints history -body {
224 catch {history nextid garbage} msg
225 set msg
226 } -match glob -result {wrong # args: should be "*history nextid"}
227
228 # "history clear"
229
230 if {[testConstraint history]} {
231 set num [history n]
232 history add "Testing"
233 history add "Testing2"
234 }
235 test history-8.1 {clear option} history {catch {history clear junk}} 1
236 test history-8.2 {clear option} history {history clear} {}
237 if {[testConstraint history]} {
238 history clear
239 history add "Testing"
240 }
241 test history-8.3 {clear option} history {history} { 1 Testing}
242
243 # miscellaneous
244
245 test history-9.1 {miscellaneous} history {catch {history gorp} msg} 1
246 test history-9.2 {miscellaneous} history {
247 catch {history gorp} msg
248 set msg
249 } {unknown or ambiguous subcommand "gorp": must be add, change, clear, event, info, keep, nextid, or redo}
250
251 # History retains references; Bug 1ae12987cb
252 test history-10.1 {references kept by history} -constraints history -setup {
253 interp create histtest
254 histtest eval {
255 # Trigger any autoloading that might be present
256 catch {history}
257 proc refcount {x} {
258 set rep [::tcl::unsupported::representation $x]
259 regexp {with a refcount of (\d+)} $rep -> rc
260 # Ignore the references due to calling this procedure
261 return [expr {$rc - 3}]
262 }
263 }
264 } -body {
265 histtest eval {
266 # A fresh object, refcount 1 from the variable we write it to
267 set obj [expr {rand()}]
268 set baseline [refcount $obj]
269 lappend result [refcount $obj]
270 history add [list list $obj]
271 lappend result [refcount $obj]
272 history clear
273 lappend result [refcount $obj]
274 }
275 } -cleanup {
276 interp delete histtest
277 } -result {1 2 1}
278 test history-10.2 {references kept by history} -constraints history -setup {
279 interp create histtest
280 histtest eval {
281 # Trigger any autoloading that might be present
282 catch {history}
283 proc refcount {x} {
284 set rep [::tcl::unsupported::representation $x]
285 regexp {with a refcount of (\d+)} $rep -> rc
286 # Ignore the references due to calling this procedure
287 return [expr {$rc - 3}]
288 }
289 }
290 } -body {
291 histtest eval {
292 # A fresh object, refcount 1 from the variable we write it to
293 set obj [expr {rand()}]
294 set baseline [refcount $obj]
295 lappend result [refcount $obj]
296 history add [list list $obj]
297 lappend result [refcount $obj]
298 rename history {}
299 lappend result [refcount $obj]
300 }
301 } -cleanup {
302 interp delete histtest
303 } -result {1 2 1}
304
305 # cleanup
306 ::tcltest::cleanupTests
307 return
308
309 # Local Variables:
310 # mode: tcl
311 # End: