-
Notifications
You must be signed in to change notification settings - Fork 3
/
plugin.js
207 lines (183 loc) · 6.63 KB
/
plugin.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
(function() {
function plugin(chai, util) {
var Assertion = chai.Assertion;
var flag = util.flag;
chai.Assertion.addMethod('alter', assertChange);
chai.assert.alters = assertInterfaceChange;
chai.assert.unaltered = assertInterfaceNoChange;
/**
* ### .alters(changeWatcher)
*
* Asserts that the value returned by `changeWatcher`
* alters after the function has run:
*
* var x = 0;
* expect(function() { x += 1; }).to.alter(function() { return x });
*
* expect(function() {}).not.to.alter(function() { return x });
*
* You can pass options to be specific about the alters expected. Use the `from`
* key to enforce a starting value, a `to` key for and ending value, and a
* `by` key to enforce a numeric alters.
*
* expect(function() { x += 1 }).to.alter(function() { return x }, {by: 1});
* expect(function() { x += 1 }).to.alter(function() { return x }, {from: x});
* expect(function() { x += 1 }).to.alter(function() { return x }, {from: x, to: x + 1});
* expect(function() { x += 1 }).to.alter(function() { return x }, {to: x + 1});
*
* @name alters
* @param {Function} changer
* @param {Function} changeWatcher
* @param {Object} options _optional_
* @api public
*/
function assertChange(changeWatcher, changeSpec, msg) {
var body = flag(this, 'object');
var chai = this;
var FAIL = {};
changeSpec = changeSpec || {};
return withFail(function() {
if(changeSpec) new Assertion(changeSpec).is.a('object');
new Assertion(body, msg).is.a('function');
new Assertion(changeWatcher, msg).is.a('function');
if(changeWatcher.length > 0 || body.length > 0) {
chai.assert(changeSpec.callback != null, 'For callback-based asynchronous tests, need to pass an error-first function as `callback:` option');
}
return runStep(changeWatcher, function(before) {
withFail(preConditions, before);
var runNextStep = function() {
return runStep(body, function() {
return runStep(changeWatcher, function(after) {
var result = withFail(postConditions, before, after);
if(result !== FAIL && changeSpec.callback) changeSpec.callback();
});
});
};
return runNextStep();
});
});
function preConditions(before) {
if('by' in changeSpec) {
if(typeof changeSpec.by !== 'number' ||
(changeSpec.from != null && typeof changeSpec.from !== 'number')) {
throw new Error('alters "by" assertions only work with numbers specified in "by" and or "from" options');
}
changeSpec.to = before + changeSpec.by;
}
if('from' in changeSpec && changeSpec.from !== before) {
throw new Error("alters 'from' value wasn't equal to " + util.inspect(before));
}
}
function postConditions(before, after) {
if('to' in changeSpec) {
chai.assert(
after === changeSpec.to
, 'expected ' + util.inspect(before) + ' to change to ' + util.inspect(changeSpec.to) + ', instead changed to ' + util.inspect(after)
, 'didn\'t expect ' + util.inspect(before) + ' to have changed to ' + util.inspect(changeSpec.to)
);
} else {
chai.assert(
after !== before
, 'expected value to have changed from ' + util.inspect(before)
, 'expected value to have remained unchanged from ' + util.inspect(before) + ', but changed to ' + util.inspect(after)
);
}
}
function runStep(fn, done) {
var handledWithFail;
if(fn.length === 0) {
handledWithFail = withFail(fn);
if(handledWithFail && handledWithFail.then) {
return handledWithFail.then(done).catch(fail);
}
return done(handledWithFail);
}
fn(function(err, val) {
if(err) return fail(err);
done(val);
});
}
function fail(err) {
if(changeSpec.callback) return changeSpec.callback(err);
throw err;
}
function withFail(fn) {
var args = [].slice.call(arguments, 1);
try {
return fn.apply(null, args);
} catch(e) {
fail(e);
}
}
}
/**
* ### .alters(changeWatcher)
*
* Asserts that the value returned by `changeWatcher`
* alters after the `changer` function has run:
*
* var x = 0;
* assert.alters(function() { x += 1; }, function() { return x });
*
* You can pass options to be specific about the alters expected. Use the `from`
* key to enforce a starting value, a `to` key for and ending value, and a
* `by` key to enforce a numeric alters.
*
* assert.alters(function() { x += 1 }, function() { return x }, {by: 1});
* assert.alters(function() { x += 1 }, function() { return x }, {from: x});
* assert.alters(function() { x += 1 }, function() { return x }, {from: x, to: x + 1});
* assert.alters(function() { x += 1 }, function() { return x }, {to: x + 1});
*
* @name alters
* @param {Function} changer
* @param {Function} changeWatcher
* @param {Object} options _optional_
* @param {String} message
* @api public
*/
function assertInterfaceChange(fn, changeWatcher, opts, msg) {
if(typeof opts === 'string') {
msg = opts;
opts = null;
}
return new Assertion(fn, msg).to.alter(changeWatcher, opts);
}
/**
* ### .alters(changeWatcher)
*
* Asserts that the value returned by `changeWatcher`
* doesn't alters after the `changer` has run:
*
* var x = 0;
* assert.unaltered(doesNothing, function() { return x });
* function doesNothing() {}
*
* @name alters
* @param {Function} changer
* @param {Function} changeWatcher
* @param {Object} options _optional_
* @param {String} message
* @api public
*/
function assertInterfaceNoChange(fn, changeWatcher, opts, msg) {
if(typeof opts === 'string') {
msg = opts;
opts = null;
}
return new Assertion(fn, msg).not.to.alter(changeWatcher, opts);
}
}
/* istanbul ignore if */
if(typeof module === 'undefined') {
if(typeof define === 'function' && define.amd) {
define([], function() {
return plugin;
});
} else {
/* global chai */
chai.use(plugin);
}
} else {
module.exports = plugin;
}
})();