@@ -11,6 +11,7 @@ import {
11
11
char ,
12
12
charIn ,
13
13
charRange ,
14
+ control ,
14
15
digit ,
15
16
exactly ,
16
17
group ,
@@ -211,6 +212,52 @@ describe('unicode', () => {
211
212
} ) ;
212
213
} ) ;
213
214
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
+
214
261
describe ( 'not' , ( ) => {
215
262
it ( 'is chainable' , ( ) => {
216
263
expect ( not . whitespace . exactly `foo` . toString ( ) ) . toBe ( '\\Sfoo' ) ;
@@ -954,6 +1001,7 @@ describe('match', () => {
954
1001
expect ( match ( exactly `foo` ) . toString ( ) ) . toBe ( 'foo' ) ;
955
1002
expect ( match ( exactly `foo` , exactly `bar` ) . toString ( ) ) . toBe ( 'foobar' ) ;
956
1003
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?)+' ) ;
957
1005
} ) ;
958
1006
it ( 'is chainable' , ( ) => {
959
1007
expect ( match ( exactly `foo` ) . char . toString ( ) ) . toBe ( 'foo.' ) ;
0 commit comments