i5-add-tests (part 4) #10
2 changed files with 20 additions and 11 deletions
|
@ -23,6 +23,8 @@ pub fn find(site: &str, channel_name: &str, e: &NetworkEnv) -> Result<String> {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use crate::test_utils::{
|
use crate::test_utils::{
|
||||||
mock_fetch_as_text_with_rss_url, stub_network_download_as_mp3, stub_network_fetch_as_bytes,
|
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<()> {
|
fn finds_rss_url() -> Result<()> {
|
||||||
//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(HashMap::from([(
|
||||||
|
"site@channel",
|
||||||
|
"the-rss-url",
|
||||||
|
)])),
|
||||||
fetch_as_bytes: stub_network_fetch_as_bytes(),
|
fetch_as_bytes: stub_network_fetch_as_bytes(),
|
||||||
download_as_mp3: stub_network_download_as_mp3(),
|
download_as_mp3: stub_network_download_as_mp3(),
|
||||||
};
|
};
|
||||||
|
@ -47,7 +52,7 @@ mod tests {
|
||||||
fn error_if_channel_name_is_invalid() -> Result<()> {
|
fn error_if_channel_name_is_invalid() -> Result<()> {
|
||||||
//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(HashMap::from([])),
|
||||||
fetch_as_bytes: stub_network_fetch_as_bytes(),
|
fetch_as_bytes: stub_network_fetch_as_bytes(),
|
||||||
download_as_mp3: stub_network_download_as_mp3(),
|
download_as_mp3: stub_network_download_as_mp3(),
|
||||||
};
|
};
|
||||||
|
|
|
@ -29,17 +29,21 @@ pub fn read_text_file(file_name: &str) -> Result<Vec<String>> {
|
||||||
.map(String::from)
|
.map(String::from)
|
||||||
.collect())
|
.collect())
|
||||||
}
|
}
|
||||||
pub fn mock_fetch_as_text_with_rss_url(url: &'static str) -> NetworkFetchAsTextFn {
|
pub fn mock_fetch_as_text_with_rss_url(
|
||||||
// TODO: returned function must return different values each time it is called
|
map: HashMap<&'static str, &'static str>,
|
||||||
Box::new(move |_url: &str| {
|
) -> NetworkFetchAsTextFn {
|
||||||
Ok(format!(
|
Box::new(move |url: &str| match map.get(url) {
|
||||||
|
Some(url) => Ok(format!(
|
||||||
r#"
|
r#"
|
||||||
<html>
|
<html>
|
||||||
<link title="RSS" href="{}">
|
<link title="RSS" href="{}">
|
||||||
</html>
|
</html>
|
||||||
"#,
|
"#,
|
||||||
url
|
url
|
||||||
))
|
)),
|
||||||
|
None => Err(Error::message(
|
||||||
|
format!("Unexpected request for {}", url).as_str(),
|
||||||
|
)),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
pub fn mock_network_fetch_as_bytes_with_rss_entries(
|
pub fn mock_network_fetch_as_bytes_with_rss_entries(
|
||||||
|
|
Loading…
Add table
Reference in a new issue