home *** CD-ROM | disk | FTP | other *** search
/ PC World 2000 December / PCWorld_2000-12_cd.bin / Komunikace / Comanche / xuibuilder / TclXML-1.1.1 / tests / defs < prev    next >
Text File  |  2000-11-02  |  14KB  |  448 lines

  1. # This file contains support code for the Tcl test suite.  It is
  2. # normally sourced by the individual files in the test suite before
  3. # they run their tests.  This improved approach to testing was designed
  4. # and initially implemented by Mary Ann May-Pumphrey of Sun Microsystems.
  5. #
  6. # Copyright (c) 1990-1994 The Regents of the University of California.
  7. # Copyright (c) 1994-1996 Sun Microsystems, Inc.
  8. #
  9. # See the file "license.terms" for information on usage and redistribution
  10. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  11. #
  12. # SCCS: @(#) defs 1.60 97/08/13 18:10:19
  13.  
  14. if ![info exists VERBOSE] {
  15.     set VERBOSE 0
  16. }
  17. if ![info exists TESTS] {
  18.     set TESTS {}
  19. }
  20.  
  21. # If tests are being run as root, issue a warning message and set a
  22. # variable to prevent some tests from running at all.
  23.  
  24. set user {}
  25. if {$tcl_platform(platform) == "unix"} {
  26.     catch {set user [exec whoami]}
  27.     if {$user == ""} {
  28.         catch {regexp {^[^(]*\(([^)]*)\)} [exec id] dummy user}
  29.     }
  30.     if {$user == ""} {set user root}
  31.     if {$user == "root"} {
  32.         puts stdout "Warning: you're executing as root.  I'll have to"
  33.         puts stdout "skip some of the tests, since they'll fail as root."
  34.     set testConfig(root) 1
  35.     }
  36. }
  37.  
  38. # Some of the tests don't work on some system configurations due to
  39. # differences in word length, file system configuration, etc.  In order
  40. # to prevent false alarms, these tests are generally only run in the
  41. # master development directory for Tcl.  The presence of a file
  42. # "doAllTests" in this directory is used to indicate that the non-portable
  43. # tests should be run.
  44.  
  45. # If there is no "memory" command (because memory debugging isn't
  46. # enabled), generate a dummy command that does nothing.
  47.  
  48. if {[info commands memory] == ""} {
  49.     proc memory args {}
  50. }
  51.  
  52. # Check configuration information that will determine which tests
  53. # to run.  To do this, create an array testConfig.  Each element
  54. # has a 0 or 1 value, and the following elements are defined:
  55. #    unixOnly -    1 means this is a UNIX platform, so it's OK
  56. #            to run tests that only work under UNIX.
  57. #    macOnly -    1 means this is a Mac platform, so it's OK
  58. #            to run tests that only work on Macs.
  59. #    pcOnly -    1 means this is a PC platform, so it's OK to
  60. #            run tests that only work on PCs.
  61. #    unixOrPc -    1 means this is a UNIX or PC platform.
  62. #    macOrPc -    1 means this is a Mac or PC platform.
  63. #    macOrUnix -    1 means this is a Mac or UNIX platform.
  64. #    nonPortable -    1 means this the tests are being running in
  65. #            the master Tcl/Tk development environment;
  66. #            Some tests are inherently non-portable because
  67. #            they depend on things like word length, file system
  68. #            configuration, window manager, etc.  These tests
  69. #            are only run in the main Tcl development directory
  70. #            where the configuration is well known.  The presence
  71. #            of the file "doAllTests" in this directory indicates
  72. #            that it is safe to run non-portable tests.
  73. #       knownBug -      The test is known to fail and the bug is not yet
  74. #                       fixed. The test will be run only if the file
  75. #                       "doBuggyTests" exists (intended for Tcl dev. group
  76. #                       internal use only).
  77. #    tempNotPc -    The inverse of pcOnly.  This flag is used to
  78. #            temporarily disable a test.
  79. #    tempNotMac -    The inverse of macOnly.  This flag is used to
  80. #            temporarily disable a test.
  81. #    nonBlockFiles - 1 means this platform supports setting files into
  82. #            nonblocking mode.
  83. #    asyncPipeClose- 1 means this platform supports async flush and
  84. #            async close on a pipe.
  85. #    unixExecs     - 1 means this machine has commands such as 'cat',
  86. #            'echo' etc available.
  87. #    notIfCompiled -    1 means this that it is safe to run tests that
  88. #                       might fail if the bytecode compiler is used. This
  89. #                       element is set 1 if the file "doAllTests" exists in
  90. #                       this directory. Normally, this element is 0 so that
  91. #                       tests that fail with the bytecode compiler are
  92. #            skipped. As of 11/2/96 these are the history tests
  93. #            since they depend on accurate source location
  94. #            information.
  95.  
  96. catch {unset testConfig}
  97. if {$tcl_platform(platform) == "unix"} {
  98.     set testConfig(unixOnly) 1
  99.     set testConfig(tempNotPc) 1
  100.     set testConfig(tempNotMac) 1
  101. } else {
  102.     set testConfig(unixOnly) 0
  103. if {$tcl_platform(platform) == "macintosh"} {
  104.     set testConfig(tempNotPc) 1
  105.     set testConfig(macOnly) 1
  106. } else {
  107.     set testConfig(macOnly) 0
  108. if {$tcl_platform(platform) == "windows"} {
  109.     set testConfig(tempNotMac) 1
  110.     set testConfig(pcOnly) 1
  111. } else {
  112.     set testConfig(pcOnly) 0
  113. }
  114. set testConfig(unixOrPc) [expr $testConfig(unixOnly) || $testConfig(pcOnly)]
  115. set testConfig(macOrPc) [expr $testConfig(macOnly) || $testConfig(pcOnly)]
  116. set testConfig(macOrUnix) [expr $testConfig(macOnly) || $testConfig(unixOnly)]
  117. set testConfig(nonPortable)    [expr [file exists doAllTests] || [file exists doAllTe]]
  118. set testConfig(knownBug) [expr [file exists doBuggyTests] || [file exists doBuggyT]]
  119. set testConfig(notIfCompiled) [file exists doAllCompilerTests]
  120.  
  121. set testConfig(unix)    $testConfig(unixOnly)
  122. set testConfig(mac)    $testConfig(macOnly)
  123. set testConfig(pc)    $testConfig(pcOnly)
  124.  
  125. set testConfig(nt)    [expr {$tcl_platform(os) == "Windows NT"}]
  126. set testConfig(95)    [expr {$tcl_platform(os) == "Windows 95"}]
  127. set testConfig(win32s)    [expr {$tcl_platform(os) == "Win32s"}]
  128.  
  129. # The following config switches are used to mark tests that crash on
  130. # certain platforms, so that they can be reactivated again when the
  131. # underlying problem is fixed.
  132.  
  133. set testConfig(pcCrash) $testConfig(macOrUnix)
  134. set testConfig(macCrash) $testConfig(unixOrPc)
  135. set testConfig(unixCrash) $testConfig(macOrPc)
  136.  
  137. if {[catch {set f [open defs r]}]} {
  138.     set testConfig(nonBlockFiles) 1
  139. } else {
  140.     if {[expr [catch {fconfigure $f -blocking off}]] == 0} {
  141.     set testConfig(nonBlockFiles) 1
  142.     } else {
  143.     set testConfig(nonBlockFiles) 0
  144.     }
  145.     close $f
  146. }
  147.  
  148. trace variable testConfig r safeFetch
  149.  
  150. proc safeFetch {n1 n2 op} {
  151.     global testConfig 
  152.  
  153.     if {($n2 != {}) && ([info exists testConfig($n2)] == 0)} {
  154.     set testConfig($n2) 0
  155.     }
  156. }
  157.  
  158. # Test for SCO Unix - cannot run async flushing tests because a potential
  159. # problem with select is apparently interfering. (Mark Diekhans).
  160.  
  161. if {$tcl_platform(platform) == "unix"} {
  162.     if {[catch {exec uname -X | fgrep {Release = 3.2v}}] == 0} {
  163.     set testConfig(asyncPipeClose) 0
  164.     } else {
  165.     set testConfig(asyncPipeClose) 1
  166.     }
  167. } else {
  168.     set testConfig(asyncPipeClose) 1
  169. }
  170.  
  171. # Test to see if execed commands such as cat, echo, rm and so forth are
  172. # present on this machine.
  173.  
  174. set testConfig(unixExecs) 1
  175. if {$tcl_platform(platform) == "macintosh"} {
  176.     set testConfig(unixExecs) 0
  177. }
  178. if {($testConfig(unixExecs) == 1) && ($tcl_platform(platform) == "windows")} {
  179.     if {[catch {exec cat defs}] == 1} {
  180.     set testConfig(unixExecs) 0
  181.     }
  182.     if {($testConfig(unixExecs) == 1) && ([catch {exec echo hello}] == 1)} {
  183.     set testConfig(unixExecs) 0
  184.     }
  185.     if {($testConfig(unixExecs) == 1) && \
  186.         ([catch {exec sh -c echo hello}] == 1)} {
  187.     set testConfig(unixExecs) 0
  188.     }
  189.     if {($testConfig(unixExecs) == 1) && ([catch {exec wc defs}] == 1)} {
  190.     set testConfig(unixExecs) 0
  191.     }
  192.     if {$testConfig(unixExecs) == 1} {
  193.     exec echo hello > removeMe
  194.         if {[catch {exec rm removeMe}] == 1} {
  195.         set testConfig(unixExecs) 0
  196.     }
  197.     }
  198.     if {($testConfig(unixExecs) == 1) && ([catch {exec sleep 1}] == 1)} {
  199.     set testConfig(unixExecs) 0
  200.     }
  201.     if {($testConfig(unixExecs) == 1) && \
  202.         ([catch {exec fgrep unixExecs defs}] == 1)} {
  203.     set testConfig(unixExecs) 0
  204.     }
  205.     if {($testConfig(unixExecs) == 1) && ([catch {exec ps}] == 1)} {
  206.     set testConfig(unixExecs) 0
  207.     }
  208.     if {($testConfig(unixExecs) == 1) && \
  209.         ([catch {exec echo abc > removeMe}] == 0) && \
  210.         ([catch {exec chmod 644 removeMe}] == 1) && \
  211.         ([catch {exec rm removeMe}] == 0)} {
  212.     set testConfig(unixExecs) 0
  213.     } else {
  214.     catch {exec rm -f removeMe}
  215.     }
  216.     if {($testConfig(unixExecs) == 1) && \
  217.         ([catch {exec mkdir removeMe}] == 1)} {
  218.     set testConfig(unixExecs) 0
  219.     } else {
  220.     catch {exec rm -r removeMe}
  221.     }
  222.     if {$testConfig(unixExecs) == 0} {
  223.     puts stdout "Warning: Unix-style executables are not available, so"
  224.     puts stdout "some tests will be skipped."
  225.     }
  226. }    
  227.  
  228. proc print_verbose {name description constraints script code answer} {
  229.     puts stdout "\n"
  230.     if {[string length $constraints]} {
  231.     puts stdout "==== $name $description\t--- ($constraints) ---"
  232.     } else {
  233.     puts stdout "==== $name $description"
  234.     }
  235.     puts stdout "==== Contents of test case:"
  236.     puts stdout "$script"
  237.     if {$code != 0} {
  238.     if {$code == 1} {
  239.         puts stdout "==== Test generated error:"
  240.         puts stdout $answer
  241.     } elseif {$code == 2} {
  242.         puts stdout "==== Test generated return exception;  result was:"
  243.         puts stdout $answer
  244.     } elseif {$code == 3} {
  245.         puts stdout "==== Test generated break exception"
  246.     } elseif {$code == 4} {
  247.         puts stdout "==== Test generated continue exception"
  248.     } else {
  249.         puts stdout "==== Test generated exception $code;  message was:"
  250.         puts stdout $answer
  251.     }
  252.     } else {
  253.     puts stdout "==== Result was:"
  254.     puts stdout "$answer"
  255.     }
  256. }
  257.  
  258. # test --
  259. # This procedure runs a test and prints an error message if the
  260. # test fails.  If VERBOSE has been set, it also prints a message
  261. # even if the test succeeds.  The test will be skipped if it
  262. # doesn't match the TESTS variable, or if one of the elements
  263. # of "constraints" turns out not to be true.
  264. #
  265. # Arguments:
  266. # name -        Name of test, in the form foo-1.2.
  267. # description -        Short textual description of the test, to
  268. #            help humans understand what it does.
  269. # constraints -        A list of one or more keywords, each of
  270. #            which must be the name of an element in
  271. #            the array "testConfig".  If any of these
  272. #            elements is zero, the test is skipped.
  273. #            This argument may be omitted.
  274. # script -        Script to run to carry out the test.  It must
  275. #            return a result that can be checked for
  276. #            correctness.
  277. # answer -        Expected result from script.
  278.  
  279. proc test {name description script answer args} {
  280.     global VERBOSE TESTS testConfig
  281.     if {[string compare $TESTS ""] != 0} then {
  282.     set ok 0
  283.     foreach test $TESTS {
  284.         if [string match $test $name] then {
  285.         set ok 1
  286.         break
  287.         }
  288.         }
  289.     if !$ok then return
  290.     }
  291.     set i [llength $args]
  292.     if {$i == 0} {
  293.     set constraints {}
  294.     } elseif {$i == 1} {
  295.     # "constraints" argument exists;  shuffle arguments down, then
  296.     # make sure that the constraints are satisfied.
  297.  
  298.     set constraints $script
  299.     set script $answer
  300.     set answer [lindex $args 0]
  301.     set doTest 0
  302.     if {[string match {*[$\[]*} $constraints] != 0} {
  303.         # full expression, e.g. {$foo > [info tclversion]}
  304.  
  305.         catch {set doTest [uplevel #0 expr [list $constraints]]} msg
  306.     } elseif {[regexp {[^.a-zA-Z0-9 ]+} $constraints] != 0} {
  307.         # something like {a || b} should be turned into 
  308.         # $testConfig(a) || $testConfig(b).
  309.  
  310.          regsub -all {[.a-zA-Z0-9]+} $constraints {$testConfig(&)} c
  311.         catch {set doTest [eval expr $c]}
  312.     } else {
  313.         # just simple constraints such as {unixOnly fonts}.
  314.  
  315.         set doTest 1
  316.         foreach constraint $constraints {
  317.         if {![info exists testConfig($constraint)]
  318.             || !$testConfig($constraint)} {
  319.             set doTest 0
  320.             break
  321.         }
  322.         }
  323.     }
  324.     if {$doTest == 0} {
  325.         if $VERBOSE then {
  326.         puts stdout "++++ $name SKIPPED: $constraints"
  327.         }
  328.         return    
  329.     }
  330.     } else {
  331.     error "wrong # args: must be \"test name description ?constraints? script answer\""
  332.     }
  333.     memory tag $name
  334.     set code [catch {uplevel $script} result]
  335.     if {$code != 0} {
  336.     print_verbose $name $description $constraints $script \
  337.         $code $result
  338.     } elseif {[string compare $result $answer] == 0} then { 
  339.     if $VERBOSE then {
  340.         if {$VERBOSE > 0} {
  341.         print_verbose $name $description $constraints $script \
  342.             $code $result
  343.         }
  344.         if {$VERBOSE != -2} {
  345.         puts stdout "++++ $name PASSED"
  346.         }
  347.     }
  348.     } else { 
  349.     print_verbose $name $description $constraints $script \
  350.         $code $result
  351.     puts stdout "---- Result should have been:"
  352.     puts stdout "$answer"
  353.     puts stdout "---- $name FAILED" 
  354.     }
  355. }
  356.  
  357. proc dotests {file args} {
  358.     global TESTS
  359.     set savedTests $TESTS
  360.     set TESTS $args
  361.     source $file
  362.     set TESTS $savedTests
  363. }
  364.  
  365. proc normalizeMsg {msg} {
  366.     regsub "\n$" [string tolower $msg] "" msg
  367.     regsub -all "\n\n" $msg "\n" msg
  368.     regsub -all "\n\}" $msg "\}" msg
  369.     return $msg
  370. }
  371.  
  372. proc makeFile {contents name} {
  373.     set fd [open $name w]
  374.     fconfigure $fd -translation lf
  375.     if {[string index $contents [expr [string length $contents] - 1]] == "\n"} {
  376.     puts -nonewline $fd $contents
  377.     } else {
  378.     puts $fd $contents
  379.     }
  380.     close $fd
  381. }
  382.  
  383. proc removeFile {name} {
  384.     file delete $name
  385. }
  386.  
  387. proc makeDirectory {name} {
  388.     file mkdir $name
  389. }
  390.  
  391. proc removeDirectory {name} {
  392.     file delete -force $name
  393. }
  394.  
  395. proc viewFile {name} {
  396.     global tcl_platform testConfig
  397.     if {($tcl_platform(platform) == "macintosh") || \
  398.         ($testConfig(unixExecs) == 0)} {
  399.     set f [open $name]
  400.     set data [read -nonewline $f]
  401.     close $f
  402.     return $data
  403.     } else {
  404.     exec cat $name
  405.     }
  406. }
  407.  
  408. # Locate tcltest executable
  409.  
  410. set tcltest [info nameofexecutable]
  411.  
  412. if {$tcltest == "{}"} {
  413.     set tcltest {}
  414.     puts "Unable to find tcltest executable, multiple process tests will fail."
  415. }
  416.  
  417. if {0 && $tcl_platform(os) != "Win32s"} {
  418.     # Don't even try running another copy of tcltest under win32s, or you 
  419.     # get an error dialog about multiple instances.
  420.  
  421.     catch {
  422.     file delete -force tmp
  423.     set f [open tmp w]
  424.     puts $f {
  425.         exit
  426.     }
  427.     close $f
  428.     set f [open "|[list $tcltest tmp]" r]
  429.     close $f
  430.     set testConfig(stdio) 1
  431.     }
  432. }
  433.  
  434. if {($tcl_platform(platform) == "windows") && ($testConfig(stdio) == 0)} {
  435.     puts "(will skip tests that redirect stdio of exec'd 32-bit applications)"
  436. }
  437.  
  438. catch {socket} msg
  439. set testConfig(socket) [expr {$msg != "sockets are not available on this system"}]
  440.  
  441. if {$testConfig(socket) == 0} {
  442.     puts "(will skip tests that use sockets)"
  443. }
  444.     
  445.         
  446.