os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/tests/load.test
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     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 by 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 # RCS: @(#) $Id: load.test,v 1.11.2.1 2004/09/14 17:02:56 das Exp $
    14 
    15 if {[lsearch [namespace children] ::tcltest] == -1} {
    16     package require tcltest 2
    17     namespace import -force ::tcltest::*
    18 }
    19 
    20 # Figure out what extension is used for shared libraries on this
    21 # platform.
    22 
    23 if {$tcl_platform(platform) == "macintosh"} {
    24     puts "can't run dynamic library tests on macintosh machines"
    25     ::tcltest::cleanupTests
    26     return
    27 }
    28 
    29 # Tests require the existence of one of the DLLs in the dltest directory.
    30 set ext [info sharedlibextension]
    31 set testDir [file join [file dirname [info nameofexecutable]] dltest]
    32 set x [file join $testDir pkga$ext]
    33 set dll "[file tail $x]Required"
    34 ::tcltest::testConstraint $dll [file readable $x]
    35 
    36 # Tests also require that this DLL has not already been loaded.
    37 set loaded "[file tail $x]Loaded"
    38 set alreadyLoaded [info loaded]
    39 ::tcltest::testConstraint $loaded \
    40 	[expr {![string match *pkga* $alreadyLoaded]}]
    41 
    42 set alreadyTotalLoaded [info loaded]
    43 
    44 # Certain tests require the 'teststaticpkg' command from tcltest
    45 
    46 ::tcltest::testConstraint teststaticpkg \
    47     [string compare {} [info commands teststaticpkg]]
    48 
    49 
    50 test load-1.1 {basic errors} {} {
    51     list [catch {load} msg] $msg
    52 } "1 {wrong \# args: should be \"load fileName ?packageName? ?interp?\"}"
    53 test load-1.2 {basic errors} {} {
    54     list [catch {load a b c d} msg] $msg
    55 } "1 {wrong \# args: should be \"load fileName ?packageName? ?interp?\"}"
    56 test load-1.3 {basic errors} {} {
    57     list [catch {load a b foobar} msg] $msg
    58 } {1 {could not find interpreter "foobar"}}
    59 test load-1.4 {basic errors} {} {
    60     list [catch {load {}} msg] $msg
    61 } {1 {must specify either file name or package name}}
    62 test load-1.5 {basic errors} {} {
    63     list [catch {load {} {}} msg] $msg
    64 } {1 {must specify either file name or package name}}
    65 test load-1.6 {basic errors} {} {
    66     list [catch {load {} Unknown} msg] $msg
    67 } {1 {package "Unknown" isn't loaded statically}}
    68 
    69 test load-2.1 {basic loading, with guess for package name} \
    70 	[list $dll $loaded] {
    71     load [file join $testDir pkga$ext]
    72     list [pkga_eq abc def] [info commands pkga_*]
    73 } {0 {pkga_eq pkga_quote}}
    74 interp create -safe child
    75 test load-2.2 {loading into a safe interpreter, with package name conversion} \
    76 	[list $dll $loaded] {
    77     load [file join $testDir pkgb$ext] pKgB child
    78     list [child eval pkgb_sub 44 13] [catch {child eval pkgb_unsafe} msg] $msg \
    79 	    [catch {pkgb_sub 12 10} msg2] $msg2
    80 } {31 1 {invalid command name "pkgb_unsafe"} 1 {invalid command name "pkgb_sub"}}
    81 test load-2.3 {loading with no _Init procedure} -constraints [list $dll $loaded] \
    82 -body {
    83     list [catch {load [file join $testDir pkgc$ext] foo} msg] $msg
    84 } -match glob -result {1 {*couldn't find procedure Foo_Init}}
    85 test load-2.4 {loading with no _SafeInit procedure} [list $dll $loaded] {
    86     list [catch {load [file join $testDir pkga$ext] {} child} msg] $msg
    87 } {1 {can't use package in a safe interpreter: no Pkga_SafeInit procedure}}
    88 
    89 test load-3.1 {error in _Init procedure, same interpreter} \
    90 	[list $dll $loaded] {
    91     list [catch {load [file join $testDir pkge$ext] pkge} msg] \
    92 	    $msg $errorInfo $errorCode
    93 } {1 {couldn't open "non_existent": no such file or directory} {couldn't open "non_existent": no such file or directory
    94     while executing
    95 "open non_existent"
    96     invoked from within
    97 "if 44 {open non_existent}"
    98     invoked from within
    99 "load [file join $testDir pkge$ext] pkge"} {POSIX ENOENT {no such file or directory}}}
   100 test load-3.2 {error in _Init procedure, slave interpreter} \
   101 	[list $dll $loaded] {
   102     catch {interp delete x}
   103     interp create x
   104     set errorCode foo
   105     set errorInfo bar
   106     set result [list [catch {load [file join $testDir pkge$ext] pkge x} msg] \
   107 	    $msg $errorInfo $errorCode]
   108     interp delete x
   109     set result
   110 } {1 {couldn't open "non_existent": no such file or directory} {couldn't open "non_existent": no such file or directory
   111     while executing
   112 "open non_existent"
   113     invoked from within
   114 "if 44 {open non_existent}"
   115     invoked from within
   116 "load [file join $testDir pkge$ext] pkge x"} {POSIX ENOENT {no such file or directory}}}
   117 
   118 test load-4.1 {reloading package into same interpreter} [list $dll $loaded] {
   119     list [catch {load [file join $testDir pkga$ext] pkga} msg] $msg
   120 } {0 {}}
   121 test load-4.2 {reloading package into same interpreter} [list $dll $loaded] {
   122     list [catch {load [file join $testDir pkga$ext] pkgb} msg] $msg
   123 } [list 1 "file \"[file join $testDir pkga$ext]\" is already loaded for package \"Pkga\""]
   124 
   125 test load-5.1 {file name not specified and no static package: pick default} \
   126 	[list $dll $loaded] {
   127     catch {interp delete x}
   128     interp create x
   129     load [file join $testDir pkga$ext] pkga
   130     load {} pkga x
   131     set result [info loaded x]
   132     interp delete x
   133     set result
   134 } [list [list [file join $testDir pkga$ext] Pkga]]
   135 
   136 # On some platforms, like SunOS 4.1.3, these tests can't be run because
   137 # they cause the process to exit.
   138 
   139 test load-6.1 {errors loading file} [list $dll $loaded nonPortable] {
   140     catch {load foo foo}
   141 } {1}
   142 
   143 test load-7.1 {Tcl_StaticPackage procedure} [list teststaticpkg] {
   144     set x "not loaded"
   145     teststaticpkg Test 1 0
   146     load {} Test
   147     load {} Test child
   148     list [set x] [child eval set x]
   149 } {loaded loaded}
   150 test load-7.2 {Tcl_StaticPackage procedure} [list teststaticpkg] {
   151     set x "not loaded"
   152     teststaticpkg Another 0 0
   153     load {} Another
   154     child eval {set x "not loaded"}
   155     list [catch {load {} Another child} msg] $msg \
   156 	[child eval set x] [set x]
   157 } {1 {can't use package in a safe interpreter: no Another_SafeInit procedure} {not loaded} loaded}
   158 test load-7.3 {Tcl_StaticPackage procedure} [list teststaticpkg] {
   159     set x "not loaded"
   160     teststaticpkg More 0 1
   161     load {} More
   162     set x
   163 } {not loaded}
   164 test load-7.4 {Tcl_StaticPackage procedure, redundant calls} \
   165     [list teststaticpkg $dll $loaded] {
   166 	teststaticpkg Double 0 1
   167 	teststaticpkg Double 0 1
   168 	info loaded
   169     } [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]
   170 
   171 test load-8.1 {TclGetLoadedPackages procedure} [list teststaticpkg $dll $loaded] {
   172     info loaded
   173 } [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]
   174 test load-8.2 {TclGetLoadedPackages procedure} [list teststaticpkg] {
   175     list [catch {info loaded gorp} msg] $msg
   176 } {1 {could not find interpreter "gorp"}}
   177 test load-8.3 {TclGetLoadedPackages procedure} [list teststaticpkg $dll $loaded] {
   178     list [info loaded {}] [info loaded child]
   179 } [list [concat [list {{} Double} {{} More} {{} Another} {{} Test} [list [file join $testDir pkga$ext] Pkga]] $alreadyLoaded] [list {{} Test} [list [file join $testDir pkgb$ext] Pkgb]]]
   180 test load-8.4 {TclGetLoadedPackages procedure} [list $dll $loaded teststaticpkg] {
   181     load [file join $testDir pkgb$ext] pkgb
   182     list [info loaded {}] [lsort [info commands pkgb_*]]
   183 } [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}]
   184 interp delete child
   185 
   186 test load-9.1 {Tcl_StaticPackage, load already-loaded package into another interp} \
   187     -constraints {teststaticpkg} \
   188     -setup {
   189 	interp create child1
   190 	interp create child2
   191 	load {} Tcltest child1
   192 	load {} Tcltest child2
   193     } \
   194     -body {
   195 	child1 eval { teststaticpkg Loadninepointone 0 1 }
   196 	child2 eval { teststaticpkg Loadninepointone 0 1 }
   197 	list \
   198 	    [child1 eval { info loaded {} }] \
   199 	    [child2 eval { info loaded {} }]
   200     } \
   201     -result {{{{} Loadninepointone} {{} Tcltest}} {{{} Loadninepointone} {{} Tcltest}}} \
   202     -cleanup { interp delete child1 ; interp delete child2 }
   203 
   204     
   205 # cleanup
   206 ::tcltest::cleanupTests
   207 return