forked from denoland/rust-urlpattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
850 lines (764 loc) · 24.7 KB
/
lib.rs
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
//! rust-urlpattern is an implementation of the
//! [URLPattern standard](https://wicg.github.io/urlpattern) for the Rust
//! programming language.
//!
//! For a usage example, see the [UrlPattern] documentation.
mod canonicalize_and_process;
mod component;
mod constructor_parser;
mod error;
mod matcher;
mod parser;
pub mod quirks;
mod regexp;
mod tokenizer;
pub use error::Error;
use url::Url;
use crate::canonicalize_and_process::is_special_scheme;
use crate::canonicalize_and_process::special_scheme_default_port;
use crate::component::Component;
use crate::regexp::RegExp;
/// The structured input used to create a URL pattern.
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub struct UrlPatternInit {
pub protocol: Option<String>,
pub username: Option<String>,
pub password: Option<String>,
pub hostname: Option<String>,
pub port: Option<String>,
pub pathname: Option<String>,
pub search: Option<String>,
pub hash: Option<String>,
pub base_url: Option<Url>,
}
impl UrlPatternInit {
pub fn parse_constructor_string<R: RegExp>(
pattern: &str,
base_url: Option<Url>,
) -> Result<UrlPatternInit, Error> {
let mut init = constructor_parser::parse_constructor_string::<R>(pattern)?;
if base_url.is_none() && init.protocol.is_none() {
return Err(Error::BaseUrlRequired);
}
init.base_url = base_url;
Ok(init)
}
// Ref: https://wicg.github.io/urlpattern/#process-a-urlpatterninit
// TODO: use UrlPatternInit for arguments?
#[allow(clippy::too_many_arguments)]
fn process(
&self,
kind: canonicalize_and_process::ProcessType,
protocol: Option<String>,
username: Option<String>,
password: Option<String>,
hostname: Option<String>,
port: Option<String>,
pathname: Option<String>,
search: Option<String>,
hash: Option<String>,
) -> Result<UrlPatternInit, Error> {
let mut result = UrlPatternInit {
protocol,
username,
password,
hostname,
port,
pathname,
search,
hash,
base_url: None,
};
let base_url = if let Some(parsed_base_url) = &self.base_url {
// TODO: check if these are correct
result.protocol = Some(parsed_base_url.scheme().to_string());
result.username = Some(parsed_base_url.username().to_string());
result.password =
Some(parsed_base_url.password().unwrap_or_default().to_string());
result.hostname =
Some(parsed_base_url.host_str().unwrap_or_default().to_string());
result.port = Some(url::quirks::port(parsed_base_url).to_string());
result.pathname =
Some(url::quirks::pathname(parsed_base_url).to_string());
result.search = Some(parsed_base_url.query().unwrap_or("").to_string());
result.hash = Some(parsed_base_url.fragment().unwrap_or("").to_string());
Some(parsed_base_url)
} else {
None
};
if let Some(protocol) = &self.protocol {
result.protocol = Some(canonicalize_and_process::process_protocol_init(
protocol, &kind,
)?);
}
if let Some(username) = &self.username {
result.username = Some(canonicalize_and_process::process_username_init(
username, &kind,
)?);
}
if let Some(password) = &self.password {
result.password = Some(canonicalize_and_process::process_password_init(
password, &kind,
)?);
}
if let Some(hostname) = &self.hostname {
result.hostname = Some(canonicalize_and_process::process_hostname_init(
hostname, &kind,
)?);
}
if let Some(port) = &self.port {
result.port = Some(canonicalize_and_process::process_port_init(
port,
result.protocol.as_deref(),
&kind,
)?);
}
if let Some(pathname) = &self.pathname {
result.pathname = Some(pathname.clone());
if let Some(base_url) = base_url {
if !base_url.cannot_be_a_base()
&& !is_absolute_pathname(pathname, &kind)
{
let baseurl_path = url::quirks::pathname(base_url);
let slash_index = baseurl_path.rfind('/');
if let Some(slash_index) = slash_index {
let new_pathname = baseurl_path[..=slash_index].to_string();
result.pathname =
Some(format!("{}{}", new_pathname, result.pathname.unwrap()));
}
}
}
result.pathname = Some(canonicalize_and_process::process_pathname_init(
&result.pathname.unwrap(),
result.protocol.as_deref(),
&kind,
)?);
}
if let Some(search) = &self.search {
result.search = Some(canonicalize_and_process::process_search_init(
search, &kind,
)?);
}
if let Some(hash) = &self.hash {
result.hash =
Some(canonicalize_and_process::process_hash_init(hash, &kind)?);
}
Ok(result)
}
}
// Ref: https://wicg.github.io/urlpattern/#is-an-absolute-pathname
fn is_absolute_pathname(
input: &str,
kind: &canonicalize_and_process::ProcessType,
) -> bool {
if input.is_empty() {
return false;
}
if input.starts_with('/') {
return true;
}
if kind == &canonicalize_and_process::ProcessType::Url {
return false;
}
// TODO: input code point length
if input.len() < 2 {
return false;
}
input.starts_with("\\/") || input.starts_with("{/")
}
// Ref: https://wicg.github.io/urlpattern/#urlpattern
/// A UrlPattern that can be matched against.
///
/// # Examples
///
/// ```
/// use urlpattern::UrlPattern;
/// use urlpattern::UrlPatternInit;
/// use urlpattern::UrlPatternMatchInput;
///
///# fn main() {
/// // Create the UrlPattern to match against.
/// let init = UrlPatternInit {
/// pathname: Some("/users/:id".to_owned()),
/// ..Default::default()
/// };
/// let pattern = <UrlPattern>::parse(init, false).unwrap();
///
/// // Match the pattern against a URL.
/// let url = "https://example.com/users/123".parse().unwrap();
/// let result = pattern.exec(UrlPatternMatchInput::Url(url)).unwrap().unwrap();
/// assert_eq!(result.pathname.groups.get("id").unwrap(), "123");
///# }
/// ```
#[derive(Debug)]
pub struct UrlPattern<R: RegExp = regex::Regex> {
protocol: Component<R>,
username: Component<R>,
password: Component<R>,
hostname: Component<R>,
port: Component<R>,
pathname: Component<R>,
search: Component<R>,
hash: Component<R>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum UrlPatternMatchInput {
Init(UrlPatternInit),
Url(Url),
}
impl<R: RegExp> UrlPattern<R> {
// Ref: https://wicg.github.io/urlpattern/#dom-urlpattern-urlpattern
/// Parse a [UrlPatternInit] into a [UrlPattern].
pub fn parse(init: UrlPatternInit, ignore_case: bool) -> Result<Self, Error> {
Self::parse_internal(init, true, ignore_case)
}
pub(crate) fn parse_internal(
init: UrlPatternInit,
report_regex_errors: bool,
ignore_case: bool,
) -> Result<Self, Error> {
let mut processed_init = init.process(
canonicalize_and_process::ProcessType::Pattern,
None,
None,
None,
None,
None,
None,
None,
None,
)?;
// If processedInit["protocol"] is a special scheme and processedInit["port"] is its corresponding default port
if let Some(protocol) = &processed_init.protocol {
if is_special_scheme(protocol) {
let default_port = special_scheme_default_port(protocol);
if default_port == processed_init.port.as_deref() {
processed_init.port = Some(String::new())
}
}
}
let protocol = Component::compile(
processed_init.protocol.as_deref(),
canonicalize_and_process::canonicalize_protocol,
parser::Options::default(),
)?
.optionally_transpose_regex_error(report_regex_errors)?;
let hostname_is_ipv6 = processed_init
.hostname
.as_deref()
.map(hostname_pattern_is_ipv6_address)
.unwrap_or(false);
let hostname = if hostname_is_ipv6 {
Component::compile(
processed_init.hostname.as_deref(),
canonicalize_and_process::canonicalize_ipv6_hostname,
parser::Options::hostname(),
)?
.optionally_transpose_regex_error(report_regex_errors)?
} else {
Component::compile(
processed_init.hostname.as_deref(),
canonicalize_and_process::canonicalize_hostname,
parser::Options::hostname(),
)?
.optionally_transpose_regex_error(report_regex_errors)?
};
let compile_options = parser::Options {
ignore_case,
..Default::default()
};
let pathname = if protocol.protocol_component_matches_special_scheme() {
Component::compile(
processed_init.pathname.as_deref(),
canonicalize_and_process::canonicalize_pathname,
parser::Options {
ignore_case,
..parser::Options::pathname()
},
)?
.optionally_transpose_regex_error(report_regex_errors)?
} else {
Component::compile(
processed_init.pathname.as_deref(),
canonicalize_and_process::canonicalize_an_opaque_pathname,
compile_options.clone(),
)?
.optionally_transpose_regex_error(report_regex_errors)?
};
Ok(UrlPattern {
protocol,
username: Component::compile(
processed_init.username.as_deref(),
canonicalize_and_process::canonicalize_username,
parser::Options::default(),
)?
.optionally_transpose_regex_error(report_regex_errors)?,
password: Component::compile(
processed_init.password.as_deref(),
canonicalize_and_process::canonicalize_password,
parser::Options::default(),
)?
.optionally_transpose_regex_error(report_regex_errors)?,
hostname,
port: Component::compile(
processed_init.port.as_deref(),
|port| canonicalize_and_process::canonicalize_port(port, None),
parser::Options::default(),
)?
.optionally_transpose_regex_error(report_regex_errors)?,
pathname,
search: Component::compile(
processed_init.search.as_deref(),
canonicalize_and_process::canonicalize_search,
compile_options.clone(),
)?
.optionally_transpose_regex_error(report_regex_errors)?,
hash: Component::compile(
processed_init.hash.as_deref(),
canonicalize_and_process::canonicalize_hash,
compile_options,
)?
.optionally_transpose_regex_error(report_regex_errors)?,
})
}
/// The pattern used to match against the protocol of the URL.
pub fn protocol(&self) -> &str {
&self.protocol.pattern_string
}
/// The pattern used to match against the username of the URL.
pub fn username(&self) -> &str {
&self.username.pattern_string
}
/// The pattern used to match against the password of the URL.
pub fn password(&self) -> &str {
&self.password.pattern_string
}
/// The pattern used to match against the hostname of the URL.
pub fn hostname(&self) -> &str {
&self.hostname.pattern_string
}
/// The pattern used to match against the port of the URL.
pub fn port(&self) -> &str {
&self.port.pattern_string
}
/// The pattern used to match against the pathname of the URL.
pub fn pathname(&self) -> &str {
&self.pathname.pattern_string
}
/// The pattern used to match against the search string of the URL.
pub fn search(&self) -> &str {
&self.search.pattern_string
}
/// The pattern used to match against the hash fragment of the URL.
pub fn hash(&self) -> &str {
&self.hash.pattern_string
}
// Ref: https://wicg.github.io/urlpattern/#dom-urlpattern-test
/// Test if a given [UrlPatternInput] (with optional base url), matches the
/// pattern.
pub fn test(&self, input: UrlPatternMatchInput) -> Result<bool, Error> {
self.matches(input).map(|res| res.is_some())
}
// Ref: https://wicg.github.io/urlpattern/#dom-urlpattern-exec
/// Execute the pattern against a [UrlPatternInput] (with optional base url),
/// returning a [UrlPatternResult] if the pattern matches. If the pattern
/// doesn't match, returns `None`.
pub fn exec(
&self,
input: UrlPatternMatchInput,
) -> Result<Option<UrlPatternResult>, Error> {
self.matches(input)
}
// Ref: https://wicg.github.io/urlpattern/#match
fn matches(
&self,
input: UrlPatternMatchInput,
) -> Result<Option<UrlPatternResult>, Error> {
let input = match crate::quirks::parse_match_input(input) {
Some(input) => input,
None => return Ok(None),
};
let protocol_exec_result = self.protocol.matcher.matches(&input.protocol);
let username_exec_result = self.username.matcher.matches(&input.username);
let password_exec_result = self.password.matcher.matches(&input.password);
let hostname_exec_result = self.hostname.matcher.matches(&input.hostname);
let port_exec_result = self.port.matcher.matches(&input.port);
let pathname_exec_result = self.pathname.matcher.matches(&input.pathname);
let search_exec_result = self.search.matcher.matches(&input.search);
let hash_exec_result = self.hash.matcher.matches(&input.hash);
match (
protocol_exec_result,
username_exec_result,
password_exec_result,
hostname_exec_result,
port_exec_result,
pathname_exec_result,
search_exec_result,
hash_exec_result,
) {
(
Some(protocol_exec_result),
Some(username_exec_result),
Some(password_exec_result),
Some(hostname_exec_result),
Some(port_exec_result),
Some(pathname_exec_result),
Some(search_exec_result),
Some(hash_exec_result),
) => Ok(Some(UrlPatternResult {
protocol: self
.protocol
.create_match_result(input.protocol.clone(), protocol_exec_result),
username: self
.username
.create_match_result(input.username.clone(), username_exec_result),
password: self
.password
.create_match_result(input.password.clone(), password_exec_result),
hostname: self
.hostname
.create_match_result(input.hostname.clone(), hostname_exec_result),
port: self
.port
.create_match_result(input.port.clone(), port_exec_result),
pathname: self
.pathname
.create_match_result(input.pathname.clone(), pathname_exec_result),
search: self
.search
.create_match_result(input.search.clone(), search_exec_result),
hash: self
.hash
.create_match_result(input.hash.clone(), hash_exec_result),
})),
_ => Ok(None),
}
}
}
// Ref: https://wicg.github.io/urlpattern/#hostname-pattern-is-an-ipv6-address
fn hostname_pattern_is_ipv6_address(input: &str) -> bool {
// TODO: code point length
if input.len() < 2 {
return false;
}
input.starts_with('[') || input.starts_with("{[") || input.starts_with("\\[")
}
// Ref: https://wicg.github.io/urlpattern/#dictdef-urlpatternresult
/// A result of a URL pattern match.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UrlPatternResult {
pub protocol: UrlPatternComponentResult,
pub username: UrlPatternComponentResult,
pub password: UrlPatternComponentResult,
pub hostname: UrlPatternComponentResult,
pub port: UrlPatternComponentResult,
pub pathname: UrlPatternComponentResult,
pub search: UrlPatternComponentResult,
pub hash: UrlPatternComponentResult,
}
// Ref: https://wicg.github.io/urlpattern/#dictdef-urlpatterncomponentresult
/// A result of a URL pattern match on a single component.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UrlPatternComponentResult {
/// The matched input for this component.
pub input: String,
/// The values for all named groups in the pattern.
pub groups: std::collections::HashMap<String, String>,
}
#[cfg(test)]
mod tests {
use regex::Regex;
use std::collections::HashMap;
use serde::Deserialize;
use url::Url;
use crate::quirks;
use crate::quirks::StringOrInit;
use crate::UrlPatternComponentResult;
use crate::UrlPatternResult;
use super::UrlPattern;
use super::UrlPatternInit;
#[derive(Deserialize)]
#[serde(untagged)]
#[allow(clippy::large_enum_variant)]
enum ExpectedMatch {
String(String),
MatchResult(MatchResult),
}
#[derive(Debug, Deserialize)]
struct ComponentResult {
input: String,
groups: HashMap<String, String>,
}
#[derive(Deserialize)]
struct TestCase {
skip: Option<String>,
pattern: Vec<quirks::StringOrInit>,
#[serde(default)]
inputs: Vec<quirks::StringOrInit>,
expected_obj: Option<quirks::StringOrInit>,
expected_match: Option<ExpectedMatch>,
#[serde(default)]
exactly_empty_components: Vec<String>,
}
#[derive(Debug, Deserialize)]
struct MatchResult {
#[serde(deserialize_with = "deserialize_match_result_inputs")]
#[serde(default)]
inputs: Option<(quirks::StringOrInit, Option<String>)>,
protocol: Option<ComponentResult>,
username: Option<ComponentResult>,
password: Option<ComponentResult>,
hostname: Option<ComponentResult>,
port: Option<ComponentResult>,
pathname: Option<ComponentResult>,
search: Option<ComponentResult>,
hash: Option<ComponentResult>,
}
fn deserialize_match_result_inputs<'de, D>(
deserializer: D,
) -> Result<Option<(quirks::StringOrInit, Option<String>)>, D::Error>
where
D: serde::Deserializer<'de>,
{
#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum MatchResultInputs {
OneArgument((quirks::StringOrInit,)),
TwoArguments(quirks::StringOrInit, String),
}
let res = Option::<MatchResultInputs>::deserialize(deserializer)?;
Ok(match res {
Some(MatchResultInputs::OneArgument((a,))) => Some((a, None)),
Some(MatchResultInputs::TwoArguments(a, b)) => Some((a, Some(b))),
None => None,
})
}
fn test_case(case: TestCase) {
let input = case.pattern.get(0).cloned();
let mut base_url = case.pattern.get(1).map(|input| match input {
StringOrInit::String(str) => str.clone(),
StringOrInit::Init(_) => unreachable!(),
});
println!("\n=====");
println!(
"Pattern: {}, {}",
serde_json::to_string(&input).unwrap(),
serde_json::to_string(&base_url).unwrap()
);
if let Some(reason) = case.skip {
println!("🟠 Skipping: {}", reason);
return;
}
let input = input.unwrap_or_else(|| StringOrInit::Init(Default::default()));
let init_res = quirks::process_construct_pattern_input(
input.clone(),
base_url.as_deref(),
);
let res =
init_res.and_then(|init_res| UrlPattern::<Regex>::parse(init_res, false)); // TODO: once tests are available set flag accordingly
let expected_obj = match case.expected_obj {
Some(StringOrInit::String(s)) if s == "error" => {
assert!(res.is_err());
println!("✅ Passed");
return;
}
Some(StringOrInit::String(_)) => unreachable!(),
Some(StringOrInit::Init(init)) => {
let base_url = init.base_url.map(|url| url.parse().unwrap());
UrlPatternInit {
protocol: init.protocol,
username: init.username,
password: init.password,
hostname: init.hostname,
port: init.port,
pathname: init.pathname,
search: init.search,
hash: init.hash,
base_url,
}
}
None => UrlPatternInit::default(),
};
let pattern = res.expect("failed to parse pattern");
if let StringOrInit::Init(quirks::UrlPatternInit {
base_url: Some(url),
..
}) = &input
{
base_url = Some(url.clone())
}
macro_rules! assert_field {
($field:ident) => {{
let mut expected = expected_obj.$field;
if expected == None {
if case
.exactly_empty_components
.contains(&stringify!($field).to_owned())
{
expected = Some(String::new())
} else if let StringOrInit::Init(quirks::UrlPatternInit {
$field: Some($field),
..
}) = &input
{
expected = Some($field.to_owned())
} else if let Some(base_url) = &base_url {
let base_url = Url::parse(base_url).unwrap();
let field = url::quirks::$field(&base_url);
let field: String = match stringify!($field) {
"protocol" if !field.is_empty() => {
field[..field.len() - 1].to_owned()
}
"search" | "hash" if !field.is_empty() => field[1..].to_owned(),
_ => field.to_owned(),
};
expected = Some(field)
} else {
expected = Some("*".to_owned())
}
}
let expected = expected.unwrap();
let pattern = &pattern.$field.pattern_string;
assert_eq!(
pattern,
&expected,
"pattern for {} does not match",
stringify!($field)
);
}};
}
assert_field!(protocol);
assert_field!(username);
assert_field!(password);
assert_field!(hostname);
assert_field!(port);
assert_field!(pathname);
assert_field!(search);
assert_field!(hash);
let input = case.inputs.get(0).cloned();
let base_url = case.inputs.get(1).map(|input| match input {
StringOrInit::String(str) => str.clone(),
StringOrInit::Init(_) => unreachable!(),
});
println!(
"Input: {}, {}",
serde_json::to_string(&input).unwrap(),
serde_json::to_string(&base_url).unwrap(),
);
let input = input.unwrap_or_else(|| StringOrInit::Init(Default::default()));
let expected_input = (input.clone(), base_url.clone());
let match_input = quirks::process_match_input(input, base_url.as_deref());
if let Some(ExpectedMatch::String(s)) = &case.expected_match {
if s == "error" {
assert!(match_input.is_err());
println!("✅ Passed");
return;
}
};
let input = match_input.expect("failed to parse match input");
if input.is_none() {
assert!(case.expected_match.is_none());
println!("✅ Passed");
return;
}
let test_res = if let Some((input, _)) = input.clone() {
pattern.test(input)
} else {
Ok(false)
};
let exec_res = if let Some((input, _)) = input.clone() {
pattern.exec(input)
} else {
Ok(None)
};
if let Some(ExpectedMatch::String(s)) = &case.expected_match {
if s == "error" {
assert!(test_res.is_err());
assert!(exec_res.is_err());
println!("✅ Passed");
return;
}
};
let expected_match = case.expected_match.map(|x| match x {
ExpectedMatch::String(_) => unreachable!(),
ExpectedMatch::MatchResult(x) => x,
});
let test = test_res.unwrap();
let actual_match = exec_res.unwrap();
assert_eq!(
test,
expected_match.is_some(),
"pattern.test result is not correct"
);
let expected_match = match expected_match {
Some(x) => x,
None => {
assert!(actual_match.is_none(), "expected match to be None");
println!("✅ Passed");
return;
}
};
let actual_match = actual_match.expect("expected match to be Some");
let expected_inputs = expected_match.inputs.unwrap_or(expected_input);
let (_, inputs) = input.unwrap();
assert_eq!(inputs, expected_inputs, "expected inputs to be identical");
let exactly_empty_components = case.exactly_empty_components;
macro_rules! convert_result {
($component:ident) => {
expected_match
.$component
.map(|c| UrlPatternComponentResult {
input: c.input,
groups: c.groups,
})
.unwrap_or_else(|| {
let mut groups = HashMap::new();
if !exactly_empty_components
.contains(&stringify!($component).to_owned())
{
groups.insert("0".to_owned(), "".to_owned());
}
UrlPatternComponentResult {
input: "".to_owned(),
groups,
}
})
};
}
let expected_result = UrlPatternResult {
protocol: convert_result!(protocol),
username: convert_result!(username),
password: convert_result!(password),
hostname: convert_result!(hostname),
port: convert_result!(port),
pathname: convert_result!(pathname),
search: convert_result!(search),
hash: convert_result!(hash),
};
assert_eq!(
actual_match, expected_result,
"pattern.exec result is not correct"
);
println!("✅ Passed");
}
#[test]
fn test_cases() {
let testdata = include_str!("./testdata/urlpatterntestdata.json");
let cases: Vec<TestCase> = serde_json::from_str(testdata).unwrap();
for case in cases {
test_case(case);
}
}
#[test]
fn issue26() {
UrlPattern::<Regex>::parse(
UrlPatternInit {
pathname: Some("/:foo.".to_owned()),
..Default::default()
},
false,
)
.unwrap();
}
}