i5-add-tests (part 4) #10
2 changed files with 53 additions and 12 deletions
|
@ -24,7 +24,7 @@ pub fn find(site: &str, channel_name: &str, e: &NetworkEnv) -> Result<String> {
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|
||||||
use crate::test_utils::{
|
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::*;
|
use super::*;
|
||||||
|
@ -33,8 +33,8 @@ mod tests {
|
||||||
//given
|
//given
|
||||||
let network_env = NetworkEnv {
|
let network_env = NetworkEnv {
|
||||||
fetch_as_text: mock_fetch_as_text_with_rss_url("the-rss-url"),
|
fetch_as_text: mock_fetch_as_text_with_rss_url("the-rss-url"),
|
||||||
fetch_as_bytes: stub_fetch_as_bytes(),
|
fetch_as_bytes: stub_network_fetch_as_bytes(),
|
||||||
download_as_mp3: stub_download_as_mp3(),
|
download_as_mp3: stub_network_download_as_mp3(),
|
||||||
};
|
};
|
||||||
//when
|
//when
|
||||||
let result = find("site", "@channel", &network_env)?;
|
let result = find("site", "@channel", &network_env)?;
|
||||||
|
@ -48,8 +48,8 @@ mod tests {
|
||||||
//given
|
//given
|
||||||
let network_env = NetworkEnv {
|
let network_env = NetworkEnv {
|
||||||
fetch_as_text: mock_fetch_as_text_with_rss_url("the-rss-url"),
|
fetch_as_text: mock_fetch_as_text_with_rss_url("the-rss-url"),
|
||||||
fetch_as_bytes: stub_fetch_as_bytes(),
|
fetch_as_bytes: stub_network_fetch_as_bytes(),
|
||||||
download_as_mp3: stub_download_as_mp3(),
|
download_as_mp3: stub_network_download_as_mp3(),
|
||||||
};
|
};
|
||||||
//when
|
//when
|
||||||
let result = find("site", "invalid-channel-name", &network_env);
|
let result = find("site", "invalid-channel-name", &network_env);
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use std::{
|
use std::{
|
||||||
|
collections::HashMap,
|
||||||
fs::{read_to_string, File},
|
fs::{read_to_string, File},
|
||||||
io::Write,
|
io::Write,
|
||||||
str::from_utf8,
|
str::from_utf8,
|
||||||
|
@ -29,6 +30,7 @@ pub fn read_text_file(file_name: &str) -> Result<Vec<String>> {
|
||||||
.collect())
|
.collect())
|
||||||
}
|
}
|
||||||
pub fn mock_fetch_as_text_with_rss_url(url: &'static str) -> NetworkFetchAsTextFn {
|
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| {
|
Box::new(move |_url: &str| {
|
||||||
Ok(format!(
|
Ok(format!(
|
||||||
r#"
|
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 {
|
pub fn mock_network_fetch_as_bytes_with_rss_entries(
|
||||||
Box::new(|_url: &str| Err(Error::message("Not implemented")))
|
feeds: HashMap<String, String>,
|
||||||
|
) -> 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 {
|
pub fn mock_file_open(real_paths: Vec<String>) -> FileOpenFn {
|
||||||
Box::new(|_url: &str| Err(Error::message("Not implemented")))
|
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 {
|
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(),
|
||||||
|
))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue