From a9eb073b5557157f9f8fffb32acf3f5fada7564e Mon Sep 17 00:00:00 2001 From: QuirinEcker Date: Sat, 28 Jan 2023 14:10:37 +0100 Subject: [PATCH] added test for helper function --- src/app/builder/asciidoctor.rs | 26 +++++++++++++++++++++----- src/test/builder/asciidoctor.rs | 19 +++++++++++++++++++ src/test/builder/mod.rs | 1 + 3 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 src/test/builder/asciidoctor.rs create mode 100644 src/test/builder/mod.rs diff --git a/src/app/builder/asciidoctor.rs b/src/app/builder/asciidoctor.rs index d235848..423b80e 100644 --- a/src/app/builder/asciidoctor.rs +++ b/src/app/builder/asciidoctor.rs @@ -29,7 +29,7 @@ fn asciidoctor_docs(in_path: &str, out_path: &str) -> process::Command { fn asciidoctor_slides(in_path: &str, out_path: &str) -> process::Command { let mut command = process::Command::new(format!("asciidoctor-revealjs-linux")); - let revealjs_path = path_between(out_path, "./dist/slides/revealjs"); + let revealjs_path = path_between(out_path.to_string(), "./dist/slides/revealjs".to_string()); command .arg(format!("{in_path}")) @@ -38,10 +38,26 @@ fn asciidoctor_slides(in_path: &str, out_path: &str) -> process::Command { return command; } -fn path_between(from: &str, to: &str) -> &str { - let from_segments = from.split("/"); - let to_segments = to.split("/"); - let last_common +fn path_between(from: String, to: String) -> String { + let from_segments: Vec<&str> = from.split("/").collect(); + let to_segments: Vec<&str> = to.split("/").collect(); + let last_matching_index = last_matching_index(&from_segments, &to_segments); + let number_of_backs = from_segments.len() - last_matching_index; + return "".to_string(); +} + +pub fn last_matching_index(from_segments: &Vec<&str>, to_segments: &Vec<&str>) -> usize { + for (index, from_segment) in from_segments.iter().enumerate() { + if let Some(to_segment) = to_segments.get(index){ + if from_segment != to_segment { + return index - 1; + } + } else { + return index - 1; + } + } + + return from_segments.len(); } pub struct AsciiDoctorDocsBuilder; diff --git a/src/test/builder/asciidoctor.rs b/src/test/builder/asciidoctor.rs new file mode 100644 index 0000000..211fda0 --- /dev/null +++ b/src/test/builder/asciidoctor.rs @@ -0,0 +1,19 @@ +use crate::app::builder::asciidoctor; + +#[test] +fn last_matching_index_0() { + let vec1 = vec!["dings", "dings", "dingens"]; + let vec2 = vec!["dings", "dings", "dings"]; + + let last_maching = asciidoctor::last_matching_index(&vec1, &vec2); + assert_eq!(last_maching, 1); +} + +#[test] +fn last_matching_index_1() { + let vec1 = vec!["dings", "dings", "dingens", "dings", "dingens"]; + let vec2 = vec!["dings", "dings", "dingens", "dings"]; + + let last_maching = asciidoctor::last_matching_index(&vec1, &vec2); + assert_eq!(last_maching, 3); +} diff --git a/src/test/builder/mod.rs b/src/test/builder/mod.rs new file mode 100644 index 0000000..118a2e8 --- /dev/null +++ b/src/test/builder/mod.rs @@ -0,0 +1 @@ +pub mod asciidoctor;