From dc1055d1c48e54534c140356cc6ce68e86699435 Mon Sep 17 00:00:00 2001 From: jem Date: Sun, 19 May 2019 11:39:19 +0200 Subject: [PATCH] add a non macro alternative --- main/src/data_test.clj | 33 ++++----- main/src/data_test/runner.clj | 74 +++++++++++++++++++ .../should-test-with-data-record-version.edn | 2 + test/src/data_test/runner_test.clj | 40 ++++++++++ test/src/data_test_test.clj | 25 +++---- 5 files changed, 142 insertions(+), 32 deletions(-) create mode 100644 main/src/data_test/runner.clj create mode 100644 test/resources/data_test_test/should-test-with-data-record-version.edn create mode 100644 test/src/data_test/runner_test.clj diff --git a/main/src/data_test.clj b/main/src/data_test.clj index d7b7458..f61418e 100644 --- a/main/src/data_test.clj +++ b/main/src/data_test.clj @@ -15,27 +15,22 @@ ; limitations under the License. (ns data-test (:require - [clojure.java.io :as io] [clojure.test :as ct] [schema.core :as s] - [aero.core :as aero])) + [data-test.runner :as runner])) -;TODO: replace schema with spec -(def TestDataSet - {:input s/Any - :expectation s/Any}) - -(defn read-data - [resource-url] - (aero/read-config resource-url)) +(s/defn test-with-data + [test-name :- s/Keyword] + (runner/run-test (runner/create-test-runner test-name))) (defmacro defdatatest [name & body] - `(do - (ct/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 (sut/read-data - (io/resource "data_test_test/should-test-with-data-macro-version.edn")) - {:keys [input expectation]} testdata] - ~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)))) diff --git a/main/src/data_test/runner.clj b/main/src/data_test/runner.clj new file mode 100644 index 0000000..ff082fc --- /dev/null +++ b/main/src/data_test/runner.clj @@ -0,0 +1,74 @@ +; Licensed to the Apache Software Foundation (ASF) under one +; or more contributor license agreements. See the NOTICE file +; distributed with this work for additional information +; regarding copyright ownership. The ASF licenses this file +; to you under the Apache License, Version 2.0 (the +; "License"); you may not use this file except in compliance +; with the License. You may obtain a copy of the License at +; +; http://www.apache.org/licenses/LICENSE-2.0 +; +; Unless required by applicable law or agreed to in writing, software +; distributed under the License is distributed on an "AS IS" BASIS, +; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +; See the License for the specific language governing permissions and +; limitations under the License. +(ns data-test.runner + (:require + [clojure.java.io :as io] + [clojure.string :as str] + [schema.core :as s] + [aero.core :as aero])) + +;TODO: replace schema with spec +(def TestDataSet + {:input s/Any + :expectation s/Any}) + +(s/defrecord TestRunner [name :- s/Keyword] + Object + (toString [_] (str "TestRunner: " (:name _) "]"))) + +(defprotocol RunTest + "Protocol for data driven tests" + (run-test [dda-test]) + (name-prefix [dda-test])) + +(s/defn dispatch-by-name :- s/Keyword + "Dispatcher for data-tests." + [runner :- TestRunner + input :- s/Any + expectation :- s/Any] + (:name runner)) + +(defmulti data-test + "Multimethod for data-test." + dispatch-by-name) + +(s/defn read-data :- TestDataSet + [resource-url :- s/Str] + (aero/read-config resource-url)) + +(s/defn data-file-prefix :- s/Str + [runner :- TestRunner] + (let [name-key (:name runner)] + (str/replace + (str/replace (str (namespace name-key) "/" (name name-key)) + #"-" "_") + #"\." "/"))) + +(extend-type TestRunner + RunTest + (name-prefix [_] + (data-file-prefix _)) + (run-test [_] + (println (data-file-prefix _)) + (let [testdata (read-data + (io/resource "data_test_test/should-test-with-data-record-version.edn")) + ;(str (data-file-prefix _) ".edn"))) + {:keys [input expectation]} testdata] + (data-test _ input expectation)))) + +(defn create-test-runner + [test-name] + (->TestRunner test-name)) diff --git a/test/resources/data_test_test/should-test-with-data-record-version.edn b/test/resources/data_test_test/should-test-with-data-record-version.edn new file mode 100644 index 0000000..030c81f --- /dev/null +++ b/test/resources/data_test_test/should-test-with-data-record-version.edn @@ -0,0 +1,2 @@ +{:input 1 + :expectation 1} \ No newline at end of file diff --git a/test/src/data_test/runner_test.clj b/test/src/data_test/runner_test.clj new file mode 100644 index 0000000..8855c1c --- /dev/null +++ b/test/src/data_test/runner_test.clj @@ -0,0 +1,40 @@ +; Licensed to the Apache Software Foundation (ASF) under one +; or more contributor license agreements. See the NOTICE file +; distributed with this work for additional information +; regarding copyright ownership. The ASF licenses this file +; to you under the Apache License, Version 2.0 (the +; "License"); you may not use this file except in compliance +; with the License. You may obtain a copy of the License at +; +; http://www.apache.org/licenses/LICENSE-2.0 +; +; Unless required by applicable law or agreed to in writing, software +; distributed under the License is distributed on an "AS IS" BASIS, +; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +; See the License for the specific language governing permissions and +; limitations under the License. +(ns data-test.runner-test + (:require + [clojure.java.io :as io] + [clojure.test :refer :all] + [schema.core :as s] + [data-test.runner :as sut])) + +(deftest should-read-data + (is (= {:simple "test"} + (sut/read-data (io/resource "simple_aero.edn")))) + (is (= {:to-be-refernced "ref-test", :key1 "ref-test", :key2 "ref-test"} + (sut/read-data (io/resource "tagged_aero.edn")))) + ) + +(deftest should-calculate-data-file-prefix + (is (= "data_test/runner_test/test_it" + (sut/data-file-prefix (sut/create-test-runner ::test-it))))) + +(s/defmethod sut/data-test ::test-it + [_ input :- s/Any expectation :- s/Any] + "my-result") + +(deftest should-reslove-multimethod + (is (= "my-result" + (sut/data-test (sut/create-test-runner ::test-it) nil nil)))) diff --git a/test/src/data_test_test.clj b/test/src/data_test_test.clj index bd427c3..bc52348 100644 --- a/test/src/data_test_test.clj +++ b/test/src/data_test_test.clj @@ -15,26 +15,25 @@ ; limitations under the License. (ns data-test-test (:require - [clojure.java.io :as io] [clojure.test :refer :all] + [clojure.java.io :as io] + [schema.core :as s] + [data-test.runner :as runner] [data-test :as sut])) -(deftest should-read-data - (is (= {:simple "test"} - (sut/read-data (io/resource "simple_aero.edn")))) - (is (= {:to-be-refernced "ref-test", :key1 "ref-test", :key2 "ref-test"} - (sut/read-data (io/resource "tagged_aero.edn")))) - ) - (deftest should-test-with-data-explicit-version - (let [testdata (sut/read-data (io/resource "data_test_test/should-test-with-data-explicit-version.edn")) + (let [testdata (runner/read-data (io/resource "data_test_test/should-test-with-data-explicit-version.edn")) {:keys [input expectation]} testdata] (is (= expectation input)))) -(sut/defdatatest should-test-with-data-macro-version - (is (= expectation - input))) +(s/defmethod runner/data-test ::should-test-with-data-record-version + [_ input :- s/Any expectation :- s/Any] + (= expectation + input)) +(deftest should-test-with-data-record-version + (is (sut/test-with-data ::should-test-with-data-record-version))) - \ No newline at end of file +;;(macroexpand '(sut/defdatatest should-test-with-data-macro-version +;; (is ture)))