@@ -19,10 +19,6 @@ If the generators you supply supports shrinking by providing a `shrink` function
19
19
on the generator function, unexpected-check will try to shrink the error space
20
20
as much as possible and therefore provide much more precise error cases.
21
21
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
-
26
22
I recommend using the plugin together with
27
23
[ chance-generators] ( https://github.com/sunesimonsen/change-generators ) as it
28
24
provides a huge range of generators and supports shrinking, but it is not a
@@ -41,30 +37,43 @@ function sort(arr) {
41
37
42
38
Then we could write a test to ensure the following:
43
39
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.
47
42
48
- We do that the following way :
43
+ First we will create an assertion for checking that an array is sorted :
49
44
50
45
``` 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:
52
57
58
+ ``` js
59
+ var g = require (' chance-generators' )(42 );
53
60
// generate arrays of numbers from -20 to 20 with length varying from 1 to 20
54
61
var numbers = g .integer ({ min: - 20 , max: 20 });
55
62
var lengths = g .integer ({ min: 1 , max: 20 });
56
63
var arrays = g .n (numbers, lengths);
64
+ ```
57
65
66
+ Finally we make the assertion:
67
+
68
+ ``` js
58
69
expect (function (arr ) {
59
70
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' );
64
73
}, ' to be valid for all' , arrays);
65
74
```
66
75
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
68
77
on converting items to strings and comparing them. So you will get the following error:
69
78
70
79
``` output
@@ -73,12 +82,7 @@ counterexample:
73
82
74
83
Generated input: [ 18, 4 ]
75
84
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
82
86
```
83
87
84
88
If we wanted to fix the problem, we would need to use a comparison function:
@@ -94,10 +98,8 @@ function sort(arr) {
94
98
``` js
95
99
expect (function (arr ) {
96
100
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' );
101
103
}, ' to be valid for all' , arrays);
102
104
```
103
105
0 commit comments