[REBOL] Assertions and DbC
From: btiffin::rogers::com at: 1-Oct-2007 21:21
Hello listers, Challenge: Make a better assert. assert: func [[catch] expr [block!]]
[ any append copy expr [ throw make error! join form expr " - assertion failed" ] ] >>
assert [true] == true Expect this to trip; the any is evaluated "in context". >> a: 23
abc: context [a: 22 assert [a = 23]] ** User Error: a = 23 - assertion failed ** Near:
assert [a = 23] This is a proper assertion >> a: 23 abc: context [a: 22 assert [a = 22]]
>> >> assert [equal? 12] ** User Error: equal? 12 - assertion failed ** Near: assert
[equal? 12] Notes; This one-liner is reformatted for mailing to the list. I copy expr
so the error message only includes the original expression on failure, both for the join
form and the Near:. This version is only valid if expr is a singleton expression. Side
effects are the callers problem. I have no deep thoughts on binding and context issues,
but it has tested ok on simple trials. The fact that the equal? shown above has it's
argument list "satisfied" is understood but disturbing, meaning this assert doesn't really
pass mustard. Cheers, Brian