From cde3f454dcda8ef1aa98f659b30da32b012fe46d Mon Sep 17 00:00:00 2001 From: sgoudham Date: Sat, 30 Apr 2022 06:05:36 +0100 Subject: [PATCH] [v0.1.0] - Add some tests --- src/links.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/links.rs b/src/links.rs index 5fa9c93..7f1acfa 100644 --- a/src/links.rs +++ b/src/links.rs @@ -138,4 +138,58 @@ impl<'a> Iterator for LinkIter<'a> { pub(crate) fn extract_template_links(contents: &str) -> LinkIter<'_> { LinkIter(WHOLE_TEMPLATE.captures_iter(contents)) +} + +#[cfg(test)] +mod link_tests { + use std::path::PathBuf; + + use crate::links::{extract_template_links, Link, LinkType, VecPair}; + use crate::replace; + + #[test] + fn test_escaped_template_link() { + let start = r" + Example Text + ```hbs + \{{#template template.md}} << an escaped link! + ```"; + let end = r" + Example Text + ```hbs + {{#template template.md}} << an escaped link! + ```"; + assert_eq!(replace(start, "", "", 0), end); + } + + #[test] + fn test_extract_zero_template_links() { + let string = "This is some text without any template links"; + assert_eq!(extract_template_links(string).collect::>(), vec![]) + } + + #[test] + fn test_extract_zero_template_links_without_args() { + let string = "{{#template templates/footer.md}}"; + assert_eq!(extract_template_links(string).collect::>(), vec![]) + } + + #[test] + fn test_extract_template_links_simple_link() { + let s = + "Some random text with {{#template file.rs}} and {{#template test.rs test=nice}}..."; + + let res = extract_template_links(s).collect::>(); + + assert_eq!( + res, + vec![Link { + start_index: 48, + end_index: 79, + link_type: LinkType::Template(PathBuf::from("test.rs")), + link_text: "{{#template test.rs test=nice}}", + args: VecPair(vec!["{test}".to_string()], vec!["nice".to_string()]) + },] + ); + } } \ No newline at end of file