Skip to content

Commit 440912a

Browse files
committed
Add tests for control
1 parent 8d1535c commit 440912a

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

test/functions.test.ts

+48
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
char,
1212
charIn,
1313
charRange,
14+
control,
1415
digit,
1516
exactly,
1617
group,
@@ -211,6 +212,52 @@ describe('unicode', () => {
211212
});
212213
});
213214

215+
describe('control', () => {
216+
it('accepts literals', () => {
217+
expect(control`e`.toString()).toBe('\\ce');
218+
expect(control('e').toString()).toBe('\\ce');
219+
expect(control`Z`.toString()).toBe('\\cZ');
220+
expect(control('Z').toString()).toBe('\\cZ');
221+
});
222+
it('accepts template literals', () => {
223+
expect(control`${'a'}`.toString()).toBe('\\ca');
224+
expect(control`${'A'}`.toString()).toBe('\\cA');
225+
});
226+
it('validates correctly', () => {
227+
expect(control`a`.toString()).toBe('\\ca');
228+
expect(control`A`.toString()).toBe('\\cA');
229+
expect(control`z`.toString()).toBe('\\cz');
230+
expect(control`Z`.toString()).toBe('\\cZ');
231+
expect(() => control``).toThrow();
232+
expect(() => control`abc`).toThrow();
233+
expect(() => control`1`).toThrow();
234+
expect(() => control` `).toThrow();
235+
});
236+
it('can be negated and quantified', () => {
237+
expect(not.control`a`.toString()).toBe('[^\\ca]');
238+
expect(oneOrMore.control`a`.toString()).toBe('\\ca+');
239+
expect(oneOrMore.not.control`a`.toString()).toBe('[^\\ca]+');
240+
expect(not.control`z`.toString()).toBe('[^\\cz]');
241+
expect(oneOrMore.control`z`.toString()).toBe('\\cz+');
242+
expect(oneOrMore.not.control`z`.toString()).toBe('[^\\cz]+');
243+
});
244+
it('is recognized as literal', () => {
245+
expect(oneOrMore.control`a`.toString()).toBe('\\ca+');
246+
});
247+
it('throws for invalid argument', () => {
248+
// @ts-expect-error - testing invalid arguments
249+
expect(() => control(1).toString()).toThrow('control character');
250+
// @ts-expect-error - testing invalid arguments
251+
expect(() => control()).toThrow();
252+
// @ts-expect-error - testing invalid arguments
253+
expect(() => control('a', 'b')).toThrow();
254+
// @ts-expect-error - testing missing arguments
255+
expect(() => control.toString()).toThrow('required parameters');
256+
// @ts-expect-error - testing missing arguments
257+
expect(() => control.control`e`.toString()).toThrow();
258+
});
259+
});
260+
214261
describe('not', () => {
215262
it('is chainable', () => {
216263
expect(not.whitespace.exactly`foo`.toString()).toBe('\\Sfoo');
@@ -954,6 +1001,7 @@ describe('match', () => {
9541001
expect(match(exactly`foo`).toString()).toBe('foo');
9551002
expect(match(exactly`foo`, exactly`bar`).toString()).toBe('foobar');
9561003
expect(match(not.behind`foo`, maybe.exactly`bar`, oneOrMore`baz`).toString()).toBe('(?<!foo)(?:bar)?(?:baz)+');
1004+
expect(oneOrMore.match(exactly`fo`.maybe`o`).toString()).toBe('(?:foo?)+');
9571005
});
9581006
it('is chainable', () => {
9591007
expect(match(exactly`foo`).char.toString()).toBe('foo.');

0 commit comments

Comments
 (0)