mirror of
https://github.com/journeyman-cc/smeagol.git
synced 2026-04-12 18:05:06 +00:00
Implement basic test backticks reading and execution
This commit is contained in:
parent
10bfa57b47
commit
3f4380c8f5
3 changed files with 69 additions and 0 deletions
|
|
@ -6,6 +6,7 @@
|
||||||
[cemerick.url :refer (url url-encode url-decode)]
|
[cemerick.url :refer (url url-encode url-decode)]
|
||||||
[clj-yaml.core :as yaml]
|
[clj-yaml.core :as yaml]
|
||||||
[markdown.core :as md]
|
[markdown.core :as md]
|
||||||
|
[smeagol.test :as test]
|
||||||
[smeagol.configuration :refer [config]]))
|
[smeagol.configuration :refer [config]]))
|
||||||
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
@ -100,6 +101,12 @@
|
||||||
(str "<pre class=\"backticks\">```" (.trim text) "\n```</pre>"))
|
(str "<pre class=\"backticks\">```" (.trim text) "\n```</pre>"))
|
||||||
|
|
||||||
|
|
||||||
|
(defn process-test
|
||||||
|
"Takes at least 3 lines assuming first is a fn name, last is output, rest inside are arguments"
|
||||||
|
[^String text ^Integer index]
|
||||||
|
(test/process text index))
|
||||||
|
|
||||||
|
|
||||||
(defn get-first-token
|
(defn get-first-token
|
||||||
"Return the first space-separated token of this `string`."
|
"Return the first space-separated token of this `string`."
|
||||||
[^String string]
|
[^String string]
|
||||||
|
|
|
||||||
4
src/smeagol/sample.clj
Normal file
4
src/smeagol/sample.clj
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
(ns smeagol.sample)
|
||||||
|
|
||||||
|
(defn pow [x]
|
||||||
|
(int (Math/pow x 2)))
|
||||||
58
src/smeagol/test.clj
Normal file
58
src/smeagol/test.clj
Normal file
|
|
@ -0,0 +1,58 @@
|
||||||
|
(ns smeagol.test
|
||||||
|
(:require [clojure.string :as s]
|
||||||
|
[clojure.edn :as edn]))
|
||||||
|
|
||||||
|
(defn- execute [{:keys [fn in]}]
|
||||||
|
(try (let [result (apply fn in)]
|
||||||
|
{:result result})
|
||||||
|
(catch Exception e
|
||||||
|
{:error (.getMessage e)})))
|
||||||
|
|
||||||
|
(defn do-test [{:keys [fn in out] :as params}]
|
||||||
|
(let [{:keys [error result]} (execute params)]
|
||||||
|
(if error
|
||||||
|
{:result :error :error error}
|
||||||
|
(if (= result out)
|
||||||
|
{:result :ok}
|
||||||
|
{:result :failure :expected out :actual result}))))
|
||||||
|
|
||||||
|
(defn- parse-value [^String line]
|
||||||
|
(try
|
||||||
|
(let [value (edn/read-string line)]
|
||||||
|
{:line line :value value})
|
||||||
|
(catch Exception e
|
||||||
|
{:line line :error (.getMessage e)})))
|
||||||
|
|
||||||
|
|
||||||
|
(defn- parse-values [texts]
|
||||||
|
(reduce (fn [acc text]
|
||||||
|
(let [{:keys [value error] :as parsed} (parse-value text)]
|
||||||
|
(if error
|
||||||
|
(reduced parsed)
|
||||||
|
(update-in acc [:values] conj value))))
|
||||||
|
{:values []}
|
||||||
|
texts))
|
||||||
|
|
||||||
|
|
||||||
|
(defn parse [^String text]
|
||||||
|
(let [lines (s/split-lines text)]
|
||||||
|
(if (-> lines count (>= 3))
|
||||||
|
(let [[sym & rest] lines]
|
||||||
|
(if-let [fn (-> sym s/trim symbol resolve)] ;; TODO: require ns
|
||||||
|
(let [{:keys [values error line] :as x} (parse-values rest)]
|
||||||
|
(if error
|
||||||
|
{:error (str "Failed parsing line: " line " due: " error)}
|
||||||
|
(let [out (last values)
|
||||||
|
in (butlast values)]
|
||||||
|
{:fn fn :out out :in in :text text})))
|
||||||
|
{:error (str "No test found with name: " (pr-str sym)) :text text}))
|
||||||
|
{:error (str "There shoud be at least 3 lines (test name, input, output), given: " (count lines))
|
||||||
|
:text text})))
|
||||||
|
|
||||||
|
|
||||||
|
(defn process [^String text ^Integer index]
|
||||||
|
(let [{:keys [error] :as params} (parse text)]
|
||||||
|
(if error
|
||||||
|
(str "<pre class=\"error\">" (pr-str params) "</pre><pre>" text "</pre>")
|
||||||
|
(let [{:keys [result] :as test-result} (do-test params)]
|
||||||
|
(str "<pre class=\"" (name result) "\">" (pr-str test-result) "</pre><pre>" text "</pre>")))))
|
||||||
Loading…
Add table
Add a link
Reference in a new issue