Skip to content

Commit 2f43b33

Browse files
add examples
1 parent 4d384cf commit 2f43b33

1 file changed

Lines changed: 57 additions & 0 deletions

File tree

examples/hello_world.clj

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
(require
2+
'[clojure.data.generators :as gen]
3+
'[clojure.test.generative :as test :refer (defspec)]
4+
'[clojure.test.generative.event :as event]
5+
'[clojure.test.generative.runner :as runner])
6+
7+
;; generators have names that shadow core names of things generated
8+
(gen/long)
9+
10+
;; generation is repeatable
11+
(repeatedly
12+
2
13+
#(binding [gen/*rnd* (java.util.Random. 42)]
14+
(gen/short)))
15+
16+
;; generation is composable
17+
(gen/vec gen/short)
18+
19+
;; size is parameterized
20+
(gen/vec gen/short 2)
21+
22+
;; size parameter can of course also be a generator
23+
(gen/vec gen/short (gen/uniform 3 5))
24+
25+
;; generators are in scope as "types" in a defspec
26+
(defspec longs-are-closed-under-increment
27+
inc ;; function under test
28+
[^long l] ;; indicates generation via gen/long
29+
(assert (instance? Long %)))
30+
31+
;; specs are functions
32+
(longs-are-closed-under-increment 4)
33+
34+
;; the next two steps are executed for you by the standard runner...
35+
36+
;; tests are extracted from vars
37+
(def tests (runner/var-tests #'longs-are-closed-under-increment))
38+
39+
;; install awesome console UI for tests
40+
(event/install-default-handlers)
41+
42+
;; run test with some generated inputs
43+
(runner/run-for
44+
(first tests)
45+
1
46+
1000)
47+
48+
;; actual test data structure exposes a lazy infinite seq of inputs
49+
(take 2 ((:inputs (first tests))))
50+
51+
;; the real story behind that console ui
52+
(reset! @#'event/handlers [])
53+
(runner/run-for
54+
(first tests)
55+
1
56+
10)
57+

0 commit comments

Comments
 (0)