-
Notifications
You must be signed in to change notification settings - Fork 3
/
expectation.js
180 lines (153 loc) · 4.3 KB
/
expectation.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
require("collections/shim");
module.exports = Expectation;
function Expectation(value, report, test) {
this.value = value;
this.report = report;
this.test = test;
this.isNot = false;
this.not = Object.create(this);
this.not.isNot = true;
this.not.not = this;
}
Expectation.prototype.assert = function (guard, messages, objects) {
return this.report.assert(guard, this.isNot, messages, objects);
};
Expectation.binaryMethod = expectationBinaryMethod;
function expectationBinaryMethod(operator, operatorName) {
return function (value) {
this.assert(
operator.call(this, this.value, value),
[
"expected",
"[not] " + operatorName
],
[
this.value,
value
]
);
};
}
function equals(left, right) {
// So that right can be an Any object with an equals override
return Object.equals(right, left);
}
Expectation.prototype.toEqual = Expectation.binaryMethod(equals, "to equal");
Expectation.prototype.toBe = Expectation.binaryMethod(Object.is, "to be");
Expectation.prototype.toNotBe = function (value) {
console.warn(new Error("toNotBe is deprecated. Use not.toBe").stack);
return this.not.toBe(value);
};
Expectation.prototype.toBeUndefined = function () {
return this.toBe(undefined);
};
Expectation.prototype.toBeDefined = function () {
return this.not.toBe(undefined);
};
Expectation.prototype.toBeNull = function () {
return this.toBe(null);
};
Expectation.prototype.toBeTruthy = Expectation.binaryMethod(Boolean, "to be truthy");
Expectation.prototype.toBeFalsy = function () {
return this.not.toBeTruthy();
};
Expectation.prototype.toContain = Expectation.binaryMethod(Object.has, "to contain");
function lessThan(a, b) {
return Object.compare(a, b) < 0;
}
Expectation.prototype.toBeLessThan = Expectation.binaryMethod(lessThan, "to be less than");
function greaterThan(a, b) {
return Object.compare(a, b) > 0;
}
Expectation.prototype.toBeGreaterThan = Expectation.binaryMethod(greaterThan, "to be greater than");
function near(a, b, epsilon) {
var difference = Math.abs(Object.compare(a, b));
if (difference === 0) {
return Object.equals(a, b);
} else {
return difference <= epsilon;
}
}
Expectation.prototype.toBeNear = function (value, epsilon) {
this.assert(
near(this.value, value, epsilon),
[
"expected",
"[not] to be near",
"within",
"above or below"
],
[
this.value,
value,
epsilon
]
);
};
function close(a, b, precision) {
return near(a, b, Math.pow(10, -precision));
}
Expectation.prototype.toBeCloseTo = function (value, precision) {
this.assert(
close(this.value, value, precision),
[
"expected",
"[not] to be close to",
"within",
"digits of precision"
],
[
this.value,
value,
precision
]
);
};
Expectation.prototype.toBeBetween = function (low, high) {
this.assert(
Object.compare(low, this.value) <= 0 &&
Object.compare(high, this.value) > 0,
[
"expected",
"[not] to be within the interval",
"inclusive to",
"exclusive"
],
[
this.value,
low,
high
]
);
};
function match(a, b) {
if (typeof b === "string") {
b = new RegExp(RegExp.escape(b));
}
return b.exec(a) != null;
}
Expectation.prototype.toMatch = Expectation.binaryMethod(match, "to match");
Expectation.prototype.toThrow = function () {
if (typeof this.value !== "function") {
this.report.assert(false, false, [
"expected function but got"
], [this.value]);
return;
}
try {
this.value();
this.assert(false, [
"expected function [not] to throw",
], []);
} catch (error) {
if (this.isNot) {
this.assert(true, [
"expected function not to throw but threw"
], [error]);
} else {
this.assert(true, [
"expected function to throw"
], []);
}
}
};