From 1219eef08933667183647bfcdf9c16a4e7408278 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Fri, 28 Jul 2023 07:50:55 +0100 Subject: [PATCH] add tests for history::find --- src/history/find.rs | 83 ++++++++++++++++++++++++++++++ test/data/with-embedded-llamma.txt | 3 ++ test/data/with-llamma.txt | 3 ++ test/data/without-llamma.txt | 2 + 4 files changed, 91 insertions(+) create mode 100644 test/data/with-embedded-llamma.txt create mode 100644 test/data/with-llamma.txt create mode 100644 test/data/without-llamma.txt diff --git a/src/history/find.rs b/src/history/find.rs index 5b760b8..c1dd584 100644 --- a/src/history/find.rs +++ b/src/history/find.rs @@ -15,3 +15,86 @@ pub fn find(link: &Link, file_name: &str) -> Result { } Ok(false) // is not already downloaded } + +#[cfg(test)] +mod test { + use crate::{history::Link, test_utils::create_text_file}; + + use super::*; + #[test] + fn true_if_line_exists() -> Result<()> { + //given + let (dir, file_name) = + create_text_file("file", include_bytes!("../../test/data/with-llamma.txt"))?; + let link = Link { + href: "llamma".to_string(), + rel: "".to_string(), + hreflang: None, + mime_type: None, + title: None, + length: None, + }; + + //when + let result = find(&link, &file_name)?; + + //then + drop(dir); + + assert_eq!(result, true); + + Ok(()) + } + + #[test] + fn false_if_line_absent() -> Result<()> { + //given + let (dir, file_name) = + create_text_file("file", include_bytes!("../../test/data/without-llamma.txt"))?; + let link = Link { + href: "llamma".to_string(), + rel: "".to_string(), + hreflang: None, + mime_type: None, + title: None, + length: None, + }; + + //when + let result = find(&link, &file_name)?; + + //then + drop(dir); + + assert_eq!(result, false); + + Ok(()) + } + + #[test] + fn false_if_embedded_within_line() -> Result<()> { + //given + let (dir, file_name) = create_text_file( + "file", + include_bytes!("../../test/data/with-embedded-llamma.txt"), + )?; + let link = Link { + href: "llamma".to_string(), + rel: "".to_string(), + hreflang: None, + mime_type: None, + title: None, + length: None, + }; + + //when + let result = find(&link, &file_name)?; + + //then + drop(dir); + + assert_eq!(result, false); + + Ok(()) + } +} diff --git a/test/data/with-embedded-llamma.txt b/test/data/with-embedded-llamma.txt new file mode 100644 index 0000000..3574d2b --- /dev/null +++ b/test/data/with-embedded-llamma.txt @@ -0,0 +1,3 @@ +this is a text file +embedded llamma +it has three lines diff --git a/test/data/with-llamma.txt b/test/data/with-llamma.txt new file mode 100644 index 0000000..cad2d82 --- /dev/null +++ b/test/data/with-llamma.txt @@ -0,0 +1,3 @@ +this is a text file +llamma +it has three lines diff --git a/test/data/without-llamma.txt b/test/data/without-llamma.txt new file mode 100644 index 0000000..61a0ef5 --- /dev/null +++ b/test/data/without-llamma.txt @@ -0,0 +1,2 @@ +this is a text file +it has three lines