home *** CD-ROM | disk | FTP | other *** search
/ Ultra Pack / UltraComputing Partner Applications.iso / SunLabs / tclTK / src / tk4.0 / tests / defs < prev    next >
Encoding:
Text File  |  1995-05-21  |  5.1 KB  |  188 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) 1994 Sun Microsystems, Inc.
  7. #
  8. # See the file "license.terms" for information on usage and redistribution
  9. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  10. #
  11. # @(#) defs 1.11 95/05/20 16:13:25
  12.  
  13. if ![info exists VERBOSE] {
  14.     set VERBOSE 0
  15. }
  16. if ![info exists TESTS] {
  17.     set TESTS {}
  18. }
  19.  
  20. # Some of the tests don't work on some system configurations due to
  21. # configuration quirks, not due to Tk problems;  in order to prevent
  22. # false alarms, these tests are only run in the master development
  23. # directory for Tk.  The presence of a file "doAllTests" in this
  24. # directory is used to indicate that these tests should be run.
  25.  
  26. set doNonPortableTests [file exists doAllTests]
  27.  
  28. # If there is no "memory" command (because memory debugging isn't
  29. # enabled), generate a dummy command that does nothing.
  30.  
  31. if {[info commands memory] == ""} {
  32.     proc memory args {}
  33. }
  34.  
  35. proc print_verbose {test_name test_description contents_of_test code answer} {
  36.     puts stdout "\n"
  37.     puts stdout "==== $test_name $test_description"
  38.     puts stdout "==== Contents of test case:"
  39.     puts stdout "$contents_of_test"
  40.     if {$code != 0} {
  41.     if {$code == 1} {
  42.         puts stdout "==== Test generated error:"
  43.         puts stdout $answer
  44.     } elseif {$code == 2} {
  45.         puts stdout "==== Test generated return exception;  result was:"
  46.         puts stdout $answer
  47.     } elseif {$code == 3} {
  48.         puts stdout "==== Test generated break exception"
  49.     } elseif {$code == 4} {
  50.         puts stdout "==== Test generated continue exception"
  51.     } else {
  52.         puts stdout "==== Test generated exception $code;  message was:"
  53.         puts stdout $answer
  54.     }
  55.     } else {
  56.     puts stdout "==== Result was:"
  57.     puts stdout "$answer"
  58.     }
  59. }
  60.  
  61. proc test {test_name test_description contents_of_test passing_results} {
  62.     global VERBOSE
  63.     global TESTS
  64.     if {[string compare $TESTS ""] != 0} then {
  65.     set ok 0
  66.     foreach test $TESTS {
  67.         if [string match $test $test_name] then {
  68.         set ok 1
  69.         break
  70.         }
  71.         }
  72.     if !$ok then return
  73.     }
  74.     memory tag $test_name
  75.     set code [catch {uplevel $contents_of_test} answer]
  76.     if {$code != 0} {
  77.     print_verbose $test_name $test_description $contents_of_test \
  78.         $code $answer
  79.     } elseif {[string compare $answer $passing_results] == 0} then { 
  80.     if $VERBOSE then {
  81.         print_verbose $test_name $test_description $contents_of_test \
  82.             $code $answer
  83.         puts stdout "++++ $test_name PASSED"
  84.     }
  85.     } else { 
  86.     print_verbose $test_name $test_description $contents_of_test \
  87.         $code $answer 
  88.     puts stdout "---- Result should have been:"
  89.     puts stdout "$passing_results"
  90.     puts stdout "---- $test_name FAILED" 
  91.     }
  92. }
  93.  
  94. proc dotests {file args} {
  95.     global TESTS
  96.     set savedTests $TESTS
  97.     set TESTS $args
  98.     source $file
  99.     set TESTS $savedTests
  100. }
  101.  
  102. # If the main window isn't already mapped (e.g. because the tests are
  103. # being run automatically) , specify a precise size for it so that the
  104. # user won't have to position it manually.
  105.  
  106. if {![winfo ismapped .]} {
  107.     wm geometry . +0+0
  108.     update
  109. }
  110.  
  111. # The following code can be used to perform tests involving a second
  112. # process running in the background.
  113.  
  114. # Locate tktest executable
  115. global argv0
  116. if { [file executable $argv0] } {
  117.     if { [string index $argv0 0] == "/" } {
  118.     set tktest $argv0
  119.     } else {
  120.     set tktest "[pwd]/$argv0"
  121.     }
  122. } elseif { [file executable ../$argv0] } {
  123.     set tktest "[pwd]/../$argv0"
  124. } else {
  125.     set tktest {}
  126.     puts "Unable to find tktest executable, skipping multiple process tests."
  127. }
  128.  
  129. # Create background process
  130.  
  131. proc setupbg {{args ""}} {
  132.     global tktest fd bgData
  133.     if {$tktest == ""} {
  134.         error "you're not running tktest so setupbg should not have been called"
  135.     }
  136.     if {[info exists fd] && ($fd != "")} {
  137.     cleanupbg
  138.     }
  139.     set fd [open "|$tktest -geometry +0+0 $args" r+]
  140.     puts $fd "puts foo; flush stdout"
  141.     flush $fd
  142.     if {[gets $fd data] < 0} {
  143.         error "unexpected EOF from \"$tktest\""
  144.     }
  145.     if [string compare $data foo] {
  146.         error "unexpected output from background process \"$data\""
  147.     }
  148.     fileevent $fd readable bgReady
  149. }
  150.  
  151. # Send a command to the background process, catching errors and
  152. # flushing I/O channels
  153. proc dobg {command} {
  154.     global fd bgData bgDone
  155.     puts $fd "catch {$command} msg; update; puts \$msg; puts **DONE**; flush stdout"
  156.     flush $fd
  157.     set bgDone 0
  158.     set bgData {}
  159.     tkwait variable bgDone
  160.     set bgData
  161. }
  162.  
  163. # Data arrived from background process.  Check for special marker
  164. # indicating end of data for this command, and make data available
  165. # to dobg procedure.
  166. proc bgReady {} {
  167.     global fd bgData bgDone
  168.     set x [gets $fd]
  169.     if [eof $fd] {
  170.     fileevent $fd readable {}
  171.     set bgDone 1
  172.     } elseif {$x == "**DONE**"} {
  173.     set bgDone 1
  174.     } else {
  175.     append bgData $x
  176.     }
  177. }
  178.  
  179. # Exit the background process, and close the pipes
  180. proc cleanupbg {} {
  181.     global fd
  182.     catch {
  183.     puts $fd "exit"
  184.     close $fd
  185.     }
  186.     set fd ""
  187. }
  188.