From 552acb357f8f79d38b1a830069e0096937eb90ef Mon Sep 17 00:00:00 2001 From: jem Date: Wed, 22 May 2019 09:21:14 +0200 Subject: [PATCH] try to use macro created deftests --- main/src/data_test.clj | 34 +++++++++++++------ main/src/data_test/runner.clj | 13 ++++--- ...> should_test_with_data_macro_version.edn} | 0 test/src/data_test_test.clj | 4 +-- 4 files changed, 34 insertions(+), 17 deletions(-) rename test/resources/data_test_test/{should-test-with-data-macro-version.edn => should_test_with_data_macro_version.edn} (100%) diff --git a/main/src/data_test.clj b/main/src/data_test.clj index dca1827..9281cbd 100644 --- a/main/src/data_test.clj +++ b/main/src/data_test.clj @@ -23,14 +23,26 @@ [test-name :- s/Keyword] (runner/run-tests (runner/create-test-runner test-name))) -(defmacro defdatatest [name & body] - '(do - (clojure.test/deftest ~name - ;TODO-1: bring macro to work - ;TODO-2: crate filename out of package/namespace/test-name.edn - ;TODO-3: enable more than one test-data-set with optional infix .## - (let [testdata (data-test/read-data - (clojure.java.io/resource - "data_test_test/should-test-with-data-macro-version.edn")) - {:keys [input expectation]} testdata] - ~@body)))) +(defn datatestdef-form [namespaced-test-key body] + '(do + (clojure.test/deftest ~(symbol (name namespaced-test-key)) + ;(let [file-prefix (data-test.runner/data-file-prefix ~namespaced-test-key) + ; testdata (data-test.runner/load-test-data file-prefix) + ; {:keys [input expectation]} testdata] + ~@body))) + +(defmacro defdatatest [n & body] + (let [namespaced-test-key (keyword (str *ns*) (name n))] + (println namespaced-test-key) + (data-test/datatestdef-form namespaced-test-key body))) + +(defmacro defdatatest2 + "Defines ...." + [name & body] + (when ct/*load-tests* + `(def ~(vary-meta name assoc :test + `(fn [] + `(let [testdata `(runner/load-test-data `(runner/data-file-prefix name)) + {:keys [input expectation]} testdata] + ~@body))) + (fn [] (ct/test-var (var ~name)))))) diff --git a/main/src/data_test/runner.clj b/main/src/data_test/runner.clj index 01bd4f7..5e77023 100644 --- a/main/src/data_test/runner.clj +++ b/main/src/data_test/runner.clj @@ -25,6 +25,13 @@ {:input s/Any :expectation s/Any}) +(def TestResult + {:input s/Any + :expectation s/Any + :output s/Any + :passed? s/Bool + :error s/Any}) + (s/defrecord TestRunner [name :- s/Keyword] Object (toString [_] (str "TestRunner: " (:name _) "]"))) @@ -32,8 +39,7 @@ (defprotocol RunTest "Protocol for data driven tests" (name-prefix [dda-test]) - (run-tests [dda-test]) - ) + (run-tests [dda-test])) (s/defn dispatch-by-name :- s/Keyword "Dispatcher for data-tests." @@ -60,8 +66,7 @@ (s/defn load-test-data [file-prefix :- s/Str] (let [file-path (str file-prefix ".edn")] - (read-data - (io/resource file-path)))) + (read-data (io/resource file-path)))) (extend-type TestRunner RunTest diff --git a/test/resources/data_test_test/should-test-with-data-macro-version.edn b/test/resources/data_test_test/should_test_with_data_macro_version.edn similarity index 100% rename from test/resources/data_test_test/should-test-with-data-macro-version.edn rename to test/resources/data_test_test/should_test_with_data_macro_version.edn diff --git a/test/src/data_test_test.clj b/test/src/data_test_test.clj index bc52348..709942b 100644 --- a/test/src/data_test_test.clj +++ b/test/src/data_test_test.clj @@ -35,5 +35,5 @@ (deftest should-test-with-data-record-version (is (sut/test-with-data ::should-test-with-data-record-version))) -;;(macroexpand '(sut/defdatatest should-test-with-data-macro-version -;; (is ture))) +(sut/defdatatest should-test-with-data-macro-version + (is true))