@@ -45,7 +45,17 @@ describe('create_routes', () => {
45
45
46
46
it ( 'sorts routes correctly' , ( ) => {
47
47
const routes = create_routes ( {
48
- files : [ 'index.html' , 'about.html' , 'post/f[xx].html' , '[wildcard].html' , 'post/foo.html' , 'post/[id].html' , 'post/bar.html' , 'post/[id].json.js' ]
48
+ files : [
49
+ 'index.html' ,
50
+ 'about.html' ,
51
+ 'post/f[xx].html' ,
52
+ '[wildcard].html' ,
53
+ 'post/foo.html' ,
54
+ 'post/[id].html' ,
55
+ 'post/bar.html' ,
56
+ 'post/[id].json.js' ,
57
+ 'post/[id([0-9-a-z]{3,})].html' ,
58
+ ]
49
59
} ) ;
50
60
51
61
assert . deepEqual (
@@ -56,13 +66,33 @@ describe('create_routes', () => {
56
66
'post/bar.html' ,
57
67
'post/foo.html' ,
58
68
'post/f[xx].html' ,
69
+ 'post/[id([0-9-a-z]{3,})].html' , // RegExp is more specific
59
70
'post/[id].json.js' ,
60
71
'post/[id].html' ,
61
72
'[wildcard].html'
62
73
]
63
74
) ;
64
75
} ) ;
65
76
77
+ it ( 'distinguishes and sorts regexp routes correctly' , ( ) => {
78
+ const routes = create_routes ( {
79
+ files : [
80
+ '[slug].html' ,
81
+ '[slug([a-z]{2})].html' ,
82
+ '[slug([0-9-a-z]{3,})].html' ,
83
+ ]
84
+ } ) ;
85
+
86
+ assert . deepEqual (
87
+ routes . map ( r => r . handlers [ 0 ] . file ) ,
88
+ [
89
+ '[slug([0-9-a-z]{3,})].html' ,
90
+ '[slug([a-z]{2})].html' ,
91
+ '[slug].html' ,
92
+ ]
93
+ ) ;
94
+ } ) ;
95
+
66
96
it ( 'prefers index page to nested route' , ( ) => {
67
97
let routes = create_routes ( {
68
98
files : [
@@ -131,6 +161,24 @@ describe('create_routes', () => {
131
161
'api/blog/[slug].js' ,
132
162
]
133
163
) ;
164
+
165
+ // RegExp routes
166
+ routes = create_routes ( {
167
+ files : [
168
+ 'blog/[slug].html' ,
169
+ 'blog/index.html' ,
170
+ 'blog/[slug([^0-9]+)].html' ,
171
+ ]
172
+ } ) ;
173
+
174
+ assert . deepEqual (
175
+ routes . map ( r => r . handlers [ 0 ] . file ) ,
176
+ [
177
+ 'blog/index.html' ,
178
+ 'blog/[slug([^0-9]+)].html' ,
179
+ 'blog/[slug].html' ,
180
+ ]
181
+ ) ;
134
182
} ) ;
135
183
136
184
it ( 'generates params' , ( ) => {
@@ -204,8 +252,15 @@ describe('create_routes', () => {
204
252
files : [ '[foo].html' , '[bar]/index.html' ]
205
253
} ) ;
206
254
} , / T h e \[ f o o \] a n d \[ b a r \] \/ i n d e x r o u t e s c l a s h / ) ;
255
+
256
+ assert . throws ( ( ) => {
257
+ create_routes ( {
258
+ files : [ '[foo([0-9-a-z]+)].html' , '[bar([0-9-a-z]+)]/index.html' ]
259
+ } ) ;
260
+ } , / T h e \[ f o o \( \[ 0 - 9 - a - z \] \+ \) \] a n d \[ b a r \( \[ 0 - 9 - a - z \] \+ \) \] \/ i n d e x r o u t e s c l a s h / ) ;
207
261
} ) ;
208
262
263
+
209
264
it ( 'matches nested routes' , ( ) => {
210
265
const route = create_routes ( {
211
266
files : [ 'settings/[submenu].html' ]
@@ -281,6 +336,14 @@ describe('create_routes', () => {
281
336
} , / I n v a l i d r o u t e \[ f o o \] \[ b a r \] \. j s — p a r a m e t e r s m u s t b e s e p a r a t e d / ) ;
282
337
} ) ;
283
338
339
+ it ( 'errors when trying to use reserved characters in route regexp' , ( ) => {
340
+ assert . throws ( ( ) => {
341
+ create_routes ( {
342
+ files : [ '[lang([a-z]{2}(?:-[a-z]{2,4})?)]' ]
343
+ } ) ;
344
+ } , / S a p p e r d o e s n o t a l l o w \( , \) , \? o r \: i n R e g E x p r o u t e s y e t / ) ;
345
+ } ) ;
346
+
284
347
it ( 'errors on 4xx.html' , ( ) => {
285
348
assert . throws ( ( ) => {
286
349
create_routes ( {
0 commit comments