Skip to content

Commit 03fdcf2

Browse files
committed
expand docs for from-each/more
1 parent b1b0e32 commit 03fdcf2

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

doc/collections.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ A simpler `from-each` example:
6666

6767
`from-each` also supports `:let` and `:when` syntax just like `for` and `doseq`.
6868

69+
See also [Expecting More](/doc/more.md) for examples of combining `from-each`
70+
with other Expectations for powerful multi-valued tests.
71+
6972
# Further Reading
7073

7174
* [Getting Started](/doc/getting-started.md)

doc/more.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ If you have multiple predicates that you expect to be satisfied by a given expre
1010
(expect (more vector? not-empty) [1 2 3])
1111
```
1212

13-
This expects the (actual) test value to be a vector and also to be non-empty (we could have specified `seq` there just as easily). This can be particularly powerful when combined with `from-each` to check that multiple expectations hold for computations applied to multiple input values.
13+
This expects the (actual) test value to be a vector and also to be non-empty (we could have specified `seq` there just as easily). This can be particularly powerful when combined with `from-each` to check that multiple expectations hold for computations applied to multiple input values:
14+
15+
```clojure
16+
(expect (more vector? not-empty)
17+
(from-each [n [1 2 3]]
18+
(into [] (range n))))
19+
```
1420

1521
> If you have expectations that should hold for **all** input values, you might want to look at [`clojure.test.check`](https://github.com/clojure/test.check) instead.
1622
@@ -82,6 +88,30 @@ Some simpler examples (taken from Expectations' original documentation):
8288
[1 2 3])
8389
```
8490

91+
`more-of` can be used with `from-each` to provide functionality similar
92+
to `are` in `clojure.test` (but more powerful):
93+
94+
```clojure
95+
(deftest are-example
96+
(are [expected start end]
97+
(= expected (range start end))
98+
[0 1 2 3] 0 4
99+
[] 0 0
100+
[1 2 3] 1 4))
101+
102+
(defexpect equivalent-to-are
103+
(expect (more-of [expected actual]
104+
expected actual)
105+
(from-each [[expected start end]
106+
[[[0 1 2 3] 0 4]
107+
[[] 0 0]
108+
[[1 2 3] 1 4]]]
109+
[expected (range start end)])))
110+
```
111+
112+
Although this is more verbose for this basic example, remember that the
113+
`expected` value could also be a predicate function, a regex, a Spec, etc.
114+
85115
# Further Reading
86116

87117
* [Getting Started](/doc/getting-started.md)

0 commit comments

Comments
 (0)