-
Notifications
You must be signed in to change notification settings - Fork 4
/
chai-timers.js
174 lines (139 loc) · 4.88 KB
/
chai-timers.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
!function (context, definition) {
if (typeof require === "function" && typeof exports === "object" && typeof module === "object") {
module.exports = definition();
} else if (typeof define === 'function' && typeof define.amd === 'object') {
define(definition);
} else {
if (!context.chai) throw new Error('Chai cannot be found in current scope.');
context.chai.use(definition());
}
}(this, function () {
function require(p) {
var path = require.resolve(p)
, mod = require.modules[path];
if (!mod) throw new Error('failed to require "' + p + '"');
if (!mod.exports) {
mod.exports = {};
mod.call(mod.exports, mod, mod.exports, require.relative(path));
}
return mod.exports;
}
require.modules = {};
require.resolve = function (path) {
var orig = path
, reg = path + '.js'
, index = path + '/index.js';
return require.modules[reg] && reg
|| require.modules[index] && index
|| orig;
};
require.register = function (path, fn) {
require.modules[path] = fn;
};
require.relative = function (parent) {
return function(p){
if ('.' != p[0]) return require(p);
var path = parent.split('/')
, segs = p.split('/');
path.pop();
for (var i = 0; i < segs.length; i++) {
var seg = segs[i];
if ('..' == seg) path.pop();
else if ('.' != seg) path.push(seg);
}
return require(path.join('/'));
};
};
require.alias = function (from, to) {
var fn = require.modules[from];
require.modules[to] = fn;
};
require.register("chai-timers.js", function(module, exports, require){
var Timer = require('./timer');
module.exports = function (chai, _) {
var Assertion = chai.Assertion;
chai.Timer = Timer;
chai.timer = function (name) {
return new Timer(name);
};
Assertion.addProperty('timer', function () {
this.assert(
this._obj instanceof Timer
, 'expected #{this} to be a chai timer'
, 'expected #{this} to not be a chai timer' );
});
[ 'started', 'stopped', 'created' ].forEach(function (when) {
Assertion.overwriteProperty(when, function (_super) {
return function () {
if (this._obj instanceof Timer) {
_.flag(this, 'timer_when', when);
} else {
_super.call(this);
}
}
});
});
Assertion.overwriteMethod('before', function (_super) {
return function assertBefore (timer2, when2) {
var timer1 = this._obj;
new Assertion(timer1).to.be.a.timer;
new Assertion(timer2).to.be.a.timer;
var when1 = _.flag(this, 'timer_when') || 'started';
when2 = when2 || when1;
var time1 = timer1[when1].getTime()
, time2 = timer2[when2].getTime();
this.assert(
time1 < time2
, 'expected timer {' + timer1.name + '} to have been ' + when1 + ' before timer {' + timer2.name + '} was ' + when2
, 'expected timer {' + timer1.name + '} to not have been ' + when1 + ' before timer {' + timer2.name + '} was ' + when2
);
};
});
Assertion.overwriteMethod('after', function (_super) {
return function assertBefore (timer2, when2) {
var timer1 = this._obj;
new Assertion(timer1).to.be.a.timer;
new Assertion(timer2).to.be.a.timer;
var when1 = _.flag(this, 'timer_when') || 'started';
when2 = when2 || when1;
var time1 = timer1[when1].getTime()
, time2 = timer2[when2].getTime();
this.assert(
time1 > time2
, 'expected timer {' + timer1.name + '} to have been ' + when1 + ' after timer {' + timer2.name + '} was ' + when2
, 'expected timer {' + timer1.name + '} to not have been ' + when1 + ' after timer {' + timer2.name + '} was' + when2
);
};
});
};
}); // module: chai-timers.js
require.register("timer.js", function(module, exports, require){
module.exports = Timer;
function Timer (name) {
this.name = name || 'timer';
this.created = new Date();
this.marks = [];
this.started = null;
this.stopped = null;
};
Object.defineProperty(Timer.prototype, 'elapsed',
{ get: function () {
var start = this.started.getTime()
, stop = this.stopped.getTime();
return stop - start;
}
});
Timer.prototype.start = function (date) {
this.started = date || new Date();
return this;
};
Timer.prototype.stop = function (date) {
this.stopped = date || new Date();
};
Timer.prototype.mark = function (date) {
this.marks.push(date || new Date());
};
}); // module: timer.js
require.alias("./chai-timers.js", "chai-timers");
return require('chai-timers');
});