Skip to content

A peer-to-peer stack for code collaboration

1.6 KB
Raw Blame
{:module 'prelude/test-eval
 :doc
   "Provides eval that adds a `:test` macro. If an the eval encounters an
   expression of the form `(:test \"desc\" tests...)` we add it to the vector
   stored in the `tests` ref. The tests are then run by `rad/prelude/test.rad`."
 :exports '[eval tests]}

(def tests
  "Reference that collects tests defined with the `:test` macro. This
  value is updated by a custom `eval`."
  (ref []))

;; Annoyingly verbose since we have neither the prelude nor pattern-matching.
(def eval
  "Evaluation function that adds :test macro to register tests."
  (fn [expr state]
    (if (list? expr)
        (if (eq? (first expr) :test)
            (do
                (def env (state->env state))
                (def test-def {:doc-test (drop 2 expr) :name (nth 1 expr) :env state})
                (def next-tests test-def)
                (def env_ (set-binding 'next-tests__ next-tests env))
                (def state (set-env env_ state))
                (eval '(do
                  (write-ref tests (add-right next-tests__ (read-ref tests)))
                  :nil
                ) state))
            (eval expr state))
        (eval expr state))))

(:test "'test' works (including :setup)"
    [:setup
       (do
          (def x 3)
          (def y (ref 0))
          (write-ref y 1))
    ]
    [(+ 3 2) ==> 5]
    [x       ==> 3]
)

(:test "'test' handles exceptions properly"
    [:setup
      (do
        (def x (catch 'any (throw 'blah) (fn [x] #t)))
        (def y (catch 'any (throw 'blah) (fn [x] #f)))
      )
    ]
    [x ==> #t]
    [y ==> #f]
)