X-Git-Url: https://git.saurik.com/redis.git/blobdiff_plain/73bd6c583b9a3e29dbbbf4f1ba8259cfe5e131b8..0a546fc01758f9a9f8b2113764c2cf963df6ef20:/tests/support/test.tcl diff --git a/tests/support/test.tcl b/tests/support/test.tcl index d2674da1..e801e1f2 100644 --- a/tests/support/test.tcl +++ b/tests/support/test.tcl @@ -2,7 +2,52 @@ set ::passed 0 set ::failed 0 set ::testnum 0 -proc test {name code okpattern} { +proc assert {condition} { + if {![uplevel 1 expr $condition]} { + puts "!! ERROR\nExpected '$value' to evaluate to true" + error "assertion" + } +} + +proc assert_match {pattern value} { + if {![string match $pattern $value]} { + puts "!! ERROR\nExpected '$value' to match '$pattern'" + error "assertion" + } +} + +proc assert_equal {expected value} { + if {$expected ne $value} { + puts "!! ERROR\nExpected '$value' to be equal to '$expected'" + error "assertion" + } +} + +proc assert_error {pattern code} { + if {[catch {uplevel 1 $code} error]} { + assert_match $pattern $error + } else { + puts "!! ERROR\nExpected an error but nothing was catched" + error "assertion" + } +} + +proc assert_encoding {enc key} { + # Swapped out values don't have an encoding, so make sure that + # the value is swapped in before checking the encoding. + set dbg [r debug object $key] + while {[string match "* swapped at:*" $dbg]} { + r debug swapin $key + set dbg [r debug object $key] + } + assert_match "* encoding:$enc *" $dbg +} + +proc assert_type {type key} { + assert_equal $type [r type $key] +} + +proc test {name code {okpattern notspecified}} { # abort if tagged with a tag to deny foreach tag $::denytags { if {[lsearch $::tags $tag] >= 0} { @@ -28,20 +73,27 @@ proc test {name code okpattern} { puts -nonewline [format "#%03d %-68s " $::testnum $name] flush stdout if {[catch {set retval [uplevel 1 $code]} error]} { - puts "EXCEPTION" - puts "\nCaught error: $error" - error "exception" - } - if {$okpattern eq $retval || [string match $okpattern $retval]} { - puts "PASSED" - incr ::passed + if {$error eq "assertion"} { + incr ::failed + } else { + puts "EXCEPTION" + puts "\nCaught error: $error" + error "exception" + } } else { - puts "!! ERROR expected\n'$okpattern'\nbut got\n'$retval'" - incr ::failed + if {$okpattern eq "notspecified" || $okpattern eq $retval || [string match $okpattern $retval]} { + puts "PASSED" + incr ::passed + } else { + puts "!! ERROR expected\n'$okpattern'\nbut got\n'$retval'" + incr ::failed + } } if {$::traceleaks} { - if {![string match {*0 leaks*} [exec leaks redis-server]]} { + set output [exec leaks redis-server] + if {![string match {*0 leaks*} $output]} { puts "--------- Test $::testnum LEAKED! --------" + puts $output exit 1 } }