Debian Codegraph @

tests/init.test

raw
1 # Functionality covered: this file contains a collection of tests for the auto
2 # loading and namespaces.
3 #
4 # Sourcing this file into Tcl runs the tests and generates output for errors.
5 # No output means no errors were found.
6 #
7 # Copyright (c) 1997 Sun Microsystems, Inc.
8 # Copyright (c) 1998-1999 Scriptics Corporation.
9 #
10 # See the file "license.terms" for information on usage and redistribution of
11 # this file, and for a DISCLAIMER OF ALL WARRANTIES.
12
13 if {"::tcltest" ni [namespace children]} {
14 package require tcltest 2.3.4
15 namespace import -force ::tcltest::*
16 }
17
18 # Clear out any namespaces called test_ns_*
19 catch {namespace delete {*}[namespace children :: test_ns_*]}
20
21 test init-0.1 {no error on initialization phase (init.tcl)} -setup {
22 interp create child
23 } -body {
24 child eval {
25 list [set v [info exists ::errorInfo]] \
26 [if {$v} {set ::errorInfo}] \
27 [set v [info exists ::errorCode]] \
28 [if {$v} {set ::errorCode}]
29 }
30 } -cleanup {
31 interp delete child
32 } -result {0 {} 0 {}}
33
34 test init-0.2 {no init.tcl from empty tcl_library, bug [43c94f95988f3057]} -setup {
35 cd [makeDirectory tmp]
36 makeFile {set ::TEST_INIT 1} init.tcl [pwd]
37 unset -nocomplain org_tcl_lib
38 if {[info exists ::env(TCL_LIBRARY)]} {
39 set org_tcl_lib $::env(TCL_LIBRARY)
40 }
41 set res [file exists [file join [pwd] init.tcl]]
42 } -body {
43 # first without tcl_library set:
44 interp create child
45 lappend res [child eval {info exists ::TEST_INIT}]; # must be 0
46 interp delete child
47 # then with current directory as tcl_library:
48 set ::env(TCL_LIBRARY) .
49 interp create child
50 lappend res [child eval {info exists ::TEST_INIT}]; # must be 1
51 interp delete child
52 set res
53 } -cleanup {
54 if {[info exists org_tcl_lib]} {
55 set ::env(TCL_LIBRARY) $org_tcl_lib
56 unset org_tcl_lib
57 } else {
58 unset -nocomplain ::env(TCL_LIBRARY)
59 }
60 removeFile init.tcl [pwd]
61 cd [workingDirectory]
62 removeDirectory tmp
63 unset -nocomplain res
64 catch { interp delete child }
65 } -result {1 0 1}
66
67 # Six cases - white box testing
68
69 test init-1.1 {auto_qualify - absolute cmd - namespace} {
70 auto_qualify ::foo::bar ::blue
71 } ::foo::bar
72 test init-1.2 {auto_qualify - absolute cmd - global} {
73 auto_qualify ::global ::sub
74 } global
75 test init-1.3 {auto_qualify - no colons cmd - global} {
76 auto_qualify nocolons ::
77 } nocolons
78 test init-1.4 {auto_qualify - no colons cmd - namespace} {
79 auto_qualify nocolons ::sub
80 } {::sub::nocolons nocolons}
81 test init-1.5 {auto_qualify - colons in cmd - global} {
82 auto_qualify foo::bar ::
83 } ::foo::bar
84 test init-1.6 {auto_qualify - colons in cmd - namespace} {
85 auto_qualify foo::bar ::sub
86 } {::sub::foo::bar ::foo::bar}
87 # Some additional tests
88 test init-1.7 {auto_qualify - multiples colons 1} {
89 auto_qualify :::foo::::bar ::blue
90 } ::foo::bar
91 test init-1.8 {auto_qualify - multiple colons 2} {
92 auto_qualify :::foo ::bar
93 } foo
94
95 # We use a child interp and auto_reset and double the tests because there is 2
96 # places where auto_loading occur (before loading the indexes files and after)
97
98 set testInterp [interp create]
99 tcltest::loadIntoChildInterpreter $testInterp {*}$argv
100 interp eval $testInterp {
101 namespace import -force ::tcltest::*
102 customMatch pairwise {apply {{mode pair} {
103 if {[llength $pair] != 2} {error "need a pair of values to check"}
104 string $mode [lindex $pair 0] [lindex $pair 1]
105 }}}
106
107 auto_reset
108 catch {rename parray {}}
109
110 test init-2.0 {load parray - stage 1} -body {
111 parray
112 } -returnCodes error -cleanup {
113 rename parray {} ;# remove it, for the next test - that should not fail.
114 } -result {wrong # args: should be "parray a ?pattern?"}
115 test init-2.1 {load parray - stage 2} -body {
116 parray
117 } -returnCodes error -result {wrong # args: should be "parray a ?pattern?"}
118 auto_reset
119 catch {rename ::safe::setLogCmd {}}
120 #unset -nocomplain auto_index(::safe::setLogCmd) auto_oldpath
121 test init-2.2 {load ::safe::setLogCmd - stage 1} {
122 ::safe::setLogCmd
123 rename ::safe::setLogCmd {} ;# should not fail
124 } {}
125 test init-2.3 {load ::safe::setLogCmd - stage 2} {
126 ::safe::setLogCmd
127 rename ::safe::setLogCmd {} ;# should not fail
128 } {}
129 auto_reset
130 catch {rename ::safe::setLogCmd {}}
131 test init-2.4 {load safe:::setLogCmd - stage 1} {
132 safe:::setLogCmd ;# intentionally 3 :
133 rename ::safe::setLogCmd {} ;# should not fail
134 } {}
135 test init-2.5 {load safe:::setLogCmd - stage 2} {
136 safe:::setLogCmd ;# intentionally 3 :
137 rename ::safe::setLogCmd {} ;# should not fail
138 } {}
139 auto_reset
140 catch {rename ::safe::setLogCmd {}}
141 test init-2.6 {load setLogCmd from safe:: - stage 1} {
142 namespace eval safe setLogCmd
143 rename ::safe::setLogCmd {} ;# should not fail
144 } {}
145 test init-2.7 {oad setLogCmd from safe:: - stage 2} {
146 namespace eval safe setLogCmd
147 rename ::safe::setLogCmd {} ;# should not fail
148 } {}
149 test init-2.8 {load tcl::HistAdd} -setup {
150 auto_reset
151 catch {rename ::tcl::HistAdd {}}
152 } -body {
153 # 3 ':' on purpose
154 tcl:::HistAdd
155 } -returnCodes error -cleanup {
156 rename ::tcl::HistAdd {}
157 } -result {wrong # args: should be "tcl:::HistAdd event ?exec?"}
158
159 test init-3.0 {random stuff in the auto_index, should still work} {
160 set auto_index(foo:::bar::blah) {
161 namespace eval foo {namespace eval bar {proc blah {} {return 1}}}
162 }
163 foo:::bar::blah
164 } 1
165
166 # Tests that compare the error stack trace generated when autoloading with
167 # that generated when no autoloading is necessary. Ideally they should be the
168 # same.
169
170 set count 0
171 foreach arg [subst -nocommands -novariables {
172 c
173 {argument
174 which spans
175 multiple lines}
176 {argument which is all on one line but which is of such great length that the Tcl C library will truncate it when appending it onto the global error stack}
177 {argument which spans multiple lines
178 and is long enough to be truncated and
179 " <- includes a false lead in the prune point search
180 and must be longer still to force truncation}
181 {contrived example: rare circumstance
182 where the point at which to prune the
183 error stack cannot be uniquely determined.
184 foo bar foo
185 "}
186 {contrived example: rare circumstance
187 where the point at which to prune the
188 error stack cannot be uniquely determined.
189 foo bar
190 "}
191 {argument that contains non-ASCII character, \u20ac, and which is of such great length that it will be longer than 150 bytes so it will be truncated by the Tcl C library}
192 }] { ;# emacs needs -> "
193
194 test init-4.$count.0 {::errorInfo produced by [unknown]} -setup {
195 auto_reset
196 } -body {
197 catch {parray a b $arg}
198 set first $::errorInfo
199 catch {parray a b $arg}
200 list $first $::errorInfo
201 } -match pairwise -result equal
202 test init-4.$count.1 {::errorInfo produced by [unknown]} -setup {
203 auto_reset
204 } -body {
205 namespace eval junk [list array set $arg [list 1 2 3 4]]
206 trace add variable ::junk::$arg read \
207 "[list error [subst {Variable \"$arg\" is write-only}]] ;# "
208 catch {parray ::junk::$arg}
209 set first $::errorInfo
210 catch {parray ::junk::$arg}
211 list $first $::errorInfo
212 } -match pairwise -result equal
213
214 incr count
215 }
216
217 test init-4.$count {[Bug 46f801ed5a]} -setup {
218 auto_reset
219 array set auto_index {demo {proc demo {} {tailcall error foo}}}
220 } -body {
221 demo
222 } -cleanup {
223 array unset auto_index demo
224 rename demo {}
225 } -returnCodes error -result foo
226
227 test init-5.0 {return options passed through ::unknown} -setup {
228 catch {rename xxx {}}
229 set ::auto_index(::xxx) {proc ::xxx {} {
230 return -code error -level 2 xxx
231 }}
232 } -body {
233 set code [catch {::xxx} foo bar]
234 set code2 [catch {::xxx} foo2 bar2]
235 list $code $foo $bar $code2 $foo2 $bar2
236 } -cleanup {
237 unset ::auto_index(::xxx)
238 } -match glob -result {2 xxx {-errorcode NONE -code 1 -level 1} 2 xxx {-code 1 -level 1 -errorcode NONE}}
239
240 cleanupTests
241 } ;# End of [interp eval $testInterp]
242
243 # cleanup
244 interp delete $testInterp
245 ::tcltest::cleanupTests
246 return
247
248 # Local Variables:
249 # mode: tcl
250 # fill-column: 78
251 # End: