Fix ensure-failing-without-answers, per Caleb Phillips' macro-replacement idea

This commit is contained in:
Colin Jones 2011-08-13 12:16:34 -05:00
parent 0b9a04acf1
commit 0978ffad12

View File

@ -1,7 +1,7 @@
(ns path-to-answer-sheet (ns path-to-answer-sheet
(:use [runner.koans :only [ordered-koans]] (:use [runner.koans :only [ordered-koans]]
[path-to-enlightenment :only [meditations __ ___]] [path-to-enlightenment :only [meditations fancy-assert __ ___]]
[clojure.string :only [join split trim]])) [clojure.string :only [join split trim] :as string]))
(def answers (def answers
{"equalities" {"__" [true {"equalities" {"__" [true
@ -225,22 +225,34 @@
(defn print-non-failing-error [koan] (defn print-non-failing-error [koan]
(println (str "\n" koan ".clj is passing without filling in the blanks"))) (println (str "\n" koan ".clj is passing without filling in the blanks")))
(defmacro ensure-failure [& forms]
(let [pairs (partition 2 forms)
tests (map (fn [[doc# code#]]
`(if (try
(fancy-assert ~code# ~doc#)
false
(catch AssertionError e# true)
(catch Exception e# true))
:pass
(throw (AssertionError. (pr-str ~doc# ~code#)))))
pairs)]
`(do ~@tests)))
(defn ensure-failing-without-answers [] (defn ensure-failing-without-answers []
(if (every? (if (every?
(fn [koan] (fn [koan]
(let [form (koan-text koan) (let [form (koan-text koan)
form (string/replace form "(meditations" "(ensure-failure")
fake-err (java.io.PrintStream. (java.io.ByteArrayOutputStream.)) fake-err (java.io.PrintStream. (java.io.ByteArrayOutputStream.))
real-err System/err real-err System/err
result (try result (try
(System/setErr fake-err)
(load-string form) (load-string form)
true true
(catch AssertionError e false) (catch AssertionError e (prn e) false)
(catch Exception e false) (catch Exception e (prn e) false))]
(finally (System/setErr real-err)))]
(if result (if result
(print-non-failing-error koan) :pass
:pass))) (print-non-failing-error koan))))
ordered-koans) ordered-koans)
(println "\nTests all fail before the answers are filled in."))) (println "\nTests all fail before the answers are filled in.")))