add testing for url extraction

This commit is contained in:
mykola2312 2024-02-24 14:47:57 +02:00
parent 95a341f77c
commit 2dba73a464

View file

@ -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);
}
}