blob: 241e5804ca2cd62da828234245334d66a3f6075c [file] [log] [blame]
Chih-Hung Hsiehe42c5052020-04-16 10:44:21 -07001#![cfg_attr(feature = "pattern", feature(pattern))]
2
3extern crate rand;
4extern crate regex;
5
6// Due to macro scoping rules, this definition only applies for the modules
7// defined below. Effectively, it allows us to use the same tests for both
8// native and dynamic regexes.
9//
10// This is also used to test the various matching engines. This one exercises
11// the normal code path which automatically chooses the engine based on the
12// regex and the input. Other dynamic tests explicitly set the engine to use.
13macro_rules! regex_new {
14 ($re:expr) => {{
15 use regex::Regex;
16 Regex::new($re)
17 }};
18}
19
20macro_rules! regex {
21 ($re:expr) => {
22 regex_new!($re).unwrap()
23 };
24}
25
26macro_rules! regex_set_new {
27 ($re:expr) => {{
28 use regex::RegexSet;
29 RegexSet::new($re)
30 }};
31}
32
33macro_rules! regex_set {
34 ($res:expr) => {
35 regex_set_new!($res).unwrap()
36 };
37}
38
39// Must come before other module definitions.
40include!("macros_str.rs");
41include!("macros.rs");
42
43mod api;
44mod api_str;
45mod crazy;
46mod flags;
47mod fowler;
48mod misc;
49mod multiline;
50mod noparse;
51mod regression;
Haibo Huang1d704cd2020-11-02 18:46:32 -080052mod regression_fuzz;
Chih-Hung Hsiehe42c5052020-04-16 10:44:21 -070053mod replace;
54mod searcher;
55mod set;
56mod shortest_match;
57mod suffix_reverse;
58#[cfg(feature = "unicode")]
59mod unicode;
60#[cfg(feature = "unicode-perl")]
61mod word_boundary;
62#[cfg(feature = "unicode-perl")]
63mod word_boundary_unicode;
64
65#[test]
66fn disallow_non_utf8() {
67 assert!(regex::Regex::new(r"(?-u)\xFF").is_err());
68 assert!(regex::Regex::new(r"(?-u).").is_err());
69 assert!(regex::Regex::new(r"(?-u)[\xFF]").is_err());
70 assert!(regex::Regex::new(r"(?-u)☃").is_err());
71}
72
73#[test]
74fn disallow_octal() {
75 assert!(regex::Regex::new(r"\0").is_err());
76}
77
78#[test]
79fn allow_octal() {
80 assert!(regex::RegexBuilder::new(r"\0").octal(true).build().is_ok());
81}
82
83#[test]
84fn oibits() {
85 use regex::bytes;
86 use regex::{Regex, RegexBuilder};
87 use std::panic::UnwindSafe;
88
89 fn assert_send<T: Send>() {}
90 fn assert_sync<T: Sync>() {}
91 fn assert_unwind_safe<T: UnwindSafe>() {}
92
93 assert_send::<Regex>();
94 assert_sync::<Regex>();
95 assert_unwind_safe::<Regex>();
96 assert_send::<RegexBuilder>();
97 assert_sync::<RegexBuilder>();
98 assert_unwind_safe::<RegexBuilder>();
99
100 assert_send::<bytes::Regex>();
101 assert_sync::<bytes::Regex>();
102 assert_unwind_safe::<bytes::Regex>();
103 assert_send::<bytes::RegexBuilder>();
104 assert_sync::<bytes::RegexBuilder>();
105 assert_unwind_safe::<bytes::RegexBuilder>();
106}
107
108// See: https://github.com/rust-lang/regex/issues/568
109#[test]
110fn oibits_regression() {
111 use regex::Regex;
112 use std::panic;
113
114 let _ = panic::catch_unwind(|| Regex::new("a").unwrap());
115}