sl@0: # Commands covered: load sl@0: # sl@0: # This file contains a collection of tests for one or more of the Tcl sl@0: # built-in commands. Sourcing this file into Tcl runs the tests and sl@0: # generates output for errors. No output means no errors were found. sl@0: # sl@0: # Copyright (c) 1995 Sun Microsystems, Inc. sl@0: # Copyright (c) 1998-1999 by Scriptics Corporation. sl@0: # sl@0: # See the file "license.terms" for information on usage and redistribution sl@0: # of this file, and for a DISCLAIMER OF ALL WARRANTIES. sl@0: # sl@0: # RCS: @(#) $Id: load.test,v 1.11.2.1 2004/09/14 17:02:56 das Exp $ sl@0: sl@0: if {[lsearch [namespace children] ::tcltest] == -1} { sl@0: package require tcltest 2 sl@0: namespace import -force ::tcltest::* sl@0: } sl@0: sl@0: # Figure out what extension is used for shared libraries on this sl@0: # platform. sl@0: sl@0: if {$tcl_platform(platform) == "macintosh"} { sl@0: puts "can't run dynamic library tests on macintosh machines" sl@0: ::tcltest::cleanupTests sl@0: return sl@0: } sl@0: sl@0: # Tests require the existence of one of the DLLs in the dltest directory. sl@0: set ext [info sharedlibextension] sl@0: set testDir [file join [file dirname [info nameofexecutable]] dltest] sl@0: set x [file join $testDir pkga$ext] sl@0: set dll "[file tail $x]Required" sl@0: ::tcltest::testConstraint $dll [file readable $x] sl@0: sl@0: # Tests also require that this DLL has not already been loaded. sl@0: set loaded "[file tail $x]Loaded" sl@0: set alreadyLoaded [info loaded] sl@0: ::tcltest::testConstraint $loaded \ sl@0: [expr {![string match *pkga* $alreadyLoaded]}] sl@0: sl@0: set alreadyTotalLoaded [info loaded] sl@0: sl@0: # Certain tests require the 'teststaticpkg' command from tcltest sl@0: sl@0: ::tcltest::testConstraint teststaticpkg \ sl@0: [string compare {} [info commands teststaticpkg]] sl@0: sl@0: sl@0: test load-1.1 {basic errors} {} { sl@0: list [catch {load} msg] $msg sl@0: } "1 {wrong \# args: should be \"load fileName ?packageName? ?interp?\"}" sl@0: test load-1.2 {basic errors} {} { sl@0: list [catch {load a b c d} msg] $msg sl@0: } "1 {wrong \# args: should be \"load fileName ?packageName? ?interp?\"}" sl@0: test load-1.3 {basic errors} {} { sl@0: list [catch {load a b foobar} msg] $msg sl@0: } {1 {could not find interpreter "foobar"}} sl@0: test load-1.4 {basic errors} {} { sl@0: list [catch {load {}} msg] $msg sl@0: } {1 {must specify either file name or package name}} sl@0: test load-1.5 {basic errors} {} { sl@0: list [catch {load {} {}} msg] $msg sl@0: } {1 {must specify either file name or package name}} sl@0: test load-1.6 {basic errors} {} { sl@0: list [catch {load {} Unknown} msg] $msg sl@0: } {1 {package "Unknown" isn't loaded statically}} sl@0: sl@0: test load-2.1 {basic loading, with guess for package name} \ sl@0: [list $dll $loaded] { sl@0: load [file join $testDir pkga$ext] sl@0: list [pkga_eq abc def] [info commands pkga_*] sl@0: } {0 {pkga_eq pkga_quote}} sl@0: interp create -safe child sl@0: test load-2.2 {loading into a safe interpreter, with package name conversion} \ sl@0: [list $dll $loaded] { sl@0: load [file join $testDir pkgb$ext] pKgB child sl@0: list [child eval pkgb_sub 44 13] [catch {child eval pkgb_unsafe} msg] $msg \ sl@0: [catch {pkgb_sub 12 10} msg2] $msg2 sl@0: } {31 1 {invalid command name "pkgb_unsafe"} 1 {invalid command name "pkgb_sub"}} sl@0: test load-2.3 {loading with no _Init procedure} -constraints [list $dll $loaded] \ sl@0: -body { sl@0: list [catch {load [file join $testDir pkgc$ext] foo} msg] $msg sl@0: } -match glob -result {1 {*couldn't find procedure Foo_Init}} sl@0: test load-2.4 {loading with no _SafeInit procedure} [list $dll $loaded] { sl@0: list [catch {load [file join $testDir pkga$ext] {} child} msg] $msg sl@0: } {1 {can't use package in a safe interpreter: no Pkga_SafeInit procedure}} sl@0: sl@0: test load-3.1 {error in _Init procedure, same interpreter} \ sl@0: [list $dll $loaded] { sl@0: list [catch {load [file join $testDir pkge$ext] pkge} msg] \ sl@0: $msg $errorInfo $errorCode sl@0: } {1 {couldn't open "non_existent": no such file or directory} {couldn't open "non_existent": no such file or directory sl@0: while executing sl@0: "open non_existent" sl@0: invoked from within sl@0: "if 44 {open non_existent}" sl@0: invoked from within sl@0: "load [file join $testDir pkge$ext] pkge"} {POSIX ENOENT {no such file or directory}}} sl@0: test load-3.2 {error in _Init procedure, slave interpreter} \ sl@0: [list $dll $loaded] { sl@0: catch {interp delete x} sl@0: interp create x sl@0: set errorCode foo sl@0: set errorInfo bar sl@0: set result [list [catch {load [file join $testDir pkge$ext] pkge x} msg] \ sl@0: $msg $errorInfo $errorCode] sl@0: interp delete x sl@0: set result sl@0: } {1 {couldn't open "non_existent": no such file or directory} {couldn't open "non_existent": no such file or directory sl@0: while executing sl@0: "open non_existent" sl@0: invoked from within sl@0: "if 44 {open non_existent}" sl@0: invoked from within sl@0: "load [file join $testDir pkge$ext] pkge x"} {POSIX ENOENT {no such file or directory}}} sl@0: sl@0: test load-4.1 {reloading package into same interpreter} [list $dll $loaded] { sl@0: list [catch {load [file join $testDir pkga$ext] pkga} msg] $msg sl@0: } {0 {}} sl@0: test load-4.2 {reloading package into same interpreter} [list $dll $loaded] { sl@0: list [catch {load [file join $testDir pkga$ext] pkgb} msg] $msg sl@0: } [list 1 "file \"[file join $testDir pkga$ext]\" is already loaded for package \"Pkga\""] sl@0: sl@0: test load-5.1 {file name not specified and no static package: pick default} \ sl@0: [list $dll $loaded] { sl@0: catch {interp delete x} sl@0: interp create x sl@0: load [file join $testDir pkga$ext] pkga sl@0: load {} pkga x sl@0: set result [info loaded x] sl@0: interp delete x sl@0: set result sl@0: } [list [list [file join $testDir pkga$ext] Pkga]] sl@0: sl@0: # On some platforms, like SunOS 4.1.3, these tests can't be run because sl@0: # they cause the process to exit. sl@0: sl@0: test load-6.1 {errors loading file} [list $dll $loaded nonPortable] { sl@0: catch {load foo foo} sl@0: } {1} sl@0: sl@0: test load-7.1 {Tcl_StaticPackage procedure} [list teststaticpkg] { sl@0: set x "not loaded" sl@0: teststaticpkg Test 1 0 sl@0: load {} Test sl@0: load {} Test child sl@0: list [set x] [child eval set x] sl@0: } {loaded loaded} sl@0: test load-7.2 {Tcl_StaticPackage procedure} [list teststaticpkg] { sl@0: set x "not loaded" sl@0: teststaticpkg Another 0 0 sl@0: load {} Another sl@0: child eval {set x "not loaded"} sl@0: list [catch {load {} Another child} msg] $msg \ sl@0: [child eval set x] [set x] sl@0: } {1 {can't use package in a safe interpreter: no Another_SafeInit procedure} {not loaded} loaded} sl@0: test load-7.3 {Tcl_StaticPackage procedure} [list teststaticpkg] { sl@0: set x "not loaded" sl@0: teststaticpkg More 0 1 sl@0: load {} More sl@0: set x sl@0: } {not loaded} sl@0: test load-7.4 {Tcl_StaticPackage procedure, redundant calls} \ sl@0: [list teststaticpkg $dll $loaded] { sl@0: teststaticpkg Double 0 1 sl@0: teststaticpkg Double 0 1 sl@0: info loaded sl@0: } [concat [list {{} Double} {{} More} {{} Another} {{} Test} [list [file join $testDir pkge$ext] Pkge] [list [file join $testDir pkgb$ext] Pkgb] [list [file join $testDir pkga$ext] Pkga]] $alreadyTotalLoaded] sl@0: sl@0: test load-8.1 {TclGetLoadedPackages procedure} [list teststaticpkg $dll $loaded] { sl@0: info loaded sl@0: } [concat [list {{} Double} {{} More} {{} Another} {{} Test} [list [file join $testDir pkge$ext] Pkge] [list [file join $testDir pkgb$ext] Pkgb] [list [file join $testDir pkga$ext] Pkga]] $alreadyTotalLoaded] sl@0: test load-8.2 {TclGetLoadedPackages procedure} [list teststaticpkg] { sl@0: list [catch {info loaded gorp} msg] $msg sl@0: } {1 {could not find interpreter "gorp"}} sl@0: test load-8.3 {TclGetLoadedPackages procedure} [list teststaticpkg $dll $loaded] { sl@0: list [info loaded {}] [info loaded child] sl@0: } [list [concat [list {{} Double} {{} More} {{} Another} {{} Test} [list [file join $testDir pkga$ext] Pkga]] $alreadyLoaded] [list {{} Test} [list [file join $testDir pkgb$ext] Pkgb]]] sl@0: test load-8.4 {TclGetLoadedPackages procedure} [list $dll $loaded teststaticpkg] { sl@0: load [file join $testDir pkgb$ext] pkgb sl@0: list [info loaded {}] [lsort [info commands pkgb_*]] sl@0: } [list [concat [list [list [file join $testDir pkgb$ext] Pkgb] {{} Double} {{} More} {{} Another} {{} Test} [list [file join $testDir pkga$ext] Pkga]] $alreadyLoaded] {pkgb_sub pkgb_unsafe}] sl@0: interp delete child sl@0: sl@0: test load-9.1 {Tcl_StaticPackage, load already-loaded package into another interp} \ sl@0: -constraints {teststaticpkg} \ sl@0: -setup { sl@0: interp create child1 sl@0: interp create child2 sl@0: load {} Tcltest child1 sl@0: load {} Tcltest child2 sl@0: } \ sl@0: -body { sl@0: child1 eval { teststaticpkg Loadninepointone 0 1 } sl@0: child2 eval { teststaticpkg Loadninepointone 0 1 } sl@0: list \ sl@0: [child1 eval { info loaded {} }] \ sl@0: [child2 eval { info loaded {} }] sl@0: } \ sl@0: -result {{{{} Loadninepointone} {{} Tcltest}} {{{} Loadninepointone} {{} Tcltest}}} \ sl@0: -cleanup { interp delete child1 ; interp delete child2 } sl@0: sl@0: sl@0: # cleanup sl@0: ::tcltest::cleanupTests sl@0: return