Fixed it, until I added more tests

This commit is contained in:
mStar aka a person 2024-02-05 16:36:15 +01:00
parent fb947cc105
commit 1885305283

View file

@ -16,6 +16,7 @@ fn clear_spaces(raw: &str) -> String {
let mut inside_quoted_codeblock = false;
let mut previous_char = 'a';
for c in raw.chars() {
if currently_quoted {
cleared.write_char(c.clone()).expect("Failed to write character to out string during whitespace normalisation");
@ -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");
}
}
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");
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())
}