mirror of
https://github.com/carp-lang/Carp.git
synced 2024-09-11 05:25:28 +03:00
Proper 'assert' macro that reports source location.
This commit is contained in:
parent
ed095b1baa
commit
a25606725e
@ -29,11 +29,13 @@
|
||||
(IO.println &(str &x))
|
||||
x))
|
||||
|
||||
;; TODO: Should be a macro and provide the location of the assertion.
|
||||
;; Also its output looks weird because of lack of 'prn'.
|
||||
(defn assert-eq [expected got]
|
||||
(if (= expected got)
|
||||
()
|
||||
(println* "ASSERTION FAILED! Expected " expected " but got " got)))
|
||||
|
||||
)
|
||||
|
||||
(defmacro assert [expr]
|
||||
(list 'if (list '= true expr)
|
||||
()
|
||||
(list 'do
|
||||
(list 'IO.println (list 'ref
|
||||
(list 'String.append (list 'String.copy "Assertion failed ")
|
||||
(list 'String.copy (source-location)))))
|
||||
'(System.exit 1))))
|
||||
|
@ -6,3 +6,9 @@
|
||||
;; (defn f []
|
||||
;; (for [n 1.4f 123.2f] ;; Should report error here, not in Macros.carp
|
||||
;; ()))
|
||||
|
||||
(defn f [x]
|
||||
(assert x))
|
||||
|
||||
(defn main []
|
||||
(f false))
|
||||
|
@ -8,4 +8,4 @@
|
||||
|
||||
;; The one allocation left after 'carp_init_globals' should be 'g' itself:
|
||||
(defn main []
|
||||
(Debug.assert-eq 1l (Debug.memory-balance)))
|
||||
(assert (= 1l (Debug.memory-balance))))
|
||||
|
Loading…
Reference in New Issue
Block a user