From a98e079669e769af300336ad32c90590fc4f5a89 Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Sat, 24 Feb 2024 14:47:57 +0200 Subject: [PATCH] add testing for url extraction --- src/bot/sanitize.rs | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/bot/sanitize.rs b/src/bot/sanitize.rs index b6ab633..ca47400 100644 --- a/src/bot/sanitize.rs +++ b/src/bot/sanitize.rs @@ -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::>() + 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); + } +} \ No newline at end of file