From 188530528300ee095299a1cf56e7516d83fcd823 Mon Sep 17 00:00:00 2001 From: mStar aka a person <12024604-mstarongitlab@users.noreply.gitlab.com> Date: Mon, 5 Feb 2024 16:36:15 +0100 Subject: [PATCH] Fixed it, until I added more tests --- src/parser/preparser.rs | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/src/parser/preparser.rs b/src/parser/preparser.rs index 298808a..07b6fd0 100644 --- a/src/parser/preparser.rs +++ b/src/parser/preparser.rs @@ -15,6 +15,7 @@ fn clear_spaces(raw: &str) -> String { let mut quote_is_single = false; let mut inside_quoted_codeblock = false; let mut previous_char = 'a'; + for c in raw.chars() { if currently_quoted { @@ -48,20 +49,19 @@ fn clear_spaces(raw: &str) -> String { '{' => { if currently_quoted && !inside_quoted_codeblock && !quote_is_single { inside_quoted_codeblock = true; - } else { - cleared.write_char(c.clone()).expect("Failed to write character to out string during whitespace normalisation"); } + cleared.write_char(c.clone()).expect("Failed to write character to out string during whitespace normalisation"); } '}' => { if currently_quoted && !inside_quoted_codeblock && !quote_is_single { inside_quoted_codeblock = false; - } else { - cleared.write_char(c.clone()).expect("Failed to write character to out string during whitespace normalisation"); } + cleared.write_char(c.clone()).expect("Failed to write character to out string during whitespace normalisation"); } x => { - if !previous_char.is_whitespace() && !x.is_whitespace() { + if !(previous_char.is_whitespace() && x.is_whitespace()) { cleared.write_char(x.clone()).expect("Failed to write character to out string during whitespace normalisation"); + } else { } previous_char = x; @@ -73,18 +73,39 @@ fn clear_spaces(raw: &str) -> String { cleared } +// Test to make sure that multiple spaces get grouped into one #[test] fn test_clear_spaces1() { - assert_eq!(clear_spaces(" "), " ".to_owned()) + assert_eq!(clear_spaces(" \n \t"), " ".to_owned()) } +// Test to make sure that only spaces are affected and not normal text #[test] fn test_clear_spaces2() { - let res = clear_spaces("foo bar"); + let res = clear_spaces("foo bar"); assert_eq!(res, "foo bar".to_owned()) } +// Test to make sure that double quoted test keeps spaces #[test] fn test_clear_spaces3() { assert_eq!(clear_spaces("\"foo bar\""), "\"foo bar\"".to_owned()) +} + +// Same as previous, but for single quotes +#[test] +fn test_clear_spaces4() { + assert_eq!(clear_spaces("'foo bar'"), "'foo bar'".to_owned()) +} + +// Test to make sure that code blocks inside single quotes are treated as quoted as well +#[test] +fn test_clear_spaces5() { + assert_eq!(clear_spaces("'foo {shouldn't compress space} inside'"), "'foo {shouldn't compress space} inside'".to_owned()) +} + +// Test to make sure that code blocks inside double quotes are treated as non-quoted +#[test] +fn test_clear_spaces6() { + assert_eq!(clear_spaces("\"foo {should compress space} inside\""), "\"foo {should compress space} inside\"".to_owned()) } \ No newline at end of file