os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/tests/autoMkindex.test
First public contribution.
1 # Commands covered: auto_mkindex auto_import
3 # This file contains tests related to autoloading and generating
4 # the autoloading index.
6 # Copyright (c) 1998 Lucent Technologies, Inc.
7 # Copyright (c) 1998-1999 by Scriptics Corporation.
9 # See the file "license.terms" for information on usage and redistribution
10 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
12 # RCS: @(#) $Id: autoMkindex.test,v 1.14.2.1 2004/10/28 00:01:06 dgp Exp $
14 if {[lsearch [namespace children] ::tcltest] == -1} {
15 package require tcltest 2
16 namespace import -force ::tcltest::*
19 makeFile {# Test file for:
22 # This file provides example cases for testing the Tcl autoloading
23 # facility. Things are much more complicated with namespaces and classes.
24 # The "auto_mkindex" facility can no longer be built on top of a simple
25 # regular expression parser. It must recognize constructs like this:
27 # namespace eval foo {
28 # proc test {x y} { ... }
29 # namespace eval bar {
30 # proc another {args} { ... }
34 # Note that procedures and itcl class definitions can be nested inside
37 # Copyright (c) 1993-1998 Lucent Technologies, Inc.
39 # This shouldn't cause any problems
40 namespace import -force blt::*
42 # Should be able to handle "proc" definitions, even if they are
43 # preceded by white space.
45 proc normal {x y} {return [expr $x+$y]}
46 proc indented {x y} {return [expr $x+$y]}
49 # Should be able to handle proc declarations within namespaces,
50 # even if they have explicit namespace paths.
52 namespace eval buried {
53 proc inside {args} {return "inside: $args"}
55 namespace export pub_*
56 proc pub_one {args} {return "one: $args"}
57 proc pub_two {args} {return "two: $args"}
59 proc buried::within {args} {return "within: $args"}
61 namespace eval buried {
62 namespace eval under {
63 proc neath {args} {return "neath: $args"}
65 namespace eval ::buried {
66 proc relative {args} {return "relative: $args"}
67 proc ::top {args} {return "top: $args"}
68 proc ::buried::explicit {args} {return "explicit: $args"}
72 # With proper hooks, we should be able to support other commands
73 # that create procedures
75 proc buried::myproc {name body args} {
76 ::proc $name $body $args
78 namespace eval ::buried {
79 proc mycmd1 args {return "mycmd"}
80 myproc mycmd2 args {return "mycmd"}
82 ::buried::myproc mycmd3 args {return "another"}
84 proc {buried::my proc} {name body args} {
85 ::proc $name $body $args
87 namespace eval ::buried {
88 proc mycmd4 args {return "mycmd"}
89 {my proc} mycmd5 args {return "mycmd"}
91 {::buried::my proc} mycmd6 args {return "another"}
93 # A correctly functioning [auto_import] won't choke when a child
94 # namespace [namespace import]s from its parent.
96 namespace eval ::parent::child {
97 namespace import ::parent::*
99 proc ::parent::child::test {} {}
104 # Save initial state of auto_mkindex_parser
106 auto_load auto_mkindex
107 if {[info exists auto_mkindex_parser::initCommands]} {
108 set saveCommands $auto_mkindex_parser::initCommands
110 proc AutoMkindexTestReset {} {
112 if {[info exists saveCommands]} {
113 set auto_mkindex_parser::initCommands $saveCommands
114 } elseif {[info exists auto_mkindex_parser::initCommands]} {
115 unset auto_mkindex_parser::initCommands
122 cd $::tcltest::temporaryDirectory
124 test autoMkindex-1.1 {remove any existing tclIndex file} {
129 test autoMkindex-1.2 {build tclIndex based on a test file} {
130 auto_mkindex . autoMkindex.tcl
134 set element "{source [file join . autoMkindex.tcl]}"
136 test autoMkindex-1.3 {examine tclIndex} {
138 auto_mkindex . autoMkindex.tcl
139 namespace eval tcl_autoMkindex_tmp {
144 foreach elem [lsort [array names auto_index]] {
145 lappend ::result [list $elem $auto_index($elem)]
148 namespace delete tcl_autoMkindex_tmp
150 } "{::buried::explicit $element} {::buried::inside $element} {{::buried::my proc} $element} {::buried::mycmd1 $element} {::buried::mycmd4 $element} {::buried::myproc $element} {::buried::pub_one $element} {::buried::pub_two $element} {::buried::relative $element} {::buried::under::neath $element} {::buried::within $element} {::parent::child::test $element} {indented $element} {normal $element} {top $element}"
153 test autoMkindex-2.1 {commands on the autoload path can be imported} {
155 auto_mkindex . autoMkindex.tcl
156 set interp [interp create]
157 set final [$interp eval {
158 namespace eval blt {}
159 set auto_path [linsert $auto_path 0 .]
160 set info [list [catch {namespace import buried::*} result] $result]
161 foreach name [lsort [info commands pub_*]] {
162 lappend info $name [namespace origin $name]
166 interp delete $interp
168 } "0 {} pub_one ::buried::pub_one pub_two ::buried::pub_two"
170 # Test auto_mkindex hooks
172 # Slave hook executes interesting code in the interp used to watch code.
174 test autoMkindex-3.1 {slaveHook} {
175 auto_mkindex_parser::slavehook {
176 _%@namespace eval ::blt {
178 _%@namespace export foo
181 auto_mkindex_parser::slavehook { _%@namespace import -force ::blt::* }
183 auto_mkindex . autoMkindex.tcl
185 # Reset initCommands to avoid trashing other tests
191 # The auto_mkindex_parser::command is used to register commands
192 # that create new commands.
194 test autoMkindex-3.2 {auto_mkindex_parser::command} {
195 auto_mkindex_parser::command buried::myproc {name args} {
198 append index [list set auto_index([fullname $name])] \
199 " \[list source \[file join \$dir [list $scriptFile]\]\]\n"
202 auto_mkindex . autoMkindex.tcl
203 namespace eval tcl_autoMkindex_tmp {
208 foreach elem [lsort [array names auto_index]] {
209 lappend ::result [list $elem $auto_index($elem)]
212 namespace delete tcl_autoMkindex_tmp
214 # Reset initCommands to avoid trashing other tests
218 } "{::buried::explicit $element} {::buried::inside $element} {{::buried::my proc} $element} {::buried::mycmd1 $element} {::buried::mycmd2 $element} {::buried::mycmd4 $element} {::buried::myproc $element} {::buried::pub_one $element} {::buried::pub_two $element} {::buried::relative $element} {::buried::under::neath $element} {::buried::within $element} {::parent::child::test $element} {indented $element} {mycmd3 $element} {normal $element} {top $element}"
221 test autoMkindex-3.3 {auto_mkindex_parser::command} {knownBug} {
222 auto_mkindex_parser::command {buried::my proc} {name args} {
226 append index [list set auto_index([fullname $name])] \
227 " \[list source \[file join \$dir [list $scriptFile]\]\]\n"
230 auto_mkindex . autoMkindex.tcl
231 namespace eval tcl_autoMkindex_tmp {
236 foreach elem [lsort [array names auto_index]] {
237 lappend ::result [list $elem $auto_index($elem)]
240 namespace delete tcl_autoMkindex_tmp
242 # Reset initCommands to avoid trashing other tests
245 proc lvalue {list pattern} {
246 set ix [lsearch $list $pattern]
248 return [lindex $list $ix]
253 list [lvalue $::result *mycmd4*] [lvalue $::result *mycmd5*] [lvalue $::result *mycmd6*]
254 } "{::buried::mycmd4 $element} {::buried::mycmd5 $element} {mycmd6 $element}"
259 package provide football 1.0
261 namespace eval ::pro:: {
263 # export only public functions.
265 namespace export {[a-z]*}
267 namespace eval ::college:: {
269 # export only public functions.
271 namespace export {[a-z]*}
274 proc ::pro::team {} {
279 proc ::college::team {} {
284 } [file join pkg samename.tcl]
287 test autoMkindex-4.1 {platform indenpendant source commands} {
289 auto_mkindex . pkg/samename.tcl
290 set f [open tclIndex r]
291 set dat [split [string trim [read $f]] "\n"]
292 set len [llength $dat]
293 set result [lsort [lrange $dat [expr {$len-2}] [expr {$len-1}]]]
296 } {{set auto_index(::college::team) [list source [file join $dir pkg samename.tcl]]} {set auto_index(::pro::team) [list source [file join $dir pkg samename.tcl]]}}
298 removeFile [file join pkg samename.tcl]
301 set dollar1 "this string contains an unescaped dollar sign -> \\$foo"
302 set dollar2 "this string contains an escaped dollar sign -> \$foo \\\$foo"
303 set bracket1 "this contains an unescaped bracket [NoSuchProc]"
304 set bracket2 "this contains an escaped bracket \[NoSuchProc\]"
305 set bracket3 "this contains nested unescaped brackets [[NoSuchProc]]"
307 } [file join pkg magicchar.tcl]
309 test autoMkindex-5.1 {escape magic tcl chars in general code} {
312 if { ![catch {auto_mkindex . pkg/magicchar.tcl}] } {
313 set f [open tclIndex r]
314 set dat [split [string trim [read $f]] "\n"]
315 set result [lindex $dat end]
319 } {set auto_index(testProc) [list source [file join $dir pkg magicchar.tcl]]}
321 removeFile [file join pkg magicchar.tcl]
324 proc {[magic mojo proc]} {} {}
325 } [file join pkg magicchar2.tcl]
327 test autoMkindex-5.2 {correctly locate auto loaded procs with []} {
330 if { ![catch {auto_mkindex . pkg/magicchar2.tcl}] } {
331 # Make a slave interp to test the autoloading
332 set c [interp create]
333 $c eval {lappend auto_path [pwd]}
334 set result [$c eval {catch {{[magic mojo proc]}}}]
340 removeFile [file join pkg magicchar2.tcl]
347 if {[info exists saveCommands]} {
350 rename AutoMkindexTestReset ""
352 removeFile autoMkindex.tcl
353 if {[file exists tclIndex]} {
354 file delete -force tclIndex
359 ::tcltest::cleanupTests