Skip to content

Commit 2466173

Browse files
committed
refactor: add more tests
1 parent 55d7a73 commit 2466173

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/constructs/__tests__/character-class.test.ts

+21
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,24 @@ test('`encodeCharacterClass` throws on empty text', () => {
169169
`"Character class should contain at least one character or character range"`,
170170
);
171171
});
172+
173+
test('showcase: negated character classes', () => {
174+
expect(notDigit).toEqualRegex(/\D/);
175+
expect(notWord).toEqualRegex(/\W/);
176+
expect(notWhitespace).toEqualRegex(/\S/);
177+
178+
expect(notDigit).toMatchString('A');
179+
expect(notDigit).not.toMatchString('1');
180+
expect(notDigit).toMatchString(' ');
181+
expect(notDigit).toMatchString('#');
182+
183+
expect(notWord).not.toMatchString('A');
184+
expect(notWord).not.toMatchString('1');
185+
expect(notWord).toMatchString(' ');
186+
expect(notWord).toMatchString('#');
187+
188+
expect(notWhitespace).toMatchString('A');
189+
expect(notWhitespace).toMatchString('1');
190+
expect(notWhitespace).not.toMatchString(' ');
191+
expect(notWhitespace).toMatchString('#');
192+
});

src/constructs/__tests__/quantifiers.test.tsx

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { digit } from '../character-class';
1+
import { buildRegExp } from '../../builders';
2+
import { any, digit } from '../character-class';
23
import { oneOrMore, optional, zeroOrMore } from '../quantifiers';
34

45
test('`oneOrMore` quantifier', () => {
@@ -59,3 +60,13 @@ test('non-greedy quantifiers', () => {
5960
expect(zeroOrMore('a', { greedy: false })).toEqualRegex(/a*?/);
6061
expect(zeroOrMore('ab', { greedy: false })).toEqualRegex(/(?:ab)*?/);
6162
});
63+
64+
test('showcase: greedy vs non-greedy quantifiers', () => {
65+
const html = '<div>Hello <em>World!</em></div>';
66+
67+
const greedyTag = buildRegExp(['<', oneOrMore(any), '>'], { global: true });
68+
expect(greedyTag).toMatchGroups(html, ['<div>Hello <em>World!</em></div>']);
69+
70+
const nonGreedyTag = buildRegExp(['<', oneOrMore(any, { greedy: false }), '>'], { global: true });
71+
expect(nonGreedyTag).toMatchGroups(html, ['<div>', '<em>', '</em>', '</div>']);
72+
});

0 commit comments

Comments
 (0)