This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
forked from mguterl/chai-datetime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchai-datetime.js
170 lines (136 loc) · 5.21 KB
/
chai-datetime.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
(function(plugin){
if (
typeof require === "function" &&
typeof exports === "object" &&
typeof module === "object"
) {
// NodeJS
module.exports = plugin;
} else if (
typeof define === "function" &&
define.amd
) {
// AMD
define(function () {
return plugin;
});
} else {
// Other environment (usually <script> tag): plug in to global chai instance directly.
chai.use(plugin);
}
}(function(chai, utils){
chai.datetime = chai.datetime || {};
chai.datetime.formatDate = function(date) {
return date.toDateString();
};
chai.datetime.formatTime = function(time) {
return time;
};
chai.datetime.equalTime = function(actual, expected) {
return actual.getTime() == expected.getTime();
};
chai.datetime.equalDate = function(actual, expected) {
return actual.toDateString() === expected.toDateString();
};
chai.datetime.beforeDate = function(actual, expected) {
return actual.getUTCFullYear() < expected.getUTCFullYear() || actual.getUTCMonth() < expected.getUTCMonth() || actual.getUTCDate() < expected.getUTCDate();
};
chai.datetime.afterDate = function(actual, expected) {
return actual.getUTCFullYear() > expected.getUTCFullYear() || actual.getUTCMonth() > expected.getUTCMonth() || actual.getUTCDate() > expected.getUTCDate();
};
chai.datetime.beforeTime = function(actual, expected) {
return actual.getTime() < expected.getTime();
};
chai.datetime.afterTime = function(actual, expected) {
return actual.getTime() > expected.getTime();
};
chai.Assertion.addChainableMethod('equalTime', function(expected) {
var actual = this._obj;
return this.assert(
chai.datetime.equalTime(expected, actual),
'expected ' + this._obj + ' to equal ' + expected,
'expected ' + this._obj + ' to not equal ' + expected,
expected.toString(),
actual.toString()
);
});
chai.Assertion.addChainableMethod('equalDate', function(expected) {
var expectedDate = chai.datetime.formatDate(expected),
actualDate = chai.datetime.formatDate(this._obj);
return this.assert(
chai.datetime.equalDate(this._obj, expected),
'expected ' + actualDate + ' to equal ' + expectedDate,
'expected ' + actualDate + ' to not equal ' + expectedDate
);
});
chai.Assertion.addChainableMethod('beforeDate', function(expected) {
var actual = this._obj;
this.assert(
chai.datetime.beforeDate(actual, expected),
'expected ' + chai.datetime.formatDate(actual) + ' to be before ' + chai.datetime.formatDate(expected),
'expected ' + chai.datetime.formatDate(actual) + ' not to be before ' + chai.datetime.formatDate(expected)
);
});
chai.Assertion.addChainableMethod('afterDate', function(expected) {
var actual = this._obj;
this.assert(
chai.datetime.afterDate(actual, expected),
'expected ' + chai.datetime.formatDate(actual) + ' to be after ' + chai.datetime.formatDate(expected),
'expected ' + chai.datetime.formatDate(actual) + ' not to be after ' + chai.datetime.formatDate(expected)
);
});
chai.Assertion.addChainableMethod('beforeTime', function(expected) {
var actual = this._obj;
this.assert(
chai.datetime.beforeTime(actual, expected),
'expected ' + chai.datetime.formatTime(actual) + ' to be before ' + chai.datetime.formatTime(expected),
'expected ' + chai.datetime.formatTime(actual) + ' not to be before ' + chai.datetime.formatTime(expected)
);
});
chai.Assertion.addChainableMethod('afterTime', function(expected) {
var actual = this._obj;
this.assert(
chai.datetime.afterTime(actual, expected),
'expected ' + chai.datetime.formatTime(actual) + ' to be after ' + chai.datetime.formatTime(expected),
'expected ' + chai.datetime.formatTime(actual) + ' not to be after ' + chai.datetime.formatTime(expected)
);
});
// Asserts
var assert = chai.assert;
assert.equalDate = function(val, exp, msg) {
new chai.Assertion(val, msg).to.be.equalDate(exp);
};
assert.notEqualDate = function(val, exp, msg) {
new chai.Assertion(val, msg).to.not.be.equalDate(exp);
};
assert.beforeDate = function(val, exp, msg) {
new chai.Assertion(val, msg).to.be.beforeDate(exp);
};
assert.notBeforeDate = function(val, exp, msg) {
new chai.Assertion(val, msg).to.not.be.beforeDate(exp);
};
assert.afterDate = function(val, exp, msg) {
new chai.Assertion(val, msg).to.be.afterDate(exp);
};
assert.notAfterDate = function(val, exp, msg) {
new chai.Assertion(val, msg).not.to.be.afterDate(exp);
};
assert.equalTime = function(val, exp, msg) {
new chai.Assertion(val, msg).to.be.equalTime(exp);
};
assert.notEqualTime = function(val, exp, msg) {
new chai.Assertion(val, msg).to.not.be.equalTime(exp);
};
assert.beforeTime = function(val, exp, msg) {
new chai.Assertion(val, msg).to.be.beforeTime(exp);
};
assert.notBeforeTime = function(val, exp, msg) {
new chai.Assertion(val, msg).not.to.be.beforeTime(exp);
};
assert.afterTime = function(val, exp, msg) {
new chai.Assertion(val, msg).to.be.afterTime(exp);
};
assert.notAfterTime = function(val, exp, msg) {
new chai.Assertion(val, msg).to.not.be.afterTime(exp);
};
}));