Skip to content

Instantly share code, notes, and snippets.

@cgrand
Created April 1, 2010 09:36
Show Gist options
  • Save cgrand/351598 to your computer and use it in GitHub Desktop.
Save cgrand/351598 to your computer and use it in GitHub Desktop.
(ns net.cgrand.repl-ay)
(defn- prompt? [x] (and (symbol? x) (.endsWith (name x) "=>")))
(def *last-ns-name*)
(defn replay* [forms]
(loop [forms forms ])
(when-let [[form & forms] (seq forms)]
(if (prompt? form)
(recur forms)
(let [r (eval form)]
(if (and forms (prompt? (first forms)))
(recur (next forms))
(let [[expected & forms] forms]
(when-not (= r expected)
(println "\nUnexpected value on line" (-> form meta :line)
"file" (-> form meta :file))
(prn '=> form)
(print "expected: ") (prn expected)
(print "actual: ") (prn r))
(recur forms)))))))
(defmacro replay [& forms]
(let [ns-name (gensym "net.cgrand.repl-ay.session")
[ns-form forms] (if (and (seq? (first forms)) (= 'ns (ffirst forms)))
[(first forms) (next forms)] ['(ns foo) forms])
ns-form (list* 'ns ns-name (nnext ns-form))]
`(binding [*last-ns-name* (ns-name *ns*)]
(eval '~ns-form)
(try
(replay* '~(vec forms))
(finally
(remove-ns '~ns-name)
(in-ns *last-ns-name*))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment