@@ -5,42 +5,67 @@ use fancy_regex::Regex;
5
5
6
6
#[ test]
7
7
fn control_character_escapes ( ) {
8
- assert_matches ( r"\a" , "\x07 " ) ;
9
- assert_matches ( r"\e" , "\x1B " ) ;
10
- assert_matches ( r"\f" , "\x0C " ) ;
11
- assert_matches ( r"\n" , "\x0A " ) ;
12
- assert_matches ( r"\r" , "\x0D " ) ;
13
- assert_matches ( r"\t" , "\x09 " ) ;
14
- assert_matches ( r"\v" , "\x0B " ) ;
8
+ assert_match ( r"\a" , "\x07 " ) ;
9
+ assert_match ( r"\e" , "\x1B " ) ;
10
+ assert_match ( r"\f" , "\x0C " ) ;
11
+ assert_match ( r"\n" , "\x0A " ) ;
12
+ assert_match ( r"\r" , "\x0D " ) ;
13
+ assert_match ( r"\t" , "\x09 " ) ;
14
+ assert_match ( r"\v" , "\x0B " ) ;
15
15
}
16
16
17
17
#[ test]
18
18
fn character_class_escapes ( ) {
19
- assert_matches ( r"[\[]" , "[" ) ;
20
- assert_matches ( r"[\^]" , "^" ) ;
19
+ assert_match ( r"[\[]" , "[" ) ;
20
+ assert_match ( r"[\^]" , "^" ) ;
21
21
22
22
// The regex crate would reject the following because it's not necessary to escape them.
23
23
// Other engines allow to escape any non-alphanumeric character.
24
- assert_matches ( r"[\<]" , "<" ) ;
25
- assert_matches ( r"[\>]" , ">" ) ;
26
- assert_matches ( r"[\.]" , "." ) ;
24
+ assert_match ( r"[\<]" , "<" ) ;
25
+ assert_match ( r"[\>]" , ">" ) ;
26
+ assert_match ( r"[\.]" , "." ) ;
27
27
28
28
// Character class escape
29
- assert_matches ( r"[\d]" , "1" ) ;
29
+ assert_match ( r"[\d]" , "1" ) ;
30
30
31
31
// Control characters
32
- assert_matches ( r"[\e]" , "\x1B " ) ;
33
- assert_matches ( r"[\n]" , "\x0A " ) ;
32
+ assert_match ( r"[\e]" , "\x1B " ) ;
33
+ assert_match ( r"[\n]" , "\x0A " ) ;
34
34
}
35
35
36
+ #[ test]
37
+ fn character_class_nested ( ) {
38
+ assert_match ( r"[[a][bc]]" , "c" ) ;
39
+ assert_match ( r"[a[^b]]" , "c" ) ;
40
+ }
41
+
42
+ #[ test]
43
+ fn character_class_intersection ( ) {
44
+ assert_match ( r"[\w&&a-c]" , "c" ) ;
45
+ assert_no_match ( r"[\w&&a-c]" , "d" ) ;
46
+
47
+ assert_match ( r"[[0-9]&&[^4]]" , "1" ) ;
48
+ assert_no_match ( r"[[0-9]&&[^4]]" , "4" ) ;
49
+ }
50
+
51
+
52
+ fn assert_match ( re : & str , text : & str ) {
53
+ let result = match_text ( re, text) ;
54
+ assert_eq ! ( result, true , "Expected regex '{}' to match text '{}'" , re, text) ;
55
+ }
56
+
57
+ fn assert_no_match ( re : & str , text : & str ) {
58
+ let result = match_text ( re, text) ;
59
+ assert_eq ! ( result, false , "Expected regex '{}' to not match text '{}'" , re, text) ;
60
+ }
36
61
37
- fn assert_matches ( re : & str , text : & str ) {
62
+ fn match_text ( re : & str , text : & str ) -> bool {
38
63
let parse_result = Regex :: new ( re) ;
39
64
assert ! ( parse_result. is_ok( ) ,
40
65
"Expected regex '{}' to be compiled successfully, got {:?}" , re, parse_result. err( ) ) ;
41
66
42
67
let regex = parse_result. unwrap ( ) ;
43
68
let match_result = regex. is_match ( text) ;
44
69
assert ! ( match_result. is_ok( ) , "Expected match to succeed, but was {:?}" , match_result) ;
45
- assert_eq ! ( match_result. ok ( ) , Some ( true ) , "Expected regex '{}' to match text '{}'" , re , text ) ;
70
+ match_result. unwrap ( )
46
71
}
0 commit comments