add multi-data-spec ability

This commit is contained in:
jem 2019-05-28 18:53:08 +02:00
parent 67af5c1d67
commit 4240c2744b
7 changed files with 32 additions and 23 deletions

View file

@ -28,13 +28,12 @@
(defmacro defdatatest [n bindings & body]
(when ct/*load-tests*
(let [namespaced-test-key# (keyword (str *ns*) (name n))
file-prefix# (fl/data-test-spec-file-prefix namespaced-test-key#)]
(let [namespaced-test-key# (keyword (str *ns*) (name n))]
`(def ~(vary-meta n assoc
:test `(fn []
(let [testdata# (fl/load-test-data ~file-prefix#)
~(symbol (first bindings)) (:input testdata#)
~(symbol (second bindings)) (:expectation testdata#)]
~@body))
:data-spec-prefix file-prefix#)
(doseq [data-spec# (fl/load-data-test-specs ~namespaced-test-key#)]
(let [~(symbol (first bindings)) (:input data-spec#)
~(symbol (second bindings)) (:expectation data-spec#)]
~@body)))
:data-spec-key namespaced-test-key#)
(fn [] (ct/test-var (var ~n)))))))

View file

@ -25,6 +25,11 @@
{:input s/Any
:expectation s/Any})
(def RuntimeTestDataSpec
(merge
TestDataSpec
{:data-spec-file s/Str}))
(s/defn read-test-data-spec :- TestDataSpec
[resource-url :- s/Str]
(aero/read-config resource-url))
@ -43,13 +48,15 @@
(map #(str prefix "." % ".edn")
(range 10)))))
(s/defn load-data-test-spec
(s/defn load-data-test-spec :- RuntimeTestDataSpec
[file-path :- s/Str]
(let [file-resource (io/resource file-path)]
(when file-resource
(read-test-data-spec file-resource))))
(merge
{:data-spec-file file-path}
(read-test-data-spec file-resource)))))
(s/defn load-data-test-specs :- [TestDataSpec]
(s/defn load-data-test-specs :- [RuntimeTestDataSpec]
[name-key :- s/Keyword]
(let [data-test-specs (filter some?
(map load-data-test-spec (data-test-spec-file-names name-key)))]
@ -58,13 +65,3 @@
{:message "Could not find test spec"
:name-key name-key}))
(into [] data-test-specs))))
(s/defn load-test-data
[file-prefix :- s/Str]
(let [file-path (str file-prefix ".edn")]
(try
(read-test-data-spec (io/resource file-path))
(catch IllegalArgumentException e
(throw (ex-info (str "Could not find test spec on " file-path)
{:message "Could not find test spec"
:file-path file-prefix} e))))))

View file

@ -50,7 +50,7 @@
(name-prefix [_]
(fl/data-test-spec-file-prefix (:name _)))
(run-tests [_]
(let [testdata (fl/load-test-data (fl/data-test-spec-file-prefix (:name _)))
(let [testdata (fl/load-data-test-spec (fl/data-test-spec-file-prefix (:name _)))
{:keys [input expectation]} testdata]
(data-test _ input expectation))))

View file

@ -0,0 +1,2 @@
{:input 1
:expectation 1}

View file

@ -0,0 +1,2 @@
{:input 2
:expectation 2}

View file

@ -46,7 +46,8 @@
(sut/data-test-spec-file-names ::test-it))))
(deftest should-load-data
(is (= {:test "data"}
(is (= {:test "data"
:data-spec-file "data_test/file_loader_test/test_it.edn"}
(sut/load-data-test-spec (str (sut/data-test-spec-file-prefix ::test-it) ".edn")))))
(deftest should-not-load-non-existing-data
@ -54,7 +55,12 @@
(sut/load-data-test-spec (str (sut/data-test-spec-file-prefix ::not-existing) ".edn")))))
(deftest should-load-data-test-specs
(is (= [{:test "data"} {:test "data1"} {:test "data9"}]
(is (= [{:test "data"
:data-spec-file "data_test/file_loader_test/test_it.edn"}
{:test "data1"
:data-spec-file "data_test/file_loader_test/test_it.1.edn"}
{:test "data9"
:data-spec-file "data_test/file_loader_test/test_it.9.edn"}]
(sut/load-data-test-specs ::test-it))))
(deftest should-throw-exception

View file

@ -40,6 +40,9 @@
; ---------------------------- macro -----------------------------
(sut/defdatatest should-test-with-data-macro-version [input expectation]
(is (= input expectation)))
(sut/defdatatest should-test-multiple-specs [input expectation]
(is (= input expectation)))
(macroexpand-1 '(sut/defdatatest should-test-with-data-macro-version [input expectation] (is (= 1 1))))