Add tests for coalesce_whitespace_escaped.

This commit is contained in:
Tom Alexander 2023-10-08 16:06:52 -04:00
parent a6adeee40b
commit 41aa0349a0
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE

View File

@ -314,9 +314,10 @@ fn impl_coalesce_whitespace_escaped<'s, C: Fn(char) -> bool>(
ret, ret,
}, },
c, c,
) if c == escape_character => { ) if c == escape_character => CoalesceWhitespaceEscaped::RequiresMutationEscaping {
CoalesceWhitespaceEscaped::RequiresMutationEscaping { ret } ret,
} matched_escape_character: c,
},
( (
CoalesceWhitespaceEscaped::RequiresMutation { CoalesceWhitespaceEscaped::RequiresMutation {
mut in_whitespace, mut in_whitespace,
@ -332,7 +333,7 @@ fn impl_coalesce_whitespace_escaped<'s, C: Fn(char) -> bool>(
} }
( (
CoalesceWhitespaceEscaped::RequiresMutation { CoalesceWhitespaceEscaped::RequiresMutation {
in_whitespace, in_whitespace: _,
mut ret, mut ret,
}, },
_, _,
@ -343,9 +344,13 @@ fn impl_coalesce_whitespace_escaped<'s, C: Fn(char) -> bool>(
ret, ret,
} }
} }
(CoalesceWhitespaceEscaped::RequiresMutationEscaping { mut ret }, c) (
if escapable_characters(c) => CoalesceWhitespaceEscaped::RequiresMutationEscaping {
{ mut ret,
matched_escape_character: _,
},
c,
) if escapable_characters(c) => {
ret.push(c); ret.push(c);
CoalesceWhitespaceEscaped::RequiresMutation { CoalesceWhitespaceEscaped::RequiresMutation {
in_whitespace: false, in_whitespace: false,
@ -353,7 +358,10 @@ fn impl_coalesce_whitespace_escaped<'s, C: Fn(char) -> bool>(
} }
} }
( (
CoalesceWhitespaceEscaped::RequiresMutationEscaping { mut ret }, CoalesceWhitespaceEscaped::RequiresMutationEscaping {
mut ret,
matched_escape_character: _,
},
' ' | '\t' | '\r' | '\n', ' ' | '\t' | '\r' | '\n',
) => { ) => {
ret.push(' '); ret.push(' ');
@ -362,8 +370,14 @@ fn impl_coalesce_whitespace_escaped<'s, C: Fn(char) -> bool>(
ret, ret,
} }
} }
(CoalesceWhitespaceEscaped::RequiresMutationEscaping { mut ret }, c) => { (
ret.push(escape_character); CoalesceWhitespaceEscaped::RequiresMutationEscaping {
mut ret,
matched_escape_character,
},
c,
) => {
ret.push(matched_escape_character);
ret.push(c); ret.push(c);
// TODO // TODO
CoalesceWhitespaceEscaped::RequiresMutation { CoalesceWhitespaceEscaped::RequiresMutation {
@ -373,19 +387,85 @@ fn impl_coalesce_whitespace_escaped<'s, C: Fn(char) -> bool>(
} }
} }
} }
// match state { match state {
// CoalesceWhitespaceEscaped::Normal => Cow::Borrowed(input), CoalesceWhitespaceEscaped::Normal => Cow::Borrowed(input),
// CoalesceWhitespaceEscaped::RequiresMutation { CoalesceWhitespaceEscaped::NormalEscaping { escape_offset: _ } => Cow::Borrowed(input),
// in_whitespace: _, CoalesceWhitespaceEscaped::RequiresMutation {
// ret, in_whitespace: _,
// } => Cow::Owned(ret), ret,
// } } => Cow::Owned(ret),
todo!() CoalesceWhitespaceEscaped::RequiresMutationEscaping {
mut ret,
matched_escape_character,
} => {
ret.push(matched_escape_character);
Cow::Owned(ret)
}
}
} }
enum CoalesceWhitespaceEscaped { enum CoalesceWhitespaceEscaped {
Normal, Normal,
NormalEscaping { escape_offset: usize }, NormalEscaping {
RequiresMutation { in_whitespace: bool, ret: String }, escape_offset: usize,
RequiresMutationEscaping { ret: String }, },
RequiresMutation {
in_whitespace: bool,
ret: String,
},
RequiresMutationEscaping {
ret: String,
matched_escape_character: char,
},
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn coalesce_whitespace_escaped_default() -> Result<(), Box<dyn std::error::Error>> {
let input = "foobarbaz";
let output = coalesce_whitespace_escaped('&', |c| "".contains(c))(input);
assert_eq!(output, "foobarbaz");
assert!(matches!(output, Cow::Borrowed(_)));
Ok(())
}
#[test]
fn coalesce_whitespace_escaped_whitespace_single() -> Result<(), Box<dyn std::error::Error>> {
let input = "foo bar baz";
let output = coalesce_whitespace_escaped('&', |c| "".contains(c))(input);
assert_eq!(output, "foo bar baz");
// TODO: Technically this should be a Borrowed but to keep the code simple for now we are treating all whitespace as causing ownership.
assert!(matches!(output, Cow::Owned(_)));
Ok(())
}
#[test]
fn coalesce_whitespace_escaped_whitespace_double() -> Result<(), Box<dyn std::error::Error>> {
let input = "foo bar baz";
let output = coalesce_whitespace_escaped('&', |c| "".contains(c))(input);
assert_eq!(output, "foo bar baz");
assert!(matches!(output, Cow::Owned(_)));
Ok(())
}
#[test]
fn coalesce_whitespace_escaped_escape_match() -> Result<(), Box<dyn std::error::Error>> {
let input = "foo &bar baz";
let output = coalesce_whitespace_escaped('&', |c| "b".contains(c))(input);
assert_eq!(output, "foo bar baz");
assert!(matches!(output, Cow::Owned(_)));
Ok(())
}
#[test]
fn coalesce_whitespace_escaped_escape_mismatch() -> Result<(), Box<dyn std::error::Error>> {
let input = "foo b&ar baz";
let output = coalesce_whitespace_escaped('&', |c| "b".contains(c))(input);
assert_eq!(output, "foo b&ar baz");
assert!(matches!(output, Cow::Owned(_)));
Ok(())
}
} }