From 2a3c6b5be0967ee1e68af3b79bc490923f39d6f7 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Fri, 4 Aug 2023 07:29:06 +0100 Subject: [PATCH] rename stubs and implement mocks --- src/feed/find.rs | 10 ++++----- src/test_utils.rs | 55 +++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 53 insertions(+), 12 deletions(-) diff --git a/src/feed/find.rs b/src/feed/find.rs index af6325a..dd6ec66 100644 --- a/src/feed/find.rs +++ b/src/feed/find.rs @@ -24,7 +24,7 @@ pub fn find(site: &str, channel_name: &str, e: &NetworkEnv) -> Result { mod tests { use crate::test_utils::{ - mock_fetch_as_text_with_rss_url, stub_download_as_mp3, stub_fetch_as_bytes, + mock_fetch_as_text_with_rss_url, stub_network_download_as_mp3, stub_network_fetch_as_bytes, }; use super::*; @@ -33,8 +33,8 @@ mod tests { //given let network_env = NetworkEnv { fetch_as_text: mock_fetch_as_text_with_rss_url("the-rss-url"), - fetch_as_bytes: stub_fetch_as_bytes(), - download_as_mp3: stub_download_as_mp3(), + fetch_as_bytes: stub_network_fetch_as_bytes(), + download_as_mp3: stub_network_download_as_mp3(), }; //when let result = find("site", "@channel", &network_env)?; @@ -48,8 +48,8 @@ mod tests { //given let network_env = NetworkEnv { fetch_as_text: mock_fetch_as_text_with_rss_url("the-rss-url"), - fetch_as_bytes: stub_fetch_as_bytes(), - download_as_mp3: stub_download_as_mp3(), + fetch_as_bytes: stub_network_fetch_as_bytes(), + download_as_mp3: stub_network_download_as_mp3(), }; //when let result = find("site", "invalid-channel-name", &network_env); diff --git a/src/test_utils.rs b/src/test_utils.rs index 2dfbe94..2008d08 100644 --- a/src/test_utils.rs +++ b/src/test_utils.rs @@ -1,4 +1,5 @@ use std::{ + collections::HashMap, fs::{read_to_string, File}, io::Write, str::from_utf8, @@ -29,6 +30,7 @@ pub fn read_text_file(file_name: &str) -> Result> { .collect()) } pub fn mock_fetch_as_text_with_rss_url(url: &'static str) -> NetworkFetchAsTextFn { + // TODO: returned function must return different values each time it is called Box::new(move |_url: &str| { Ok(format!( r#" @@ -40,15 +42,54 @@ pub fn mock_fetch_as_text_with_rss_url(url: &'static str) -> NetworkFetchAsTextF )) }) } -pub fn stub_fetch_as_bytes() -> NetworkFetchAsBytesFn { - Box::new(|_url: &str| Err(Error::message("Not implemented"))) +pub fn mock_network_fetch_as_bytes_with_rss_entries( + feeds: HashMap, +) -> NetworkFetchAsBytesFn { + Box::new(move |url| { + if let Some(feed) = feeds.get(url).cloned() { + Ok(bytes::Bytes::from(feed)) + } else { + Err(Error::message(format!("No mock feed: {}", url).as_str())) + } + }) } -pub fn stub_download_as_mp3() -> NetworkDownloadAsMp3Fn { - Box::new(|_url: &str| Err(Error::message("Not implemented"))) +pub fn mock_file_open(real_paths: Vec) -> FileOpenFn { + Box::new(move |path: &str| { + if real_paths.contains(&path.to_string()) { + Ok(File::open(path)?) + } else { + Err(Error::message( + format!("Not implemented: file_open: {}", path).as_str(), + )) + } + }) } -pub fn stub_file_open() -> FileOpenFn { - Box::new(|_path: &str| Err(Error::message("Not implemented"))) + +pub fn stub_network_fetch_as_bytes() -> NetworkFetchAsBytesFn { + Box::new(|url: &str| { + Err(Error::message( + format!("Not implemented: network_fetch_as_bytes: {}", url).as_str(), + )) + }) } +pub fn stub_network_download_as_mp3() -> NetworkDownloadAsMp3Fn { + Box::new(|url: &str| { + Err(Error::message( + format!("Not implemented: network_download_as_mp3: {}", url).as_str(), + )) + }) +} +// pub fn stub_file_open() -> FileOpenFn { +// Box::new(|path: &str| { +// Err(Error::message( +// format!("Not implemented: file_open: {}", path).as_str(), +// )) +// }) +// } pub fn stub_file_append_line() -> FileAppendLineFn { - Box::new(|_: &str, _: &str| Err(Error::message("Not implemented"))) + Box::new(|path: &str, line: &str| { + Err(Error::message( + format!("Not implemented: file_append_line: {} to {}", line, path).as_str(), + )) + }) }