• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, MatcherBase, MatcherResult};
17 use regex::Regex;
18 use std::fmt::Debug;
19 use std::ops::Deref;
20 
21 /// Matches a string the entirety of which which matches the given regular
22 /// expression.
23 ///
24 /// This is similar to [`contains_regex`][crate::matchers::contains_regex],
25 /// except that the match must cover the whole string and not a substring.
26 ///
27 /// Both the actual value and the expected regular expression may be either a
28 /// `String` or a string reference.
29 ///
30 /// ```
31 /// # use googletest::prelude::*;
32 /// # fn should_pass_1() -> Result<()> {
33 /// verify_that!("Some value", matches_regex("S.*e"))?;  // Passes
34 /// #     Ok(())
35 /// # }
36 /// # fn should_fail_1() -> Result<()> {
37 /// verify_that!("Another value", matches_regex("Some"))?;   // Fails
38 /// #     Ok(())
39 /// # }
40 /// # fn should_fail_2() -> Result<()> {
41 /// verify_that!("Some value", matches_regex("Some"))?;   // Fails
42 /// #     Ok(())
43 /// # }
44 /// # fn should_pass_2() -> Result<()> {
45 /// verify_that!("Some value".to_string(), matches_regex(".*v.*e"))?;   // Passes
46 /// verify_that!("Some value", matches_regex(".*v.*e".to_string()))?;   // Passes
47 /// #     Ok(())
48 /// # }
49 /// # should_pass_1().unwrap();
50 /// # should_fail_1().unwrap_err();
51 /// # should_fail_2().unwrap_err();
52 /// # should_pass_2().unwrap();
53 /// ```
54 ///
55 /// Panics if the given `pattern` is not a syntactically valid regular
56 /// expression.
57 // N.B. This returns the concrete type rather than an impl Matcher so that it
58 // can act simultaneously as a Matcher<str> and a Matcher<String>. Otherwise the
59 // compiler treats it as a Matcher<str> only and the code
60 //   verify_that!("Some value".to_string(), matches_regex(".*value"))?;
61 // doesn't compile.
matches_regex<PatternT: Deref<Target = str>>( pattern: PatternT, ) -> MatchesRegexMatcher<PatternT>62 pub fn matches_regex<PatternT: Deref<Target = str>>(
63     pattern: PatternT,
64 ) -> MatchesRegexMatcher<PatternT> {
65     let adjusted_pattern = format!("^{}$", pattern.deref());
66     let regex = Regex::new(adjusted_pattern.as_str()).unwrap();
67     MatchesRegexMatcher { regex, pattern, _adjusted_pattern: adjusted_pattern }
68 }
69 
70 /// A matcher matching a string-like type matching a given regular expression.
71 ///
72 /// Intended only to be used from the function [`matches_regex`] only.
73 /// Should not be referenced by code outside this library.
74 #[derive(MatcherBase)]
75 pub struct MatchesRegexMatcher<PatternT: Deref<Target = str>> {
76     regex: Regex,
77     pattern: PatternT,
78     _adjusted_pattern: String,
79 }
80 
81 impl<PatternT, ActualT> Matcher<ActualT> for MatchesRegexMatcher<PatternT>
82 where
83     PatternT: Deref<Target = str>,
84     ActualT: AsRef<str> + Debug + Copy,
85 {
matches(&self, actual: ActualT) -> MatcherResult86     fn matches(&self, actual: ActualT) -> MatcherResult {
87         self.regex.is_match(actual.as_ref()).into()
88     }
89 
describe(&self, matcher_result: MatcherResult) -> Description90     fn describe(&self, matcher_result: MatcherResult) -> Description {
91         match matcher_result {
92             MatcherResult::Match => {
93                 format!("matches the regular expression {:#?}", self.pattern.deref()).into()
94             }
95             MatcherResult::NoMatch => {
96                 format!("doesn't match the regular expression {:#?}", self.pattern.deref()).into()
97             }
98         }
99     }
100 }
101 
102 #[cfg(test)]
103 mod tests {
104     use crate::matcher::MatcherResult;
105     use crate::prelude::*;
106 
107     #[test]
matches_regex_matches_string_reference_with_pattern() -> Result<()>108     fn matches_regex_matches_string_reference_with_pattern() -> Result<()> {
109         let matcher = matches_regex("S.*e");
110 
111         let result = matcher.matches("Some value");
112 
113         verify_that!(result, eq(MatcherResult::Match))
114     }
115 
116     #[test]
matches_regex_does_not_match_string_without_pattern() -> Result<()>117     fn matches_regex_does_not_match_string_without_pattern() -> Result<()> {
118         let matcher = matches_regex("Another");
119 
120         let result = matcher.matches("Some value");
121 
122         verify_that!(result, eq(MatcherResult::NoMatch))
123     }
124 
125     #[test]
matches_regex_does_not_match_string_only_beginning_of_which_matches() -> Result<()>126     fn matches_regex_does_not_match_string_only_beginning_of_which_matches() -> Result<()> {
127         let matcher = matches_regex("Some");
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_end_of_which_matches() -> Result<()>135     fn matches_regex_does_not_match_string_only_end_of_which_matches() -> Result<()> {
136         let matcher = matches_regex("value");
137 
138         let result = matcher.matches("Some value");
139 
140         verify_that!(result, eq(MatcherResult::NoMatch))
141     }
142 
143     #[test]
matches_regex_matches_owned_string_with_pattern() -> Result<()>144     fn matches_regex_matches_owned_string_with_pattern() -> Result<()> {
145         let matcher = matches_regex(".*value");
146 
147         let result = matcher.matches(&"Some value".to_string());
148 
149         verify_that!(result, eq(MatcherResult::Match))
150     }
151 
152     #[test]
matches_regex_matches_string_when_regex_has_beginning_of_string_marker() -> Result<()>153     fn matches_regex_matches_string_when_regex_has_beginning_of_string_marker() -> Result<()> {
154         let matcher = matches_regex("^Some value");
155 
156         let result = matcher.matches("Some value");
157 
158         verify_that!(result, eq(MatcherResult::Match))
159     }
160 
161     #[test]
matches_regex_matches_string_when_regex_has_end_of_string_marker() -> Result<()>162     fn matches_regex_matches_string_when_regex_has_end_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_both_end_markers() -> Result<()>171     fn matches_regex_matches_string_when_regex_has_both_end_markers() -> 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_reference_with_owned_string() -> Result<()>180     fn matches_regex_matches_string_reference_with_owned_string() -> Result<()> {
181         let matcher = matches_regex(".*value".to_string());
182 
183         let result = matcher.matches("Some value");
184 
185         verify_that!(result, eq(MatcherResult::Match))
186     }
187 
188     #[test]
verify_that_works_with_owned_string() -> Result<()>189     fn verify_that_works_with_owned_string() -> Result<()> {
190         verify_that!("Some value".to_string(), matches_regex(".*value"))
191     }
192 
193     #[test]
matches_regex_displays_quoted_debug_of_pattern() -> Result<()>194     fn matches_regex_displays_quoted_debug_of_pattern() -> Result<()> {
195         let matcher = matches_regex("\n");
196 
197         verify_that!(
198             Matcher::<&str>::describe(&matcher, MatcherResult::Match),
199             displays_as(eq("matches the regular expression \"\\n\""))
200         )
201     }
202 }
203