Skip to content

Commit 230d279

Browse files
committed
Ensure meditations expressions alternate properly
Hat tip to @SeriousPony for the report
1 parent b3579b1 commit 230d279

File tree

4 files changed

+26
-17
lines changed

4 files changed

+26
-17
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ syntax: glob
2828
.cake/
2929
.lein-failures
3030
.lein-deps-sum
31+
.lein-repl-history
3132
autodoc/**

src/koan_engine/core.clj

+9-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,16 @@
44
(def __ :fill-in-the-blank)
55
(def ___ (fn [& args] __))
66

7+
(defn ensure-valid-meditation [doc-expression-pairs]
8+
(doseq [[doc expression] doc-expression-pairs]
9+
(when-not (string? doc)
10+
(throw (ex-info (str "Meditations must be alternating doc/expression pairs\n"
11+
"Expected " doc " to be a documentation string")
12+
{:line (:line (meta doc))}))))
13+
doc-expression-pairs)
14+
715
(defmacro meditations [& forms]
8-
(let [pairs (partition 2 forms)
16+
(let [pairs (ensure-valid-meditation (partition 2 forms))
917
tests (map (fn [[doc# code#]]
1018
`(u/fancy-assert ~code# ~doc#))
1119
pairs)]

src/koan_engine/koans.clj

+14-13
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,28 @@
2121
(first more)
2222
(recur more)))))
2323

24+
(defn report-error [file-path line error]
25+
(let [message (or (.getMessage error) (.toString error))]
26+
(println "\nNow meditate upon"
27+
(str file-path
28+
(when line (str ":" line))))
29+
(println "---------------------")
30+
(println "Assertion failed!")
31+
(println (.replaceFirst message "^Assert failed: " ""))))
32+
2433
(defn tests-pass? [dojo-path file-path]
2534
(u/with-dojo [dojo-path]
2635
(print "Considering" (str file-path "..."))
2736
(println)
2837
(flush)
2938
(try (load-file file-path)
3039
true
40+
(catch clojure.lang.ExceptionInfo ei
41+
(report-error file-path (:line (ex-data ei)) ei)
42+
false)
3143
(catch Throwable e
32-
(let [message (or (.getMessage e) (.toString e))
33-
; TODO: use ex-info or something to clean this up
34-
line (when-let [groups (first (re-seq #"^\[LINE (\d+)\] "
35-
message))]
36-
(last groups))]
37-
(println "\nNow meditate upon"
38-
(str file-path
39-
(when line (str ":" line))))
40-
(println "---------------------")
41-
(println "Assertion failed!")
42-
(println (.replaceFirst
43-
(.replaceFirst message "^Assert failed: " "")
44-
"^\\[LINE \\d+\\] " "")))
44+
45+
(report-error file-path nil e)
4546
false))))
4647

4748
(defn namaste []

src/koan_engine/util.clj

+2-3
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@
3535
([x message]
3636
`(try (safe-assert ~x ~message)
3737
(catch Throwable e#
38-
(throw (Exception. (str ~(when-let [line (:line (meta x))]
39-
(str "[LINE " line "] "))
40-
'~message "\n" '~x)))))))
38+
(throw (ex-info (str '~message "\n" '~x)
39+
{:line (:line (meta '~x))}))))))
4140

4241
(defn read-project []
4342
(let [rdr (clojure.lang.LineNumberingPushbackReader.

0 commit comments

Comments
 (0)