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