Chih-Hung Hsieh | e42c505 | 2020-04-16 10:44:21 -0700 | [diff] [blame] | 1 | #![cfg_attr(feature = "pattern", feature(pattern))] |
| 2 | |
Joel Galenson | 3874808 | 2021-05-19 16:51:51 -0700 | [diff] [blame^] | 3 | use regex; |
Chih-Hung Hsieh | e42c505 | 2020-04-16 10:44:21 -0700 | [diff] [blame] | 4 | |
| 5 | // Due to macro scoping rules, this definition only applies for the modules |
| 6 | // defined below. Effectively, it allows us to use the same tests for both |
| 7 | // native and dynamic regexes. |
| 8 | // |
| 9 | // This is also used to test the various matching engines. This one exercises |
| 10 | // the normal code path which automatically chooses the engine based on the |
| 11 | // regex and the input. Other dynamic tests explicitly set the engine to use. |
| 12 | macro_rules! regex_new { |
| 13 | ($re:expr) => {{ |
| 14 | use regex::Regex; |
| 15 | Regex::new($re) |
| 16 | }}; |
| 17 | } |
| 18 | |
| 19 | macro_rules! regex { |
| 20 | ($re:expr) => { |
| 21 | regex_new!($re).unwrap() |
| 22 | }; |
| 23 | } |
| 24 | |
| 25 | macro_rules! regex_set_new { |
| 26 | ($re:expr) => {{ |
| 27 | use regex::RegexSet; |
| 28 | RegexSet::new($re) |
| 29 | }}; |
| 30 | } |
| 31 | |
| 32 | macro_rules! regex_set { |
| 33 | ($res:expr) => { |
| 34 | regex_set_new!($res).unwrap() |
| 35 | }; |
| 36 | } |
| 37 | |
| 38 | // Must come before other module definitions. |
| 39 | include!("macros_str.rs"); |
| 40 | include!("macros.rs"); |
| 41 | |
| 42 | mod api; |
| 43 | mod api_str; |
| 44 | mod crazy; |
| 45 | mod flags; |
| 46 | mod fowler; |
| 47 | mod misc; |
| 48 | mod multiline; |
| 49 | mod noparse; |
| 50 | mod regression; |
Haibo Huang | 1d704cd | 2020-11-02 18:46:32 -0800 | [diff] [blame] | 51 | mod regression_fuzz; |
Chih-Hung Hsieh | e42c505 | 2020-04-16 10:44:21 -0700 | [diff] [blame] | 52 | mod replace; |
| 53 | mod searcher; |
| 54 | mod set; |
| 55 | mod shortest_match; |
| 56 | mod suffix_reverse; |
| 57 | #[cfg(feature = "unicode")] |
| 58 | mod unicode; |
| 59 | #[cfg(feature = "unicode-perl")] |
| 60 | mod word_boundary; |
| 61 | #[cfg(feature = "unicode-perl")] |
| 62 | mod word_boundary_unicode; |
| 63 | |
| 64 | #[test] |
| 65 | fn disallow_non_utf8() { |
| 66 | assert!(regex::Regex::new(r"(?-u)\xFF").is_err()); |
| 67 | assert!(regex::Regex::new(r"(?-u).").is_err()); |
| 68 | assert!(regex::Regex::new(r"(?-u)[\xFF]").is_err()); |
| 69 | assert!(regex::Regex::new(r"(?-u)☃").is_err()); |
| 70 | } |
| 71 | |
| 72 | #[test] |
| 73 | fn disallow_octal() { |
| 74 | assert!(regex::Regex::new(r"\0").is_err()); |
| 75 | } |
| 76 | |
| 77 | #[test] |
| 78 | fn allow_octal() { |
| 79 | assert!(regex::RegexBuilder::new(r"\0").octal(true).build().is_ok()); |
| 80 | } |
| 81 | |
| 82 | #[test] |
| 83 | fn oibits() { |
| 84 | use regex::bytes; |
Elliott Hughes | ffb6030 | 2021-04-01 17:11:40 -0700 | [diff] [blame] | 85 | use regex::{Regex, RegexBuilder, RegexSet, RegexSetBuilder}; |
| 86 | use std::panic::{RefUnwindSafe, UnwindSafe}; |
Chih-Hung Hsieh | e42c505 | 2020-04-16 10:44:21 -0700 | [diff] [blame] | 87 | |
| 88 | fn assert_send<T: Send>() {} |
| 89 | fn assert_sync<T: Sync>() {} |
| 90 | fn assert_unwind_safe<T: UnwindSafe>() {} |
Elliott Hughes | ffb6030 | 2021-04-01 17:11:40 -0700 | [diff] [blame] | 91 | fn assert_ref_unwind_safe<T: RefUnwindSafe>() {} |
Chih-Hung Hsieh | e42c505 | 2020-04-16 10:44:21 -0700 | [diff] [blame] | 92 | |
| 93 | assert_send::<Regex>(); |
| 94 | assert_sync::<Regex>(); |
| 95 | assert_unwind_safe::<Regex>(); |
Elliott Hughes | ffb6030 | 2021-04-01 17:11:40 -0700 | [diff] [blame] | 96 | assert_ref_unwind_safe::<Regex>(); |
Chih-Hung Hsieh | e42c505 | 2020-04-16 10:44:21 -0700 | [diff] [blame] | 97 | assert_send::<RegexBuilder>(); |
| 98 | assert_sync::<RegexBuilder>(); |
| 99 | assert_unwind_safe::<RegexBuilder>(); |
Elliott Hughes | ffb6030 | 2021-04-01 17:11:40 -0700 | [diff] [blame] | 100 | assert_ref_unwind_safe::<RegexBuilder>(); |
Chih-Hung Hsieh | e42c505 | 2020-04-16 10:44:21 -0700 | [diff] [blame] | 101 | |
| 102 | assert_send::<bytes::Regex>(); |
| 103 | assert_sync::<bytes::Regex>(); |
| 104 | assert_unwind_safe::<bytes::Regex>(); |
Elliott Hughes | ffb6030 | 2021-04-01 17:11:40 -0700 | [diff] [blame] | 105 | assert_ref_unwind_safe::<bytes::Regex>(); |
Chih-Hung Hsieh | e42c505 | 2020-04-16 10:44:21 -0700 | [diff] [blame] | 106 | assert_send::<bytes::RegexBuilder>(); |
| 107 | assert_sync::<bytes::RegexBuilder>(); |
| 108 | assert_unwind_safe::<bytes::RegexBuilder>(); |
Elliott Hughes | ffb6030 | 2021-04-01 17:11:40 -0700 | [diff] [blame] | 109 | assert_ref_unwind_safe::<bytes::RegexBuilder>(); |
| 110 | |
| 111 | assert_send::<RegexSet>(); |
| 112 | assert_sync::<RegexSet>(); |
| 113 | assert_unwind_safe::<RegexSet>(); |
| 114 | assert_ref_unwind_safe::<RegexSet>(); |
| 115 | assert_send::<RegexSetBuilder>(); |
| 116 | assert_sync::<RegexSetBuilder>(); |
| 117 | assert_unwind_safe::<RegexSetBuilder>(); |
| 118 | assert_ref_unwind_safe::<RegexSetBuilder>(); |
| 119 | |
| 120 | assert_send::<bytes::RegexSet>(); |
| 121 | assert_sync::<bytes::RegexSet>(); |
| 122 | assert_unwind_safe::<bytes::RegexSet>(); |
| 123 | assert_ref_unwind_safe::<bytes::RegexSet>(); |
| 124 | assert_send::<bytes::RegexSetBuilder>(); |
| 125 | assert_sync::<bytes::RegexSetBuilder>(); |
| 126 | assert_unwind_safe::<bytes::RegexSetBuilder>(); |
| 127 | assert_ref_unwind_safe::<bytes::RegexSetBuilder>(); |
Chih-Hung Hsieh | e42c505 | 2020-04-16 10:44:21 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | // See: https://github.com/rust-lang/regex/issues/568 |
| 131 | #[test] |
| 132 | fn oibits_regression() { |
| 133 | use regex::Regex; |
| 134 | use std::panic; |
| 135 | |
| 136 | let _ = panic::catch_unwind(|| Regex::new("a").unwrap()); |
| 137 | } |
Elliott Hughes | ffb6030 | 2021-04-01 17:11:40 -0700 | [diff] [blame] | 138 | |
| 139 | // See: https://github.com/rust-lang/regex/issues/750 |
| 140 | #[test] |
| 141 | #[cfg(target_pointer_width = "64")] |
| 142 | fn regex_is_reasonably_small() { |
| 143 | use std::mem::size_of; |
| 144 | |
| 145 | use regex::bytes; |
| 146 | use regex::{Regex, RegexSet}; |
| 147 | |
| 148 | assert_eq!(16, size_of::<Regex>()); |
| 149 | assert_eq!(16, size_of::<RegexSet>()); |
| 150 | assert_eq!(16, size_of::<bytes::Regex>()); |
| 151 | assert_eq!(16, size_of::<bytes::RegexSet>()); |
| 152 | } |