Ignore:
Timestamp:
Mar 18, 2011, 8:45:29 AM (14 years ago)
Author:
mmc
Message:

Finished everything for a 1.0 version of the tester tool. The tester
successfully reports errors for the tool in the "example" directory.
It catches differences in input values and output results. It can look
for tests that are expected to fail and detect mismatches in their
standard output. Still needs a "build" mode to create/modify tests,
but this much handles the regression testing part pretty well.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tester/scripts/test.tcl

    r2136 r2139  
    2424    public method getResult {}
    2525    public method getTestInfo {args}
     26    public method getRunInfo {args}
    2627    public method getDiffs {args}
    2728
    28     public method getRunobj {}
    2929    public method getTestobj {}
    3030    public method getTestxml {}
    3131
    3232    public method run {args}
     33    public method abort {}
    3334    public method regoldenize {}
    3435
     
    4445    private method _setResult {name}
    4546    private method _computeDiffs {obj1 obj2 args}
     47    private method _buildFailure {str}
    4648
    4749    # use this to add tests to the "run" queue
     
    6062itcl::body Rappture::Tester::Test::constructor {toolobj testxml args} {
    6163    set _toolobj $toolobj
    62 
    6364    set _testxml $testxml
    6465    set _testobj [Rappture::library $testxml]
    65 
    66     # HACK: Add a new input to differentiate between results
    67     $_testobj put input.TestRun.current "Golden"
    6866
    6967    eval configure $args
     
    113111
    114112# ----------------------------------------------------------------------
     113# USAGE: getRunInfo <path>
     114# USAGE: getRunInfo children <path>
     115# USAGE: getRunInfo element ?-as type? <path>
     116#
     117# Returns info about the most recent run at the specified <path> in
     118# the XML.  If the <path> is missing or misspelled, this method returns
     119# "" instead of an error.
     120# ----------------------------------------------------------------------
     121itcl::body Rappture::Tester::Test::getRunInfo {args} {
     122    if {[llength $args] == 1} {
     123        set path [lindex $args 0]
     124        return [$_runobj get $path]
     125    }
     126    return [eval $_runobj $args]
     127}
     128
     129# ----------------------------------------------------------------------
    115130# USAGE: run ?-output callback path value path value ...?
    116131#
     
    132147    foreach path [Rappture::entities -as path $_testobj input] {
    133148        if {[$_testobj element -as type $path.current] ne ""} {
    134 puts "  override: $path = [$_testobj get $path.current]"
    135149            lappend args $path [$_testobj get $path.current]
    136150        }
     
    147161        } elseif {[Rappture::library isvalid $result]} {
    148162            set _runobj $result
    149             set idiffs [_computeDiffs [$_toolobj xml] $_testobj -in input]
    150             set odiffs [_computeDiffs $_testobj $_runobj -in output]
    151             set _diffs [concat $idiffs $odiffs]
    152 puts "DIFFS:"
    153 foreach rec $_diffs {
    154 puts ">> $rec"
    155 }
    156 
    157             # HACK: Add a new input to differentiate between results
    158             $_runobj put input.TestRun.current "Test result"
    159 
    160             # any differences from expected result mean test failed
    161             if {[llength $_diffs] == 0} {
     163
     164            if {[$_testobj get output.status] ne "ok"} {
     165                # expected test to fail, but it didn't
     166                set idiffs [_computeDiffs [$_toolobj xml] $_runobj -in input]
     167                set odiffs [_computeDiffs $_testobj $_runobj -what run]
     168                set _diffs [concat $idiffs $odiffs]
     169                _setResult "Fail"
     170            } else {
     171                set idiffs [_computeDiffs [$_toolobj xml] $_testobj -in input]
     172                set odiffs [_computeDiffs $_testobj $_runobj -in output]
     173                set _diffs [concat $idiffs $odiffs]
     174
     175                # any differences from expected result mean test failed
     176                if {[llength $_diffs] == 0} {
     177                    _setResult "Pass"
     178                } else {
     179                    _setResult "Fail"
     180                }
     181            }
     182            return "finished"
     183        } else {
     184            set _runobj [_buildFailure $result]
     185            if {[$_testobj get output.status] eq "failed"
     186                  && [$_testobj get output.log] eq $result} {
    162187                _setResult "Pass"
    163188            } else {
     189                set idiffs [_computeDiffs [$_toolobj xml] $_runobj -in input]
     190                set odiffs [_computeDiffs $_testobj $_runobj -what run]
     191                set _diffs [concat $idiffs $odiffs]
    164192                _setResult "Fail"
    165193            }
    166194            return "finished"
     195        }
     196    } else {
     197        set _runobj [_buildFailure $result]
     198        if {[$_testobj get output.status] eq "failed"
     199              && [$_testobj get output.log] eq $result} {
     200            _setResult "Pass"
    167201        } else {
     202            set idiffs [_computeDiffs [$_toolobj xml] $_runobj -in input]
     203            set odiffs [_computeDiffs $_testobj $_runobj -what run]
     204            set _diffs [concat $idiffs $odiffs]
    168205            _setResult "Fail"
    169             return "failed: $result"
    170         }
    171     } else {
    172         _setResult "Fail"
    173         tk_messageBox -icon error -message "Tool failed: $result"
     206        }
    174207        return "finished"
    175208    }
     209}
     210
     211# ----------------------------------------------------------------------
     212# USAGE: abort
     213#
     214# Causes the current test kicked off by the "run" method to be aborted.
     215# ----------------------------------------------------------------------
     216itcl::body Rappture::Tester::Test::abort {} {
     217    $_toolobj abort
    176218}
    177219
     
    234276    # otherwise, compare the golden test vs. the test result
    235277    return [_computeDiffs $_testobj $_runobj -in $path -detail max]
    236 }
    237 
    238 # -----------------------------------------------------------------------
    239 # USAGE: getRunobj
    240 #
    241 # Returns the library object generated by the previous run of the test.
    242 # Throws an error if the test has not been run.
    243 # -----------------------------------------------------------------------
    244 itcl::body Rappture::Tester::Test::getRunobj {} {
    245     if {$_runobj eq ""} {
    246         error "Test has not yet been run."
    247     }
    248     return $_runobj
    249278}
    250279
     
    300329# ----------------------------------------------------------------------
    301330# USAGE: _computeDiffs <xmlObj1> <xmlObj2> ?-in xxx? \
    302 #            ?-what value|attrs|all? ?-detail min|max?
     331#            ?-what value|attrs|run|all? ?-detail min|max?
    303332#
    304333# Used internally to compute differences between two different XML
     
    324353    Rappture::getopts args params {
    325354        value -in output
    326         value -what all
     355        value -what "attrs value"
    327356        value -detail min
    328357    }
    329358    if {$params(-what) == "all"} {
    330         set params(-what) "attrs value"
     359        set params(-what) "attrs value run"
     360    }
     361
     362    # handle any run output diffs first, so they appear at the top
     363    # report this as one incident -- not separate reports for status/log
     364    set rlist ""
     365    if {[lsearch $params(-what) "run"] >= 0} {
     366        set st1 [$obj1 get output.status]
     367        set st2 [$obj2 get output.status]
     368        if {$st1 ne $st2} {
     369            # status changes are most serious
     370            lappend rlist [list -what status -path output.status \
     371                -obj1 $obj1 -obj2 $obj2]
     372        } else {
     373            set log1 [$obj1 get output.log]
     374            set log2 [$obj2 get output.log]
     375            if {$log1 ne $log2} {
     376                # flag log changes instead if status agrees
     377                lappend rlist [list -what status -path output.log \
     378                    -obj1 $obj1 -obj2 $obj2]
     379            }
     380        }
    331381    }
    332382
    333383    # scan through the specified sections or paths
    334     set rlist ""
    335384    foreach elem $params(-in) {
    336385        if {[string first . $elem] >= 0} {
     
    359408                        lappend details -val1 $val1 -val2 $val2
    360409
    361 puts "COMPARE: $path $obj1 =?= $obj2"
    362 puts "  $val1 =?= $val2"
    363410                        if {$val1 eq "" || $val2 eq ""} {
    364411                            lappend rlist [linsert $details 0 -what "value c"]
     
    422469                        }
    423470                      }
     471                      run {
     472                        # do nothing -- already handled above
     473
     474                        # handled this comparison
     475                        set v2paths [lreplace $v2paths $i $i]
     476                      }
    424477                      default {
    425                         error "bad part \"$part\": should be attrs, value"
     478                        error "bad part \"$part\": should be attrs, value, run"
    426479                      }
    427480                    }
     
    437490    }
    438491    return $rlist
     492}
     493
     494# ----------------------------------------------------------------------
     495# USAGE: _buildFailure <output>
     496#
     497# Returns a new Rappture::library object that contains a copy of the
     498# original test with the given <output> and a failing status.  This
     499# is used to represent the result of a test that aborts without
     500# producing a valid run.xml file.
     501# ----------------------------------------------------------------------
     502itcl::body Rappture::Tester::Test::_buildFailure {output} {
     503    set info "<?xml version=\"1.0\"?>\n[$_testobj xml]"
     504    set obj [Rappture::LibraryObj ::#auto $info]
     505    $obj remove test
     506
     507    $obj put output.time [clock format [clock seconds]]
     508    $obj put output.status failed
     509    $obj put output.user $::tcl_platform(user)
     510    $obj put output.log $output
     511
     512    return $obj
    439513}
    440514
Note: See TracChangeset for help on using the changeset viewer.