1
- import { describe , it , expect } from "vitest" ;
1
+ import { describe , it , expect , TestOptions } from "vitest" ;
2
2
import * as pathToRegexp from "./index" ;
3
3
4
4
interface ParserTestSet {
5
5
path : string ;
6
6
options ?: pathToRegexp . ParseOptions ;
7
7
expected : pathToRegexp . Token [ ] ;
8
+ testOptions ?: TestOptions ;
8
9
}
9
10
10
11
interface CompileTestSet {
11
12
path : string ;
12
13
options ?: pathToRegexp . CompileOptions ;
14
+ testOptions ?: TestOptions ;
13
15
tests : Array < {
14
16
input : pathToRegexp . ParamData | undefined ;
15
17
expected : string | null ;
@@ -19,6 +21,7 @@ interface CompileTestSet {
19
21
interface MatchTestSet {
20
22
path : pathToRegexp . Path ;
21
23
options ?: pathToRegexp . MatchOptions ;
24
+ testOptions ?: TestOptions ;
22
25
tests : Array < {
23
26
input : string ;
24
27
matches : ( string | undefined ) [ ] | null ;
@@ -2919,6 +2922,92 @@ const MATCH_TESTS: MatchTestSet[] = [
2919
2922
} ,
2920
2923
] ,
2921
2924
} ,
2925
+
2926
+ /**
2927
+ * https://github.com/pillarjs/path-to-regexp/pull/270
2928
+ */
2929
+ {
2930
+ path : "/files/:path*.:ext*" ,
2931
+ tests : [
2932
+ {
2933
+ input : "/files/hello/world.txt" ,
2934
+ matches : [ "/files/hello/world.txt" , "hello/world" , "txt" ] ,
2935
+ expected : {
2936
+ path : "/files/hello/world.txt" ,
2937
+ index : 0 ,
2938
+ params : { path : [ "hello" , "world" ] , ext : [ "txt" ] } ,
2939
+ } ,
2940
+ } ,
2941
+ {
2942
+ input : "/files/my/photo.jpg/gif" ,
2943
+ matches : [ "/files/my/photo.jpg/gif" , "my/photo.jpg/gif" , undefined ] ,
2944
+ expected : {
2945
+ path : "/files/my/photo.jpg/gif" ,
2946
+ index : 0 ,
2947
+ params : { path : [ "my" , "photo.jpg" , "gif" ] , ext : undefined } ,
2948
+ } ,
2949
+ } ,
2950
+ ] ,
2951
+ } ,
2952
+ {
2953
+ path : "#/*" ,
2954
+ tests : [
2955
+ {
2956
+ input : "#/" ,
2957
+ matches : [ "#/" , undefined ] ,
2958
+ expected : { path : "#/" , index : 0 , params : { } } ,
2959
+ } ,
2960
+ ] ,
2961
+ } ,
2962
+ {
2963
+ path : "/foo/:bar*" ,
2964
+ tests : [
2965
+ {
2966
+ input : "/foo/test1//test2" ,
2967
+ matches : [ "/foo/test1//test2" , "test1//test2" ] ,
2968
+ expected : {
2969
+ path : "/foo/test1//test2" ,
2970
+ index : 0 ,
2971
+ params : { bar : [ "test1" , "test2" ] } ,
2972
+ } ,
2973
+ } ,
2974
+ ] ,
2975
+ } ,
2976
+ {
2977
+ path : "/entity/:id/*" ,
2978
+ tests : [
2979
+ {
2980
+ input : "/entity/foo" ,
2981
+ matches : [ "/entity/foo" , "foo" , undefined ] ,
2982
+ expected : { path : "/entity/foo" , index : 0 , params : { id : "foo" } } ,
2983
+ } ,
2984
+ {
2985
+ input : "/entity/foo/" ,
2986
+ matches : [ "/entity/foo/" , "foo" , undefined ] ,
2987
+ expected : { path : "/entity/foo/" , index : 0 , params : { id : "foo" } } ,
2988
+ } ,
2989
+ ] ,
2990
+ } ,
2991
+ {
2992
+ path : "/test/*" ,
2993
+ tests : [
2994
+ {
2995
+ input : "/test" ,
2996
+ matches : [ "/test" , undefined ] ,
2997
+ expected : { path : "/test" , index : 0 , params : { } } ,
2998
+ } ,
2999
+ {
3000
+ input : "/test/" ,
3001
+ matches : [ "/test/" , undefined ] ,
3002
+ expected : { path : "/test/" , index : 0 , params : { } } ,
3003
+ } ,
3004
+ {
3005
+ input : "/test/route" ,
3006
+ matches : [ "/test/route" , "route" ] ,
3007
+ expected : { path : "/test/route" , index : 0 , params : { "0" : [ "route" ] } } ,
3008
+ } ,
3009
+ ] ,
3010
+ } ,
2922
3011
] ;
2923
3012
2924
3013
/**
@@ -3003,8 +3092,8 @@ describe("path-to-regexp", () => {
3003
3092
3004
3093
describe . each ( PARSER_TESTS ) (
3005
3094
"parse $path with $options" ,
3006
- ( { path, options, expected } ) => {
3007
- it ( "should parse the path" , ( ) => {
3095
+ ( { path, options, expected, testOptions } ) => {
3096
+ it ( "should parse the path" , testOptions , ( ) => {
3008
3097
const data = pathToRegexp . parse ( path , options ) ;
3009
3098
expect ( data . tokens ) . toEqual ( expected ) ;
3010
3099
} ) ;
@@ -3013,32 +3102,38 @@ describe("path-to-regexp", () => {
3013
3102
3014
3103
describe . each ( COMPILE_TESTS ) (
3015
3104
"compile $path with $options" ,
3016
- ( { path, options, tests } ) => {
3105
+ ( { path, options, tests, testOptions = { } } ) => {
3017
3106
const toPath = pathToRegexp . compile ( path , options ) ;
3018
3107
3019
- it . each ( tests ) ( "should compile $input" , ( { input, expected } ) => {
3020
- if ( expected === null ) {
3021
- expect ( ( ) => {
3022
- toPath ( input ) ;
3023
- } ) . toThrow ( ) ;
3024
- } else {
3025
- expect ( toPath ( input ) ) . toEqual ( expected ) ;
3026
- }
3027
- } ) ;
3108
+ it . each ( tests ) (
3109
+ "should compile $input" ,
3110
+ testOptions ,
3111
+ ( { input, expected } ) => {
3112
+ if ( expected === null ) {
3113
+ expect ( ( ) => toPath ( input ) ) . toThrow ( ) ;
3114
+ } else {
3115
+ expect ( toPath ( input ) ) . toEqual ( expected ) ;
3116
+ }
3117
+ } ,
3118
+ ) ;
3028
3119
} ,
3029
3120
) ;
3030
3121
3031
3122
describe . each ( MATCH_TESTS ) (
3032
3123
"match $path with $options" ,
3033
- ( { path, options, tests } ) => {
3124
+ ( { path, options, tests, testOptions = { } } ) => {
3034
3125
const keys : pathToRegexp . Key [ ] = [ ] ;
3035
3126
const re = pathToRegexp . pathToRegexp ( path , keys , options ) ;
3036
3127
const match = pathToRegexp . match ( path , options ) ;
3037
3128
3038
- it . each ( tests ) ( "should match $input" , ( { input, matches, expected } ) => {
3039
- expect ( exec ( re , input ) ) . toEqual ( matches ) ;
3040
- expect ( match ( input ) ) . toEqual ( expected ) ;
3041
- } ) ;
3129
+ it . each ( tests ) (
3130
+ "should match $input" ,
3131
+ testOptions ,
3132
+ ( { input, matches, expected } ) => {
3133
+ expect ( exec ( re , input ) ) . toEqual ( matches ) ;
3134
+ expect ( match ( input ) ) . toEqual ( expected ) ;
3135
+ } ,
3136
+ ) ;
3042
3137
} ,
3043
3138
) ;
3044
3139
0 commit comments