From 2732a925e309d02660f9560e7aa17cd54db39197 Mon Sep 17 00:00:00 2001 From: Gregory Date: Wed, 10 Feb 2021 21:57:32 -0500 Subject: [PATCH] Move tests to replacer too --- src/input.rs | 62 -------------------------------------------- src/replacer.rs | 68 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 63 deletions(-) diff --git a/src/input.rs b/src/input.rs index 2f58fa9..c7a1484 100644 --- a/src/input.rs +++ b/src/input.rs @@ -82,65 +82,3 @@ impl App { } } } - -#[cfg(test)] -mod tests { - use super::*; - - fn replace<'a>( - look_for: impl Into, - replace_with: impl Into, - 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"); - } -} diff --git a/src/replacer.rs b/src/replacer.rs index 367de0c..1ecb1a4 100644 --- a/src/replacer.rs +++ b/src/replacer.rs @@ -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, + replace_with: impl Into, + 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"); + } +} +