-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.js
365 lines (305 loc) · 11.4 KB
/
test.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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
'use strict';
const path = require('path');
const assert = require('assert');
const yoAssert = require('.');
const noop = () => {};
describe('yeoman-assert', () => {
beforeEach(() => {
process.chdir(path.join(__dirname, 'fixtures'));
});
it('extend native assert module', () => {
yoAssert.implement(yoAssert, assert);
});
describe('.file()', () => {
it('accept a file that exists', () => {
assert.doesNotThrow(yoAssert.file.bind(yoAssert, 'testFile'));
});
it('accept an array of files all of which exist', () => {
assert.doesNotThrow(yoAssert.file.bind(yoAssert, ['testFile', 'testFile2']));
});
it('reject a file that does not exist', () => {
assert.throws(yoAssert.file.bind(yoAssert, 'etherealTestFile'));
});
it('reject multiple files one of which does not exist', () => {
assert.throws(yoAssert.file.bind(yoAssert, ['testFile', 'intangibleTestFile']));
});
});
describe('.noFile()', () => {
it('accept a file that does not exist', () => {
assert.doesNotThrow(yoAssert.noFile.bind(yoAssert, 'etherealTestFile'));
});
it('accept an array of files all of which do not exist', () => {
assert.doesNotThrow(
yoAssert.noFile.bind(yoAssert, ['etherealTestFile', 'intangibleTestFile']));
});
it('reject a file that exists', () => {
assert.throws(yoAssert.noFile.bind(yoAssert, 'testFile'));
});
it('reject an array of files one of which exists', () => {
assert.throws(
yoAssert.noFile.bind(yoAssert, ['testFile', 'etherealTestFile']));
});
});
describe('.fileContent()', () => {
it('accept a file and regex when the file content matches the regex', () => {
assert.doesNotThrow(yoAssert.fileContent.bind(yoAssert, 'testFile', /Roses are red/));
});
it('accept a file and string when the file contains the string', () => {
assert.doesNotThrow(yoAssert.fileContent.bind(yoAssert, 'testFile', 'Roses are red'));
});
it('reject a file and regex when the file content does not match the regex', () => {
assert.throws(yoAssert.fileContent.bind(yoAssert, 'testFile', /Roses are blue/));
});
it('reject a file and string when the file content does not contain the string', () => {
assert.throws(yoAssert.fileContent.bind(yoAssert, 'testFile', 'Roses are blue'));
});
it('accept an array of file/regex pairs when each file\'s content matches the corresponding regex', () => {
const arg = [
['testFile', /Roses are red/],
['testFile2', /Violets are blue/]
];
assert.doesNotThrow(yoAssert.fileContent.bind(yoAssert, arg));
});
it('reject an array of file/regex pairs when one file\'s content does not matches the corresponding regex', () => {
const arg = [
['testFile', /Roses are red/],
['testFile2', /Violets are orange/]
];
assert.throws(yoAssert.fileContent.bind(yoAssert, arg));
});
});
describe('.equalsFileContent()', () => {
it('accept a file and string when the file content equals the string', () => {
assert.doesNotThrow(yoAssert.equalsFileContent.bind(yoAssert, 'testFile', 'Roses are red.\n'));
});
it('reject a file and string when the file content does not equal the string', () => {
assert.throws(yoAssert.equalsFileContent.bind(yoAssert, 'testFile', 'Roses are red'));
});
it('accept an array of file/string pairs when each file\'s content equals the corresponding string', () => {
const arg = [
['testFile', 'Roses are red.\n'],
['testFile2', 'Violets are blue.\n']
];
assert.doesNotThrow(yoAssert.equalsFileContent.bind(yoAssert, arg));
});
it('reject an array of file/string pairs when one file\'s content does not equal the corresponding string', () => {
const arg = [
['testFile', 'Roses are red.\n'],
['testFile2', 'Violets are green.\n']
];
assert.throws(yoAssert.equalsFileContent.bind(yoAssert, arg));
});
});
describe('.noFileContent()', () => {
it('accept a file and regex when the file content does not match the regex', () => {
assert.doesNotThrow(yoAssert.noFileContent.bind(yoAssert, 'testFile', /Roses are blue/));
});
it('accept a file and string when the file content does not contain the string', () => {
assert.doesNotThrow(yoAssert.noFileContent.bind(yoAssert, 'testFile', 'Roses are blue'));
});
it('reject a file and regex when the file content matches the regex', () => {
assert.throws(yoAssert.noFileContent.bind(yoAssert, 'testFile', /Roses are red/));
});
it('reject a file and string when the file content contain the string', () => {
assert.throws(yoAssert.noFileContent.bind(yoAssert, 'testFile', 'Roses are red'));
});
it('accept an array of file/regex pairs when each file\'s content does not match its corresponding regex', () => {
const arg = [
['testFile', /Roses are green/],
['testFile2', /Violets are orange/]
];
assert.doesNotThrow(yoAssert.noFileContent.bind(yoAssert, arg));
});
it('reject an array of file/regex pairs when one file\'s content does matches its corresponding regex', () => {
const arg = [
['testFile', /Roses are red/],
['testFile2', /Violets are orange/]
];
assert.throws(yoAssert.noFileContent.bind(yoAssert, arg));
});
});
describe('.textEqual()', () => {
it('pass with two similar simple lines', () => {
assert.doesNotThrow(yoAssert.textEqual.bind(yoAssert,
'I have a yellow cat',
'I have a yellow cat'
));
});
it('fails with two different simple lines', () => {
assert.throws(yoAssert.textEqual.bind(yoAssert,
'I have a yellow cat',
'I have a brown cat'
));
});
it('pass with two similar simple lines with different new line types', () => {
assert.doesNotThrow(yoAssert.textEqual.bind(yoAssert,
'I have a\nyellow cat',
'I have a\r\nyellow cat'
));
});
});
describe('.implement()', () => {
beforeEach(function () {
this.subject = {foo: noop, bar: noop};
this.interfaceSome = ['foo'];
this.interfaceComplete = ['foo', 'bar'];
this.interfaceMore = ['foo', 'yo'];
});
it('pass if an object implement an interface', function () {
assert.doesNotThrow(yoAssert.implement.bind(yoAssert, this.subject, this.interfaceSome));
assert.doesNotThrow(yoAssert.implement.bind(yoAssert, this.subject, this.interfaceComplete));
});
it('fails if methods are missing', function () {
assert.throws(yoAssert.implement.bind(yoAssert, this.subject, this.interfaceMore));
});
it('allow interface to be an object (using its object.keys)', function () {
const interfacePass = {foo: noop};
const interfaceFail = {yop: noop};
assert.doesNotThrow(yoAssert.implement.bind(yoAssert, this.subject, interfacePass));
assert.throws(yoAssert.implement.bind(yoAssert, this.subject, interfaceFail));
});
it('when object is passed in, it only check it implements the methods', function () {
const expected = {foo: noop, yop: 'some arg'};
assert.doesNotThrow(yoAssert.implement.bind(yoAssert, this.subject, expected));
});
});
describe('.notImplement()', () => {
beforeEach(function () {
this.subject = {foo: noop, bar: noop};
this.interfaceSome = ['foo'];
});
it('pass if an object doesn\'t implement an interface', function () {
assert.doesNotThrow(yoAssert.notImplement.bind(yoAssert, this.subject, ['stuff']));
});
it('fails if methods are present', function () {
assert.throws(yoAssert.notImplement.bind(yoAssert, this.subject, ['foo']));
});
});
describe('.objectContent()', () => {
it('pass if object contains the keys', () => {
assert.doesNotThrow(yoAssert.objectContent.bind(yoAssert, {
a: 'foo'
}, {
a: 'foo'
}));
});
it('pass if object contains nested objects and arrays', () => {
assert.doesNotThrow(yoAssert.objectContent.bind(yoAssert, {
a: {b: 'foo'},
b: [0, 'a'],
c: 'a'
}, {
a: {b: 'foo'},
b: [0, 'a']
}));
});
it('pass if array is incomplete', () => {
assert.doesNotThrow(yoAssert.objectContent.bind(yoAssert, {
b: [0, 'a']
}, {
b: [0]
}));
});
it('fails if object does not contain a key', () => {
assert.throws(yoAssert.objectContent.bind(yoAssert, {}, {
a: 'foo'
}));
});
it('fails if nested object does not contain a key', () => {
assert.throws(yoAssert.objectContent.bind(yoAssert, {
a: {}
}, {
a: {b: 'foo'}
}));
});
});
describe('.noObjectContent()', () => {
it('fails if object contains the keys', () => {
assert.throws(yoAssert.noObjectContent.bind(yoAssert, {
a: 'foo'
}, {
a: 'foo'
}));
});
it('pass if object contains nested objects and arrays', () => {
assert.throws(yoAssert.noObjectContent.bind(yoAssert, {
a: {b: 'foo'},
b: [0, 'a'],
c: 'a'
}, {
a: {b: 'foo'},
b: [0, 'a']
}));
});
it('pass if array is incomplete', () => {
assert.throws(yoAssert.noObjectContent.bind(yoAssert, {
b: [0, 'a']
}, {
b: [0]
}));
});
it('pass if object does not contain a key', () => {
assert.doesNotThrow(yoAssert.noObjectContent.bind(yoAssert, {}, {
a: 'foo'
}));
});
it('pass if nested object does not contain a key', () => {
assert.doesNotThrow(yoAssert.noObjectContent.bind(yoAssert, {
a: {}
}, {
a: {b: 'foo'}
}));
});
});
describe('.jsonFileContent()', () => {
const file = path.join(__dirname, 'fixtures/dummy.json');
it('is aliased to .JSONFileContent()', () => {
assert(yoAssert.jsonFileContent === yoAssert.JSONFileContent);
});
it('pass if file contains the keys', () => {
assert.doesNotThrow(yoAssert.jsonFileContent.bind(yoAssert, file, {
a: {b: 1},
b: [1, 2],
d: null
}));
});
it('fails if file does not contain the keys', () => {
assert.throws(yoAssert.jsonFileContent.bind(yoAssert, file, {
a: {b: 1},
b: 'a'
}));
assert.throws(yoAssert.jsonFileContent.bind(yoAssert, file, {
a: {b: 3},
b: [1]
}));
});
it('fails if file does not exists', () => {
assert.throws(yoAssert.jsonFileContent.bind(yoAssert, 'does-not-exist', {}));
});
});
describe('.noJsonFileContent()', () => {
const file = path.join(__dirname, 'fixtures/dummy.json');
it('is aliased to .noJSONFileContent()', () => {
assert(yoAssert.noJsonFileContent === yoAssert.noJSONFileContent);
});
it('fails if file contains the keys', () => {
assert.throws(yoAssert.noJsonFileContent.bind(yoAssert, file, {
a: {b: 1},
b: [1, 2]
}));
});
it('pass if file does not contain the keys', () => {
assert.doesNotThrow(yoAssert.noJsonFileContent.bind(yoAssert, file, {
c: {b: 1},
b: 'a'
}));
assert.doesNotThrow(yoAssert.noJsonFileContent.bind(yoAssert, file, {
a: {b: 3},
b: [2]
}));
});
it('fails if file does not exists', () => {
assert.throws(yoAssert.noJsonFileContent.bind(yoAssert, 'does-not-exist', {}));
});
});
});