1 // Copyright 2022 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 use crate::description::Description;
16 use crate::matcher::{Matcher, MatcherResult};
17 use regex::Regex;
18 use std::fmt::Debug;
19 use std::marker::PhantomData;
20 use std::ops::Deref;
21 
22 /// Matches a string the entirety of which which matches the given regular
23 /// expression.
24 ///
25 /// This is similar to [`contains_regex`][crate::matchers::contains_regex],
26 /// except that the match must cover the whole string and not a substring.
27 ///
28 /// Both the actual value and the expected regular expression may be either a
29 /// `String` or a string reference.
30 ///
31 /// ```
32 /// # use googletest::prelude::*;
33 /// # fn should_pass_1() -> Result<()> {
34 /// verify_that!("Some value", matches_regex("S.*e"))?;  // Passes
35 /// #     Ok(())
36 /// # }
37 /// # fn should_fail_1() -> Result<()> {
38 /// verify_that!("Another value", matches_regex("Some"))?;   // Fails
39 /// #     Ok(())
40 /// # }
41 /// # fn should_fail_2() -> Result<()> {
42 /// verify_that!("Some value", matches_regex("Some"))?;   // Fails
43 /// #     Ok(())
44 /// # }
45 /// # fn should_pass_2() -> Result<()> {
46 /// verify_that!("Some value".to_string(), matches_regex(".*v.*e"))?;   // Passes
47 /// verify_that!("Some value", matches_regex(".*v.*e".to_string()))?;   // Passes
48 /// #     Ok(())
49 /// # }
50 /// # should_pass_1().unwrap();
51 /// # should_fail_1().unwrap_err();
52 /// # should_fail_2().unwrap_err();
53 /// # should_pass_2().unwrap();
54 /// ```
55 ///
56 /// Panics if the given `pattern` is not a syntactically valid regular
57 /// expression.
58 // N.B. This returns the concrete type rather than an impl Matcher so that it
59 // can act simultaneously as a Matcher<str> and a Matcher<String>. Otherwise the
60 // compiler treats it as a Matcher<str> only and the code
61 //   verify_that!("Some value".to_string(), matches_regex(".*value"))?;
62 // doesn't compile.
matches_regex<ActualT: ?Sized, PatternT: Deref<Target = str>>( pattern: PatternT, ) -> MatchesRegexMatcher<ActualT, PatternT>63 pub fn matches_regex<ActualT: ?Sized, PatternT: Deref<Target = str>>(
64     pattern: PatternT,
65 ) -> MatchesRegexMatcher<ActualT, PatternT> {
66     let adjusted_pattern = format!("^{}$", pattern.deref());
67     let regex = Regex::new(adjusted_pattern.as_str()).unwrap();
68     MatchesRegexMatcher {
69         regex,
70         pattern,
71         _adjusted_pattern: adjusted_pattern,
72         phantom: Default::default(),
73     }
74 }
75 
76 /// A matcher matching a string-like type matching a given regular expression.
77 ///
78 /// Intended only to be used from the function [`matches_regex`] only.
79 /// Should not be referenced by code outside this library.
80 pub struct MatchesRegexMatcher<ActualT: ?Sized, PatternT: Deref<Target = str>> {
81     regex: Regex,
82     pattern: PatternT,
83     _adjusted_pattern: String,
84     phantom: PhantomData<ActualT>,
85 }
86 
87 impl<PatternT, ActualT> Matcher for MatchesRegexMatcher<ActualT, PatternT>
88 where
89     PatternT: Deref<Target = str>,
90     ActualT: AsRef<str> + Debug + ?Sized,
91 {
92     type ActualT = ActualT;
93 
matches(&self, actual: &Self::ActualT) -> MatcherResult94     fn matches(&self, actual: &Self::ActualT) -> MatcherResult {
95         self.regex.is_match(actual.as_ref()).into()
96     }
97 
describe(&self, matcher_result: MatcherResult) -> Description98     fn describe(&self, matcher_result: MatcherResult) -> Description {
99         match matcher_result {
100             MatcherResult::Match => {
101                 format!("matches the regular expression {:#?}", self.pattern.deref()).into()
102             }
103             MatcherResult::NoMatch => {
104                 format!("doesn't match the regular expression {:#?}", self.pattern.deref()).into()
105             }
106         }
107     }
108 }
109 
110 #[cfg(test)]
111 mod tests {
112     use super::{matches_regex, MatchesRegexMatcher};
113     use crate::matcher::{Matcher, MatcherResult};
114     use crate::prelude::*;
115 
116     #[test]
matches_regex_matches_string_reference_with_pattern() -> Result<()>117     fn matches_regex_matches_string_reference_with_pattern() -> Result<()> {
118         let matcher = matches_regex("S.*e");
119 
120         let result = matcher.matches("Some value");
121 
122         verify_that!(result, eq(MatcherResult::Match))
123     }
124 
125     #[test]
matches_regex_does_not_match_string_without_pattern() -> Result<()>126     fn matches_regex_does_not_match_string_without_pattern() -> Result<()> {
127         let matcher = matches_regex("Another");
128 
129         let result = matcher.matches("Some value");
130 
131         verify_that!(result, eq(MatcherResult::NoMatch))
132     }
133 
134     #[test]
matches_regex_does_not_match_string_only_beginning_of_which_matches() -> Result<()>135     fn matches_regex_does_not_match_string_only_beginning_of_which_matches() -> Result<()> {
136         let matcher = matches_regex("Some");
137 
138         let result = matcher.matches("Some value");
139 
140         verify_that!(result, eq(MatcherResult::NoMatch))
141     }
142 
143     #[test]
matches_regex_does_not_match_string_only_end_of_which_matches() -> Result<()>144     fn matches_regex_does_not_match_string_only_end_of_which_matches() -> Result<()> {
145         let matcher = matches_regex("value");
146 
147         let result = matcher.matches("Some value");
148 
149         verify_that!(result, eq(MatcherResult::NoMatch))
150     }
151 
152     #[test]
matches_regex_matches_owned_string_with_pattern() -> Result<()>153     fn matches_regex_matches_owned_string_with_pattern() -> Result<()> {
154         let matcher = matches_regex(".*value");
155 
156         let result = matcher.matches(&"Some value".to_string());
157 
158         verify_that!(result, eq(MatcherResult::Match))
159     }
160 
161     #[test]
matches_regex_matches_string_when_regex_has_beginning_of_string_marker() -> Result<()>162     fn matches_regex_matches_string_when_regex_has_beginning_of_string_marker() -> Result<()> {
163         let matcher = matches_regex("^Some value");
164 
165         let result = matcher.matches("Some value");
166 
167         verify_that!(result, eq(MatcherResult::Match))
168     }
169 
170     #[test]
matches_regex_matches_string_when_regex_has_end_of_string_marker() -> Result<()>171     fn matches_regex_matches_string_when_regex_has_end_of_string_marker() -> Result<()> {
172         let matcher = matches_regex("Some value$");
173 
174         let result = matcher.matches("Some value");
175 
176         verify_that!(result, eq(MatcherResult::Match))
177     }
178 
179     #[test]
matches_regex_matches_string_when_regex_has_both_end_markers() -> Result<()>180     fn matches_regex_matches_string_when_regex_has_both_end_markers() -> Result<()> {
181         let matcher = matches_regex("^Some value$");
182 
183         let result = matcher.matches("Some value");
184 
185         verify_that!(result, eq(MatcherResult::Match))
186     }
187 
188     #[test]
matches_regex_matches_string_reference_with_owned_string() -> Result<()>189     fn matches_regex_matches_string_reference_with_owned_string() -> Result<()> {
190         let matcher = matches_regex(".*value".to_string());
191 
192         let result = matcher.matches("Some value");
193 
194         verify_that!(result, eq(MatcherResult::Match))
195     }
196 
197     #[test]
verify_that_works_with_owned_string() -> Result<()>198     fn verify_that_works_with_owned_string() -> Result<()> {
199         verify_that!("Some value".to_string(), matches_regex(".*value"))
200     }
201 
202     #[test]
matches_regex_displays_quoted_debug_of_pattern() -> Result<()>203     fn matches_regex_displays_quoted_debug_of_pattern() -> Result<()> {
204         let matcher: MatchesRegexMatcher<&str, _> = matches_regex("\n");
205 
206         verify_that!(
207             Matcher::describe(&matcher, MatcherResult::Match),
208             displays_as(eq("matches the regular expression \"\\n\""))
209         )
210     }
211 }
212