Move tests to replacer too

This commit is contained in:
Gregory 2021-02-10 21:57:32 -05:00
parent 675fc614f6
commit 2732a925e3
No known key found for this signature in database
GPG key ID: 2E44FAEEDC94B1E2
2 changed files with 67 additions and 63 deletions

View file

@ -82,65 +82,3 @@ impl App {
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn replace<'a>(
look_for: impl Into<String>,
replace_with: impl Into<String>,
literal: bool,
flags: Option<&'static str>,
src: &'static str,
target: &'static str,
) {
let replacer = Replacer::new(
look_for.into(),
replace_with.into(),
literal,
flags.map(ToOwned::to_owned),
)
.unwrap();
assert_eq!(
std::str::from_utf8(&replacer.replace(src.as_bytes())),
Ok(target)
);
}
#[test]
fn default_global() {
replace("a", "b", false, None, "aaa", "bbb");
}
#[test]
fn escaped_char_preservation() {
replace("a", "b", false, None, "a\\n", "b\\n");
}
#[test]
fn case_sensitive_default() {
replace("abc", "x", false, None, "abcABC", "xABC");
replace("abc", "x", true, None, "abcABC", "xABC");
}
#[test]
fn sanity_check_literal_replacements() {
replace("((special[]))", "x", true, None, "((special[]))y", "xy");
}
#[test]
fn unescape_regex_replacements() {
replace("test", r"\n", false, None, "testtest", "\n\n");
}
#[test]
fn no_unescape_literal_replacements() {
replace("test", r"\n", true, None, "testtest", r"\n\n");
}
#[test]
fn full_word_replace() {
replace("abc", "def", false, Some("w"), "abcd abc", "abcd def");
}
}

View file

@ -71,7 +71,10 @@ impl Replacer {
Ok(())
}
pub(crate) fn replace<'a>(&'a self, content: &'a [u8]) -> std::borrow::Cow<'a, [u8]> {
pub(crate) fn replace<'a>(
&'a self,
content: &'a [u8],
) -> std::borrow::Cow<'a, [u8]> {
if self.is_literal {
self.regex.replace_all(
&content,
@ -116,3 +119,66 @@ impl Replacer {
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
fn replace<'a>(
look_for: impl Into<String>,
replace_with: impl Into<String>,
literal: bool,
flags: Option<&'static str>,
src: &'static str,
target: &'static str,
) {
let replacer = Replacer::new(
look_for.into(),
replace_with.into(),
literal,
flags.map(ToOwned::to_owned),
)
.unwrap();
assert_eq!(
std::str::from_utf8(&replacer.replace(src.as_bytes())),
Ok(target)
);
}
#[test]
fn default_global() {
replace("a", "b", false, None, "aaa", "bbb");
}
#[test]
fn escaped_char_preservation() {
replace("a", "b", false, None, "a\\n", "b\\n");
}
#[test]
fn case_sensitive_default() {
replace("abc", "x", false, None, "abcABC", "xABC");
replace("abc", "x", true, None, "abcABC", "xABC");
}
#[test]
fn sanity_check_literal_replacements() {
replace("((special[]))", "x", true, None, "((special[]))y", "xy");
}
#[test]
fn unescape_regex_replacements() {
replace("test", r"\n", false, None, "testtest", "\n\n");
}
#[test]
fn no_unescape_literal_replacements() {
replace("test", r"\n", true, None, "testtest", r"\n\n");
}
#[test]
fn full_word_replace() {
replace("abc", "def", false, Some("w"), "abcd abc", "abcd def");
}
}