forked from hiiamboris/red-pure-fun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassert.red
45 lines (37 loc) · 1 KB
/
assert.red
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
Red [
title: "Assert function for contract programming"
file: %assert.red
author: [email protected]
tabs: 2
license: 'MIT
usage: {
`? assert` should help
uncomment the line at the end to disable assertions
}
]
#include %block-magic.red
unless attempt [get in system/words 'assert] [
assert: function [
"Yer typical assertion"
contract [block!] "contract"
/comment {
contract can have one the of 3 formats:
[condition "error message"]
[condition 'word-to-blame]
[condition] <- in this case last word of condition is blamed}
/local tmp
][
set [cond msg] tmp: block-magic/transmute contract ;-- TODO: replace this with `reduce contract` when GC is available
unless cond [
print ["ASSERTION FAILURE:" mold contract]
either string? msg [
cause-error 'script 'invalid-arg [msg]
][
if none? msg [msg: last contract]
cause-error 'script 'invalid-refine-arg [msg mold/part/flat get msg 1024]
]
]
block-magic/dispel tmp
]
assert: :comment ;-- uncomment to disable assertions
]