File: hello_world.clj

package info (click to toggle)
test-generative-clojure 1.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 296 kB
  • sloc: xml: 94; sh: 19; makefile: 14
file content (74 lines) | stat: -rw-r--r-- 1,580 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
(require
 '[clojure.data.generators :as gen]
 '[clojure.test.generative :as test :refer (defspec)]
 '[clojure.test.generative.runner :as runner])

;; generators have names that shadow core names of things generated
(gen/long)

;; generation is repeatable
(repeatedly
 2 
 #(binding [gen/*rnd* (java.util.Random. 42)]
   (gen/short)))

;; generation is composable
(gen/vec gen/short)

;; size is parameterized
(gen/vec gen/short 2)

;; size parameter can of course also be a generator
(gen/vec gen/short (gen/uniform 3 5))

;; generators are in scope as "types" in a defspec
(defspec longs-are-closed-under-increment
  inc ;; function under test
  [^long l]  ;; indicates generation via gen/long
  (assert (instance? Long %)))

;; specs are functions
(longs-are-closed-under-increment 4)

;; the next two steps are executed for you by the standard runner...

;; tests are extracted from vars
(def tests  (runner/get-tests #'longs-are-closed-under-increment))

(first tests)

;; run test with some generated inputs
(runner/run-one
 (first tests)
 1000
 [42])

(runner/run-n
 2
 1000
 tests)

;; repl-friendly use
(runner/run-vars
 2 1000 #'longs-are-closed-under-increment)

;; peek at what defspec tells us
(meta #'longs-are-closed-under-increment)

;; test that will fail
(defspec collections-are-small
  count
  [^{:tag (gen/vec gen/short (gen/uniform 0 25))} l]
  (assert (< % 20)))

;; run as from REPL
(runner/run-vars
 2 1000 #'collections-are-small)
(ex-data *e)

;; run as from suite
(runner/run-suite {:threads 1 :msec 500} (runner/get-tests #'collections-are-small))