Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Engine pulls paths and answers from :koan-resource.

  • Loading branch information...
commit 8a4cdb7084e4d1ea79588908b350d6c75d790cb8 1 parent acbb5ec
@sritchie sritchie authored
View
2  project.clj
@@ -1,4 +1,4 @@
-(defproject cascalog-koan-engine "0.1.0"
+(defproject koan-engine "0.1.0"
:description "Koan Engine for Clojure projects."
:dependencies [[org.clojure/clojure "1.3.0"]
[cascalog "1.8.4"]
View
8 resources/koans.clj
@@ -1 +1,7 @@
-[[]]
+[["tuples" {"__" [[["truth."]]
+ [[1]]
+ [[1]]
+ [[1]]
+ [[4]]
+ ?x
+ ?name ?name]}]]
View
101 src/koan_engine/checker.clj
@@ -1,19 +1,12 @@
(ns koan-engine.checker
(:refer-clojure :exclude [test])
- (:use [koan-engine.koans :only [ordered-koans]]
- [koan-engine.util :only [fancy-assert with-dojo]]
- [clojure.string :only [join split trim] :as string]))
+ (:use [clojure.java.io :only (resource)]
+ [koan-engine.koans :only [ordered-koans]]
+ [clojure.string :only [join split trim] :as string])
+ (:require [koan-engine.util :as u]))
-;; TODO: Move out into resources directory!
-(def answers
- {"tuples" {"__" [[["truth."]]
- [[1]]
- [[1]]
- [[1]]
- [[4]]
- '?x
- '?name '?name
- ]}})
+(defn mk-answers [koan-resource]
+ (into {} (u/try-read (resource koan-resource))))
(defn replace-with [s k replacements]
(let [unreplaced-texts (split s (re-pattern (str "\\b" k "\\b")))]
@@ -25,11 +18,12 @@
(defn koan-text [koan-root koan]
(slurp (str koan-root "/" koan ".clj")))
-(defn answers-for [koan sym]
- ((answers koan {}) sym []))
+(defn answers-for [koan-resource koan sym]
+ (let [answers (mk-answers koan-resource)]
+ ((answers koan {}) sym [])))
-(defn fill-in-answers [text koan sym]
- (replace-with text sym (answers-for koan sym)))
+(defn fill-in-answers [koan-resource text koan sym]
+ (replace-with text sym (answers-for koan-resource koan sym)))
(defn print-non-failing-error [koan]
(println (str "\n" koan ".clj is passing without filling in the blanks")))
@@ -38,7 +32,7 @@
(let [pairs (partition 2 forms)
tests (map (fn [[doc# code#]]
`(if (try
- (fancy-assert ~code# ~doc#)
+ (u/fancy-assert ~code# ~doc#)
false
(catch AssertionError e# true)
(catch Exception e# true))
@@ -47,39 +41,42 @@
pairs)]
`(do ~@tests)))
-(defn ensure-failing-without-answers [koan-root dojo-path]
- (if (every?
- (fn [koan]
- (let [form (koan-text koan-root koan)
- form (string/replace form "(meditations" "(ensure-failure")
- fake-err (java.io.PrintStream. (java.io.ByteArrayOutputStream.))
- real-err System/err
- result (try (with-dojo [dojo-path]
- (load-string form))
- true
- (catch AssertionError e (prn e) false)
- (catch Exception e (prn e) false))]
- (if result
- :pass
- (print-non-failing-error koan))))
- ordered-koans)
- (println "\nTests all fail before the answers are filled in.")))
+(defn ensure-failing-without-answers [opts]
+ (let [{:keys [dojo-resource koan-resource koan-root]} opts]
+ (when (every?
+ (fn [koan]
+ (let [form (koan-text koan-root koan)
+ form (string/replace form "(meditations" "(ensure-failure")
+ fake-err (java.io.PrintStream. (java.io.ByteArrayOutputStream.))
+ real-err System/err
+ result (try (u/with-dojo [dojo-resource]
+ (load-string form))
+ true
+ (catch AssertionError e (prn e) false)
+ (catch Exception e (prn e) false))]
+ (if result
+ :pass
+ (print-non-failing-error koan))))
+ (ordered-koans koan-resource))
+ (println "\nTests all fail before the answers are filled in."))))
-(defn ensure-passing-with-answers [koan-root dojo-path]
- (try (dorun
- (map
- (fn [koan]
- (with-dojo [dojo-path]
- (load-string (-> (koan-text koan-root koan)
- (fill-in-answers koan "__")
- (fill-in-answers koan "___")))))
- ordered-koans))
- (println "\nAll tests pass after the answers are filled in.")
- (catch Exception e
- (println "\nAnswer sheet fail: " e)
- (.printStackTrace e)
- (println "Answer sheet fail"))))
+(defn ensure-passing-with-answers [opts]
+ (let [{:keys [dojo-resource koan-resource koan-root]} opts
+ filler (partial fill-in-answers koan-resource)]
+ (try (dorun
+ (map
+ (fn [koan]
+ (u/with-dojo [dojo-resource]
+ (load-string (-> (koan-text koan-root koan)
+ (filler koan "__")
+ (filler koan "___")))))
+ (ordered-koans koan-resource)))
+ (println "\nAll tests pass after the answers are filled in.")
+ (catch Exception e
+ (println "\nAnswer sheet fail: " e)
+ (.printStackTrace e)
+ (println "Answer sheet fail")))))
-(defn test [{:keys [dojo-resource koan-root]}]
- (ensure-failing-without-answers koan-root dojo-resource)
- (ensure-passing-with-answers koan-root dojo-resource))
+(defn test [opts]
+ (ensure-failing-without-answers opts)
+ (ensure-passing-with-answers opts))
View
4 src/koan_engine/core.clj
@@ -4,10 +4,6 @@
(def __ :fill-in-the-blank)
(def ___ (fn [& args] __))
-;; TODO: Place these in a required ns:
-;; [cascalog.testing :only (test?-)]
-;; [cascalog.util :only (defalias)]
-
(defmacro meditations [& forms]
(let [pairs (partition 2 forms)
tests (map (fn [[doc# code#]]
View
18 src/koan_engine/freshness.clj
@@ -12,23 +12,25 @@
(constantly
(clj-files-in (file koan-root))))
-(defn report-refresh [dojo-path report]
+(defn report-refresh [opt-map report]
(when-let [refreshed-files (seq (:reloaded report))]
- (let [these-koans (filter
- (among-paths? refreshed-files)
- (ordered-koan-paths))]
- (when (every? (partial tests-pass? dojo-path)
+ (let [{:keys [dojo-resource koan-resource koan-root]} opt-map
+ path-seq (ordered-koan-paths koan-root koan-resource)
+ these-koans (filter (among-paths? refreshed-files)
+ path-seq)]
+ (when (every? (partial tests-pass? dojo-resource)
these-koans)
- (if-let [next-koan-file (file (next-koan-path (last these-koans)))]
+ (if-let [next-koan-file (file (next-koan-path path-seq
+ (last these-koans)))]
(report-refresh {:reloaded [next-koan-file]})
(namaste))))
(println))
:refreshed)
(defn refresh!
- [{:keys [koan-root dojo-resource]}]
+ [{:keys [koan-root] :as opts}]
(freshener (files-to-keep-fresh koan-root)
- (partial report-refresh dojo-resource)))
+ (partial report-refresh opts)))
(def scheduler (ScheduledThreadPoolExecutor. 1))
View
23 src/koan_engine/koans.clj
@@ -3,28 +3,19 @@
(:require [koan-engine.util :as u]))
;; TODO: Proper koan validation. Accept the path as an argument.
-(defn ordered-koans []
- (if-let [conf-path (resource "koans.clj")]
- (try (let [conf (-> conf-path slurp read-string)]
- (u/safe-assert (map? conf) "Koans aren't valid!")
- conf)
- (catch RuntimeException e
- (throw (Exception. "Error reading job-conf.clj!\n\n") e)))
- {}))
+(defn ordered-koans [answer-path]
+ (map first (u/try-read (resource answer-path))))
-(def ordered-koans
- ["tuples"])
-
-(defn ordered-koan-paths []
+(defn ordered-koan-paths [koan-root answer-path]
(map (fn [koan-name]
- (.getCanonicalPath (file "src" "koans" (str koan-name ".clj"))))
- ordered-koans))
+ (.getCanonicalPath (file koan-root (str koan-name ".clj"))))
+ (ordered-koans answer-path)))
(defn among-paths? [files]
(into #{} (map #(.getCanonicalPath %) files)))
-(defn next-koan-path [last-koan-path]
- (loop [[this-koan & more :as koan-paths] (ordered-koan-paths)]
+(defn next-koan-path [koan-path-seq last-koan-path]
+ (loop [[this-koan & more :as koan-paths] koan-path-seq]
(when (seq more)
(if (= last-koan-path this-koan)
(first more)
View
8 src/koan_engine/runner.clj
@@ -3,19 +3,13 @@
(:require [koan-engine.util :as u]
[koan-engine.checker :as checker]))
-;; TODO: Incorporate each of these custom options in the runner.
-;; koan-root: check!
-;; dojo-resource: check!
-;; answer-resource: NOPE
-;; koan-resource: NOPE
-
(def default-koan-map
{:koan-root "src/koans"
:dojo-resource "dojo.clj"
:koan-resource "koans.clj"})
;; TODO: Run validations on koan-map.
-;; Do all resources exist?
+;; Do all resources exist? Does this matter?
(defn -main [task]
(u/require-version (u/parse-required-version))
(let [koan-map (merge default-koan-map
View
3  src/koan_engine/util.clj
@@ -52,6 +52,9 @@
('org.clojure/clojure))]
(map read-string (take 3 (s/split version-string #"[\.\-]")))))
+(defn try-read [path]
+ (when path (read-string (slurp path))))
+
(defmacro do-isolated [& forms]
`(binding [*ns* (create-ns (gensym "jail"))]
(refer 'clojure.core)
Please sign in to comment.
Something went wrong with that request. Please try again.