This repository has been archived by the owner on Jul 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.js
129 lines (115 loc) · 3.47 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const λ = require('fantasy-check/src/adapters/nodeunit');
const {equals} = require('fantasy-equality');
const {functionLength} = require('fantasy-helpers');
const {identity} = require('fantasy-combinators');
const {Tuple2, Tuple3, Tuple4, Tuple5} = require('../../fantasy-tuples');
function map(a, f) {
var accum = [],
total,
i;
for (i = 0, total = a.length; i < total; i++) {
accum[i] = f(a[i]);
}
return accum;
}
function fill(s, f) {
return map(range(0, s), f);
}
function range(a, b) {
const total = b - a;
const rec = function(x, y) {
if (y - a >= total)
return x;
x[y] = y++;
return rec(x, y);
};
return rec([], a);
}
function tuple2Of() {
const self = this.getInstance(this, tuple2Of);
self.types = [].slice.call(arguments);
return self;
}
function tuple3Of() {
const self = this.getInstance(this, tuple3Of);
self.types = [].slice.call(arguments);
return self;
}
function tuple4Of() {
const self = this.getInstance(this, tuple4Of);
self.types = [].slice.call(arguments);
return self;
}
function tuple5Of() {
const self = this.getInstance(this, tuple5Of);
self.types = [].slice.call(arguments);
return self;
}
function arbTuple(t, n) {
return function(a, s) {
return t.apply(this, map(
fill(n, (i) => a.types[i]),
(arg) => this.arb(arg, s)
));
};
};
const isTuple2Of = λ.isInstanceOf(tuple2Of);
const isTuple3Of = λ.isInstanceOf(tuple3Of);
const isTuple4Of = λ.isInstanceOf(tuple4Of);
const isTuple5Of = λ.isInstanceOf(tuple5Of);
function foldLeft(a, v, f) {
var i;
for (i = 0; i < a.length; i++) {
v = f(v, a[i]);
}
return v;
}
function zipWith(a, b) {
var accum = [],
total = Math.min(a.length, b.length),
i;
for(i = 0; i<total; i++) {
accum[i] = tuples.Tuple2(a[i], b[i]);
}
return accum;
}
const λʹ = λ
.property('equals', equals)
.property('checkTagged', function(type, args, access) {
var env = this;
return function(test) {
const length = functionLength(type);
fill(length, identity).forEach(function (value, index) {
function property() {
return access(type.apply(this, arguments), index) === arguments[index];
}
property._length = length;
var report = env.forAll(property, args),
result = report.fold(
function(fail) {
return env.Tuple2(
false,
'Failed after ' + fail.tries + ' tries: ' + fail.inputs.toString()
);
},
function() {
return env.Tuple2(
true,
'OK'
);
}
);
test.ok(result._1, result._2);
});
test.done();
};
})
.property('tuple2Of', tuple2Of)
.property('tuple3Of', tuple3Of)
.property('tuple4Of', tuple4Of)
.property('tuple5Of', tuple5Of)
.method('arb', isTuple2Of, arbTuple(Tuple2, 2))
.method('arb', isTuple3Of, arbTuple(Tuple3, 3))
.method('arb', isTuple4Of, arbTuple(Tuple4, 4))
.method('arb', isTuple5Of, arbTuple(Tuple5, 5));
module.exports = λʹ;