-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathnamed-group-regexp.js
More file actions
57 lines (47 loc) · 1.87 KB
/
named-group-regexp.js
File metadata and controls
57 lines (47 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
'use strict';
const namedGroupRE = require( '../../../../lib/util/named-group-regexp' ).namedGroupRE;
describe( 'named PCRE group RegExp', () => {
it( 'is a regular expression', () => {
expect( namedGroupRE ).toBeInstanceOf( RegExp );
} );
it( 'will not match an arbitrary string', () => {
const pathComponent = 'author';
const result = pathComponent.match( namedGroupRE );
expect( result ).toBeNull();
} );
it( 'identifies the name and RE pattern for a PCRE named group', () => {
const pathComponent = '(?P<parent>[\\d]+)';
const result = pathComponent.match( namedGroupRE );
expect( result ).not.toBeNull();
expect( result[ 1 ] ).toBe( 'parent' );
expect( result[ 2 ] ).toBe( '[\\d]+' );
} );
it( 'identifies the name and RE pattern for another group', () => {
const pathComponent = '(?P<id>\\d+)';
const result = pathComponent.match( namedGroupRE );
expect( result ).not.toBeNull();
expect( result[ 1 ] ).toBe( 'id' );
expect( result[ 2 ] ).toBe( '\\d+' );
} );
it( 'identifies RE patterns including forward slashes', () => {
const pathComponent = '(?P<plugin>[a-z\\/\\.\\-_]+)';
const result = pathComponent.match( namedGroupRE );
expect( result ).not.toBeNull();
expect( result[ 1 ] ).toBe( 'plugin' );
expect( result[ 2 ] ).toBe( '[a-z\\/\\.\\-_]+' );
} );
it( 'will match an empty string if a "RE Pattern" if the pattern is omitted', () => {
const pathComponent = '(?P<id>)';
const result = pathComponent.match( namedGroupRE );
expect( result ).not.toBeNull();
expect( result[ 1 ] ).toBe( 'id' );
expect( result[ 2 ] ).toBe( '' );
} );
it( 'correctly handles WP 5.5 plugins routes', () => {
const path = '(?P<plugin>[^.\\/]+(?:\\/[^.\\/]+)?)';
const result = path.match( namedGroupRE );
expect( result ).not.toBeNull();
expect( result[ 1 ] ).toBe( 'plugin' );
expect( result[ 2 ] ).toBe( '[^.\\/]+(?:\\/[^.\\/]+)?' );
} );
} );