This repository has been archived by the owner on Nov 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 131
/
assertion.js
277 lines (247 loc) · 6.99 KB
/
assertion.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
/*
* should.js - assertion library
* Copyright(c) 2010-2013 TJ Holowaychuk <tj@vision-media.ca>
* Copyright(c) 2013-2016 Denis Bardadym <bardadymchik@gmail.com>
* MIT Licensed
*/
import AssertionError from './assertion-error';
/**
* should Assertion
* @param {*} obj Given object for assertion
* @constructor
* @memberOf should
* @static
*/
export function Assertion(obj) {
this.obj = obj;
this.anyOne = false;
this.negate = false;
this.params = {actual: obj};
}
Assertion.prototype = {
constructor: Assertion,
/**
* Base method for assertions.
*
* Before calling this method need to fill Assertion#params object. This method usually called from other assertion methods.
* `Assertion#params` can contain such properties:
* * `operator` - required string containing description of this assertion
* * `obj` - optional replacement for this.obj, it usefull if you prepare more clear object then given
* * `message` - if this property filled with string any others will be ignored and this one used as assertion message
* * `expected` - any object used when you need to assert relation between given object and expected. Like given == expected (== is a relation)
* * `details` - additional string with details to generated message
*
* @memberOf Assertion
* @category assertion
* @param {*} expr Any expression that will be used as a condition for asserting.
* @example
*
* var a = new should.Assertion(42);
*
* a.params = {
* operator: 'to be magic number',
* }
*
* a.assert(false);
* //throws AssertionError: expected 42 to be magic number
*/
assert: function(expr) {
if (expr) {
return this;
}
var params = this.params;
if ('obj' in params && !('actual' in params)) {
params.actual = params.obj;
} else if (!('obj' in params) && !('actual' in params)) {
params.actual = this.obj;
}
params.stackStartFunction = params.stackStartFunction || this.assert;
params.negate = this.negate;
params.assertion = this;
throw new AssertionError(params);
},
/**
* Shortcut for `Assertion#assert(false)`.
*
* @memberOf Assertion
* @category assertion
* @example
*
* var a = new should.Assertion(42);
*
* a.params = {
* operator: 'to be magic number',
* }
*
* a.fail();
* //throws AssertionError: expected 42 to be magic number
*/
fail: function() {
return this.assert(false);
}
};
/**
* Assertion used to delegate calls of Assertion methods inside of Promise.
* It has almost all methods of Assertion.prototype
*
* @param {Promise} obj
*/
export function PromisedAssertion(/* obj */) {
Assertion.apply(this, arguments);
}
/**
* Make PromisedAssertion to look like promise. Delegate resolve and reject to given promise.
*
* @private
* @returns {Promise}
*/
PromisedAssertion.prototype.then = function(resolve, reject) {
return this.obj.then(resolve, reject);
};
/**
* Way to extend Assertion function. It uses some logic
* to define only positive assertions and itself rule with negative assertion.
*
* All actions happen in subcontext and this method take care about negation.
* Potentially we can add some more modifiers that does not depends from state of assertion.
*
* @memberOf Assertion
* @static
* @param {String} name Name of assertion. It will be used for defining method or getter on Assertion.prototype
* @param {Function} func Function that will be called on executing assertion
* @example
*
* Assertion.add('asset', function() {
* this.params = { operator: 'to be asset' }
*
* this.obj.should.have.property('id').which.is.a.Number()
* this.obj.should.have.property('path')
* })
*/
Assertion.add = function(name, func) {
Object.defineProperty(Assertion.prototype, name, {
enumerable: true,
configurable: true,
value: function() {
var context = new Assertion(this.obj, this, name);
context.anyOne = this.anyOne;
try {
func.apply(context, arguments);
} catch (e) {
// check for fail
if (e instanceof AssertionError) {
// negative fail
if (this.negate) {
this.obj = context.obj;
this.negate = false;
return this;
}
if (context !== e.assertion) {
context.params.previous = e;
}
// positive fail
context.negate = false;
context.fail();
}
// throw if it is another exception
throw e;
}
// negative pass
if (this.negate) {
context.negate = true; // because .fail will set negate
context.params.details = 'false negative fail';
context.fail();
}
// positive pass
if (!this.params.operator) {
this.params = context.params; // shortcut
}
this.obj = context.obj;
this.negate = false;
return this;
}
});
Object.defineProperty(PromisedAssertion.prototype, name, {
enumerable: true,
configurable: true,
value: function() {
var args = arguments;
this.obj = this.obj.then(function(a) {
return a[name].apply(a, args);
});
return this;
}
});
};
/**
* Add chaining getter to Assertion like .a, .which etc
*
* @memberOf Assertion
* @static
* @param {string} name name of getter
* @param {function} [onCall] optional function to call
*/
Assertion.addChain = function(name, onCall) {
onCall = onCall || function() {};
Object.defineProperty(Assertion.prototype, name, {
get: function() {
onCall.call(this);
return this;
},
enumerable: true
});
Object.defineProperty(PromisedAssertion.prototype, name, {
enumerable: true,
configurable: true,
get: function() {
this.obj = this.obj.then(function(a) {
return a[name];
});
return this;
}
});
};
/**
* Create alias for some `Assertion` property
*
* @memberOf Assertion
* @static
* @param {String} from Name of to map
* @param {String} to Name of alias
* @example
*
* Assertion.alias('true', 'True')
*/
Assertion.alias = function(from, to) {
var desc = Object.getOwnPropertyDescriptor(Assertion.prototype, from);
if (!desc) {
throw new Error('Alias ' + from + ' -> ' + to + ' could not be created as ' + from + ' not defined');
}
Object.defineProperty(Assertion.prototype, to, desc);
var desc2 = Object.getOwnPropertyDescriptor(PromisedAssertion.prototype, from);
if (desc2) {
Object.defineProperty(PromisedAssertion.prototype, to, desc2);
}
};
/**
* Negation modifier. Current assertion chain become negated. Each call invert negation on current assertion.
*
* @name not
* @property
* @memberOf Assertion
* @category assertion
*/
Assertion.addChain('not', function() {
this.negate = !this.negate;
});
/**
* Any modifier - it affect on execution of sequenced assertion to do not `check all`, but `check any of`.
*
* @name any
* @property
* @memberOf Assertion
* @category assertion
*/
Assertion.addChain('any', function() {
this.anyOne = true;
});