@@ -44,3 +44,94 @@ assert.throws(() => pattern.test(1), {
4444assert . throws ( ( ) => pattern . test ( '' , 1 ) , {
4545 code : 'ERR_INVALID_ARG_TYPE' ,
4646} ) ;
47+
48+ // Per WebIDL, undefined/null for optional arguments with defaults should use
49+ // the default value (empty URLPatternInit {}) rather than throwing.
50+ // For a union type (USVString or URLPatternInit), null resolves to the
51+ // dictionary branch, yielding an empty URLPatternInit.
52+
53+ // Constructor: undefined input should be treated as empty init.
54+ {
55+ const p = new URLPattern ( undefined ) ;
56+ assert . strictEqual ( p . protocol , '*' ) ;
57+ assert . strictEqual ( p . hostname , '*' ) ;
58+ }
59+
60+ // Constructor: null input should be treated as empty init.
61+ {
62+ const p = new URLPattern ( null ) ;
63+ assert . strictEqual ( p . protocol , '*' ) ;
64+ assert . strictEqual ( p . hostname , '*' ) ;
65+ }
66+
67+ // Constructor: undefined input with undefined baseURL.
68+ {
69+ const p = new URLPattern ( undefined , undefined ) ;
70+ assert . strictEqual ( p . protocol , '*' ) ;
71+ assert . strictEqual ( p . pathname , '*' ) ;
72+ }
73+
74+ // Constructor: null input with null baseURL.
75+ {
76+ const p = new URLPattern ( null , null ) ;
77+ assert . strictEqual ( p . protocol , '*' ) ;
78+ assert . strictEqual ( p . pathname , '*' ) ;
79+ }
80+
81+ // Constructor: valid input with undefined options.
82+ {
83+ const p = new URLPattern ( { pathname : '/foo' } , undefined ) ;
84+ assert . strictEqual ( p . pathname , '/foo' ) ;
85+ }
86+
87+ // Constructor: valid input with null options.
88+ {
89+ const p = new URLPattern ( { pathname : '/foo' } , null ) ;
90+ assert . strictEqual ( p . pathname , '/foo' ) ;
91+ }
92+
93+ // Constructor: string input, undefined baseURL, undefined options.
94+ {
95+ const p = new URLPattern ( 'https://example.com/foo' , undefined , undefined ) ;
96+ assert . strictEqual ( p . hostname , 'example.com' ) ;
97+ assert . strictEqual ( p . pathname , '/foo' ) ;
98+ }
99+
100+ // Constructor: string input, null baseURL, null options.
101+ {
102+ const p = new URLPattern ( 'https://example.com/foo' , null , null ) ;
103+ assert . strictEqual ( p . hostname , 'example.com' ) ;
104+ assert . strictEqual ( p . pathname , '/foo' ) ;
105+ }
106+
107+ // exec() and test(): undefined input should be treated as empty init.
108+ {
109+ const p = new URLPattern ( ) ;
110+ assert . strictEqual ( p . test ( undefined ) , true ) ;
111+ assert . strictEqual ( p . test ( undefined , undefined ) , true ) ;
112+ assert . notStrictEqual ( p . exec ( undefined ) , null ) ;
113+ assert . notStrictEqual ( p . exec ( undefined , undefined ) , null ) ;
114+ }
115+
116+ // exec() and test(): null input should be treated as empty init.
117+ {
118+ const p = new URLPattern ( ) ;
119+ assert . strictEqual ( p . test ( null ) , true ) ;
120+ assert . strictEqual ( p . test ( null , null ) , true ) ;
121+ assert . notStrictEqual ( p . exec ( null ) , null ) ;
122+ assert . notStrictEqual ( p . exec ( null , null ) , null ) ;
123+ }
124+
125+ // exec() and test(): valid input with undefined baseURL.
126+ {
127+ const p = new URLPattern ( { protocol : 'https' } ) ;
128+ assert . strictEqual ( p . test ( 'https://example.com' , undefined ) , true ) ;
129+ assert . notStrictEqual ( p . exec ( 'https://example.com' , undefined ) , null ) ;
130+ }
131+
132+ // exec() and test(): valid input with null baseURL.
133+ {
134+ const p = new URLPattern ( { protocol : 'https' } ) ;
135+ assert . strictEqual ( p . test ( 'https://example.com' , null ) , true ) ;
136+ assert . notStrictEqual ( p . exec ( 'https://example.com' , null ) , null ) ;
137+ }
0 commit comments