remove multimethod-runner & add first output

This commit is contained in:
jem 2019-05-29 17:39:51 +02:00
parent 15637254ae
commit fc7a14a2de
3 changed files with 21 additions and 77 deletions

View file

@ -17,15 +17,11 @@
(:require (:require
[clojure.test :as ct] [clojure.test :as ct]
[schema.core :as s] [schema.core :as s]
[data-test.runner :as runner] [data-test.reporter :as reporter]
[data-test.file-loader :as fl])) [data-test.file-loader :as fl]))
(def TestDataSpec fl/TestDataSpec) (def TestDataSpec fl/TestDataSpec)
(s/defn test-with-data
[test-name :- s/Keyword]
(runner/run-tests (runner/create-test-runner test-name)))
(defmacro defdatatest [n bindings & body] (defmacro defdatatest [n bindings & body]
(when ct/*load-tests* (when ct/*load-tests*
(let [namespaced-test-key# (keyword (str *ns*) (name n))] (let [namespaced-test-key# (keyword (str *ns*) (name n))]
@ -34,9 +30,13 @@
(doseq [data-spec# (fl/load-data-test-specs ~namespaced-test-key#)] (doseq [data-spec# (fl/load-data-test-specs ~namespaced-test-key#)]
(let [~(symbol (first bindings)) (:input data-spec#) (let [~(symbol (first bindings)) (:input data-spec#)
~(symbol (second bindings)) (:expectation data-spec#) ~(symbol (second bindings)) (:expectation data-spec#)
data-spec-file# (:data-spec-file data-spec#)] data-spec-file# (:data-spec-file data-spec#)
message# (new java.io.StringWriter)]
(binding [ct/*testing-contexts* (binding [ct/*testing-contexts*
(conj ct/*testing-contexts* data-spec-file#)] (conj ct/*testing-contexts* data-spec-file#)
~@body)))) ct/*test-out* message#]
~@body)
(reporter/write data-spec-file# data-spec# message#)
)))
:data-spec-key namespaced-test-key#) :data-spec-key namespaced-test-key#)
(fn [] (ct/test-var (var ~n))))))) (fn [] (ct/test-var (var ~n)))))))

View file

@ -13,16 +13,19 @@
; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
; See the License for the specific language governing permissions and ; See the License for the specific language governing permissions and
; limitations under the License. ; limitations under the License.
(ns data-test.runner-test (ns data-test.reporter
(:require (:require
[clojure.test :refer :all] [clojure.java.io :as io]
[clojure.string :as str]
[schema.core :as s] [schema.core :as s]
[data-test.runner :as sut])) [aero.core :as aero]))
(s/defmethod sut/data-test ::test-it (s/defn write
[_ input :- s/Any expectation :- s/Any] [data-spec-file :- s/Str
"my-result") data-spec
message]
(deftest should-reslove-multimethod (let [output-file (str "target/datatest/" data-spec-file)]
(is (= "my-result" (clojure.java.io/make-parents output-file)
(sut/data-test (sut/create-test-runner ::test-it) nil nil)))) (spit output-file (merge
{:message (str message)}
data-spec))))

View file

@ -1,59 +0,0 @@
; 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
[schema.core :as s]
[data-test.file-loader :as fl]))
(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 _) "]")))
(defprotocol RunTest
"Protocol for data driven tests"
(name-prefix [dda-test])
(run-tests [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)
(extend-type TestRunner
RunTest
(name-prefix [_]
(fl/data-test-spec-file-prefix (:name _)))
(run-tests [_]
(let [testdata (fl/load-data-test-spec (fl/data-test-spec-file-prefix (:name _)))
{:keys [input expectation]} testdata]
(data-test _ input expectation))))
(defn create-test-runner
[test-name]
(->TestRunner test-name))