add a non macro alternative
This commit is contained in:
parent
0dd940f820
commit
dc1055d1c4
5 changed files with 142 additions and 32 deletions
|
@ -15,27 +15,22 @@
|
||||||
; limitations under the License.
|
; limitations under the License.
|
||||||
(ns data-test
|
(ns data-test
|
||||||
(:require
|
(:require
|
||||||
[clojure.java.io :as io]
|
|
||||||
[clojure.test :as ct]
|
[clojure.test :as ct]
|
||||||
[schema.core :as s]
|
[schema.core :as s]
|
||||||
[aero.core :as aero]))
|
[data-test.runner :as runner]))
|
||||||
|
|
||||||
;TODO: replace schema with spec
|
(s/defn test-with-data
|
||||||
(def TestDataSet
|
[test-name :- s/Keyword]
|
||||||
{:input s/Any
|
(runner/run-test (runner/create-test-runner test-name)))
|
||||||
:expectation s/Any})
|
|
||||||
|
|
||||||
(defn read-data
|
|
||||||
[resource-url]
|
|
||||||
(aero/read-config resource-url))
|
|
||||||
|
|
||||||
(defmacro defdatatest [name & body]
|
(defmacro defdatatest [name & body]
|
||||||
`(do
|
'(do
|
||||||
(ct/deftest ~name
|
(clojure.test/deftest ~name
|
||||||
;TODO-1: bring macro to work
|
;TODO-1: bring macro to work
|
||||||
;TODO-2: crate filename out of package/namespace/test-name.edn
|
;TODO-2: crate filename out of package/namespace/test-name.edn
|
||||||
;TODO-3: enable more than one test-data-set with optional infix .##
|
;TODO-3: enable more than one test-data-set with optional infix .##
|
||||||
(let [testdata (sut/read-data
|
(let [testdata (data-test/read-data
|
||||||
(io/resource "data_test_test/should-test-with-data-macro-version.edn"))
|
(clojure.java.io/resource
|
||||||
{:keys [input expectation]} testdata]
|
"data_test_test/should-test-with-data-macro-version.edn"))
|
||||||
~body))))
|
{:keys [input expectation]} testdata]
|
||||||
|
~@body))))
|
||||||
|
|
74
main/src/data_test/runner.clj
Normal file
74
main/src/data_test/runner.clj
Normal file
|
@ -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))
|
|
@ -0,0 +1,2 @@
|
||||||
|
{:input 1
|
||||||
|
:expectation 1}
|
40
test/src/data_test/runner_test.clj
Normal file
40
test/src/data_test/runner_test.clj
Normal file
|
@ -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))))
|
|
@ -15,26 +15,25 @@
|
||||||
; limitations under the License.
|
; limitations under the License.
|
||||||
(ns data-test-test
|
(ns data-test-test
|
||||||
(:require
|
(:require
|
||||||
[clojure.java.io :as io]
|
|
||||||
[clojure.test :refer :all]
|
[clojure.test :refer :all]
|
||||||
|
[clojure.java.io :as io]
|
||||||
|
[schema.core :as s]
|
||||||
|
[data-test.runner :as runner]
|
||||||
[data-test :as sut]))
|
[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
|
(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]
|
{:keys [input expectation]} testdata]
|
||||||
(is (= expectation
|
(is (= expectation
|
||||||
input))))
|
input))))
|
||||||
|
|
||||||
(sut/defdatatest should-test-with-data-macro-version
|
(s/defmethod runner/data-test ::should-test-with-data-record-version
|
||||||
(is (= expectation
|
[_ input :- s/Any expectation :- s/Any]
|
||||||
input)))
|
(= expectation
|
||||||
|
input))
|
||||||
|
|
||||||
|
(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)))
|
||||||
|
|
Loading…
Reference in a new issue