1- import { capture , oneOrMore } from '../..' ;
1+ import {
2+ any ,
3+ anyOf ,
4+ buildRegExp ,
5+ capture ,
6+ digit ,
7+ inverted ,
8+ oneOrMore ,
9+ ref ,
10+ word ,
11+ wordBoundary ,
12+ } from '../..' ;
213
314test ( '`capture` pattern' , ( ) => {
415 expect ( capture ( 'a' ) ) . toEqualRegex ( / ( a ) / ) ;
@@ -12,3 +23,96 @@ test('`capture` matching', () => {
1223 expect ( [ 'a' , capture ( 'b' ) ] ) . toMatchGroups ( 'ab' , [ 'ab' , 'b' ] ) ;
1324 expect ( [ 'a' , capture ( 'b' ) , capture ( 'c' ) ] ) . toMatchGroups ( 'abc' , [ 'abc' , 'b' , 'c' ] ) ;
1425} ) ;
26+
27+ test ( 'named `capture` pattern' , ( ) => {
28+ expect ( capture ( 'a' , { as : 'xyz' } ) ) . toEqualRegex ( '(?<xyz>a)' ) ;
29+ expect ( capture ( 'abc' , { as : 'xyz' } ) ) . toEqualRegex ( '(?<xyz>abc)' ) ;
30+ expect ( capture ( oneOrMore ( 'abc' ) , { as : 'xyz' } ) ) . toEqualRegex ( '(?<xyz>(?:abc)+)' ) ;
31+ expect ( oneOrMore ( capture ( 'abc' , { as : 'xyz' } ) ) ) . toEqualRegex ( '(?<xyz>abc)+' ) ;
32+ } ) ;
33+
34+ test ( 'named `capture` matching' , ( ) => {
35+ expect ( capture ( 'b' , { as : 'x1' } ) ) . toMatchGroups ( 'ab' , [ 'b' , 'b' ] ) ;
36+ expect ( capture ( 'b' , { as : 'x1' } ) ) . toMatchNamedGroups ( 'ab' , { x1 : 'b' } ) ;
37+
38+ expect ( [ 'a' , capture ( 'b' , { as : 'x1' } ) ] ) . toMatchGroups ( 'ab' , [ 'ab' , 'b' ] ) ;
39+ expect ( [ 'a' , capture ( 'b' , { as : 'x1' } ) ] ) . toMatchNamedGroups ( 'ab' , { x1 : 'b' } ) ;
40+
41+ expect ( [ capture ( 'a' ) , capture ( 'b' , { as : 'x1' } ) , capture ( 'c' , { as : 'x2' } ) ] ) . toMatchGroups (
42+ 'abc' ,
43+ [ 'abc' , 'a' , 'b' , 'c' ] ,
44+ ) ;
45+ expect ( [ capture ( 'a' ) , capture ( 'b' , { as : 'x1' } ) , capture ( 'c' , { as : 'x2' } ) ] ) . toMatchNamedGroups (
46+ 'abc' ,
47+ { x1 : 'b' , x2 : 'c' } ,
48+ ) ;
49+ } ) ;
50+
51+ // Should have `ref0` as name.
52+ const firstRef = ref ( ) ;
53+
54+ test ( '`reference` pattern' , ( ) => {
55+ expect ( [ firstRef ] ) . toEqualRegex ( / \k<ref0 > / ) ;
56+ expect ( [ ref ( 'xyz' ) ] ) . toEqualRegex ( / \k<xyz > / ) ;
57+ expect ( [ capture ( any , { as : firstRef } ) , ' ' , firstRef ] ) . toEqualRegex ( '(?<ref0>.) \\k<ref0>' ) ;
58+
59+ const otherRef = ref ( 'r123' ) ;
60+ expect ( [ 'xx' , capture ( any , { as : otherRef } ) , ' ' , otherRef , 'xx' ] ) . toEqualRegex (
61+ 'xx(?<r123>.) \\k<r123>xx' ,
62+ ) ;
63+ } ) ;
64+
65+ test ( '`reference` matching basic case' , ( ) => {
66+ const someRef = ref ( ) ;
67+ expect ( [ capture ( word , { as : someRef } ) , someRef ] ) . toMatchString ( 'aa' ) ;
68+ expect ( [ capture ( digit , { as : someRef } ) , someRef ] ) . toMatchString ( '11' ) ;
69+
70+ expect ( [ capture ( any , { as : someRef } ) , someRef ] ) . not . toMatchString ( 'ab' ) ;
71+
72+ expect ( [ capture ( digit , { as : someRef } ) , someRef ] ) . not . toMatchString ( '1a' ) ;
73+ expect ( [ capture ( digit , { as : someRef } ) , someRef ] ) . not . toMatchString ( 'a1' ) ;
74+ } ) ;
75+
76+ test ( '`reference` matching HTML attributes' , ( ) => {
77+ const quoteRef = ref ( 'quote' ) ;
78+ const quote = anyOf ( '"\'' ) ;
79+ const htmlAttributeRegex = buildRegExp ( [
80+ wordBoundary ,
81+ capture ( oneOrMore ( word ) , { as : 'name' } ) ,
82+ '=' ,
83+ capture ( quote , { as : quoteRef } ) ,
84+ capture ( oneOrMore ( inverted ( quote ) ) , { as : 'value' } ) ,
85+ quoteRef ,
86+ ] ) ;
87+
88+ expect ( htmlAttributeRegex ) . toMatchNamedGroups ( 'a="b"' , {
89+ name : 'a' ,
90+ quote : '"' ,
91+ value : 'b' ,
92+ } ) ;
93+ expect ( htmlAttributeRegex ) . toMatchNamedGroups ( 'aa="bbb"' , {
94+ name : 'aa' ,
95+ quote : '"' ,
96+ value : 'bbb' ,
97+ } ) ;
98+ expect ( htmlAttributeRegex ) . toMatchNamedGroups ( `aa='bbb'` , {
99+ name : 'aa' ,
100+ quote : `'` ,
101+ value : 'bbb' ,
102+ } ) ;
103+ expect ( htmlAttributeRegex ) . toMatchNamedGroups ( '<input type="number" />' , {
104+ quote : '"' ,
105+ name : 'type' ,
106+ value : 'number' ,
107+ } ) ;
108+ expect ( htmlAttributeRegex ) . toMatchNamedGroups ( `<input type='number' />` , {
109+ quote : "'" ,
110+ name : 'type' ,
111+ value : 'number' ,
112+ } ) ;
113+
114+ expect ( htmlAttributeRegex ) . not . toMatchString ( `aa="bbb'` ) ;
115+ expect ( htmlAttributeRegex ) . not . toMatchString ( `aa='bbb"` ) ;
116+ expect ( htmlAttributeRegex ) . not . toMatchString ( `<input type='number" />` ) ;
117+ expect ( htmlAttributeRegex ) . not . toMatchString ( `<input type="number' />` ) ;
118+ } ) ;
0 commit comments