/
unit.js
439 lines (361 loc) · 12.1 KB
/
unit.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/// <reference types="cypress" />
import {
parseGrep,
parseTitleGrep,
parseFullTitleGrep,
parseTagsGrep,
shouldTestRun,
shouldTestRunTags,
shouldTestRunTitle,
} from '../../src/utils'
describe('utils', () => {
context('parseTitleGrep', () => {
it('grabs the positive title', () => {
const parsed = parseTitleGrep('hello w')
expect(parsed).to.deep.equal({
title: 'hello w',
invert: false,
})
})
it('trims the string', () => {
const parsed = parseTitleGrep(' hello w ')
expect(parsed).to.deep.equal({
title: 'hello w',
invert: false,
})
})
it('inverts the string', () => {
const parsed = parseTitleGrep('-hello w')
expect(parsed).to.deep.equal({
title: 'hello w',
invert: true,
})
})
it('trims the inverted the string', () => {
const parsed = parseTitleGrep(' -hello w ')
expect(parsed).to.deep.equal({
title: 'hello w',
invert: true,
})
})
it('returns null for undefined input', () => {
const parsed = parseTitleGrep()
expect(parsed).to.equal(null)
})
})
context('parseFullTitleGrep', () => {
it('returns list of title greps', () => {
const parsed = parseFullTitleGrep('hello; one; -two')
expect(parsed).to.deep.equal([
{ title: 'hello', invert: false },
{ title: 'one', invert: false },
{ title: 'two', invert: true },
])
})
})
context('parseTagsGrep', () => {
it('parses AND tags', () => {
// run only the tests with all 3 tags
const parsed = parseTagsGrep('@tag1+@tag2+@tag3')
expect(parsed).to.deep.equal([
// single OR part
[
// with 3 AND parts
{ tag: '@tag1', invert: false },
{ tag: '@tag2', invert: false },
{ tag: '@tag3', invert: false },
],
])
})
it('handles dashes in the tag', () => {
const parsed = parseTagsGrep('@smoke+@screen-b')
expect(parsed).to.deep.equal([
[
{ tag: '@smoke', invert: false },
{ tag: '@screen-b', invert: false },
],
])
})
it('parses OR tags spaces', () => {
// run tests with tag1 OR tag2 or tag3
const parsed = parseTagsGrep('@tag1 @tag2 @tag3')
expect(parsed).to.deep.equal([
[{ tag: '@tag1', invert: false }],
[{ tag: '@tag2', invert: false }],
[{ tag: '@tag3', invert: false }],
])
})
it('parses OR tags commas', () => {
// run tests with tag1 OR tag2 or tag3
const parsed = parseTagsGrep('@tag1,@tag2,@tag3')
expect(parsed).to.deep.equal([
[{ tag: '@tag1', invert: false }],
[{ tag: '@tag2', invert: false }],
[{ tag: '@tag3', invert: false }],
])
})
it('parses inverted tag', () => {
const parsed = parseTagsGrep('-@tag1')
expect(parsed).to.deep.equal([[{ tag: '@tag1', invert: true }]])
})
it('parses tag1 but not tag2 with space', () => {
const parsed = parseTagsGrep('@tag1 -@tag2')
expect(parsed).to.deep.equal([
[{ tag: '@tag1', invert: false }],
[{ tag: '@tag2', invert: true }],
])
})
it('forgives extra spaces', () => {
const parsed = parseTagsGrep(' @tag1 -@tag2 ')
expect(parsed).to.deep.equal([
[{ tag: '@tag1', invert: false }],
[{ tag: '@tag2', invert: true }],
])
})
it('parses tag1 but not tag2 with comma', () => {
const parsed = parseTagsGrep('@tag1,-@tag2')
expect(parsed).to.deep.equal([
[{ tag: '@tag1', invert: false }],
[{ tag: '@tag2', invert: true }],
])
})
it('filters out empty tags', () => {
const parsed = parseTagsGrep(',, @tag1,-@tag2,, ,, ,')
expect(parsed).to.deep.equal([
[{ tag: '@tag1', invert: false }],
[{ tag: '@tag2', invert: true }],
])
})
// TODO: would need to change the tokenizer
it.skip('parses tag1 but not tag2', () => {
const parsed = parseTagsGrep('@tag1-@tag2')
expect(parsed).to.deep.equal([
[
{ tag: '@tag1', invert: false },
{ tag: '@tag2', invert: true },
],
])
})
it('allows all tags to be inverted', () => {
const parsed = parseTagsGrep('--@tag1,--@tag2')
expect(parsed).to.deep.equal([
[{ tag: '@tag1', invert: true }, { tag: '@tag2', invert: true }],
])
})
})
context('parseGrep', () => {
// no need to exhaustively test the parsing
// since we want to confirm it works via test names
// and not through the implementation details of
// the parsed object
it('creates just the title grep', () => {
const parsed = parseGrep('hello w')
expect(parsed).to.deep.equal({
title: [
{
title: 'hello w',
invert: false,
},
],
tags: [],
})
})
it('creates object from the grep string only', () => {
const parsed = parseGrep('hello w')
expect(parsed).to.deep.equal({
title: [
{
title: 'hello w',
invert: false,
},
],
tags: [],
})
// check how the parsed grep works against specific tests
expect(shouldTestRun(parsed, 'hello w')).to.equal(true)
expect(shouldTestRun(parsed, 'hello no')).to.equal(false)
})
it('matches one of the titles', () => {
// also should trim each title
const parsed = parseGrep(' hello w; work 2 ')
expect(parsed).to.deep.equal({
title: [
{
title: 'hello w',
invert: false,
},
{
title: 'work 2',
invert: false,
},
],
tags: [],
})
// check how the parsed grep works against specific tests
expect(shouldTestRun(parsed, 'hello w')).to.equal(true)
expect(shouldTestRun(parsed, 'this work 2 works')).to.equal(true)
expect(shouldTestRun(parsed, 'hello no')).to.equal(false)
})
it('creates object from the grep string and tags', () => {
const parsed = parseGrep('hello w', '@tag1+@tag2+@tag3')
expect(parsed).to.deep.equal({
title: [
{
title: 'hello w',
invert: false,
},
],
tags: [
// single OR part
[
// with 3 AND parts
{ tag: '@tag1', invert: false },
{ tag: '@tag2', invert: false },
{ tag: '@tag3', invert: false },
],
],
})
// check how the parsed grep works against specific tests
expect(shouldTestRun(parsed, 'hello w'), 'needs tags').to.equal(false)
expect(shouldTestRun(parsed, 'hello no')).to.equal(false)
// not every tag is present
expect(shouldTestRun(parsed, ['@tag1', '@tag2'])).to.equal(false)
expect(shouldTestRun(parsed, ['@tag1', '@tag2', '@tag3'])).to.equal(true)
expect(
shouldTestRun(parsed, ['@tag1', '@tag2', '@tag3', '@tag4']),
).to.equal(true)
// title matches, but tags do not
expect(shouldTestRun(parsed, 'hello w', ['@tag1', '@tag2'])).to.equal(
false,
)
// tags and title match
expect(
shouldTestRun(parsed, 'hello w', ['@tag1', '@tag2', '@tag3']),
).to.equal(true)
})
})
context('shouldTestRunTags', () => {
// when the user types "used" string
// and the test has the given tags, make sure
// our parsing and decision logic computes the expected result
const shouldIt = (used, tags, expected) => {
const parsedTags = parseTagsGrep(used)
expect(
shouldTestRunTags(parsedTags, tags),
`"${used}" against "${tags}"`,
).to.equal(expected)
}
it('handles AND tags', () => {
shouldIt('smoke+slow', ['fast', 'smoke'], false)
shouldIt('smoke+slow', ['mobile', 'smoke', 'slow'], true)
shouldIt('smoke+slow', ['slow', 'extra', 'smoke'], true)
shouldIt('smoke+slow', ['smoke'], false)
})
it('handles OR tags', () => {
// smoke OR slow
shouldIt('smoke slow', ['fast', 'smoke'], true)
shouldIt('smoke', ['mobile', 'smoke', 'slow'], true)
shouldIt('slow', ['slow', 'extra', 'smoke'], true)
shouldIt('smoke', ['smoke'], true)
shouldIt('smoke', ['slow'], false)
})
it('handles invert tag', () => {
// should not run - we are excluding the "slow"
shouldIt('smoke+-slow', ['smoke', 'slow'], false)
shouldIt('mobile+-slow', ['smoke', 'slow'], false)
shouldIt('smoke -slow', ['smoke', 'fast'], true)
shouldIt('-slow', ['smoke', 'slow'], false)
shouldIt('-slow', ['smoke'], true)
// no tags in the test
shouldIt('-slow', [], true)
})
})
context('shouldTestRun', () => {
// a little utility function to parse the given grep string
// and apply the first argument in shouldTestRun
const checkName = (grep, grepTags) => {
const parsed = parseGrep(grep, grepTags)
expect(parsed).to.be.an('object')
return (testName, testTags = []) => {
expect(testName, 'test title').to.be.a('string')
expect(testTags, 'test tags').to.be.an('array')
return shouldTestRun(parsed, testName, testTags)
}
}
it('simple tag', () => {
const parsed = parseGrep('@tag1')
expect(shouldTestRun(parsed, 'no tag1 here')).to.be.false
expect(shouldTestRun(parsed, 'has @tag1 in the name')).to.be.true
})
it('with invert title', () => {
const t = checkName('-hello')
expect(t('no greetings')).to.be.true
expect(t('has hello world')).to.be.false
})
it('with invert option', () => {
const t = checkName(null, '-@tag1')
expect(t('no tags here')).to.be.true
expect(t('has tag1', ['@tag1'])).to.be.false
expect(t('has other tags', ['@tag2'])).to.be.true
})
it('with AND option', () => {
const t = checkName('', '@tag1+@tag2')
expect(t('no tag1 here')).to.be.false
expect(t('has only @tag1', ['@tag1'])).to.be.false
expect(t('has only @tag2', ['@tag2'])).to.be.false
expect(t('has both tags', ['@tag1', '@tag2'])).to.be.true
})
it('with OR option', () => {
const t = checkName(null, '@tag1 @tag2')
expect(t('no tag1 here')).to.be.false
expect(t('has only @tag1 in the name', ['@tag1'])).to.be.true
expect(t('has only @tag2 in the name', ['@tag2'])).to.be.true
expect(t('has @tag1 and @tag2 in the name', ['@tag1', '@tag2'])).to.be
.true
})
it('OR with AND option', () => {
const t = checkName(null, '@tag1 @tag2+@tag3')
expect(t('no tag1 here')).to.be.false
expect(t('has only @tag1 in the name', ['@tag1'])).to.be.true
expect(t('has only @tag2 in the name', ['@tag2'])).to.be.false
expect(t('has only @tag2 in the name and also @tag3', ['@tag2', '@tag3']))
.to.be.true
expect(
t('has @tag1 and @tag2 and @tag3 in the name', [
'@tag1',
'@tag2',
'@tag3',
]),
).to.be.true
})
it('Multiple invert strings and a simple one', () => {
const t = checkName('-name;-hey;number')
expect(t('number should only be matches without a n-a-m-e')).to.be.true
expect(t('number can\'t be name')).to.be.false
expect(t('The man needs a name')).to.be.false
expect(t('number hey name')).to.be.false
expect(t('numbers hey name')).to.be.false
expect(t('number hsey nsame')).to.be.true
expect(t('This wont match')).to.be.false
})
it('Only inverted strings', () => {
const t = checkName('-name;-hey')
expect(t('I\'m matched')).to.be.true
expect(t('hey! I\'m not')).to.be.false
expect(t('My name is weird')).to.be.false
})
})
context('parseFullTitleGrep', () => {
const shouldIt = (search, testName, expected) => {
const parsed = parseFullTitleGrep(search)
expect(
shouldTestRunTitle(parsed, testName),
`"${search}" against title "${testName}"`,
).to.equal(expected)
}
it('passes for substring', () => {
shouldIt('hello w', 'hello world', true)
shouldIt('-hello w', 'hello world', false)
})
})
})