i5-add-tests (part 4) #10

Merged
kemitix merged 39 commits from i5-add-tests into main 2023-08-05 06:50:01 +01:00
2 changed files with 20 additions and 11 deletions
Showing only changes of commit e44da378c6 - Show all commits

View file

@ -23,6 +23,8 @@ pub fn find(site: &str, channel_name: &str, e: &NetworkEnv) -> Result<String> {
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use crate::test_utils::{
mock_fetch_as_text_with_rss_url, stub_network_download_as_mp3, stub_network_fetch_as_bytes,
};
@ -32,7 +34,10 @@ mod tests {
fn finds_rss_url() -> Result<()> {
//given
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(HashMap::from([(
"site@channel",
"the-rss-url",
)])),
fetch_as_bytes: stub_network_fetch_as_bytes(),
download_as_mp3: stub_network_download_as_mp3(),
};
@ -47,7 +52,7 @@ mod tests {
fn error_if_channel_name_is_invalid() -> Result<()> {
//given
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(HashMap::from([])),
fetch_as_bytes: stub_network_fetch_as_bytes(),
download_as_mp3: stub_network_download_as_mp3(),
};

View file

@ -29,17 +29,21 @@ pub fn read_text_file(file_name: &str) -> Result<Vec<String>> {
.map(String::from)
.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!(
pub fn mock_fetch_as_text_with_rss_url(
map: HashMap<&'static str, &'static str>,
) -> NetworkFetchAsTextFn {
Box::new(move |url: &str| match map.get(url) {
Some(url) => Ok(format!(
r#"
<html>
<link title="RSS" href="{}">
</html>
"#,
<html>
<link title="RSS" href="{}">
</html>
"#,
url
))
)),
None => Err(Error::message(
format!("Unexpected request for {}", url).as_str(),
)),
})
}
pub fn mock_network_fetch_as_bytes_with_rss_entries(