[25] | 1 | # Commands covered: source |
---|
| 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) 1991-1993 The Regents of the University of California. |
---|
| 8 | # Copyright (c) 1994-1996 Sun Microsystems, Inc. |
---|
| 9 | # Copyright (c) 1998-2000 by Scriptics Corporation. |
---|
| 10 | # Contributions from Don Porter, NIST, 2003. (not subject to US copyright) |
---|
| 11 | # |
---|
| 12 | # See the file "license.terms" for information on usage and redistribution |
---|
| 13 | # of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
| 14 | # |
---|
| 15 | # RCS: @(#) $Id: source.test,v 1.13 2006/03/21 11:12:29 dkf Exp $ |
---|
| 16 | |
---|
| 17 | if {[catch {package require tcltest 2.1}]} { |
---|
| 18 | puts stderr "Skipping tests in [info script]. tcltest 2.1 required." |
---|
| 19 | return |
---|
| 20 | } |
---|
| 21 | |
---|
| 22 | namespace eval ::tcl::test::source { |
---|
| 23 | namespace import ::tcltest::* |
---|
| 24 | |
---|
| 25 | test source-1.1 {source command} -setup { |
---|
| 26 | set x "old x value" |
---|
| 27 | set y "old y value" |
---|
| 28 | set z "old z value" |
---|
| 29 | set sourcefile [makeFile { |
---|
| 30 | set x 22 |
---|
| 31 | set y 33 |
---|
| 32 | set z 44 |
---|
| 33 | } source.file] |
---|
| 34 | } -body { |
---|
| 35 | source $sourcefile |
---|
| 36 | list $x $y $z |
---|
| 37 | } -cleanup { |
---|
| 38 | removeFile source.file |
---|
| 39 | } -result {22 33 44} |
---|
| 40 | test source-1.2 {source command} -setup { |
---|
| 41 | set sourcefile [makeFile {list result} source.file] |
---|
| 42 | } -body { |
---|
| 43 | source $sourcefile |
---|
| 44 | } -cleanup { |
---|
| 45 | removeFile source.file |
---|
| 46 | } -result result |
---|
| 47 | test source-1.3 {source command} -setup { |
---|
| 48 | set sourcefile [makeFile {} source.file] |
---|
| 49 | set fd [open $sourcefile w] |
---|
| 50 | fconfigure $fd -translation lf |
---|
| 51 | puts $fd "list a b c \\" |
---|
| 52 | puts $fd "d e f" |
---|
| 53 | close $fd |
---|
| 54 | } -body { |
---|
| 55 | source $sourcefile |
---|
| 56 | } -cleanup { |
---|
| 57 | removeFile source.file |
---|
| 58 | } -result {a b c d e f} |
---|
| 59 | |
---|
| 60 | proc ListGlobMatch {expected actual} { |
---|
| 61 | if {[llength $expected] != [llength $actual]} { |
---|
| 62 | return 0 |
---|
| 63 | } |
---|
| 64 | foreach e $expected a $actual { |
---|
| 65 | if {![string match $e $a]} { |
---|
| 66 | return 0 |
---|
| 67 | } |
---|
| 68 | } |
---|
| 69 | return 1 |
---|
| 70 | } |
---|
| 71 | customMatch listGlob [namespace which ListGlobMatch] |
---|
| 72 | |
---|
| 73 | test source-2.3 {source error conditions} -setup { |
---|
| 74 | set sourcefile [makeFile { |
---|
| 75 | set x 146 |
---|
| 76 | error "error in sourced file" |
---|
| 77 | set y $x |
---|
| 78 | } source.file] |
---|
| 79 | } -body { |
---|
| 80 | list [catch {source $sourcefile} msg] $msg $::errorInfo |
---|
| 81 | } -cleanup { |
---|
| 82 | removeFile source.file |
---|
| 83 | } -match listGlob -result [list 1 {error in sourced file} \ |
---|
| 84 | {error in sourced file |
---|
| 85 | while executing |
---|
| 86 | "error "error in sourced file"" |
---|
| 87 | (file "*source.file" line 3) |
---|
| 88 | invoked from within |
---|
| 89 | "source $sourcefile"}] |
---|
| 90 | test source-2.4 {source error conditions} -setup { |
---|
| 91 | set sourcefile [makeFile {break} source.file] |
---|
| 92 | } -body { |
---|
| 93 | source $sourcefile |
---|
| 94 | } -cleanup { |
---|
| 95 | removeFile source.file |
---|
| 96 | } -returnCodes break |
---|
| 97 | test source-2.5 {source error conditions} -setup { |
---|
| 98 | set sourcefile [makeFile {continue} source.file] |
---|
| 99 | } -body { |
---|
| 100 | source $sourcefile |
---|
| 101 | } -cleanup { |
---|
| 102 | removeFile source.file |
---|
| 103 | } -returnCodes continue |
---|
| 104 | test source-2.6 {source error conditions} -setup { |
---|
| 105 | set sourcefile [makeFile {} _non_existent_] |
---|
| 106 | removeFile _non_existent_ |
---|
| 107 | } -body { |
---|
| 108 | list [catch {source $sourcefile} msg] $msg $::errorCode |
---|
| 109 | } -match listGlob -result [list 1 \ |
---|
| 110 | {couldn't read file "*_non_existent_": no such file or directory} \ |
---|
| 111 | {POSIX ENOENT {no such file or directory}}] |
---|
| 112 | |
---|
| 113 | test source-3.1 {return in middle of source file} -setup { |
---|
| 114 | set sourcefile [makeFile { |
---|
| 115 | set x new-x |
---|
| 116 | return allDone |
---|
| 117 | set y new-y |
---|
| 118 | } source.file] |
---|
| 119 | } -body { |
---|
| 120 | set x old-x |
---|
| 121 | set y old-y |
---|
| 122 | set z [source $sourcefile] |
---|
| 123 | list $x $y $z |
---|
| 124 | } -cleanup { |
---|
| 125 | removeFile source.file |
---|
| 126 | } -result {new-x old-y allDone} |
---|
| 127 | test source-3.2 {return with special code etc.} -setup { |
---|
| 128 | set sourcefile [makeFile { |
---|
| 129 | set x new-x |
---|
| 130 | return -code break "Silly result" |
---|
| 131 | set y new-y |
---|
| 132 | } source.file] |
---|
| 133 | } -body { |
---|
| 134 | source $sourcefile |
---|
| 135 | } -cleanup { |
---|
| 136 | removeFile source.file |
---|
| 137 | } -returnCodes break -result {Silly result} |
---|
| 138 | test source-3.3 {return with special code etc.} -setup { |
---|
| 139 | set sourcefile [makeFile { |
---|
| 140 | set x new-x |
---|
| 141 | return -code error "Simulated error" |
---|
| 142 | set y new-y |
---|
| 143 | } source.file] |
---|
| 144 | } -body { |
---|
| 145 | list [catch {source $sourcefile} msg] $msg $::errorInfo $::errorCode |
---|
| 146 | } -cleanup { |
---|
| 147 | removeFile source.file |
---|
| 148 | } -result {1 {Simulated error} {Simulated error |
---|
| 149 | while executing |
---|
| 150 | "source $sourcefile"} NONE} |
---|
| 151 | test source-3.4 {return with special code etc.} -setup { |
---|
| 152 | set sourcefile [makeFile { |
---|
| 153 | set x new-x |
---|
| 154 | return -code error -errorinfo "Simulated errorInfo stuff" |
---|
| 155 | set y new-y |
---|
| 156 | } source.file] |
---|
| 157 | } -body { |
---|
| 158 | list [catch {source $sourcefile} msg] $msg $::errorInfo $::errorCode |
---|
| 159 | } -cleanup { |
---|
| 160 | removeFile source.file |
---|
| 161 | } -result {1 {} {Simulated errorInfo stuff |
---|
| 162 | invoked from within |
---|
| 163 | "source $sourcefile"} NONE} |
---|
| 164 | test source-3.5 {return with special code etc.} -setup { |
---|
| 165 | set sourcefile [makeFile { |
---|
| 166 | set x new-x |
---|
| 167 | return -code error -errorinfo "Simulated errorInfo stuff" \ |
---|
| 168 | -errorcode {a b c} |
---|
| 169 | set y new-y |
---|
| 170 | } source.file] |
---|
| 171 | } -body { |
---|
| 172 | list [catch {source $sourcefile} msg] $msg $::errorInfo $::errorCode |
---|
| 173 | } -cleanup { |
---|
| 174 | removeFile source.file |
---|
| 175 | } -result {1 {} {Simulated errorInfo stuff |
---|
| 176 | invoked from within |
---|
| 177 | "source $sourcefile"} {a b c}} |
---|
| 178 | |
---|
| 179 | test source-6.1 {source is binary ok} -setup { |
---|
| 180 | # Note [makeFile] writes in the system encoding. |
---|
| 181 | # [source] defaults to reading in the system encoding. |
---|
| 182 | set sourcefile [makeFile [list set x "a b\0c"] source.file] |
---|
| 183 | } -body { |
---|
| 184 | set x {} |
---|
| 185 | source $sourcefile |
---|
| 186 | string length $x |
---|
| 187 | } -cleanup { |
---|
| 188 | removeFile source.file |
---|
| 189 | } -result 5 |
---|
| 190 | test source-6.2 {source skips everything after Ctrl-Z: Bug 2040} -setup { |
---|
| 191 | set sourcefile [makeFile "set x ab\32c" source.file] |
---|
| 192 | } -body { |
---|
| 193 | set x {} |
---|
| 194 | source $sourcefile |
---|
| 195 | string length $x |
---|
| 196 | } -cleanup { |
---|
| 197 | removeFile source.file |
---|
| 198 | } -result 2 |
---|
| 199 | |
---|
| 200 | test source-7.1 {source -encoding test} -setup { |
---|
| 201 | set sourcefile [makeFile {} source.file] |
---|
| 202 | file delete $sourcefile |
---|
| 203 | set f [open $sourcefile w] |
---|
| 204 | fconfigure $f -encoding utf-8 |
---|
| 205 | puts $f "set symbol(square-root) \u221A; set x correct" |
---|
| 206 | close $f |
---|
| 207 | } -body { |
---|
| 208 | set x unset |
---|
| 209 | source -encoding utf-8 $sourcefile |
---|
| 210 | set x |
---|
| 211 | } -cleanup { |
---|
| 212 | removeFile source.file |
---|
| 213 | } -result correct |
---|
| 214 | test source-7.2 {source -encoding test} -setup { |
---|
| 215 | # This tests for bad interactions between [source -encoding] |
---|
| 216 | # and use of the Control-Z character (\u001A) as a cross-platform |
---|
| 217 | # EOF character by [source]. Here we write out and the [source] a |
---|
| 218 | # file that contains the byte \x1A, although not the character \u001A in |
---|
| 219 | # the indicated encoding. |
---|
| 220 | set sourcefile [makeFile {} source.file] |
---|
| 221 | file delete $sourcefile |
---|
| 222 | set f [open $sourcefile w] |
---|
| 223 | fconfigure $f -encoding unicode |
---|
| 224 | puts $f "set symbol(square-root) \u221A; set x correct" |
---|
| 225 | close $f |
---|
| 226 | } -body { |
---|
| 227 | set x unset |
---|
| 228 | source -encoding unicode $sourcefile |
---|
| 229 | set x |
---|
| 230 | } -cleanup { |
---|
| 231 | removeFile source.file |
---|
| 232 | } -result correct |
---|
| 233 | test source-7.3 {source -encoding: syntax} -body { |
---|
| 234 | # Have to spell out the -encoding option |
---|
| 235 | source -e utf-8 no_file |
---|
| 236 | } -returnCodes 1 -match glob -result {bad option*} |
---|
| 237 | test source-7.4 {source -encoding: syntax} -setup { |
---|
| 238 | set sourcefile [makeFile {} source.file] |
---|
| 239 | } -body { |
---|
| 240 | source -encoding no-such-encoding $sourcefile |
---|
| 241 | } -cleanup { |
---|
| 242 | removeFile source.file |
---|
| 243 | } -returnCodes 1 -match glob -result {unknown encoding*} |
---|
| 244 | test source-7.5 {source -encoding: correct operation} -setup { |
---|
| 245 | set sourcefile [makeFile {} source.file] |
---|
| 246 | file delete $sourcefile |
---|
| 247 | set f [open $sourcefile w] |
---|
| 248 | fconfigure $f -encoding utf-8 |
---|
| 249 | puts $f "proc \u20ac {} {return foo}" |
---|
| 250 | close $f |
---|
| 251 | } -body { |
---|
| 252 | source -encoding utf-8 $sourcefile |
---|
| 253 | \u20ac |
---|
| 254 | } -cleanup { |
---|
| 255 | removeFile source.file |
---|
| 256 | rename \u20ac {} |
---|
| 257 | } -result foo |
---|
| 258 | test source-7.6 {source -encoding: mismatch encoding error} -setup { |
---|
| 259 | set sourcefile [makeFile {} source.file] |
---|
| 260 | file delete $sourcefile |
---|
| 261 | set f [open $sourcefile w] |
---|
| 262 | fconfigure $f -encoding utf-8 |
---|
| 263 | puts $f "proc \u20ac {} {return foo}" |
---|
| 264 | close $f |
---|
| 265 | } -body { |
---|
| 266 | source -encoding ascii $sourcefile |
---|
| 267 | \u20ac |
---|
| 268 | } -cleanup { |
---|
| 269 | removeFile source.file |
---|
| 270 | } -returnCodes error -match glob -result {invalid command name*} |
---|
| 271 | |
---|
| 272 | cleanupTests |
---|
| 273 | } |
---|
| 274 | namespace delete ::tcl::test::source |
---|
| 275 | return |
---|