+proc fail {msg} {
+ error "assertion:$msg"
+}
+
+proc assert {condition} {
+ if {![uplevel 1 [list expr $condition]]} {
+ error "assertion:Expected condition '$condition' to be true ([uplevel 1 [list subst -nocommands $condition]])"
+ }
+}
+
+proc assert_match {pattern value} {
+ if {![string match $pattern $value]} {
+ error "assertion:Expected '$value' to match '$pattern'"
+ }
+}
+
+proc assert_equal {expected value} {
+ if {$expected ne $value} {
+ error "assertion:Expected '$value' to be equal to '$expected'"
+ }
+}
+
+proc assert_error {pattern code} {
+ if {[catch {uplevel 1 $code} error]} {
+ assert_match $pattern $error
+ } else {
+ error "assertion:Expected an error but nothing was catched"
+ }
+}
+
+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]
+}
+
+# Wait for the specified condition to be true, with the specified number of
+# max retries and delay between retries. Otherwise the 'elsescript' is
+# executed.
+proc wait_for_condition {maxtries delay e _else_ elsescript} {
+ while {[incr maxtries -1] >= 0} {
+ if {[uplevel 1 [list expr $e]]} break
+ after $delay
+ }
+ if {$maxtries == -1} {
+ uplevel 1 $elsescript
+ }
+}
+
+# Test if TERM looks like to support colors
+proc color_term {} {
+ expr {[info exists ::env(TERM)] && [string match *xterm* $::env(TERM)]}
+}
+
+proc colorstr {color str} {
+ if {[color_term]} {
+ set b 0
+ if {[string range $color 0 4] eq {bold-}} {
+ set b 1
+ set color [string range $color 5 end]
+ }
+ switch $color {
+ red {set colorcode {31}}
+ green {set colorcode {32}}
+ yellow {set colorcode {33}}
+ blue {set colorcode {34}}
+ magenta {set colorcode {35}}
+ cyan {set colorcode {36}}
+ white {set colorcode {37}}
+ default {set colorcode {37}}
+ }
+ if {$colorcode ne {}} {
+ return "\033\[$b;${colorcode};40m$str\033\[0m"
+ }
+ } else {
+ return $str
+ }
+}
+
+proc test {name code {okpattern undefined}} {