| [25] | 1 | # Functionality covered: this file contains a collection of tests for the |
|---|
| 2 | # auto loading and namespaces. |
|---|
| 3 | # |
|---|
| 4 | # Sourcing this file into Tcl runs the tests and generates output for |
|---|
| 5 | # errors. No output means no errors were found. |
|---|
| 6 | # |
|---|
| 7 | # Copyright (c) 1997 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: init.test,v 1.18 2007/12/13 15:26:06 dgp Exp $ |
|---|
| 14 | |
|---|
| 15 | if {[lsearch [namespace children] ::tcltest] == -1} { |
|---|
| 16 | package require tcltest 2 |
|---|
| 17 | namespace import -force ::tcltest::* |
|---|
| 18 | } |
|---|
| 19 | |
|---|
| 20 | # Clear out any namespaces called test_ns_* |
|---|
| 21 | catch {namespace delete {*}[namespace children :: test_ns_*]} |
|---|
| 22 | |
|---|
| 23 | # Six cases - white box testing |
|---|
| 24 | |
|---|
| 25 | test init-1.1 {auto_qualify - absolute cmd - namespace} { |
|---|
| 26 | auto_qualify ::foo::bar ::blue |
|---|
| 27 | } ::foo::bar |
|---|
| 28 | |
|---|
| 29 | test init-1.2 {auto_qualify - absolute cmd - global} { |
|---|
| 30 | auto_qualify ::global ::sub |
|---|
| 31 | } global |
|---|
| 32 | |
|---|
| 33 | test init-1.3 {auto_qualify - no colons cmd - global} { |
|---|
| 34 | auto_qualify nocolons :: |
|---|
| 35 | } nocolons |
|---|
| 36 | |
|---|
| 37 | test init-1.4 {auto_qualify - no colons cmd - namespace} { |
|---|
| 38 | auto_qualify nocolons ::sub |
|---|
| 39 | } {::sub::nocolons nocolons} |
|---|
| 40 | |
|---|
| 41 | test init-1.5 {auto_qualify - colons in cmd - global} { |
|---|
| 42 | auto_qualify foo::bar :: |
|---|
| 43 | } ::foo::bar |
|---|
| 44 | |
|---|
| 45 | test init-1.6 {auto_qualify - colons in cmd - namespace} { |
|---|
| 46 | auto_qualify foo::bar ::sub |
|---|
| 47 | } {::sub::foo::bar ::foo::bar} |
|---|
| 48 | |
|---|
| 49 | # Some additional tests |
|---|
| 50 | |
|---|
| 51 | test init-1.7 {auto_qualify - multiples colons 1} { |
|---|
| 52 | auto_qualify :::foo::::bar ::blue |
|---|
| 53 | } ::foo::bar |
|---|
| 54 | |
|---|
| 55 | test init-1.8 {auto_qualify - multiple colons 2} { |
|---|
| 56 | auto_qualify :::foo ::bar |
|---|
| 57 | } foo |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | # we use a sub interp and auto_reset and double the tests because there is 2 |
|---|
| 61 | # places where auto_loading occur (before loading the indexes files and after) |
|---|
| 62 | |
|---|
| 63 | set testInterp [interp create] |
|---|
| 64 | interp eval $testInterp [list set argv $argv] |
|---|
| 65 | interp eval $testInterp [list package require tcltest] |
|---|
| 66 | interp eval $testInterp [list namespace import -force ::tcltest::*] |
|---|
| 67 | |
|---|
| 68 | interp eval $testInterp { |
|---|
| 69 | |
|---|
| 70 | auto_reset |
|---|
| 71 | catch {rename parray {}} |
|---|
| 72 | |
|---|
| 73 | test init-2.0 {load parray - stage 1} { |
|---|
| 74 | set ret [catch {parray} error] |
|---|
| 75 | rename parray {} ; # remove it, for the next test - that should not fail. |
|---|
| 76 | list $ret $error |
|---|
| 77 | } {1 {wrong # args: should be "parray a ?pattern?"}} |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | test init-2.1 {load parray - stage 2} { |
|---|
| 81 | set ret [catch {parray} error] |
|---|
| 82 | list $ret $error |
|---|
| 83 | } {1 {wrong # args: should be "parray a ?pattern?"}} |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | auto_reset |
|---|
| 87 | catch {rename ::safe::setLogCmd {}} |
|---|
| 88 | #unset auto_index(::safe::setLogCmd) |
|---|
| 89 | #unset auto_oldpath |
|---|
| 90 | |
|---|
| 91 | test init-2.2 {load ::safe::setLogCmd - stage 1} { |
|---|
| 92 | ::safe::setLogCmd |
|---|
| 93 | rename ::safe::setLogCmd {} ; # should not fail |
|---|
| 94 | } {} |
|---|
| 95 | |
|---|
| 96 | test init-2.3 {load ::safe::setLogCmd - stage 2} { |
|---|
| 97 | ::safe::setLogCmd |
|---|
| 98 | rename ::safe::setLogCmd {} ; # should not fail |
|---|
| 99 | } {} |
|---|
| 100 | |
|---|
| 101 | auto_reset |
|---|
| 102 | catch {rename ::safe::setLogCmd {}} |
|---|
| 103 | |
|---|
| 104 | test init-2.4 {load safe:::setLogCmd - stage 1} { |
|---|
| 105 | safe:::setLogCmd ; # intentionally 3 : |
|---|
| 106 | rename ::safe::setLogCmd {} ; # should not fail |
|---|
| 107 | } {} |
|---|
| 108 | |
|---|
| 109 | test init-2.5 {load safe:::setLogCmd - stage 2} { |
|---|
| 110 | safe:::setLogCmd ; # intentionally 3 : |
|---|
| 111 | rename ::safe::setLogCmd {} ; # should not fail |
|---|
| 112 | } {} |
|---|
| 113 | |
|---|
| 114 | auto_reset |
|---|
| 115 | catch {rename ::safe::setLogCmd {}} |
|---|
| 116 | |
|---|
| 117 | test init-2.6 {load setLogCmd from safe:: - stage 1} { |
|---|
| 118 | namespace eval safe setLogCmd |
|---|
| 119 | rename ::safe::setLogCmd {} ; # should not fail |
|---|
| 120 | } {} |
|---|
| 121 | |
|---|
| 122 | test init-2.7 {oad setLogCmd from safe:: - stage 2} { |
|---|
| 123 | namespace eval safe setLogCmd |
|---|
| 124 | rename ::safe::setLogCmd {} ; # should not fail |
|---|
| 125 | } {} |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | test init-2.8 {load tcl::HistAdd} -setup { |
|---|
| 130 | auto_reset |
|---|
| 131 | catch {rename ::tcl::HistAdd {}} |
|---|
| 132 | } -body { |
|---|
| 133 | # 3 ':' on purpose |
|---|
| 134 | list [catch {tcl:::HistAdd} error] $error |
|---|
| 135 | } -cleanup { |
|---|
| 136 | rename ::tcl::HistAdd {} ; |
|---|
| 137 | } -result {1 {wrong # args: should be "tcl:::HistAdd command ?exec?"}} |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | test init-3.0 {random stuff in the auto_index, should still work} { |
|---|
| 141 | set auto_index(foo:::bar::blah) { |
|---|
| 142 | namespace eval foo {namespace eval bar {proc blah {} {return 1}}} |
|---|
| 143 | } |
|---|
| 144 | foo:::bar::blah |
|---|
| 145 | } 1 |
|---|
| 146 | |
|---|
| 147 | # Tests that compare the error stack trace generated when autoloading |
|---|
| 148 | # with that generated when no autoloading is necessary. Ideally they |
|---|
| 149 | # should be the same. |
|---|
| 150 | |
|---|
| 151 | set count 0 |
|---|
| 152 | foreach arg [subst -nocommands -novariables { |
|---|
| 153 | c |
|---|
| 154 | {argument |
|---|
| 155 | which spans |
|---|
| 156 | multiple lines} |
|---|
| 157 | {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} |
|---|
| 158 | {argument which spans multiple lines |
|---|
| 159 | and is long enough to be truncated and |
|---|
| 160 | " <- includes a false lead in the prune point search |
|---|
| 161 | and must be longer still to force truncation} |
|---|
| 162 | {contrived example: rare circumstance |
|---|
| 163 | where the point at which to prune the |
|---|
| 164 | error stack cannot be uniquely determined. |
|---|
| 165 | foo bar foo |
|---|
| 166 | "} |
|---|
| 167 | {contrived example: rare circumstance |
|---|
| 168 | where the point at which to prune the |
|---|
| 169 | error stack cannot be uniquely determined. |
|---|
| 170 | foo bar |
|---|
| 171 | "} |
|---|
| 172 | {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} |
|---|
| 173 | }] { |
|---|
| 174 | |
|---|
| 175 | test init-4.$count.0 {::errorInfo produced by [unknown]} { |
|---|
| 176 | auto_reset |
|---|
| 177 | catch {parray a b $arg} |
|---|
| 178 | set first $::errorInfo |
|---|
| 179 | catch {parray a b $arg} |
|---|
| 180 | set second $::errorInfo |
|---|
| 181 | string equal $first $second |
|---|
| 182 | } 1 |
|---|
| 183 | |
|---|
| 184 | test init-4.$count.1 {::errorInfo produced by [unknown]} { |
|---|
| 185 | auto_reset |
|---|
| 186 | namespace eval junk [list array set $arg [list 1 2 3 4]] |
|---|
| 187 | trace variable ::junk::$arg r \ |
|---|
| 188 | "[list error [subst {Variable \"$arg\" is write-only}]] ;# " |
|---|
| 189 | catch {parray ::junk::$arg} |
|---|
| 190 | set first $::errorInfo |
|---|
| 191 | catch {parray ::junk::$arg} |
|---|
| 192 | set second $::errorInfo |
|---|
| 193 | string equal $first $second |
|---|
| 194 | } 1 |
|---|
| 195 | |
|---|
| 196 | incr count |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | test init-5.0 {return options passed through ::unknown} -setup { |
|---|
| 200 | catch {rename xxx {}} |
|---|
| 201 | set ::auto_index(::xxx) {proc ::xxx {} { |
|---|
| 202 | return -code error -level 2 xxx |
|---|
| 203 | }} |
|---|
| 204 | } -body { |
|---|
| 205 | set code [catch {::xxx} foo bar] |
|---|
| 206 | set code2 [catch {::xxx} foo2 bar2] |
|---|
| 207 | list $code $foo $bar $code2 $foo2 $bar2 |
|---|
| 208 | } -cleanup { |
|---|
| 209 | unset ::auto_index(::xxx) |
|---|
| 210 | } -result {2 xxx {-errorcode NONE -code 1 -level 1} 2 xxx {-code 1 -level 1 -errorcode NONE}} |
|---|
| 211 | |
|---|
| 212 | cleanupTests |
|---|
| 213 | } ;# End of [interp eval $testInterp] |
|---|
| 214 | |
|---|
| 215 | # cleanup |
|---|
| 216 | interp delete $testInterp |
|---|
| 217 | ::tcltest::cleanupTests |
|---|
| 218 | return |
|---|
| 219 | |
|---|