diff options
| author | Mariot Tsitoara <[email protected]> | 2021-01-02 01:23:38 +0100 |
|---|---|---|
| committer | Mariot Tsitoara <[email protected]> | 2021-01-02 01:23:38 +0100 |
| commit | ffc40a2a095ddc6a4af31ffa34f4cbe5df7a2985 (patch) | |
| tree | 0b1a68631e9dbe66d9c0849c446dd27164a34e95 | |
| parent | Bump version (diff) | |
| download | chan-downloader-ffc40a2a095ddc6a4af31ffa34f4cbe5df7a2985.tar.xz chan-downloader-ffc40a2a095ddc6a4af31ffa34f4cbe5df7a2985.zip | |
Add tests to lib
| -rw-r--r-- | src/lib.rs | 64 |
1 files changed, 58 insertions, 6 deletions
@@ -22,12 +22,14 @@ use reqwest::{Client, Error}; /// ``` /// use reqwest::Client; /// use std::env; +/// use std::fs::remove_file; /// let client = Client::new(); -/// let workpath = env::current_dir().unwrap(); +/// let workpath = env::current_dir().unwrap().join("1489266570954.jpg"); /// let url = "https://i.4cdn.org/wg/1489266570954.jpg"; -/// let answer = chan_downloader::save_image(url, workpath, client); +/// let answer = chan_downloader::save_image(url, workpath.to_str().unwrap(), &client).unwrap(); /// -/// assert_eq!(url, answer); +/// assert_eq!(workpath.to_str().unwrap(), answer); +/// remove_file(answer).unwrap(); /// ``` pub fn save_image(url: &str, path: &str, client: &Client) -> Result<String, Error> { let mut response = client.get(url).send()?; @@ -47,7 +49,7 @@ pub fn save_image(url: &str, path: &str, client: &Client) -> Result<String, Erro /// use reqwest::Client; /// let client = Client::new(); /// let url = "https://boards.4chan.org/wg/thread/6872254"; -/// match chan_downloader::get_page_content(url, client) { +/// match chan_downloader::get_page_content(url, &client) { /// Ok(page) => println!("Content: {}", page), /// Err(err) => eprintln!("Error: {}", err), /// } @@ -85,9 +87,9 @@ pub fn get_thread_infos(url: &str) -> (&str, &str) { /// use reqwest::Client; /// let client = Client::new(); /// let url = "https://boards.4chan.org/wg/thread/6872254"; -/// match chan_downloader::get_page_content(url, client) { +/// match chan_downloader::get_page_content(url, &client) { /// Ok(page_string) => { -/// let (links_iter, number_of_links) = get_image_links(page_string.as_str()); +/// let (links_iter, number_of_links) = chan_downloader::get_image_links(page_string.as_str()); /// assert_eq!(number_of_links, 4); /// @@ -109,3 +111,53 @@ pub fn get_image_links(page_content: &str) -> (CaptureMatches, usize) { let number_of_links = RE.captures_iter(page_content).count() / 2; (links_iter, number_of_links) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_gets_thread_infos() { + let url = "https://boards.4chan.org/wg/thread/6872254"; + let (board_name, thread_id) = get_thread_infos(url); + assert_eq!(board_name, "wg"); + assert_eq!(thread_id, "6872254"); + } + + #[test] + fn it_gets_image_links() { + let (links_iter, number_of_links) = get_image_links(" + <a href=\"//i.4cdn.org/wg/1489266570954.jpg\" target=\"_blank\">stickyop.jpg</a> + <a href=\"//i.4cdn.org/wg/1489266570954.jpg\" target=\"_blank\">stickyop.jpg</a> + "); + assert_eq!(number_of_links, 1); + for cap in links_iter.step_by(2) { + let url = &cap[1]; + let filename = &cap[2]; + assert_eq!(url, "//i.4cdn.org/wg/1489266570954.jpg"); + assert_eq!(filename, "1489266570954.jpg"); + } + } + + #[test] + fn it_gets_page_content() { + use reqwest::Client; + let client = Client::new(); + let url = "https://raw.githubusercontent.com/mariot/chan-downloader/master/.gitignore"; + let result = get_page_content(url, &client).unwrap(); + assert_eq!(result, "/target/\nCargo.lock\n**/*.rs.bk\n"); + } + + #[test] + fn it_saves_image() { + use reqwest::Client; + use std::env; + use std::fs::remove_file; + let client = Client::new(); + let workpath = env::current_dir().unwrap().join("1489266570954.jpg"); + let url = "https://i.4cdn.org/wg/1489266570954.jpg"; + let answer = save_image(url, workpath.to_str().unwrap(), &client).unwrap(); + assert_eq!(workpath.to_str().unwrap(), answer); + remove_file(answer).unwrap(); + } +} |