Skip to content

Commit 0722aee

Browse files
minor enhancements:
- specs return result, not validation result - report framework summaries separately - events always go somewhere
1 parent 70d217c commit 0722aee

3 files changed

Lines changed: 56 additions & 20 deletions

File tree

src/main/clojure/clojure/test/generative.clj

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
(meta &form))))
5656

5757
(defmacro is
58+
"Assert that v is true, otherwise fail the current generative
59+
test (with optional msg)."
5860
([v] (with-meta `(is ~v nil) (meta &form)))
5961
([v msg]
6062
`(let [~'actual ~v ~'expected '~v]
@@ -67,6 +69,28 @@
6769
(meta &form))))))
6870

6971
(defmacro defspec
72+
"Defines a function named name that expects args. The defined
73+
function binds '%' to the result of calling fn-to-test with args,
74+
and runs validator-body forms (if any), which have access to both
75+
args and %. The defined function.
76+
77+
Args must have type hints (i.e. :tag metdata), which are
78+
interpreted as instructions for generating test input
79+
data. Unquoted names in type hints are resolved in the
80+
c.t.g.generators namespace, which has generator functions for
81+
common Clojure data types. For example, the following argument list
82+
declares that 'seed' is an int, and that 'iters' is an int in the
83+
uniform distribution from 1 to 100:
84+
85+
[^int seed ^{:tag (uniform 1 100)} iters]
86+
87+
Backquoted names in an argument list are resolved in the current
88+
namespace, allowing arbitrary generators, e.g.
89+
90+
[^{:tag `scary-word} word]
91+
92+
The function c.t.g.runner/run-iter takes a var naming a test, and runs
93+
a single test iteration, generating inputs based on the arg type hints."
7094
[name fn-to-test args & validator-body]
7195
(when-let [missing-tags (->> (map #(list % (-> % meta :tag)) args)
7296
(filter (fn [[_ tag]] (nil? tag)))
@@ -76,7 +100,8 @@
76100
::inputs (into [] (map #(-> % meta :tag tag->gen eval) args))))
77101
~(into [] (map (fn [a#] (with-meta a# (dissoc (meta a#) :tag))) args))
78102
(let [~'% (apply ~fn-to-test ~args)]
79-
~@validator-body)))
103+
~@validator-body
104+
~'%)))
80105

81106

82107

src/main/clojure/clojure/test/generative/event.clj

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
; You must not remove this notice, or any other, from this software.
99

1010
(ns clojure.test.generative.event
11-
(:require [clojure.test.generative.config :as config]))
11+
(:require [clojure.test.generative.config :as config]
12+
[clojure.test.generative.io :as io]))
1213

1314
(set! *warn-on-reflection* true)
1415

@@ -107,10 +108,13 @@
107108
(add-handler (load-var-val (symbol handler)))))
108109

109110
(defn report-fn
110-
"Call the installed handles for an event"
111+
"Call the installed handlers for an event, or io/pprint if no handlers
112+
installed."
111113
[event]
112-
(doseq [f @handlers]
113-
(f event)))
114+
(if-let [hs (seq @handlers)]
115+
(doseq [h hs]
116+
(h event))
117+
(io/pprint event)))
114118

115119
(defmacro report
116120
[type & args]

src/main/clojure/clojure/test/generative/runner.clj

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -158,18 +158,23 @@
158158
(defn run-all-tests
159159
"Run generative tests and clojure.test tests"
160160
[nses threads msec]
161-
(let [event-counts (atom {})
162-
event-counter #(when-not (contains? (:tags %) :begin)
163-
(when-let [type (:type %)]
164-
(swap! event-counts update-in [type] (fnil inc 0))))]
165-
(event/with-handler event-counter
166-
(event/report :test/library :name 'clojure.test)
167-
(binding [ctest/report cta/report-adapter]
168-
(apply ctest/run-tests (filter has-clojure-test-tests? nses)))
169-
(event/report :test/library :name 'clojure.test.generative)
170-
(run-generative-tests nses threads msec)
171-
(io/await)
172-
@event-counts)))
161+
(let [run-with-counts
162+
(fn [lib f]
163+
(let [event-counts (atom {})
164+
event-counter #(when-not (contains? (:tags %) :begin)
165+
(when-let [type (:type %)]
166+
(swap! event-counts update-in [type] (fnil inc 0))))]
167+
(event/report :test/library :name lib)
168+
(event/with-handler event-counter (f))
169+
@event-counts))
170+
ct-results (run-with-counts 'clojure.test
171+
#(binding [ctest/report cta/report-adapter]
172+
(apply ctest/run-tests (filter has-clojure-test-tests? nses))))
173+
ctg-results (run-with-counts 'clojure.test.generative
174+
#(run-generative-tests nses threads msec))]
175+
(io/await)
176+
{'clojure.test ct-results
177+
'clojure.test.generative ctg-results}))
173178

174179
(defn failed?
175180
[result]
@@ -187,9 +192,11 @@
187192
(doseq [ns nses] (require ns))
188193
(event/install-default-handlers)
189194
(try
190-
(let [result (run-all-tests nses (:threads conf) (:msec conf))]
191-
(println "\n" result)
192-
(System/exit (if (failed? result) 1 0)))
195+
(let [results (run-all-tests nses (:threads conf) (:msec conf))]
196+
(doseq [[k v] results]
197+
(println (str "\nFramework " k))
198+
(println v))
199+
(System/exit (if (some failed? (vals results)) 1 0)))
193200
(catch Throwable t
194201
(.printStackTrace t)
195202
(System/exit -1))

0 commit comments

Comments
 (0)