summaryrefslogtreecommitdiffstats
path: root/debian/tests/test_modules/chai-iterator/chai-iterator.js
blob: e2c9f9343d662e4e403b2aa3c79980a92a340596 (plain)
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
(function (chaiIterator) {
	'use strict';

	/* istanbul ignore else */
	if (typeof module === 'object' && typeof exports === 'object') {
		module.exports = chaiIterator;
	} else if (typeof define === 'function' && define.amd) {
		define(function () {
			return chaiIterator;
		});
	} else {
		chai.use(chaiIterator);
	}
})(function (chai, utils) {
	'use strict';

	var Assertion = chai.Assertion;
	var assert = chai.assert;

	var noop = function () {};

	var ELLIPSIS = unquote('...');

	// Expect/should assertions =============================

	Assertion.addProperty('iterable', function () {
		this.assert(isIterable(this._obj),
			'expected #{this} to be iterable',
			'expected #{this} not to be iterable'
		);
	});

	Assertion.addProperty('iterate', function () {
		new Assertion(this._obj).iterable;

		utils.flag(this, 'iterate', true);
	});

	Assertion.addMethod('over', iterateMethod('to iterate over', function (exp) {
		var it = this._obj[Symbol.iterator]();
		var actual = slice(it, exp.length + 2);
		var suffix = it.next().done ? [] : [ELLIPSIS];

		return actual.concat(suffix);
	}));

	Assertion.addMethod('from', iterateMethod('to begin iteration with', function (exp) {
		return slice(this._obj[Symbol.iterator](), exp.length);
	}));

	Assertion.addMethod('until', iterateMethod('to end iteration with', function (exp) {
		var it = this._obj[Symbol.iterator]();
		var actual = slice(it, exp.length);
		var step = it.next();

		while (!step.done) {
			actual.push(step.value);
			actual.shift();
			step = it.next();
		}

		return actual;
	}));

	Assertion.addProperty('for', noop);

	Assertion.overwriteChainableMethod('lengthOf', function (_super) {
		return function (exp) {
			if (utils.flag(this, 'iterate')) {
				var len = iterableLength(this._obj, exp);

				this.assert(
					len === exp,
					'expected #{this} to iterate for length of #{exp}, but got #{act}',
					'expected #{this} not to iterate for length of #{exp}',
					exp,
					len
				);
			} else {
				_super.apply(this, arguments);
			}
		};
	}, function (_super) {
		return function () {
			_super.apply(this);
		};
	});

	Assertion.overwriteMethod('above', function (_super) {
		return function (exp) {
			if (utils.flag(this, 'iterate')) {
				var len = iterableLength(this._obj, exp);

				this.assert(
					len > exp,
					'expected #{this} to iterate for length above #{exp}, but got #{act}',
					'expected #{this} not to iterate for length above #{exp}',
					exp,
					len
				);
			} else {
				_super.apply(this, arguments);
			}
		};
	});

	Assertion.overwriteMethod('below', function (_super) {
		return function (exp) {
			if (utils.flag(this, 'iterate')) {
				var max = exp + 100;
				var len = iterableLength(this._obj, max);
				var act = len === Infinity ? unquote('more than ' + max) : len;

				this.assert(
					len < exp,
					'expected #{this} to iterate for length below #{exp}, but got #{act}',
					'expected #{this} not to iterate for length below #{exp}',
					exp,
					act
				);
			} else {
				_super.apply(this, arguments);
			}
		};
	});

	Assertion.overwriteMethod('least', function (_super) {
		return function (exp) {
			if (utils.flag(this, 'iterate')) {
				var len = iterableLength(this._obj, exp);

				this.assert(
					len >= exp,
					'expected #{this} to iterate for length of at least #{exp}, but got #{act}',
					'expected #{this} not to iterate for length of at least #{exp}',
					exp,
					len
				);
			} else {
				_super.apply(this, arguments);
			}
		};
	});

	Assertion.overwriteMethod('most', function (_super) {
		return function (exp) {
			if (utils.flag(this, 'iterate')) {
				var max = exp + 100;
				var len = iterableLength(this._obj, max);
				var act = len === Infinity ? unquote('more than ' + max) : len;

				this.assert(
					len <= exp,
					'expected #{this} to iterate for length of at most #{exp}, but got #{act}',
					'expected #{this} not to iterate for length of at most #{exp}',
					exp,
					act
				);
			} else {
				_super.apply(this, arguments);
			}
		};
	});

	Assertion.overwriteMethod('within', function (_super) {
		return function (min, max) {
			if (utils.flag(this, 'iterate')) {
				var cutoff = max + 100;
				var len = iterableLength(this._obj, cutoff);
				var exp = unquote(min + 'and' + max);
				var act = len === Infinity ? unquote('more than ' + cutoff) : len;

				this.assert(
					min <= len && len <= max,
					'expected #{this} to iterate for length within #{exp}, but got #{act}',
					'expected #{this} not to iterate for length within #{exp}',
					exp,
					act
				);
			} else {
				_super.apply(this, arguments);
			}
		};
	});

	// Assert methods =======================================

	assert.isIterable = function (value, msg) {
		new Assertion(value, msg).iterable;
	};

	assert.isNotIterable = function (value, msg) {
		new Assertion(value, msg).not.iterable;
	};

	assert.iteratesFrom = function (value, exp, msg) {
		new Assertion(value, msg).iterate.from(exp);
	};

	assert.doesNotIterateFrom = function (value, exp, msg) {
		new Assertion(value, msg).not.iterate.from(exp);
	};

	assert.deepIteratesFrom = function (value, exp, msg) {
		new Assertion(value, msg).deep.iterate.from(exp);
	};

	assert.doesNotDeepIterateFrom = function (value, exp, msg) {
		new Assertion(value, msg).not.deep.iterate.from(exp);
	};

	assert.iteratesOver = function (value, exp, msg) {
		new Assertion(value, msg).iterate.over(exp);
	};

	assert.doesNotIterateOver = function (value, exp, msg) {
		new Assertion(value, msg).not.iterate.over(exp);
	};

	assert.deepIteratesOver = function (value, exp, msg) {
		new Assertion(value, msg).deep.iterate.over(exp);
	};

	assert.doesNotDeepIterateOver = function (value, exp, msg) {
		new Assertion(value, msg).not.deep.iterate.over(exp);
	};

	assert.iteratesUntil = function (value, exp, msg) {
		new Assertion(value, msg).iterate.until(exp);
	};

	assert.doesNotIterateUntil = function (value, exp, msg) {
		new Assertion(value, msg).not.iterate.until(exp);
	};

	assert.deepIteratesUntil = function (value, exp, msg) {
		new Assertion(value, msg).deep.iterate.until(exp);
	};

	assert.doesNotDeepIterateUntil = function (value, exp, msg) {
		new Assertion(value, msg).not.deep.iterate.until(exp);
	};

	var _lengthOf = assert.lengthOf;

	assert.lengthOf = function (value, exp, msg) {
		if (isIterable(value) && typeof value.length !== 'number') {
			new Assertion(value, msg).iterate.for.lengthOf(exp);
		} else {
			_lengthOf.apply(assert, arguments);
		}
	};

	// Helpers ==============================================

	function iterateMethod(predicate, getActual) {
		return function (iterable) {
			assert(utils.flag(this, 'iterate'), 'the iterate flag must be set');
			new Assertion(iterable).iterable;

			var exp = slice(iterable[Symbol.iterator]());
			var act = getActual.call(this, exp);

			var deep = utils.flag(this, 'deep') ? ' deep' : '';

			this.assert(compareArrays(exp, act, deep),
				'expected #{this} ' + predicate + deep + ' values #{exp}, but got #{act}',
				'expected #{this} not ' + predicate + deep + ' values #{exp}',
				exp,
				act
			);
		};
	}

	// Utilities ============================================

	function isIterable(value) {
		return value !== undefined && value !== null && typeof value[Symbol.iterator] === 'function';
	}

	function iterableLength(iterable, max) {
		max = max === undefined ? Infinity : max;

		var it = iterable[Symbol.iterator]();

		for (var i = 0; i <= max; i++) {
			if (it.next().done) {
				return i;
			}
		}

		return Infinity;
	}

	function strictEqual(a, b) {
		return a === b;
	}

	function compareArrays(exp, act, deep) {
		var equalFn = deep ? utils.eql : strictEqual;

		return exp.length === act.length && exp.every(function (value, i) {
			return equalFn(value, act[i]);
		});
	}

	function slice(it, stop) {
		stop = stop === undefined ? Infinity : stop;

		var result = [];
		var max = stop - 1;
		var step = it.next();

		for (var i = 0; i <= max && !step.done; i++) {
			result.push(step.value);
			if (i < max) {
				step = it.next();
			}
		}

		return result;
	}

	function unquote(str) {
		return {
			inspect: function () {
				return this.toString();
			},
			toString: function () {
				return str;
			}
		};
	}
});