The qc function
qc(prop, generators...)
This function executes a check of the function prop using generators. It uses the following algorithm:
- Set
sizeto0. - Generate a value for each generator by calling it with
size. - Call
propwith all generated values and capture the result. - If the result is
false, return thefailureobject. - If the result is
undefined, increment size and repeat from step 2 unlessundefinedhas been returned 200 times, in that case return thefailureobject. - If the result is a string, count how many times this string has been returned and calculate a histogram. Repeat from step 2.
- Otherwise repeat from step 2.
- When this has been repeated 100 times, return the
successobject
As you can see, the return values of the prop function determine the behaviour of this method. This allows properties like this:
prop = (int) ->
unless int is 0
test(int)to work seamlessly. The string return type is useful for debugging tests if you want to see if certain code paths are excersized.
The return objects look like this (both failure and success):
Key | Type | Value |
|---|---|---|
| Boolean |
|
| Array | The last set of example values |
| String | If failure this will explain why the test failed. If success this will give some statistics on the test as well as a potential histogram of |
qc.forall(generators..., prop)
This is like the qc function, but it ignores the return value of prop completely (i.e. prop will run 100 times regardless of its behaviour unless it throws). This is useful as a control flow construct around which you can use another tools expectations, i.e.
qc.forAll qc.int, (i) ->
expect(i + i).toBe(2 * i)qc.forAll(qc.int, function(i) {
expect(i + i).toBe(2 * i);
});Updated about 1 year ago
