Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

Assertions and DbC

 [1/2] 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

 [2/2] from: santilli:gabriele::gmail at: 23-Oct-2007 17:38


2007/10/2, Brian Tiffin <btiffin-rogers.com>:
> >> assert [equal? 12] > ** User Error: equal? 12 - assertion failed > ** Near: assert [equal? 12]
What happens here is: equal? 12 throw make error! ... ie. you are comparing 12 with the result of THROW (which never returns). That's why you don't get a complaint by EQUAL? (it never gets all its arguments). From the way you use it, it seems to me it may be better as: assert: func [[catch] expr [block!]] [ unless all expr [ throw make error! join mold/only expr " - assertion failed" ] ] (With that you can also write things like assert [a = 1 b = 2].) HTH, Gabriele.