konacha-chai-matchers 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,286 @@
1
+ !function (name, definition) {
2
+ if (typeof define == 'function' && typeof define.amd == 'object') define(definition);
3
+ else this[name] = definition();
4
+ }('chai_stats', function () {
5
+ // CommonJS require()
6
+ function require(p){
7
+ var path = require.resolve(p)
8
+ , mod = require.modules[path];
9
+ if (!mod) throw new Error('failed to require "' + p + '"');
10
+ if (!mod.exports) {
11
+ mod.exports = {};
12
+ mod.call(mod.exports, mod, mod.exports, require.relative(path));
13
+ }
14
+ return mod.exports;
15
+ }
16
+
17
+ require.modules = {};
18
+
19
+ require.resolve = function (path){
20
+ var orig = path
21
+ , reg = path + '.js'
22
+ , index = path + '/index.js';
23
+ return require.modules[reg] && reg
24
+ || require.modules[index] && index
25
+ || orig;
26
+ };
27
+
28
+ require.register = function (path, fn){
29
+ require.modules[path] = fn;
30
+ };
31
+
32
+ require.relative = function (parent) {
33
+ return function(p){
34
+ if ('.' != p[0]) return require(p);
35
+
36
+ var path = parent.split('/')
37
+ , segs = p.split('/');
38
+ path.pop();
39
+
40
+ for (var i = 0; i < segs.length; i++) {
41
+ var seg = segs[i];
42
+ if ('..' == seg) path.pop();
43
+ else if ('.' != seg) path.push(seg);
44
+ }
45
+
46
+ return require(path.join('/'));
47
+ };
48
+ };
49
+
50
+
51
+ require.register("calc", function (module, exports, require) {
52
+ /*!
53
+ * Chai Stats - calculation utilities
54
+ * Copyright (c) 2012 Jake Luer <jake@alogicalparadox.com>
55
+ * MIT Licenced
56
+ */
57
+
58
+ /**
59
+ * # sum
60
+ *
61
+ * Returns the sum of a given array of numbers.
62
+ *
63
+ * @param {Array} numbers to sum
64
+ * @returns sum
65
+ */
66
+
67
+ exports.sum = function (nums) {
68
+ var res = 0;
69
+ for (var i = 0; i < nums.length; i++)
70
+ res += nums[i];
71
+ return res;
72
+ };
73
+
74
+ /**
75
+ * # mean
76
+ *
77
+ * Returns the mean (average) of a given array of numbers.
78
+ *
79
+ * @param {Array} numbers to average
80
+ * @returs mean
81
+ */
82
+
83
+ exports.mean = function (nums) {
84
+ var sum = exports.sum(nums);
85
+ return sum / nums.length;
86
+ };
87
+
88
+ /**
89
+ * # sdeviation
90
+ *
91
+ * Returns the standard deviation of a given array of numbers.
92
+ *
93
+ * @param {Array} numbers for stdev
94
+ * @return standard deviation
95
+ */
96
+
97
+ exports.sdeviation = function (nums) {
98
+ var devs = []
99
+ , mean = exports.mean(nums);
100
+ for (var i = 0; i < nums.length; i++)
101
+ devs.push(nums[i] - mean);
102
+ for (var d = 0; d < devs.length; d++)
103
+ devs[d] = Math.pow(devs[d], 2);
104
+ var davg = exports.sum(devs) / (devs.length - 1);
105
+ return Math.sqrt(davg);
106
+ };
107
+
108
+ }); // module calc
109
+
110
+
111
+ require.register("stats", function (module, exports, require) {
112
+ var calc = require('./calc');
113
+
114
+ module.exports = function (chai, _) {
115
+ var Assertion = chai.Assertion
116
+ , flag = _.flag
117
+
118
+ Assertion.addProperty('almost', function () {
119
+ flag(this, 'almost', true);
120
+ });
121
+
122
+ /**
123
+ * # .almost.equal(expected, [precision])
124
+ *
125
+ * The same as NumPy's assert_almost_equal, for scalars.
126
+ * Assert near equality: abs(expected-actual) < 0.5 * 10**(-decimal)
127
+ *
128
+ * expect(3.1415).to.almost.equal(3.14159, 3);
129
+ * assert.almostEqual(3.1416, 3.14159, 3, 'these numbers are almost equal');
130
+ *
131
+ * @name equal
132
+ * @param {Number} actual
133
+ * @param {Number} expected
134
+ * @param {Number} decimal
135
+ * @param {String} message
136
+ * @api public
137
+ */
138
+
139
+ Assertion.overwriteMethod('equal', function(_super) {
140
+ return function equal (exp, precision) {
141
+ if (flag(this, 'almost')) {
142
+ var act = flag(this, 'object')
143
+ if (null == precision) precision = 7;
144
+ this.assert(
145
+ Math.abs(act - exp) < 0.5 * Math.pow(10, -precision)
146
+ , "expected #{this} to equal #{exp} up to " + _.inspect(precision) + " decimal places"
147
+ , "expected #{this} to not equal #{exp} up to " + _.inspect(precision) + " decimal places"
148
+ , exp
149
+ , act
150
+ );
151
+ } else {
152
+ _super.apply(this, arguments);
153
+ }
154
+ };
155
+ });
156
+
157
+ _.addMethod(chai.assert, 'almostEqual', function (act, exp, precision, msg) {
158
+ new Assertion(act, msg).to.almost.equal(exp, precision);
159
+ });
160
+
161
+ /**
162
+ * # .deepAlmostEqual(actual, expected, [decimal, message])
163
+ *
164
+ * The same as NumPy's assert_almost_equal, for objects whose leaves are all numbers.
165
+ * Assert near equality: abs(expected-actual) < 0.5 * 10**(-decimal) for every leaf.
166
+ *
167
+ * assert.deepAlmostEqual({pi: 3.1416}, {pi: 3.14159}, 3);
168
+ *
169
+ * @name equal
170
+ * @param {*} actual
171
+ * @param {*} expected
172
+ * @param {Number} decimal
173
+ * @param {String} message
174
+ * @api public
175
+ */
176
+
177
+ Assertion.overwriteMethod('eql', function (_super) {
178
+ return function eql (exp, precision) {
179
+ if (flag(this, 'almost')) {
180
+ if (null == precision) precision = 7;
181
+ var act = flag(this, 'object')
182
+ , tol = 0.5 * Math.pow(10, -precision);
183
+
184
+ function deepEql (act, exp) {
185
+ if (Object(act) === act){
186
+ for (var k in act) {
187
+ if (!(deepEql(act[k], exp[k]))) {
188
+ return false;
189
+ }
190
+ }
191
+ return true;
192
+ } else {
193
+ return Math.abs(act - exp) < tol;
194
+ }
195
+ };
196
+
197
+ this.assert(
198
+ deepEql(act, exp)
199
+ , "expected #{this} to equal #{exp} up to " + _.inspect(precision) + ' decimal places'
200
+ , "expected #{this} to not equal #{exp} up to " + _.inspect(precision) + ' decimal places'
201
+ , exp
202
+ , act
203
+ );
204
+ } else {
205
+ _super.apply(this, arguments);
206
+ }
207
+ };
208
+ });
209
+
210
+ _.addMethod(chai.assert, 'deepAlmostEqual', function (act, exp, precision, msg) {
211
+ new Assertion(act, msg).to.almost.eql(exp, precision);
212
+ });
213
+
214
+ /**
215
+ * ### .sum
216
+ *
217
+ * Modifies the assertion subject with the sum of an
218
+ * array of number so it can be compared using chai's
219
+ * core assertions.
220
+ *
221
+ * expect([ 1, 2, 3 ]).to.have.sum.equal(6);
222
+ * expect([ 1, 2, 3 ]).to.have.sum.above(5);
223
+ * expect([ 1, 2, 3 ]).to.have.sum.below(7);
224
+ *
225
+ * @name sum
226
+ * @expects {Array}
227
+ * @api public
228
+ */
229
+
230
+ Assertion.addProperty('sum', function () {
231
+ var obj = flag(this, 'object');
232
+ new Assertion(obj).to.be.instanceOf(Array);
233
+ flag(this, 'object', calc.sum(obj));
234
+ });
235
+
236
+ /**
237
+ * ### .mean
238
+ *
239
+ * Modifies the assertion subject with the mean of an
240
+ * array of number so it can be compared using chai's
241
+ * core assertions.
242
+ *
243
+ * expect([ 1, 2, 3 ]).to.have.mean.equal(2);
244
+ * expect([ 1, 2, 3 ]).to.have.mean.above(1.5);
245
+ * expect([ 1, 2, 3 ]).to.have.mean.below(2.5);
246
+ *
247
+ * @name mean
248
+ * @expects {Array}
249
+ * @api public
250
+ */
251
+
252
+ Assertion.addProperty('mean', function () {
253
+ var obj = flag(this, 'object');
254
+ new Assertion(obj).to.be.instanceOf(Array);
255
+ flag(this, 'object', calc.mean(obj));
256
+ });
257
+
258
+ function deviation () {
259
+ var obj = flag(this, 'object');
260
+ new Assertion(obj).to.be.instanceOf(Array);
261
+ flag(this, 'object', calc.sdeviation(obj));
262
+ }
263
+
264
+ /**
265
+ * ### .deviation
266
+ *
267
+ * Modifies the assertion subject with the standard
268
+ * deviations of an array of number so it can be
269
+ * compared using chai's core assertions.
270
+ *
271
+ * expect([ 1, 2, 3, 4 ]).to.have.deviation.almost.equal(1.290, 2);
272
+ *
273
+ * @name mean
274
+ * @expects {Array}
275
+ * @api public
276
+ */
277
+
278
+ Assertion.addProperty('stdev', deviation);
279
+ Assertion.addProperty('deviation', deviation);
280
+ };
281
+
282
+ }); // module stats
283
+ return require('stats');
284
+ });
285
+
286
+ chai.use(chai_stats);
@@ -0,0 +1,174 @@
1
+ !function (context, definition) {
2
+ if (typeof require === "function" && typeof exports === "object" && typeof module === "object") {
3
+ module.exports = definition();
4
+ } else if (typeof define === 'function' && typeof define.amd === 'object') {
5
+ define(definition);
6
+ } else {
7
+ if (!context.chai) throw new Error('Chai cannot be found in current scope.');
8
+ context.chai.use(definition());
9
+ }
10
+ }(this, function () {
11
+
12
+
13
+ function require(p) {
14
+ var path = require.resolve(p)
15
+ , mod = require.modules[path];
16
+ if (!mod) throw new Error('failed to require "' + p + '"');
17
+ if (!mod.exports) {
18
+ mod.exports = {};
19
+ mod.call(mod.exports, mod, mod.exports, require.relative(path));
20
+ }
21
+ return mod.exports;
22
+ }
23
+
24
+ require.modules = {};
25
+
26
+ require.resolve = function (path) {
27
+ var orig = path
28
+ , reg = path + '.js'
29
+ , index = path + '/index.js';
30
+ return require.modules[reg] && reg
31
+ || require.modules[index] && index
32
+ || orig;
33
+ };
34
+
35
+ require.register = function (path, fn) {
36
+ require.modules[path] = fn;
37
+ };
38
+
39
+ require.relative = function (parent) {
40
+ return function(p){
41
+ if ('.' != p[0]) return require(p);
42
+
43
+ var path = parent.split('/')
44
+ , segs = p.split('/');
45
+ path.pop();
46
+
47
+ for (var i = 0; i < segs.length; i++) {
48
+ var seg = segs[i];
49
+ if ('..' == seg) path.pop();
50
+ else if ('.' != seg) path.push(seg);
51
+ }
52
+
53
+ return require(path.join('/'));
54
+ };
55
+ };
56
+
57
+ require.alias = function (from, to) {
58
+ var fn = require.modules[from];
59
+ require.modules[to] = fn;
60
+ };
61
+
62
+
63
+ require.register("chai-timers.js", function(module, exports, require){
64
+ var Timer = require('./timer');
65
+
66
+ module.exports = function (chai, _) {
67
+ var Assertion = chai.Assertion;
68
+
69
+ chai.Timer = Timer;
70
+
71
+ chai.timer = function (name) {
72
+ return new Timer(name);
73
+ };
74
+
75
+ Assertion.addProperty('timer', function () {
76
+ this.assert(
77
+ this._obj instanceof Timer
78
+ , 'expected #{this} to be a chai timer'
79
+ , 'expected #{this} to not be a chai timer' );
80
+ });
81
+
82
+ [ 'started', 'stopped', 'created' ].forEach(function (when) {
83
+ Assertion.overwriteProperty(when, function (_super) {
84
+ return function () {
85
+ if (this._obj instanceof Timer) {
86
+ _.flag(this, 'timer_when', when);
87
+ } else {
88
+ _super.call(this);
89
+ }
90
+ }
91
+ });
92
+ });
93
+
94
+ Assertion.overwriteMethod('before', function (_super) {
95
+ return function assertBefore (timer2, when2) {
96
+ var timer1 = this._obj;
97
+ new Assertion(timer1).to.be.a.timer;
98
+ new Assertion(timer2).to.be.a.timer;
99
+
100
+ var when1 = _.flag(this, 'timer_when') || 'started';
101
+ when2 = when2 || when1;
102
+ var time1 = timer1[when1].getTime()
103
+ , time2 = timer2[when2].getTime();
104
+
105
+ this.assert(
106
+ time1 < time2
107
+ , 'expected timer {' + timer1.name + '} to have been ' + when1 + ' before timer {' + timer2.name + '} was ' + when2
108
+ , 'expected timer {' + timer1.name + '} to not have been ' + when1 + ' before timer {' + timer2.name + '} was ' + when2
109
+ );
110
+ };
111
+ });
112
+
113
+ Assertion.overwriteMethod('after', function (_super) {
114
+ return function assertBefore (timer2, when2) {
115
+ var timer1 = this._obj;
116
+ new Assertion(timer1).to.be.a.timer;
117
+ new Assertion(timer2).to.be.a.timer;
118
+
119
+ var when1 = _.flag(this, 'timer_when') || 'started';
120
+ when2 = when2 || when1;
121
+ var time1 = timer1[when1].getTime()
122
+ , time2 = timer2[when2].getTime();
123
+
124
+ this.assert(
125
+ time1 > time2
126
+ , 'expected timer {' + timer1.name + '} to have been ' + when1 + ' after timer {' + timer2.name + '} was ' + when2
127
+ , 'expected timer {' + timer1.name + '} to not have been ' + when1 + ' after timer {' + timer2.name + '} was' + when2
128
+ );
129
+ };
130
+ });
131
+
132
+ };
133
+
134
+ }); // module: chai-timers.js
135
+
136
+ require.register("timer.js", function(module, exports, require){
137
+
138
+ module.exports = Timer;
139
+
140
+ function Timer (name) {
141
+ this.name = name || 'timer';
142
+ this.created = new Date();
143
+ this.marks = [];
144
+ this.started = null;
145
+ this.stopped = null;
146
+ };
147
+
148
+ Object.defineProperty(Timer.prototype, 'elapsed',
149
+ { get: function () {
150
+ var start = this.started.getTime()
151
+ , stop = this.stopped.getTime();
152
+ return stop - start;
153
+ }
154
+ });
155
+
156
+ Timer.prototype.start = function (date) {
157
+ this.started = date || new Date();
158
+ return this;
159
+ };
160
+
161
+ Timer.prototype.stop = function (date) {
162
+ this.stopped = date || new Date();
163
+ };
164
+
165
+ Timer.prototype.mark = function (date) {
166
+ this.marks.push(date || new Date());
167
+ };
168
+
169
+ }); // module: timer.js
170
+
171
+ require.alias("./chai-timers.js", "chai-timers");
172
+
173
+ return require('chai-timers');
174
+ });