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