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 | 1×
1×
1156×
1×
1156×
1×
1156×
1×
275×
275×
1×
367×
29×
1×
82×
45×
25×
1×
39×
8×
31×
31×
31×
4×
27×
50×
50×
7×
4×
23×
1×
8×
4×
1×
47×
13×
1×
58×
23×
1×
16×
8×
1×
18×
8×
1×
16×
10×
1×
16×
10×
1×
215×
215×
215×
69×
2×
67×
213×
2×
1×
163×
163×
163×
163×
161×
161×
149×
163×
11×
1×
18×
4×
274×
132×
4591×
28×
1×
| import {
MatchError,
ExactMatchError,
EqualMatchError,
RegexMatchError,
TruthyMatchError,
ContentsMatchError,
LessThanMatchError,
GreaterThanMatchError,
ErrorMatchError,
FunctionCallMatchError
} from "./_errors";
/**
* Allows checking of test outcomes
* @param actualValue - the value or function under test
*/
export function Expect(actualValue: any) {
return new Matcher(actualValue);
}
/**
* Gives functionality to ensure the outcome of a test is as expected
*/
export class Matcher {
private _actualValue: any;
private _shouldMatch: boolean = true;
public constructor(actualValue: any) {
this._actualValue = actualValue;
}
/**
* Any subsequent matcher function will be looking for the opposite criteria
*/
public get not(): Matcher {
this._shouldMatch = !this._shouldMatch;
return this;
}
/**
* Checks that a value is identical to another
* @param expectedValue - the value that will be used to match
*/
public toBe(expectedValue: any) {
if (expectedValue !== this._actualValue === this._shouldMatch) {
throw new ExactMatchError(this._actualValue, expectedValue, this._shouldMatch);
}
}
/**
* Checks that a value is equal to another (for objects the function will check for deep equality)
* @param expectedValue - the value that will be used to match
*/
public toEqual(expectedValue: any) {
// exclude the double equals in this case from review as this is what we want to do
if (expectedValue != this._actualValue === this._shouldMatch) { // tslint:disable-line:triple-equals
if (typeof expectedValue !== "object" || this._checkObjectsAreDeepEqual(expectedValue, this._actualValue) !== this._shouldMatch) {
throw new EqualMatchError(this._actualValue, expectedValue, this._shouldMatch);
}
}
}
private _checkObjectsAreDeepEqual(objectA: any, objectB: any): boolean {
// if one object is an array and the other is not then they are not equal
if (Array.isArray(objectA) !== Array.isArray(objectB)) {
return false;
}
// get all the property keys for each object
let objectAKeys = Object.keys(objectA);
let objectBKeys = Object.keys(objectB);
// if they don't have the same amount of properties then clearly not
if (objectAKeys.length !== objectBKeys.length) {
return false;
}
// check all the properties of each object
for (let i = 0; i < objectAKeys.length; i++) {
let objectAKey = objectAKeys[i];
// if the property values are not the same
if (objectA[objectAKey] !== objectB[objectAKey]) {
// and it's not an object or the objects are not equal
if (typeof(objectA[objectAKey]) !== "object" || this._checkObjectsAreDeepEqual(objectA[objectAKey], objectB[objectAKey]) === false) {
// then not deep equal
return false;
}
}
}
// all properties match so all is good
return true;
}
/**
* Checks that a value conforms to a regular expression
* @param regex - the regular expression that the actual value should match
*/
public toMatch(regex: RegExp) {
if (!regex.test(this._actualValue) === this._shouldMatch) {
throw new RegexMatchError(this._actualValue, regex, this._shouldMatch);
}
}
/**
* Checks that a value is not undefined
*/
public toBeDefined() {
if (this._actualValue === undefined === this._shouldMatch) {
throw new ExactMatchError(this._actualValue, undefined, !this._shouldMatch);
}
}
/**
* Checks that a value is null
*/
public toBeNull() {
if (this._actualValue !== null === this._shouldMatch) {
throw new ExactMatchError(this._actualValue, null, this._shouldMatch);
}
}
/**
* Checks that a value is equivalent to boolean true
*/
public toBeTruthy() {
if ((this._actualValue && !this._shouldMatch) || (!this._actualValue && this._shouldMatch)) {
throw new TruthyMatchError(this._actualValue, this._shouldMatch);
}
}
/**
* Checks that a string contains another string or an array contains a specific item
* @param expectedContent - the string or array item that the value should contain
*/
public toContain(expectedContent: any) {
if (this._actualValue.indexOf(expectedContent) === -1 === this._shouldMatch) {
throw new ContentsMatchError(this._actualValue, expectedContent, this._shouldMatch);
}
}
/**
* Checks that a number is less than a given limit
* @param upperLimit - the number that the number under test should be less than
*/
public toBeLessThan(upperLimit: number) {
if (this._actualValue < upperLimit !== this._shouldMatch) {
throw new LessThanMatchError(this._actualValue, upperLimit, this._shouldMatch);
}
}
/**
* Checks that a number is greater than a given limit
* @param lowerLimit - the number that the number under test should be greater than
*/
public toBeGreaterThan(lowerLimit: number) {
if (this._actualValue > lowerLimit !== this._shouldMatch) {
throw new GreaterThanMatchError(this._actualValue, lowerLimit, this._shouldMatch);
}
}
/**
* Checks that a function throws an error when executed
*/
public toThrow() {
let errorThrown: Error;
try {
this._actualValue();
}
catch (error) {
if (!this._shouldMatch) {
throw new ErrorMatchError(error, this._shouldMatch);
}
errorThrown = error;
}
if (this._shouldMatch && errorThrown === undefined) {
throw new ErrorMatchError(undefined, this._shouldMatch);
}
}
/**
* Checks that a function throws a specific error
* @param errorType - the type of error that should be thrown
* @param errorMessage - the message that the error should have
*/
public toThrowError(errorType: new (...args: Array<any>) => Error, errorMessage: string) {
let threwRightError = false;
let actualError: Error;
try {
this._actualValue();
}
catch (error) {
actualError = error;
if (error instanceof errorType && error.message === errorMessage) {
threwRightError = true;
}
}
if (!threwRightError === this._shouldMatch) {
throw new ErrorMatchError(actualError, this._shouldMatch, (<any>errorType), errorMessage);
}
}
/**
* Checks that a spy has been called
*/
public toHaveBeenCalled() {
if (this._actualValue.calls.length === 0 === this._shouldMatch) {
throw new FunctionCallMatchError(this._actualValue, this._shouldMatch);
}
}
/**
* Checks that a spy has been called with the specified arguments
* @param args - a list of arguments that the spy should have been called with
*/
public toHaveBeenCalledWith(...args: Array<any>) {
if (this._actualValue.calls.filter((call: any) => {
return call.args.filter((arg: any, index: number) => arg === args[index]).length === args.length && // all call arguments match expected arguments
call.args.length === args.length; // and the call has the same amount of arguments
}).length === 0 === this._shouldMatch) {
throw new FunctionCallMatchError(this._actualValue, this._shouldMatch, args);
}
}
}
|