Skip to content

Commit bd20642

Browse files
Enhance existing test assertions
Centralize list of supported characters.
1 parent 0a7fd54 commit bd20642

File tree

3 files changed

+42
-17
lines changed

3 files changed

+42
-17
lines changed

src/constants/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const whitespace = ` \t\r\n`.split('');
2+
3+
const digits = '0123456789'.split('');
4+
5+
const basicLowercase = 'abcdefghijklmnopqrstuvwxyz'.split('');
6+
const basicUppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
7+
const basicSpecial = '~`!@#$%^&*()-_=+<,>.?/[]{}|\\:;"\''.split('');
8+
9+
const all = ([] as string[]).concat(
10+
whitespace,
11+
digits,
12+
basicLowercase,
13+
basicUppercase,
14+
basicSpecial
15+
);
16+
17+
export const Chars = { all };

src/expanders/character-class-pattern.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { CharacterClass } from 'regexp-tree/ast';
2+
import { Chars } from '../constants';
23
import Expander from '../Expander';
34
import Expansion from '../Expansion';
45
import sortRandom from '../sorts/fisher-yates-random';
@@ -18,16 +19,7 @@ function getReferencedCodePoints(
1819
return [expression.codePoint];
1920
}
2021

21-
const allCharOptions =
22-
' \t\r\n' +
23-
'abcdefghijklmnopqrstuvwxyz' +
24-
'ABCDEFGHIJKLMNOPQRSTUVWXYZ' +
25-
'0123456789' +
26-
'~`!@#$%^&*()-_=+<,>.?/[]{}|\\:;"\'';
27-
28-
const allCodePointOptions = allCharOptions
29-
.split('')
30-
.map(char => char.charCodeAt(0));
22+
const allCodePointOptions = Chars.all.map(char => char.charCodeAt(0));
3123

3224
/**
3325
* Expand an expression which represents a single character from a

src/pattern.spec.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { when } from 'jest-when';
2+
import { Chars } from './constants';
23
import Expansion from './Expansion';
34
import { fill } from './helpers/utils';
45
import * as patternLib from './pattern';
@@ -363,7 +364,7 @@ describe('expand', () => {
363364
}
364365

365366
const result = expandAll('[^abc]');
366-
expect(result.length).toBeGreaterThan(1);
367+
expect(result).toHaveLength(95);
367368
result.forEach(testExpansion);
368369
});
369370

@@ -374,7 +375,7 @@ describe('expand', () => {
374375
}
375376

376377
const result = expandAll('[^246]');
377-
expect(result.length).toBeGreaterThan(1);
378+
expect(result).toHaveLength(95);
378379
result.forEach(testExpansion);
379380
});
380381

@@ -385,7 +386,7 @@ describe('expand', () => {
385386
}
386387

387388
const result = expandAll('[^a-p]');
388-
expect(result.length).toBeGreaterThan(1);
389+
expect(result).toHaveLength(82);
389390
result.forEach(testExpansion);
390391
});
391392

@@ -396,7 +397,7 @@ describe('expand', () => {
396397
}
397398

398399
const result = expandAll('[^0-8]');
399-
expect(result.length).toBeGreaterThan(1);
400+
expect(result).toHaveLength(89);
400401
result.forEach(testExpansion);
401402
});
402403

@@ -407,7 +408,7 @@ describe('expand', () => {
407408
}
408409

409410
const result = expandAll('[^aeiou0-5A-T]');
410-
expect(result.length).toBeGreaterThan(1);
411+
expect(result).toHaveLength(67);
411412
result.forEach(testExpansion);
412413
});
413414

@@ -438,10 +439,14 @@ describe('expand', () => {
438439
it.each([/./, /\w/, /\W/, /\d/, /\D/, /\s/, /\S/])(
439440
'expands the single character class %p',
440441
(charClass: RegExp) => {
442+
function testExpansion(expansion: string) {
443+
expect(expansion).toHaveLength(1);
444+
expect(expansion).toMatch(charClass);
445+
}
446+
441447
const result = expandAll(charClass);
442448
expect(result.length).toBeGreaterThan(1);
443-
expect(result[0]).toHaveLength(1);
444-
expect(result[0]).toMatch(charClass);
449+
result.forEach(testExpansion);
445450
}
446451
);
447452

@@ -562,6 +567,17 @@ describe('expand', () => {
562567
}
563568
);
564569

570+
it.each([/(.|\r)/s, /[\s\S]/])(
571+
'includes all supported characters in %p',
572+
regex => {
573+
const result = expandAll(regex);
574+
575+
Chars.all.forEach(char => {
576+
expect(result).toContain(char);
577+
});
578+
}
579+
);
580+
565581
it('expands repeated character class', () => {
566582
const allTwoDigitNumbers = fill(0, 99).map(num =>
567583
num.toString().padStart(2, '0')

0 commit comments

Comments
 (0)