-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontract.spec.js
304 lines (248 loc) · 16.3 KB
/
contract.spec.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// -*- js-indent-level: 2 -*-
"use strict";
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*jshint eqeqeq:true, bitwise:true, forin:true, immed:true, latedef: true, newcap: true undef: true, strict: true */
/*global exports, require, describe, it */
var should = require('should');
var __ = require('underscore');
var c = require('./contract.face');
Array.prototype.toString = function () {
return "[" + this.join(", ") + "]";
};
var oldToString = Object.prototype.toString;
Object.prototype.toString = function () {
var that = this;
if (__.isObject(that))
return "{ " + __.chain(that).keys().map(function (k) { return k+": " + that[k];}).value().join(", ") + " }";
else return oldToString.call(that);
};
var keyvals = function(data) {
return __.map(data, function(v, k) { return [k, v]; });
};
should.Assertion.prototype.throwError = function (message) {
this.throw(message);
};
should.Assertion.prototype.throwContract = function (message) {
this.throwType(c.ContractError, message);
};
should.Assertion.prototype.throwType = function(type, message){
var fn = this.obj, err = {} , errorInfo = '' , caught, ok;
try {
var v = fn();
caught = false;
ok = false;
errorInfo = "but the function returned " + v;
} catch (e) {
err = e;
caught = true;
}
if (caught) {
console.log('\ncontracts/contract.spec.js Line 49:\n'+err+'\n'+err.renderedStack+'\n\n');
if (err.name !== type.name) {
ok = false;
errorInfo = "but the error was " + err;
} else if (!message) {
ok = true;
} else if (typeof message === 'string') {
ok = message === err.message;
errorInfo = "with a message exactly '" + message + "', but got '" + err.message + "'";
} else if (message instanceof RegExp) {
ok = message.test(err.message);
errorInfo = "with a message matching " + message + "', but got '" + err.message + "'";
} else {
throw new Error("should.throw expects a string or a regexp");
}
}
this.assert(
ok,
'expected a ' + type.name + ' to be thrown ' + errorInfo,
'expected no ' + type.name + ' to be thrown, got "' + err.message + '"');
return this;
};
describe ("toContract", function () {
it ("passes contracts", function () { c.toContract(c.any).contractName.should.eql(c.any.contractName); });
it ("wraps objects", function () { c.toContract({}).should.be.an.instanceof(c.Contract); } );
it ("wraps arrays", function () { c.toContract([c.any]).should.be.an.instanceof(c.Contract); });
it ("wraps values", function () { c.toContract(5).contractName.should.be.eql(c.value(5).contractName); });
});
describe ("any", function () {
it ("pass 5", function () { c.any.check(5).should.eql(5); });
});
describe ("nothing", function () {
it ("rejects 5", function () { (function () { c.nothing.check(5); }).should.throwContract(); });
it ("report check name", function () { (function () { c.nothing.wrap(5, 'test'); }).should.throwContract(/test/); });
});
describe ("value", function () {
it ("pass same", function () { c.value(5).check(5).should.eql(5); });
it ("reject different", function () { (function () { c.value(5).check(6); }).should.throwContract(); });
});
describe ("string", function () {
it ("pass string", function () { c.string.check("asd").should.eql("asd"); });
it ("reject different", function () { (function () { c.string.check(6); }).should.throwContract(); });
});
describe("pred", function () {
it ("returns a contract", function () { c.pred(function(v) { return false; }).should.instanceof(c.Contract); });
});
describe ("and", function () {
it ("passes two", function () { c.and(c.string, c.value("asd")).check("asd").should.eql("asd"); });
it ("fails first", function () { (function () { c.and(c.string, c.value("asd")).check(5); }).should.throwContract(); });
it ("fails second", function () { (function () { c.and(c.string, c.value("asd")).check("aaa"); }).should.throwContract(); });
});
describe ("or", function () {
it ("passes first", function () { c.or(c.string, c.value(6)).check("asd").should.eql("asd"); });
it ("passes second", function () { c.or(c.string, c.value(6)).check(6).should.eql(6); });
it ("fails", function () { (function () { c.or(c.string, c.value(6)).check(0); }).should.throwContract(); });
it ("two fn cannot be wrapped", function () { (function () { c.or(c.fn(), c.fn())
.wrap(function () {}, function () {}); })
.should.throwError(/at most one/); });
});
describe ("matches", function () {
it ("passes", function () { c.matches(/x+/).check("---xxxxx ").should.ok; });
it ("fail", function () { (function () { c.matches(/x+/).check("--- "); }).should.throwContract(); });
});
describe ("array", function () {
it ("fails non-arrays", function () { (function () { c.array(c.any).check(5); }).should.throwContract(); });
it ("passes empty", function () { c.array(c.any).check([]).should.eql([]); });
it ("passes simple", function () { c.array(c.value(5)).check([5, 5]).should.eql([5, 5]); });
it ("fails first", function () { (function () { c.array(c.value(5)).check([10, 5]); }).should.throwContract(); });
it ("fails second", function () { (function () { c.array(c.value(5)).check([5, 10]); }).should.throwContract(); });
it ("passes nested", function () { c.array(c.array(c.value(5))).check([[5], [5, 5]]).should.ok; });
it ("fails nested", function () { (function () { c.array(c.array(c.value(5))).check([[5], [5, 10]]); }).should.throwContract(); });
});
describe ("tuple", function () {
it ("fails non-arrays", function () { (function () { c.tuple(c.any).check(5); }).should.throwContract(); });
it ("fails empty", function () { (function () { c.tuple(c.any).check([]); }).should.throwContract(); });
it ("passes simple", function () { c.tuple(c.value(5), c.value(10)).check([5, 10]).should.eql([5, 10]); });
it ("passes longer", function () { c.tuple(c.value(5), c.value(10)).check([5, 10, "x"]).should.eql([5, 10, "x"]); });
it ("fails first", function () { (function () { c.tuple(c.value(5), c.value(10)).check([10, 5]); }).should.throwContract(); });
it ("fails second", function () { (function () { c.tuple(c.value(5), c.value(10)).check([5, 20]); }).should.throwContract(); });
it ("passes nested", function () { c.tuple(c.string, c.tuple(c.value(5), c.string), c.number).check(["a", [5, "b"], 5]).should.ok; });
it ("fails nested", function () { (function () { c.tuple(c.string, c.tuple(c.value(5), c.string), c.number).check(["a", [5, 10], 5]); })
.should.throwContract(); });
});
describe ("hash", function () {
it ("passes", function () { c.hash(c.string).check({x:"aaa", y:"bbb"}).should.eql({x:"aaa", y:"bbb"}); });
it ("fails", function () { (function () { c.hash(c.string).check({x:"aaa", y:5}); }).should.throwContract(); });
it ("wrap wraps fields and fails", function () {
(function () {
var x = {thk:function(){}};
c.hash(c.fn()).wrap(x).thk(5);
}).should.throwContract(/Wrong number/);
});
});
describe ("object regression", function () {
it ("one wrapping field and one non-wrapping field",
function () {
c.object({x: c.string, fn: c.fn()}).wrap({x:"foo", fn: function () {}}).x.should.eql('foo'); });
});
describe ("object", function () {
it ("fails non-objects", function () { (function () { c.object().check(5); }).should.throwContract(); });
it ("passes empty", function () { c.object().check({}).should.eql({}); });
it ("passes simple", function () { c.object({x: c.value(5)}).check({x: 5}).should.eql({x: 5}); });
it ("fails first", function () { (function () { c.object({x: c.value(5), y: c.value(10)}).check({ x: 10, y:10}); }).should.throwContract(); });
it ("fails second", function () { (function () { c.object({x: c.value(5), y: c.value(10)}).check({ x: 5, y:2}); }).should.throwContract(); });
it ("passes nested", function () { c.object({x: c.object({y: c.value(5)})}).check({x: {y: 5}}).should.ok; });
it ("fails nested", function () { (function () { c.object({x: c.object({y: c.value(5)})}).check({x: { y: 10}}); }).should.throwContract(); });
it ("fails missing field", function () { (function () { c.object({x: c.value(5), y:c.value(10)}).check({ x: 5, z: 10}); }).should.throwContract(); });
it ("fails missing field, nested", function () { (function () { c.object({x: c.object({y: c.value(5)})}).check({x: { z: 10}}); }).should.throwContract(); });
it ("optional field missing", function () { c.object({x: c.value(5), y:c.optional(c.value(10))}).check({x: 5}).should.ok; });
it ("optional field, passing", function () { c.object({x: c.value(5), y:c.optional(c.value(10))}).check({x: 5, y:10}).should.ok; });
it ("optional field, failing", function () { (function () { c.object({x: c.value(5), y:c.optional(c.value(10))}).check({ x: 5, y:5}); }).should.throwContract(); });
it ("optional field, nested, failing", function () { (function () { c.object({x: c.value(5), y: c.optional(c.object({z: c.value(10)}))})
.check({x: 5, y:{z: 0}}); })
.should.throwContract(); });
it ("extra fields, passing", function () { c.object({x: c.value(5)}).check({x:5, y:10}).should.eql({x:5, y:10}); });
it ("extra fields, wrapping", function () { c.object({fn: c.fn()}).wrap({fn: function () {}, x: 5}).x.should.eql(5); });
it ("wrap wraps fields and fails", function () {
(function () {
var x = {thk:function(){}};
c.object({thk:c.fn()}).wrap(x).thk(5);
}).should.throwContract(/Wrong number/);
});
it ("wrap maintains prototypes", function () {
var x = {thk:function(){}};
x.__proto__.x = 5;
c.object({thk:c.fn()}).wrap(x).__proto__.should.eql({x:5});
});
it ("extends passes", function () {
c.object({x: c.number}).extend({y: c.string}).check({x:5, y:'asd'}).should.eql({x:5, y:'asd'});
});
it ("extends fails", function () {
(function () { c.object({x: c.number}).extend({y: c.string}).check({x:5}); }).should.throwContract();
});
});
describe ("strict", function () {
it ("passes a good object", function () { c.object({x: c.value(10)}).strict().check({x: 10}).should.ok; });
it ("passes a good tuple", function () { c.tuple(c.value(10)).strict().check([10]).should.ok; });
it ("passes double strict on a good object", function () { c.object({x: c.value(10)}).strict().strict().check({x: 10}).should.ok; });
it ("passes double strict on a good tuple", function () { c.tuple(c.value(10)).strict().strict().check([10]).should.ok; });
it ("fails an object", function () { (function () { c.object({x: c.value(10)}).strict().check({x: 10, y:20}); }).should.throwContract(); });
it ("fails an object, multiple", function () { (function () { c.object({x: c.value(10)}).strict().check({x: 10, y:20, z:30}); }).should.throwContract(); });
it ("fails a tuple", function () { (function () { c.tuple(c.value(10)).strict().check([10, 20]); }).should.throwContract(); });
});
describe ("fn", function () {
var id = function(x) { return x; };
var strId = function(x) { return "" + x; };
var twoId = function(x, y) { return [x, y]; };
var manyId = function (/* ... */ ) { return arguments[0]; };
var thisId = function (x) { return this.x; };
var idC = c.fn(c.number).returns(c.number);
var strIdC = c.fn(c.number).returns(c.string);
var twoIdC = c.fn(c.number, c.string).returns(c.tuple(c.number, c.string));
var manyIdC = c.fn().extraArgs([c.number]).returns(c.number);
var thisC = c.fn(c.number).ths({x: c.string}).returns(c.string);
var oneOptC = c.fn(c.number, c.optional(c.number));
it ("is a function", function () { idC.wrap(id).should.instanceof(Function); });
it ("passes id(number)", function () { idC.wrap(id)(5).should.eql(5); });
it ("passes strId(number)", function () { strIdC.wrap(strId)(10).should.eql("10"); });
it ("passes twoId(number, string)", function () { twoIdC.wrap(twoId)(5, "x")
.should.eql([5, "x"]); });
it ("passes manyId(num, num, str)", function () { manyIdC.wrap(manyId)(5, 7, 10).should.eql(5); });
it ("fails on non-function", function () { (function () { idC.wrap(5); }).should.throwContract(); });
it ("fails on wrong number of args", function () { (function () { idC.wrap(id)(5, 6); }).should.throwContract(); });
it ("fails on input", function () { (function () { idC.wrap(id)("boo"); }).should.throwContract(); });
it ("fails on 2nd input", function () { (function () { twoIdC.wrap(twoId)(5, 10); }).should.throwContract(); });
it ("fails on output", function () { (function () { strIdC.wrap(id)(10); }).should.throwContract(); });
it ("fails on extra input", function () { (function () { manyIdC.wrap(manyId)(5, 6, "boo", 7); }).should.throwContract(); });
it ("fails on return when extra arguments", function () {(function () { manyIdC.wrap(strId)(5, 6, 7); }).should.throwContract(/for the return/);});
it ("success on this", function () { var v = {x:"w", getX: thisC.wrap(thisId) };
v.getX(4).should.eql("w"); });
it ("fails on this", function () {(function () { var v = {x:50, getX: thisC.wrap(thisId) };
v.getX(4); }).should.throwContract(/this/); });
it ("passes w missing opt", function () { oneOptC.wrap(twoId)(10).should.eql([10, undefined]); });
it ("passes none missing", function () { oneOptC.wrap(twoId)(10, 20).should.eql([10, 20]); });
it ("passes with extra", function () { oneOptC.extraArgs(c.any).wrap(twoId)(10, 20, 30).should.eql([10, 20]); });
it ("fails too few", function () { (function () { oneOptC.wrap(twoId)(); }).should.throwContract(/few/); });
it ("fails too many", function () { (function () { oneOptC.wrap(twoId)(10, 20, 30); }).should.throwContract(/many/); });
});
describe ("fun", function () {
var id = function(x) { return x; };
var strId = function(x) { return "" + x; };
var twoId = function(x, y) { return [x, y]; };
var manyId = function ( /* ... */ ) { return arguments[0]; };
var thisId = function (x) { return this.x; };
var idC = c.fun({ the_arg: c.number }).returns(c.number);
var strIdC = c.fun({ the_arg: c.number }).returns(c.string);
var twoIdC = c.fun({ fstArg: c.number}, { sndArg: c.string}).returns(c.tuple(c.number, c.string));
var manyIdC = c.fun().extraArgs([c.number]).returns(c.number);
var thisC = c.fun({y: c.number}).ths({x: c.string}).returns(c.string);
it ("is a function", function () { idC.wrap(id).should.instanceof(Function); });
it ("passes id(number)", function () { idC.wrap(id)(5).should.eql(5); });
it ("passes strId(number)", function () { strIdC.wrap(strId)(10).should.eql("10"); });
it ("passes twoId(number, string)", function () { twoIdC.wrap(twoId)(5, "x")
.should.eql([5, "x"]); });
it ("passes manyId(num, num, str)", function () { manyIdC.wrap(manyId)(5, 7, 10).should.eql(5); });
it ("fails on non-function", function () { (function () { idC.wrap(5); }).should.throwContract(/fun/); });
it ("fails on wrong number of args", function () { (function () { idC.wrap(id)(5, 6); }).should.throwContract(/Wrong number/); });
it ("fails on input", function () { (function () { idC.wrap(id)("boo"); }).should.throwContract(/the_arg/); });
it ("fails on 2nd input", function () { (function () { twoIdC.wrap(twoId)(5, 10); }).should.throwContract(/sndArg/); });
it ("fails on output", function () { (function () { strIdC.wrap(id)(10); }).should.throwContract(/for the return/); });
it ("fails on extra input", function () { (function () { manyIdC.wrap(manyId)(5, 6, "boo", 7); }).should.throwContract(/3rd extra argument/); });
it ("fails on return when extra arguments", function () {(function () { manyIdC.wrap(strId)(5, 6, 7); }).should.throwContract(/for the return/);});
it ("success on this", function () { var v = {x:"w", getX: thisC.wrap(thisId, 'thisId' ) };
v.getX(4).should.eql("w"); });
it ("fails on this", function () {(function () { var v = {x:50, getX: thisC.wrap(thisId) };
v.getX(5); }).should.throwContract(/this/); });
});