Skip to content

Commit 4322926

Browse files
committed
Fixed up the readme a bit
1 parent 6a8111b commit 4322926

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

documentation/index.md

+26-24
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ If the generators you supply supports shrinking by providing a `shrink` function
1919
on the generator function, unexpected-check will try to shrink the error space
2020
as much as possible and therefore provide much more precise error cases.
2121

22-
This project is still in it early days and asynchronous testing are missing. It
23-
is currently not possible to configure the number of iterations the tests should
24-
be executed either.
25-
2622
I recommend using the plugin together with
2723
[chance-generators](https://github.com/sunesimonsen/change-generators) as it
2824
provides a huge range of generators and supports shrinking, but it is not a
@@ -41,30 +37,43 @@ function sort(arr) {
4137

4238
Then we could write a test to ensure the following:
4339

44-
* that the resulting array has the same size as the input array.
45-
* that the first item in the sorted array is less than or equal to all items in the input array.
46-
* that the last item in the sorted array is greater than or equal to all items in the input array.
40+
* the resulting array has the same size as the input array.
41+
* the resulting array is sorted.
4742

48-
We do that the following way:
43+
First we will create an assertion for checking that an array is sorted:
4944

5045
```js
51-
var g = require('chance-generators')(42);
46+
expect.addAssertion('<array> to be sorted', function (expect, subject) {
47+
var isSorted = subject.every(function (x, i) {
48+
return subject.slice(i).every(function (y) {
49+
return x <= y;
50+
});
51+
});
52+
expect(isSorted, 'to be true');
53+
});
54+
```
55+
56+
Then we generate the input arrays:
5257

58+
```js
59+
var g = require('chance-generators')(42);
5360
// generate arrays of numbers from -20 to 20 with length varying from 1 to 20
5461
var numbers = g.integer({ min: -20, max: 20 });
5562
var lengths = g.integer({ min: 1, max: 20 });
5663
var arrays = g.n(numbers, lengths);
64+
```
5765

66+
Finally we make the assertion:
67+
68+
```js
5869
expect(function (arr) {
5970
var sorted = sort(arr);
60-
61-
expect(sorted, 'to have length', arr.length)
62-
.and('first item to be less than or equal to all', arr)
63-
.and('last item to be greater than or equal to all', arr);
71+
expect(sorted, 'to have length', arr.length);
72+
expect(sort(arr), 'to be sorted');
6473
}, 'to be valid for all', arrays);
6574
```
6675

67-
But that assumption as actually not true as the build in sort functions is based
76+
But that assumption as actually not true as the build-in sort functions is based
6877
on converting items to strings and comparing them. So you will get the following error:
6978

7079
```output
@@ -73,12 +82,7 @@ counterexample:
7382
7483
Generated input: [ 18, 4 ]
7584
76-
expected [ 18, 4 ] first item to be less than or equal to all [ 18, 4 ]
77-
78-
[
79-
18,
80-
4 // should be greater than or equal to 18
81-
]
85+
expected [ 18, 4 ] to be sorted
8286
```
8387

8488
If we wanted to fix the problem, we would need to use a comparison function:
@@ -94,10 +98,8 @@ function sort(arr) {
9498
```js
9599
expect(function (arr) {
96100
var sorted = sort(arr);
97-
98-
expect(sorted, 'to have length', arr.length)
99-
.and('first item to be less than or equal to all', arr)
100-
.and('last item to be greater than or equal to all', arr);
101+
expect(sorted, 'to have length', arr.length);
102+
expect(sort(arr), 'to be sorted');
101103
}, 'to be valid for all', arrays);
102104
```
103105

test/mocha.opts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--reporter spec
22
--recursive
33
--check-leaks
4-
--require ./bootstrap.js
4+
--require ./bootstrap-unexpected-markdown.js
55
--compilers md:unexpected-markdown

0 commit comments

Comments
 (0)