Commit a0cfd519 authored by Oran Agra's avatar Oran Agra
Browse files

test infra: improve prints on failed assertions

sometimes we have several assertions with the same condition in the same test
at different stages, and when these fail (the ones that print the condition
text) you don't know which one it was. other assertions didn't print the
condition text (variable names), just the expected and unexpected values.

So now, all assertions print context line, and conditin text.

besides, one of the major differences between 'assert' and 'assert_equal',
is that the later is able to print the value that doesn't match the expected.
if there is a rare non-reproducible failure, it is helpful to know what was
the value the test encountered and how far it was from the threshold.

So now, adding assert_lessthan and assert_range that can be used in some places.
were we used just 'assert { a > b }' so far.
parent 764b420f
...@@ -11,28 +11,55 @@ proc fail {msg} { ...@@ -11,28 +11,55 @@ proc fail {msg} {
proc assert {condition} { proc assert {condition} {
if {![uplevel 1 [list expr $condition]]} { if {![uplevel 1 [list expr $condition]]} {
error "assertion:Expected condition '$condition' to be true ([uplevel 1 [list subst -nocommands $condition]])" set context "(context: [info frame -1])"
error "assertion:Expected [uplevel 1 [list subst -nocommands $condition]] $context"
} }
} }
proc assert_no_match {pattern value} { proc assert_no_match {pattern value} {
if {[string match $pattern $value]} { if {[string match $pattern $value]} {
error "assertion:Expected '$value' to not match '$pattern'" set context "(context: [info frame -1])"
error "assertion:Expected '$value' to not match '$pattern' $context"
} }
} }
proc assert_match {pattern value} { proc assert_match {pattern value} {
if {![string match $pattern $value]} { if {![string match $pattern $value]} {
error "assertion:Expected '$value' to match '$pattern'" set context "(context: [info frame -1])"
error "assertion:Expected '$value' to match '$pattern' $context"
} }
} }
proc assert_equal {expected value {detail ""}} { proc assert_equal {value expected {detail ""}} {
if {$expected ne $value} { if {$expected ne $value} {
if {$detail ne ""} { if {$detail ne ""} {
set detail " (detail: $detail)" set detail "(detail: $detail)"
} else {
set detail "(context: [info frame -1])"
}
error "assertion:Expected '$value' to be equal to '$expected' $detail"
}
}
proc assert_lessthan {value expected {detail ""}} {
if {!($value < $expected)} {
if {$detail ne ""} {
set detail "(detail: $detail)"
} else {
set detail "(context: [info frame -1])"
}
error "assertion:Expected '$value' to be lessthan to '$expected' $detail"
}
}
proc assert_range {value min max {detail ""}} {
if {!($value <= $max && $value >= $min)} {
if {$detail ne ""} {
set detail "(detail: $detail)"
} else {
set detail "(context: [info frame -1])"
} }
error "assertion:Expected '$value' to be equal to '$expected'$detail" error "assertion:Expected '$value' to be between to '$min' and '$max' $detail"
} }
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment