Introduce the auto-refreshing runner via fresh.

This commit is contained in:
Colin Jones 2011-04-29 22:54:49 -05:00
parent 7408b164fc
commit 3644a48bcd
3 changed files with 69 additions and 25 deletions

View File

@ -1,4 +1,5 @@
(defproject functional-koans "0.3.7" (defproject functional-koans "0.3.7"
:description "The functional koans" :description "The functional koans"
:dependencies [[org.clojure/clojure "1.3.0-alpha6"] :dependencies [[org.clojure/clojure "1.3.0-alpha6"]
[fresh "1.0.1"]
[jline "0.9.94" :exclusions [junit]]]) [jline "0.9.94" :exclusions [junit]]])

View File

@ -212,18 +212,17 @@
(println (str "\n" koan ".clj is passing without filling in the blanks"))) (println (str "\n" koan ".clj is passing without filling in the blanks")))
(defn ensure-failing-without-answers [] (defn ensure-failing-without-answers []
(binding [path-to-enlightenment/handle-problem (binding [clojure.test/*test-out*
(constantly :correctly-failing-test)
clojure.test/*test-out*
(java.io.PrintWriter. (java.io.ByteArrayOutputStream.))] (java.io.PrintWriter. (java.io.ByteArrayOutputStream.))]
(dorun (map (if (every?
(fn [koan] (fn [koan]
(let [form (koan-text koan) (let [form (koan-text koan)
result (load-string form)] result (load-string form)]
(when (= :pass result) (if result
(print-non-failing-error koan)))) (print-non-failing-error koan)
:pass)))
ordered-koans)))) ordered-koans)
(println "\nTests all fail before the answers are filled in."))))
(defn ensure-passing-with-answers [] (defn ensure-passing-with-answers []
(try (try
@ -234,7 +233,7 @@
(fill-in-answers koan "__") (fill-in-answers koan "__")
(fill-in-answers koan "___")))) (fill-in-answers koan "___"))))
ordered-koans)) ordered-koans))
(println "\nAll tests pass when the answers are filled in.") (println "\nAll tests pass after the answers are filled in.")
(catch Exception e (catch Exception e
(println "\nAnswer sheet fail: " e) (println "\nAnswer sheet fail: " e)

View File

@ -1,26 +1,23 @@
(ns path-to-enlightenment (ns path-to-enlightenment
(:use [clojure.test]) (:use [fresh.core :only [clj-files-in freshener]]
[clojure.java.io :only [file]])
(:require [clojure.set] (:require [clojure.set]
[clojure.string] [clojure.string]
[clojure.test])) [clojure.test])
(:import
[java.util.concurrent ScheduledThreadPoolExecutor TimeUnit]))
(def __ :fill-in-the-blank) (def __ :fill-in-the-blank)
(def ___ (fn [& args] __)) (def ___ (fn [& args] __))
(defn ^:dynamic handle-problem []
(System/exit 0))
(defmacro meditations [& forms] (defmacro meditations [& forms]
(let [pairs (partition 2 forms) (let [pairs (conj (partition 2 forms)
pairs (conj pairs
["Bootstrap for file/line reporting" ["Bootstrap for file/line reporting"
'(clojure.test/is (= 0 0))]) '(clojure.test/is (= 0 0))])
tests (map (fn [[doc# code#]] tests (map (fn [[doc# code#]]
`(if (is ~code# ~doc#) `(clojure.test/is ~code# ~doc#))
:pass
(handle-problem)))
pairs)] pairs)]
`(do ~@tests))) `(and ~@tests)))
(def ordered-koans (def ordered-koans
["equalities" ["equalities"
@ -63,7 +60,54 @@
(map read-string (map read-string
(take 3 (clojure.string/split version-string #"[\.\-]"))))) (take 3 (clojure.string/split version-string #"[\.\-]")))))
(defn files-to-keep-fresh []
(clj-files-in (file "src" "koans")))
(defn- tests-pass? [file-path]
(use 'path-to-enlightenment)
(load-file file-path))
(defn- among-paths? [files]
(into #{} (map #(.getCanonicalPath %) files)))
(defn- ordered-koan-paths []
(map (fn [koan-name]
(.getCanonicalPath (file "src" "koans" (str koan-name ".clj"))))
ordered-koans))
(defn- next-koan-path [last-koan-path]
(loop [[this-koan & more :as koan-paths] (ordered-koan-paths)]
(when (seq more)
(if (= last-koan-path this-koan)
(first more)
(recur more)))))
(defn namaste []
(println "\nYou have achieved clojure enlightenment. Namaste."))
(defn report-refresh [report]
(when-let [refreshed-files (seq (:reloaded report))]
(let [these-koans (filter
(among-paths? refreshed-files)
(ordered-koan-paths))]
(println "Refreshing:" these-koans)
(when (every? tests-pass? these-koans)
(if-let [next-koan-file (file (next-koan-path (last these-koans)))]
(report-refresh {:reloaded [next-koan-file]})
(namaste))))
(println))
:refreshed)
(def refresh! (freshener files-to-keep-fresh report-refresh))
(def scheduler (ScheduledThreadPoolExecutor. 1))
(defn setup-freshener []
(.scheduleWithFixedDelay scheduler refresh! 0 500 TimeUnit/MILLISECONDS)
(.awaitTermination scheduler Long/MAX_VALUE TimeUnit/SECONDS))
(defn run [] (defn run []
(require-version (parse-required-version)) (require-version (parse-required-version))
(doall (map (comp load (partial str "koans/")) ordered-koans)) (setup-freshener))
(println "You have achieved clojure enlightenment. Namaste."))