Debian Codegraph @

tests/load.test

raw
1 # Commands covered: load
2 #
3 # This file contains a collection of tests for one or more of the Tcl
4 # built-in commands. Sourcing this file into Tcl runs the tests and
5 # generates output for errors. No output means no errors were found.
6 #
7 # Copyright (c) 1995 Sun Microsystems, Inc.
8 # Copyright (c) 1998-1999 Scriptics Corporation.
9 #
10 # See the file "license.terms" for information on usage and redistribution
11 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
12
13 if {"::tcltest" ni [namespace children]} {
14 package require tcltest 2.5
15 namespace import -force ::tcltest::*
16 }
17
18 ::tcltest::loadTestedCommands
19 catch [list package require -exact Tcltest [info patchlevel]]
20
21 # Figure out what extension is used for shared libraries on this
22 # platform.
23 if {![info exists ext]} {
24 set ext [info sharedlibextension]
25 }
26 # Tests require the existence of one of the DLLs in the dltest directory.
27 set testDir [file join [file dirname [info nameofexecutable]] dltest]
28 set x [file join $testDir pkga$ext]
29 set dll "[file tail $x]Required"
30 testConstraint $dll [file readable $x]
31
32 # Tests also require that this DLL has not already been loaded.
33 set loaded "[file tail $x]Loaded"
34 set alreadyLoaded [info loaded {}]
35 testConstraint $loaded [expr {![string match *pkga* $alreadyLoaded]}]
36
37 set alreadyTotalLoaded [info loaded]
38
39 # Certain tests require the 'teststaticpkg' command from tcltest
40
41 testConstraint teststaticpkg [llength [info commands teststaticpkg]]
42
43 # Test load-10.1 requires the 'testsimplefilesystem' command from tcltest
44
45 testConstraint testsimplefilesystem \
46 [llength [info commands testsimplefilesystem]]
47
48 test load-1.1 {basic errors} -returnCodes error -body {
49 load
50 } -result {wrong # args: should be "load ?-global? ?-lazy? ?--? fileName ?packageName? ?interp?"}
51 test load-1.2 {basic errors} -returnCodes error -body {
52 load a b c d
53 } -result {wrong # args: should be "load ?-global? ?-lazy? ?--? fileName ?packageName? ?interp?"}
54 test load-1.3 {basic errors} -returnCodes error -body {
55 load a b foobar
56 } -result {could not find interpreter "foobar"}
57 test load-1.4 {basic errors} -returnCodes error -body {
58 load -global {}
59 } -result {must specify either file name or package name}
60 test load-1.5 {basic errors} -returnCodes error -body {
61 load -lazy {} {}
62 } -result {must specify either file name or package name}
63 test load-1.6 {basic errors} -returnCodes error -body {
64 load {} Unknown
65 } -result {package "Unknown" isn't loaded statically}
66 test load-1.7 {basic errors} -returnCodes error -body {
67 load -abc foo
68 } -result {bad option "-abc": must be -global, -lazy, or --}
69 test load-1.8 {basic errors} -returnCodes error -body {
70 load -global
71 } -result {couldn't figure out package name for -global}
72
73 test load-2.1 {basic loading, with guess for package name} \
74 [list $dll $loaded] {
75 load -global [file join $testDir pkga$ext]
76 list [pkga_eq abc def] [lsort [info commands pkga_*]]
77 } {0 {pkga_eq pkga_quote}}
78 interp create -safe child
79 test load-2.2 {loading into a safe interpreter, with package name conversion} \
80 [list $dll $loaded] {
81 load -lazy [file join $testDir pkgb$ext] Pkgb child
82 list [child eval pkgb_sub 44 13] [catch {child eval pkgb_unsafe} msg] $msg \
83 [catch {pkgb_sub 12 10} msg2] $msg2
84 } {31 1 {invalid command name "pkgb_unsafe"} 1 {invalid command name "pkgb_sub"}}
85 test load-2.3 {loading with no _Init procedure} -constraints [list $dll $loaded] \
86 -body {
87 list [catch {load [file join $testDir pkgc$ext] Foo} msg] $msg $errorCode
88 } -match glob \
89 -result [list 1 {cannot find symbol "Foo_Init"*} \
90 {TCL LOOKUP LOAD_SYMBOL *Foo_Init}]
91 test load-2.4 {loading with no _SafeInit procedure} [list $dll $loaded] {
92 list [catch {load [file join $testDir pkga$ext] {} child} msg] $msg
93 } {1 {can't use package in a safe interpreter: no Pkga_SafeInit procedure}}
94
95 test load-3.1 {error in _Init procedure, same interpreter} \
96 [list $dll $loaded] {
97 list [catch {load [file join $testDir pkge$ext] Pkge} msg] \
98 $msg $::errorInfo $::errorCode
99 } {1 {couldn't open "non_existent": no such file or directory} {couldn't open "non_existent": no such file or directory
100 while executing
101 "open non_existent"
102 invoked from within
103 "if 44 {open non_existent}"
104 invoked from within
105 "load [file join $testDir pkge$ext] Pkge"} {POSIX ENOENT {no such file or directory}}}
106 test load-3.2 {error in _Init procedure, child interpreter} \
107 [list $dll $loaded] {
108 catch {interp delete x}
109 interp create x
110 set ::errorCode foo
111 set ::errorInfo bar
112 set result [list [catch {load [file join $testDir pkge$ext] Pkge x} msg] \
113 $msg $::errorInfo $::errorCode]
114 interp delete x
115 set result
116 } {1 {couldn't open "non_existent": no such file or directory} {couldn't open "non_existent": no such file or directory
117 while executing
118 "open non_existent"
119 invoked from within
120 "if 44 {open non_existent}"
121 invoked from within
122 "load [file join $testDir pkge$ext] Pkge x"} {POSIX ENOENT {no such file or directory}}}
123
124 test load-4.1 {reloading package into same interpreter} [list $dll $loaded] {
125 list [catch {load [file join $testDir pkga$ext] Pkga} msg] $msg
126 } {0 {}}
127 test load-4.2 {reloading package into same interpreter} -setup {
128 catch {load [file join $testDir pkga$ext] Pkga}
129 } -constraints [list $dll $loaded] -returnCodes error -body {
130 load [file join $testDir pkga$ext] Pkgb
131 } -result "file \"[file join $testDir pkga$ext]\" is already loaded for package \"Pkga\""
132
133 test load-5.1 {file name not specified and no static package: pick default} -setup {
134 catch {interp delete x}
135 interp create x
136 } -constraints [list $dll $loaded] -body {
137 load -global [file join $testDir pkga$ext] Pkga
138 load {} Pkga x
139 info loaded x
140 } -cleanup {
141 interp delete x
142 } -result [list [list [file join $testDir pkga$ext] Pkga]]
143
144 # On some platforms, like SunOS 4.1.3, these tests can't be run because
145 # they cause the process to exit.
146 #
147 # As of 2005, such ancient broken systems no longer matter.
148
149 test load-6.1 {errors loading file} [list $dll $loaded] {
150 catch {load foo foo}
151 } {1}
152
153 test load-7.1 {Tcl_StaticPackage procedure} [list teststaticpkg] {
154 set x "not loaded"
155 teststaticpkg Test 1 0
156 load {} Test
157 load {} Test child
158 list [set x] [child eval set x]
159 } {loaded loaded}
160 test load-7.2 {Tcl_StaticPackage procedure} [list teststaticpkg] {
161 set x "not loaded"
162 teststaticpkg Another 0 0
163 load {} Another
164 child eval {set x "not loaded"}
165 list [catch {load {} Another child} msg] $msg \
166 [child eval set x] [set x]
167 } {1 {can't use package in a safe interpreter: no Another_SafeInit procedure} {not loaded} loaded}
168 test load-7.3 {Tcl_StaticPackage procedure} [list teststaticpkg] {
169 set x "not loaded"
170 teststaticpkg More 0 1
171 load {} More
172 set x
173 } {not loaded}
174 catch {load [file join $testDir pkga$ext] Pkga}
175 catch {load [file join $testDir pkgb$ext] Pkgb}
176 catch {load [file join $testDir pkge$ext] Pkge}
177 set currentRealPackages [list [list [file join $testDir pkge$ext] Pkge] [list [file join $testDir pkgb$ext] Pkgb] [list [file join $testDir pkga$ext] Pkga]]
178 test load-7.4 {Tcl_StaticPackage procedure, redundant calls} -setup {
179 teststaticpkg Test 1 0
180 teststaticpkg Another 0 0
181 teststaticpkg More 0 1
182 } -constraints [list teststaticpkg $dll $loaded] -body {
183 teststaticpkg Double 0 1
184 teststaticpkg Double 0 1
185 info loaded
186 } -result [list {{} Double} {{} More} {{} Another} {{} Test} {*}$currentRealPackages {*}$alreadyTotalLoaded]
187
188 testConstraint teststaticpkg_8.x 0
189 if {[testConstraint teststaticpkg]} {
190 catch {
191 teststaticpkg Test 1 1
192 teststaticpkg Another 0 1
193 teststaticpkg More 0 1
194 teststaticpkg Double 0 1
195 testConstraint teststaticpkg_8.x 1
196 }
197 }
198
199 test load-8.1 {TclGetLoadedPackages procedure} [list teststaticpkg_8.x $dll $loaded] {
200 lsort -index 1 [info loaded]
201 } [lsort -index 1 [list {{} Double} {{} More} {{} Another} {{} Test} {*}$currentRealPackages {*}$alreadyTotalLoaded]]
202 test load-8.2 {TclGetLoadedPackages procedure} -constraints {teststaticpkg_8.x} -body {
203 info loaded gorp
204 } -returnCodes error -result {could not find interpreter "gorp"}
205 test load-8.3a {TclGetLoadedPackages procedure} [list teststaticpkg_8.x $dll $loaded] {
206 lsort -index 1 [info loaded {}]
207 } [lsort -index 1 [list {{} Double} {{} More} {{} Another} {{} Test} [list [file join $testDir pkga$ext] Pkga] [list [file join $testDir pkgb$ext] Pkgb] {*}$alreadyLoaded]]
208 test load-8.3b {TclGetLoadedPackages procedure} [list teststaticpkg_8.x $dll $loaded] {
209 lsort -index 1 [info loaded child]
210 } [lsort -index 1 [list {{} Test} [list [file join $testDir pkgb$ext] Pkgb]]]
211 test load-8.4 {TclGetLoadedPackages procedure} [list teststaticpkg_8.x $dll $loaded] {
212 load [file join $testDir pkgb$ext] Pkgb
213 list [lsort -index 1 [info loaded {}]] [lsort [info commands pkgb_*]]
214 } [list [lsort -index 1 [concat [list [list [file join $testDir pkgb$ext] Pkgb] {{} Double} {{} More} {{} Another} {{} Test} [list [file join $testDir pkga$ext] Pkga]] $alreadyLoaded]] {pkgb_demo pkgb_sub pkgb_unsafe}]
215 interp delete child
216
217 test load-9.1 {Tcl_StaticPackage, load already-loaded package into another interp} -setup {
218 interp create child1
219 interp create child2
220 load {} Tcltest child1
221 load {} Tcltest child2
222 } -constraints {teststaticpkg} -body {
223 child1 eval { teststaticpkg Loadninepointone 0 1 }
224 child2 eval { teststaticpkg Loadninepointone 0 1 }
225 list [child1 eval { info loaded {} }] \
226 [child2 eval { info loaded {} }]
227 } -match glob -cleanup {
228 interp delete child1
229 interp delete child2
230 } -result {{{{} Loadninepointone} {* Tcltest}} {{{} Loadninepointone} {* Tcltest}}}
231
232 test load-10.1 {load from vfs} -setup {
233 set dir [pwd]
234 cd $testDir
235 testsimplefilesystem 1
236 } -constraints [list $dll $loaded testsimplefilesystem] -body {
237 list [catch {load simplefs:/pkgd$ext Pkgd} msg] $msg
238 } -result {0 {}} -cleanup {
239 testsimplefilesystem 0
240 cd $dir
241 unset dir
242 }
243
244 test load-11.1 {Load TclOO extension using Stubs (Bug [f51efe99a7])} \
245 [list $dll $loaded] {
246 load [file join $testDir pkgooa$ext]
247 list [pkgooa_stubsok] [lsort [info commands pkgooa_*]]
248 } {1 pkgooa_stubsok}
249
250 # cleanup
251 unset ext
252 ::tcltest::cleanupTests
253 return
254
255 # Local Variables:
256 # mode: tcl
257 # End: