1
- # Introduction to simple- check
1
+ # Introduction to test. check
2
2
3
- simple- check is a tool for writing property-based tests. This differs from
3
+ test. check is a tool for writing property-based tests. This differs from
4
4
traditional unit-testing, where you write individual test-cases. With
5
- simple- check you write universal quantifications, properties that should hold
5
+ test. check you write universal quantifications, properties that should hold
6
6
true for all input. For example, for all vectors, reversing the vector should
7
7
preserve the count. Reversing it twice should equal the input. In this guide,
8
8
we'll cover the thought process for coming up with properties, as well as the
@@ -16,9 +16,9 @@ the output should be in ascending order. We also might want to make sure that
16
16
the count of the input is preserved. Our test might look like:
17
17
18
18
``` clojure
19
- (require '[simple- check.core :as sc])
20
- (require '[simple- check.generators :as gen])
21
- (require '[simple- check.properties :as prop])
19
+ (require '[test. check.core :as sc])
20
+ (require '[test. check.generators :as gen])
21
+ (require '[test. check.properties :as prop])
22
22
23
23
(defn ascending?
24
24
" clojure.core/sorted? doesn't do what we might expect, so we write our
@@ -39,9 +39,9 @@ the count of the input is preserved. Our test might look like:
39
39
```
40
40
41
41
What if we were to forget to actually sort our vector? The test will fail, and
42
- then simple- check will try and find 'smaller' inputs that still cause the test
42
+ then test. check will try and find 'smaller' inputs that still cause the test
43
43
to fail. For example, the function might originally fail with input:
44
- ` [5 4 2 2 2] ` , but simple- check will shrink this down to ` [0 -1] ` (or ` [1 0] ` ).
44
+ ` [5 4 2 2 2] ` , but test. check will shrink this down to ` [0 -1] ` (or ` [1 0] ` ).
45
45
46
46
``` clojure
47
47
(def bad-property
@@ -60,14 +60,14 @@ generators that we write ourselves.
60
60
## Generators
61
61
62
62
In order to write our property, we'll use generators. A generator knows how to
63
- generate random values for a specific type. The ` simple- check.generators`
63
+ generate random values for a specific type. The ` test. check.generators`
64
64
namespace has many built-in generators, as well as combinators for creating
65
65
your own new generators. You can write sophisticated generators just by
66
66
combining the existing generators with the given combinators. As we write
67
67
generators, we can see them in practice with the ` sample ` function:
68
68
69
69
``` clojure
70
- (require '[simple- check.generators :as gen])
70
+ (require '[test. check.generators :as gen])
71
71
72
72
(gen/sample gen/int)
73
73
; ; => (0 1 -1 0 -1 4 4 2 7 1)
@@ -89,7 +89,7 @@ or get a lazy-seq of values:
89
89
```
90
90
91
91
You may notice that as you ask for more values, the 'size' of the generated
92
- values increases. As simple- check generates more values, it increases the
92
+ values increases. As test. check generates more values, it increases the
93
93
'size' of the generated values. This allows tests to fail early, for simple
94
94
values, and only increase the size as the test continues to pass.
95
95
@@ -259,7 +259,7 @@ And if we try and `sample` our generator:
259
259
260
260
``` clojure
261
261
(gen/sample tree)
262
- ; ; => NullPointerException simple- check.generators/gen-bind/fn--1244 (generators.clj:147)
262
+ ; ; => NullPointerException test. check.generators/gen-bind/fn--1244 (generators.clj:147)
263
263
```
264
264
265
265
It turns out, we can't create recursive values (tree refers to itself in it's
@@ -279,12 +279,12 @@ And now if we try and `sample`:
279
279
280
280
``` clojure
281
281
(gen/sample (tree )) ; ; we now have to 'call' tree to get our generator
282
- ; ; => StackOverflowError simple- check.generators/return (generators.clj:161)
282
+ ; ; => StackOverflowError test. check.generators/return (generators.clj:161)
283
283
```
284
284
285
285
Progress. It turns out, we don't have a deterministic way to stop our
286
286
recursion. Our tree can just be created deeper and deeper. What we'd like is
287
- some way to control the maximum depth of the tree. Fortunately, _ simple- check_
287
+ some way to control the maximum depth of the tree. Fortunately, _ test. check_
288
288
provides a function to help: ` sized ` . ` sized ` takes a function that takes an
289
289
integer size, and returns a generator based on this size. We can use this size
290
290
parameter to decide when to stop recurring. We'll say that when size is 0,
0 commit comments