summaryrefslogtreecommitdiffstats
path: root/third_party/rust/regex/tests/replace.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /third_party/rust/regex/tests/replace.rs
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/regex/tests/replace.rs')
-rw-r--r--third_party/rust/regex/tests/replace.rs183
1 files changed, 183 insertions, 0 deletions
diff --git a/third_party/rust/regex/tests/replace.rs b/third_party/rust/regex/tests/replace.rs
new file mode 100644
index 0000000000..f26ae46030
--- /dev/null
+++ b/third_party/rust/regex/tests/replace.rs
@@ -0,0 +1,183 @@
+macro_rules! replace(
+ ($name:ident, $which:ident, $re:expr,
+ $search:expr, $replace:expr, $result:expr) => (
+ #[test]
+ fn $name() {
+ let re = regex::Regex::new($re).unwrap();
+ assert_eq!(re.$which($search, $replace), $result);
+ }
+ );
+);
+
+replace!(first, replace, r"[0-9]", "age: 26", "Z", "age: Z6");
+replace!(plus, replace, r"[0-9]+", "age: 26", "Z", "age: Z");
+replace!(all, replace_all, r"[0-9]", "age: 26", "Z", "age: ZZ");
+replace!(groups, replace, r"([^ ]+)[ ]+([^ ]+)", "w1 w2", "$2 $1", "w2 w1");
+replace!(
+ double_dollar,
+ replace,
+ r"([^ ]+)[ ]+([^ ]+)",
+ "w1 w2",
+ "$2 $$1",
+ "w2 $1"
+);
+// replace!(adjacent_index, replace,
+// r"([^aeiouy])ies$", "skies", "$1y", "sky");
+replace!(
+ named,
+ replace_all,
+ r"(?P<first>[^ ]+)[ ]+(?P<last>[^ ]+)(?P<space>[ ]*)",
+ "w1 w2 w3 w4",
+ "$last $first$space",
+ "w2 w1 w4 w3"
+);
+replace!(
+ trim,
+ replace_all,
+ "^[ \t]+|[ \t]+$",
+ " \t trim me\t \t",
+ "",
+ "trim me"
+);
+replace!(number_hyphen, replace, r"(.)(.)", "ab", "$1-$2", "a-b");
+// replace!(number_underscore, replace, r"(.)(.)", "ab", "$1_$2", "a_b");
+replace!(
+ simple_expand,
+ replace_all,
+ r"([a-z]) ([a-z])",
+ "a b",
+ "$2 $1",
+ "b a"
+);
+replace!(
+ literal_dollar1,
+ replace_all,
+ r"([a-z]+) ([a-z]+)",
+ "a b",
+ "$$1",
+ "$1"
+);
+replace!(
+ literal_dollar2,
+ replace_all,
+ r"([a-z]+) ([a-z]+)",
+ "a b",
+ "$2 $$c $1",
+ "b $c a"
+);
+replace!(
+ no_expand1,
+ replace,
+ r"([^ ]+)[ ]+([^ ]+)",
+ "w1 w2",
+ regex::NoExpand("$2 $1"),
+ "$2 $1"
+);
+replace!(
+ no_expand2,
+ replace,
+ r"([^ ]+)[ ]+([^ ]+)",
+ "w1 w2",
+ regex::NoExpand("$$1"),
+ "$$1"
+);
+replace!(
+ closure_returning_reference,
+ replace,
+ r"([0-9]+)",
+ "age: 26",
+ |captures: &regex::Captures<'_>| { captures[1][0..1].to_owned() },
+ "age: 2"
+);
+replace!(
+ closure_returning_value,
+ replace,
+ r"[0-9]+",
+ "age: 26",
+ |_captures: &regex::Captures<'_>| "Z".to_owned(),
+ "age: Z"
+);
+
+// See https://github.com/rust-lang/regex/issues/314
+replace!(
+ match_at_start_replace_with_empty,
+ replace_all,
+ r"foo",
+ "foobar",
+ "",
+ "bar"
+);
+
+// See https://github.com/rust-lang/regex/issues/393
+replace!(single_empty_match, replace, r"^", "bar", "foo", "foobar");
+
+// See https://github.com/rust-lang/regex/issues/399
+replace!(
+ capture_longest_possible_name,
+ replace_all,
+ r"(.)",
+ "b",
+ "${1}a $1a",
+ "ba "
+);
+
+replace!(
+ impl_string,
+ replace,
+ r"[0-9]",
+ "age: 26",
+ "Z".to_string(),
+ "age: Z6"
+);
+replace!(
+ impl_string_ref,
+ replace,
+ r"[0-9]",
+ "age: 26",
+ &"Z".to_string(),
+ "age: Z6"
+);
+replace!(
+ impl_cow_str_borrowed,
+ replace,
+ r"[0-9]",
+ "age: 26",
+ std::borrow::Cow::<'_, str>::Borrowed("Z"),
+ "age: Z6"
+);
+replace!(
+ impl_cow_str_borrowed_ref,
+ replace,
+ r"[0-9]",
+ "age: 26",
+ &std::borrow::Cow::<'_, str>::Borrowed("Z"),
+ "age: Z6"
+);
+replace!(
+ impl_cow_str_owned,
+ replace,
+ r"[0-9]",
+ "age: 26",
+ std::borrow::Cow::<'_, str>::Owned("Z".to_string()),
+ "age: Z6"
+);
+replace!(
+ impl_cow_str_owned_ref,
+ replace,
+ r"[0-9]",
+ "age: 26",
+ &std::borrow::Cow::<'_, str>::Owned("Z".to_string()),
+ "age: Z6"
+);
+
+#[test]
+fn replacen_no_captures() {
+ let re = regex::Regex::new(r"[0-9]").unwrap();
+ assert_eq!(re.replacen("age: 1234", 2, "Z"), "age: ZZ34");
+}
+
+#[test]
+fn replacen_with_captures() {
+ let re = regex::Regex::new(r"([0-9])").unwrap();
+ assert_eq!(re.replacen("age: 1234", 2, "${1}Z"), "age: 1Z2Z34");
+}