-
Notifications
You must be signed in to change notification settings - Fork 2
/
chai-changes.js
340 lines (339 loc) · 14.4 KB
/
chai-changes.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Generated by CoffeeScript 1.7.1
(function() {
(function(chaiChanges) {
if (typeof require === "function" && typeof exports === "object" && typeof module === "object") {
return module.exports = chaiChanges;
} else if (typeof define === "function" && define.amd) {
return define(function() {
return chaiChanges;
});
} else {
return chai.use(chaiChanges);
}
})(function(chai, utils) {
var byAtLeast, byAtMost, changeBy, changeByAssert, changeFrom, changeFromAssert, changeFromBeginAssert, changeTo, changeToAssert, changeToBeginAssert, clone, flag, formatFunction, inspect, noChangeAssert;
inspect = utils.inspect;
flag = utils.flag;
chai.Assertion.addMethod('when', function(val, options) {
var action, definedActions, done, isPromise, newPromise, object, promiseCallback, result, _i, _j, _len, _len1;
if (options == null) {
options = {};
}
definedActions = flag(this, 'whenActions') || [];
object = flag(this, 'object');
flag(this, 'whenObject', object);
for (_i = 0, _len = definedActions.length; _i < _len; _i++) {
action = definedActions[_i];
if (typeof action.before === "function") {
action.before(this);
}
}
result = val();
isPromise = (typeof result === 'object') && (typeof result.then === 'function');
if (isPromise) {
done = options != null ? options.notify : void 0;
if (done == null) {
done = function() {};
}
promiseCallback = (function(_this) {
return function() {
var error, _j, _len1;
try {
for (_j = 0, _len1 = definedActions.length; _j < _len1; _j++) {
action = definedActions[_j];
if (typeof action.after === "function") {
action.after(_this);
}
}
return done();
} catch (_error) {
error = _error;
done(error);
throw error;
}
};
})(this);
newPromise = result.then(promiseCallback, promiseCallback);
this.then = newPromise.then.bind(newPromise);
} else {
for (_j = 0, _len1 = definedActions.length; _j < _len1; _j++) {
action = definedActions[_j];
if (typeof action.after === "function") {
action.after(this);
}
}
}
return this;
});
noChangeAssert = function(context) {
var endValue, negate, object, relevant, result, startValue;
relevant = flag(context, 'no-change');
if (!relevant) {
return;
}
negate = flag(context, 'negate');
flag(context, 'negate', this.negate);
object = flag(context, 'whenObject');
startValue = flag(context, 'changeStart');
endValue = object();
result = !utils.eql(endValue, startValue);
context.assert(result, "expected `" + (formatFunction(object)) + "` to change, but it stayed " + (utils.inspect(startValue)), "expected `" + (formatFunction(object)) + "` not to change, but it changed from " + (utils.inspect(startValue)) + " to " + (utils.inspect(endValue)));
return flag(context, 'negate', negate);
};
changeByAssert = function(context) {
var actualDelta, endValue, negate, object, startValue;
negate = flag(context, 'negate');
flag(context, 'negate', this.negate);
object = flag(context, 'whenObject');
startValue = flag(context, 'changeStart');
endValue = object();
actualDelta = endValue - startValue;
context.assert(this.expectedDelta === actualDelta, "expected `" + (formatFunction(object)) + "` to change by " + this.expectedDelta + ", but it changed by " + actualDelta, "expected `" + (formatFunction(object)) + "` not to change by " + this.expectedDelta + ", but it did");
return flag(context, 'negate', negate);
};
clone = function(obj) {
var key, result, value;
if (obj !== Object(obj)) {
return obj;
}
if (Array.isArray(obj)) {
return obj.slice();
}
result = {};
for (key in obj) {
value = obj[key];
result[key] = value;
}
return result;
};
changeToBeginAssert = function(context) {
var negate, object, result, startValue;
negate = flag(context, 'negate');
flag(context, 'negate', this.negate);
object = flag(context, 'whenObject');
startValue = object();
flag(context, 'changeToStart', clone(startValue));
result = !utils.eql(startValue, this.expectedEndValue);
if (negate) {
result = !result;
}
context.assert(result, "expected `" + (formatFunction(object)) + "` to change to " + (utils.inspect(this.expectedEndValue)) + ", but it was already " + (utils.inspect(startValue)), "not supported");
return flag(context, 'negate', negate);
};
changeToAssert = function(context) {
var changed, endValue, negate, object, result, startValue;
negate = flag(context, 'negate');
flag(context, 'negate', this.negate);
object = flag(context, 'whenObject');
startValue = flag(context, 'changeToStart');
endValue = object();
if (!negate) {
changed = !utils.eql(startValue, endValue);
context.assert(changed, "expected `" + (formatFunction(object)) + "` to change to " + (utils.inspect(this.expectedEndValue)) + ", but it did not change from " + (utils.inspect(startValue)), "not supported");
}
if (this.expectedEndValue instanceof RegExp) {
result = this.expectedEndValue.test(endValue);
} else {
result = utils.eql(endValue, this.expectedEndValue);
}
context.assert(result, "expected `" + (formatFunction(object)) + "` to change to " + (utils.inspect(this.expectedEndValue)) + ", but it changed to " + (utils.inspect(endValue)), "expected `" + (formatFunction(object)) + "` not to change to " + (utils.inspect(this.expectedEndValue)) + ", but it did");
return flag(context, 'negate', negate);
};
changeFromBeginAssert = function(context) {
var negate, object, result, startValue;
negate = flag(context, 'negate');
flag(context, 'negate', this.negate);
object = flag(context, 'whenObject');
startValue = object();
if (this.expectedStartValue instanceof RegExp) {
result = this.expectedStartValue.test(startValue);
} else {
result = utils.eql(startValue, this.expectedStartValue);
}
context.assert(result, "expected the change of `" + (formatFunction(object)) + "` to start from " + (utils.inspect(this.expectedStartValue)) + ", but it started from " + (utils.inspect(startValue)), "expected the change of `" + (formatFunction(object)) + "` not to start from " + (utils.inspect(this.expectedStartValue)) + ", but it did");
return flag(context, 'negate', negate);
};
changeFromAssert = function(context) {
var endValue, negate, object, result, startValue;
negate = flag(context, 'negate');
flag(context, 'negate', this.negate);
object = flag(context, 'whenObject');
startValue = flag(context, 'changeStart');
endValue = object();
result = !utils.eql(startValue, endValue);
if (negate) {
result = !result;
}
context.assert(result, "expected `" + (formatFunction(object)) + "` to change from " + (utils.inspect(this.expectedStartValue)) + ", but it did not change", "not supported");
return flag(context, 'negate', negate);
};
chai.Assertion.addProperty('change', function() {
var definedActions;
flag(this, 'no-change', true);
definedActions = flag(this, 'whenActions') || [];
definedActions.push({
negate: flag(this, 'negate'),
before: function(context) {
var startValue;
startValue = flag(context, 'whenObject')();
return flag(context, 'changeStart', startValue);
},
after: noChangeAssert
});
return flag(this, 'whenActions', definedActions);
});
chai.Assertion.addProperty('increase', function() {
var definedActions;
definedActions = flag(this, 'whenActions') || [];
definedActions.push({
negate: flag(this, 'negate'),
before: function(context) {
var startValue;
startValue = flag(context, 'whenObject')();
return flag(context, 'increaseStart', startValue);
},
after: function(context) {
var endValue, negate, object, startValue;
object = flag(context, 'whenObject');
endValue = object();
startValue = flag(context, 'increaseStart');
negate = flag(context, 'negate');
flag(context, 'negate', this.negate);
if (!negate) {
context.assert(startValue !== endValue, "expected `" + (formatFunction(object)) + "` to increase, but it did not change", "not supported");
}
context.assert(startValue < endValue, "expected `" + (formatFunction(object)) + "` to increase, but it decreased by " + (startValue - endValue), "expected `" + (formatFunction(object)) + "` not to increase, but it increased by " + (endValue - startValue));
return flag(context, 'negate', negate);
}
});
return flag(this, 'whenActions', definedActions);
});
chai.Assertion.addProperty('decrease', function() {
var definedActions;
definedActions = flag(this, 'whenActions') || [];
definedActions.push({
negate: flag(this, 'negate'),
before: function(context) {
var startValue;
startValue = flag(context, 'whenObject')();
return flag(context, 'decreaseStart', startValue);
},
after: function(context) {
var endValue, negate, object, startValue;
object = flag(context, 'whenObject');
endValue = object();
startValue = flag(context, 'decreaseStart');
negate = flag(context, 'negate');
flag(context, 'negate', this.negate);
if (!negate) {
context.assert(startValue !== endValue, "expected `" + (formatFunction(object)) + "` to decrease, but it did not change", "not supported");
}
context.assert(startValue > endValue, "expected `" + (formatFunction(object)) + "` to decrease, but it increased by " + (endValue - startValue), "expected `" + (formatFunction(object)) + "` not to decrease, but it decreased by " + (startValue - endValue));
return flag(context, 'negate', negate);
}
});
return flag(this, 'whenActions', definedActions);
});
byAtLeast = function(amount) {
var definedActions;
definedActions = flag(this, 'whenActions') || [];
definedActions.push({
negate: flag(this, 'negate'),
before: function(context) {
var startValue;
startValue = flag(context, 'whenObject')();
return flag(context, 'atLeastStart', startValue);
},
after: function(context) {
var difference, endValue, negate, object, startValue;
object = flag(context, 'whenObject');
endValue = object();
startValue = flag(context, 'atLeastStart');
negate = flag(context, 'negate');
flag(context, 'negate', this.negate);
difference = Math.abs(endValue - startValue);
context.assert(difference >= amount, "expected `" + (formatFunction(object)) + "` to change by at least " + amount + ", but changed by " + difference, "not supported");
return flag(context, 'negate', negate);
}
});
return flag(this, 'whenActions', definedActions);
};
chai.Assertion.addChainableMethod('atLeast', byAtLeast, function() {
return this;
});
byAtMost = function(amount) {
var definedActions;
definedActions = flag(this, 'whenActions') || [];
definedActions.push({
negate: flag(this, 'negate'),
before: function(context) {
var startValue;
startValue = flag(context, 'whenObject')();
return flag(context, 'atMostStart', startValue);
},
after: function(context) {
var difference, endValue, negate, object, startValue;
object = flag(context, 'whenObject');
endValue = object();
startValue = flag(context, 'atMostStart');
negate = flag(context, 'negate');
flag(context, 'negate', this.negate);
difference = Math.abs(endValue - startValue);
context.assert(difference <= amount, "expected `" + (formatFunction(object)) + "` to change by at most " + amount + ", but changed by " + difference, "not supported");
return flag(context, 'negate', negate);
}
});
return flag(this, 'whenActions', definedActions);
};
chai.Assertion.addChainableMethod('atMost', byAtMost, function() {
return this;
});
formatFunction = function(func) {
return func.toString().replace(/^\s*function \(\) {\s*/, '').replace(/\s+}$/, '').replace(/\s*return\s*/, '');
};
changeBy = function(delta) {
var definedActions;
flag(this, 'no-change', false);
definedActions = flag(this, 'whenActions') || [];
definedActions.push({
negate: flag(this, 'negate'),
expectedDelta: delta,
after: changeByAssert
});
return flag(this, 'whenActions', definedActions);
};
chai.Assertion.addChainableMethod('by', changeBy, function() {
return this;
});
changeTo = function(endValue) {
var definedActions;
flag(this, 'no-change', false);
definedActions = flag(this, 'whenActions') || [];
definedActions.push({
negate: flag(this, 'negate'),
expectedEndValue: endValue,
before: changeToBeginAssert,
after: changeToAssert
});
return flag(this, 'whenActions', definedActions);
};
chai.Assertion.addChainableMethod('to', changeTo, function() {
return this;
});
changeFrom = function(startValue) {
var definedActions;
flag(this, 'no-change', false);
definedActions = flag(this, 'whenActions') || [];
definedActions.push({
negate: flag(this, 'negate'),
expectedStartValue: startValue,
before: changeFromBeginAssert,
after: changeFromAssert
});
return flag(this, 'whenActions', definedActions);
};
return chai.Assertion.addChainableMethod('from', changeFrom, function() {
return this;
});
});
}).call(this);