biscuit

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

assert.fnl (883B)


      1 ;; these are macros; this module should be imported
      2 ;; using import-macros
      3 
      4 ;; e.g. (import-macros { : expect= } :anoia.assert)
      5 
      6 (fn expect [assertion]
      7   (let [msg (.. "expectation failed: " (view assertion))]
      8     `(when (not ,assertion)
      9        (assert false ,msg))))
     10 
     11 (fn expect= [actual expected]
     12   `(let [view# (. (require :fennel) :view)
     13          ve# (view# ,expected)
     14          va# (view# ,actual)]
     15      (when (not (= ve# va#))
     16        (assert false
     17                (.. "\nexpected " ve# "\ngot " va#)
     18                ))))
     19 
     20 (fn expect-near [actual expected]
     21   `(let [delta# (math.abs (- ,actual ,expected))
     22          e# ,expected
     23          a# ,actual]
     24      (assert (< delta# 0.00000001)
     25              (..  "expected " e# " to be approximately " a#))))
     26 
     27 
     28 (fn define-tests [& body]
     29   (when _G.RUNNING_TESTS
     30     `(do ,(unpack body))))
     31 
     32 { : define-tests : expect : expect= : expect-near }