add testing for url extraction
This commit is contained in:
parent
df1d3fc05b
commit
a98e079669
1 changed files with 19 additions and 4 deletions
|
|
@ -4,9 +4,24 @@ use regex::Regex;
|
|||
const RE_URL: &str =
|
||||
r"(http|ftp|https):\/\/([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])";
|
||||
|
||||
pub fn extract_urls(text: &str) -> Vec<&str> {
|
||||
pub fn extract_url(text: &str) -> Option<&str> {
|
||||
let re = Regex::new(RE_URL).unwrap();
|
||||
re.find_iter(text)
|
||||
.map(|m| m.as_str())
|
||||
.collect::<Vec<&str>>()
|
||||
match re.find(text) {
|
||||
Some(m) => Some(m.as_str()),
|
||||
None => None
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::bot::sanitize::extract_url;
|
||||
|
||||
#[test]
|
||||
fn test_extract_url() {
|
||||
// https://www.youtube.com/watch?v=00000000000
|
||||
|
||||
assert_eq!(extract_url("test http://www.test.com/id/1"), Some("http://www.test.com/id/1"));
|
||||
assert_eq!(extract_url("https://www.test.com 3"), Some("https://www.test.com"));
|
||||
assert_eq!(extract_url("there is no any url"), None);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue