Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved all namespaces into koan-engine heirarchy.
- Loading branch information
Showing
11 changed files
with
219 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,4 +1,5 @@ | |||
(load "path_to_enlightenment") | (load "koan_engine/core") | ||
(do | (do | ||
(in-ns 'path-to-enlightenment) | (in-ns 'koan-engine.core) | ||
(run)) | (println "RUNNINBG") | ||
(-main)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,4 +1,4 @@ | |||
(load "path_to_answer_sheet") | (load "koan_engine/checker") | ||
(do | (do | ||
(in-ns 'path-to-answer-sheet) | (in-ns 'koan-engine.checker) | ||
(run)) | (run)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,94 @@ | |||
(ns koan-engine.checker | |||
(:use cascalog.api | |||
[koan-engine.koans :only [ordered-koans]] | |||
[koan-engine.core :only [meditations ?= __ ___]] | |||
[koan-engine.util :only [fancy-assert]] | |||
[clojure.string :only [join split trim] :as string])) | |||
|
|||
;; TODO: Use cascalog.api elsewhere. | |||
;; TODO: add ?= back in somewhere! | |||
;; TODO: Move out into resources directory! | |||
|
|||
(def answers | |||
{"tuples" {"__" [[["truth."]] | |||
[[1]] | |||
[[1]] | |||
[[1]] | |||
[[4]] | |||
'?x | |||
'?name '?name | |||
]}}) | |||
|
|||
(defn replace-with [s k replacements] | |||
(let [unreplaced-texts (split s (re-pattern (str "\\b" k "\\b")))] | |||
(join (butlast | |||
(interleave unreplaced-texts | |||
(concat (map pr-str replacements) | |||
(repeat k))))))) | |||
|
|||
;; TODO: Get path from root in resources directory. | |||
(defn koan-text [koan] | |||
(slurp (str "src/koans/" koan ".clj"))) | |||
|
|||
(defn answers-for [koan sym] | |||
((answers koan {}) sym [])) | |||
|
|||
(defn fill-in-answers [text koan sym] | |||
(replace-with text sym (answers-for koan sym))) | |||
|
|||
(defn print-non-failing-error [koan] | |||
(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))) | |||
|
|||
;; This needs to occur in the project-koan-specific namespace, else | |||
;; load-string won't have the proper requirements. | |||
;; | |||
;; In the actual koan we prefix form with some use and requires. | |||
(defn ensure-failing-without-answers [] | |||
(if (every? | |||
(fn [koan] | |||
(let [form (koan-text koan) | |||
form (string/replace form "(meditations" "(ensure-failure") | |||
fake-err (java.io.PrintStream. (java.io.ByteArrayOutputStream.)) | |||
real-err System/err | |||
result (try | |||
(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-passing-with-answers [] | |||
(try | |||
(dorun (map | |||
(fn [koan] | |||
(load-string | |||
(-> (koan-text 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 run [] | |||
(ensure-failing-without-answers) | |||
(ensure-passing-with-answers)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,27 @@ | |||
(ns koan-engine.core | |||
(:use [cascalog.testing :only (test?-)] | |||
[cascalog.util :only (defalias)] | |||
[koan-engine.freshness :only [setup-freshener]]) | |||
(:require [koan-engine.util :as u])) | |||
|
|||
(def __ :fill-in-the-blank) | |||
(def ___ (fn [& args] __)) | |||
|
|||
;; TODO: Place these in a required ns: | |||
;; [cascalog.testing :only (test?-)] | |||
;; [cascalog.util :only (defalias)] | |||
|
|||
;; TODO: Move out! | |||
;; More concise for koans. | |||
(defalias ?= test?-) | |||
|
|||
(defmacro meditations [& forms] | |||
(let [pairs (partition 2 forms) | |||
tests (map (fn [[doc# code#]] | |||
`(u/fancy-assert ~code# ~doc#)) | |||
pairs)] | |||
`(do ~@tests))) | |||
|
|||
(defn -main [] | |||
(u/require-version (u/parse-required-version)) | |||
(setup-freshener)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,50 @@ | |||
(ns koan-engine.util | |||
(:require [clojure.string :as s])) | |||
|
|||
(defn version< | |||
"< for Clojure's version map." | |||
[v1 v2] | |||
(let [[major-a major-b] (map :major [v1 v2]) | |||
[minor-a minor-b] (map :minor [v1 v2])] | |||
(or (< major-a major-b) | |||
(and (== major-a major-b) | |||
(< minor-a minor-b))))) | |||
|
|||
(defn require-version [[req-major req-minor]] | |||
(when (version< {:major req-major, :minor req-minor} | |||
*clojure-version*) | |||
(throw (Exception. | |||
(format "Clojure version %s.%s or higher required." | |||
req-major req-minor))))) | |||
|
|||
(defmacro safe-assert | |||
"Assertion with support for a message argument in all Clojure | |||
versions. (Pre-1.3.0, `assert` didn't accept a second argument and | |||
threw an error.)" | |||
([x] `(safe-assert ~x "")) | |||
([x msg] | |||
(if (version< *clojure-version* {:major 1, :minor 3}) | |||
`(assert ~x) | |||
`(assert ~x ~msg)))) | |||
|
|||
(defmacro fancy-assert | |||
"Assertion with fancy error messaging." | |||
([x] (fancy-assert x "")) | |||
([x message] | |||
`(try (safe-assert ~x ~message) | |||
(catch Exception e# | |||
(throw (Exception. (str '~message "\n" '~x ) | |||
e#)))))) | |||
|
|||
(defn parse-required-version [] | |||
(let [rdr (clojure.lang.LineNumberingPushbackReader. | |||
(java.io.FileReader. (java.io.File. "project.clj"))) | |||
project-form (read rdr) | |||
version-string (->> project-form | |||
(drop 3) | |||
(apply hash-map) | |||
:dependencies | |||
(map (fn [xs] (vec (take 2 xs)))) | |||
(into {}) | |||
('org.clojure/clojure))] | |||
(map read-string (take 3 (s/split version-string #"[\.\-]"))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.