Skip to content

Commit

Permalink
core: Add the ys::taptest testing library
Browse files Browse the repository at this point in the history
  • Loading branch information
ingydotnet committed Jul 4, 2024
1 parent 64af767 commit f9e902a
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
6 changes: 5 additions & 1 deletion core/src/yamlscript/runtime.clj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
[ys.json]
[ys.yaml]
[ys.ys :as ys]
[ys.taptest]
[yamlscript.common :as common]
[yamlscript.util
:refer [abspath
Expand Down Expand Up @@ -126,7 +127,10 @@
'std (use-ns 'std ys.std)
'ys (use-ns 'ys ys.ys)
'json (use-ns 'json ys.json)
'yaml (use-ns 'yaml ys.yaml)}
'yaml (use-ns 'yaml ys.yaml)

'ys.taptest (use-ns 'ys.taptest ys.taptest)}


:classes (classes-map
'[clojure.lang.Atom
Expand Down
69 changes: 69 additions & 0 deletions core/src/ys/taptest.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
(ns ys.taptest
(:require
[clojure.string :as str]
[ys.ys :as ys]
[yamlscript.util :refer [die]])
(:refer-clojure
:exclude [test]))

(def counter (atom 0))

(defn- passed [test]
(let
[name (get test "name")
count (deref counter)]
(if name
(println (str "ok " count " - " name))
(println (str "ok " count)))))

(defn- failed [test got]
(let
[name (get test "name")
count (deref counter)
want (get test "want")]
(if name
(println (str "not ok " count " - " name))
(println (str "not ok " count)))
(println
(str " got: '" got "'\nexpected: '" want "'" "\n"))))

(defn plan [n]
(println (str "1.." n)))

(defn test [tests]
(doall
(for [test tests]
(let
[count (swap! counter inc)
code (or
(get test "eval")
(die (str "taptest: Test " count " is missing 'eval' key")))
code (str "!yamlscript/v0\n" code "\n")
got (cond
(string? code) (ys/eval code)
:else (die (str "taptest: Invalid eval: '"
code "'. Must be string.")))
want (get test "want")
like (get test "like")
has (get test "has")]
(cond
want (if (= got want)
(passed test)
(failed test got))
like (let [rgx (re-pattern like)]
(if (re-find rgx got)
(passed test)
(failed test got)))
has (if (str/includes? got has)
(passed test)
(failed test got))
:else (die (str "taptest: Test " count
" requires one of: 'want', 'like', 'has'")))))))

(defn done
([n] (let [n (or n (deref counter))]
(println (str "1.." n))))
([] (done nil)))

(comment
)
9 changes: 9 additions & 0 deletions core/src/ys/ys.clj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
get-yspath]])
(:refer-clojure
:exclude [compile
eval
for
load-file
use
Expand Down Expand Up @@ -73,6 +74,14 @@
(defn if [cond then else]
(if cond then else))

(defn eval [ys-code]
(let [clj-code (yamlscript.compiler/compile ys-code)
ret (sci/binding
[sci/file "EVAL"
FILE "EVAL"]
(sci/eval-string+ @sci-ctx clj-code))]
(:val ret)))

(defn load-file [ys-file]
(let [ys-file (abspath ys-file (dirname @sci/file))
clj-code (->
Expand Down

0 comments on commit f9e902a

Please sign in to comment.