aboutsummaryrefslogtreecommitdiff
path: root/tests/replace.rs
diff options
context:
space:
mode:
authorChih-Hung Hsieh <chh@google.com>2020-04-16 10:44:21 -0700
committerChih-Hung Hsieh <chh@google.com>2020-04-16 10:45:53 -0700
commite42c505f54ac2e7b2ca7d0197304cac1b4f605e9 (patch)
tree7ca22ebdbdb7d0c9dc5c66d31f564aeaf51cece4 /tests/replace.rs
parent815d0a2344b5321fd47f0cb7dba96e4cb4e84615 (diff)
downloadregex-e42c505f54ac2e7b2ca7d0197304cac1b4f605e9.tar.gz
Import 'regex' package vesion 1.3.6
* Add OWNERS * No Android.bp yet Bug: 152884384 Test: make Change-Id: I455caf7833b6c437c1c133bc7b2f47b83da9cbce
Diffstat (limited to 'tests/replace.rs')
-rw-r--r--tests/replace.rs132
1 files changed, 132 insertions, 0 deletions
diff --git a/tests/replace.rs b/tests/replace.rs
new file mode 100644
index 0000000..c156a39
--- /dev/null
+++ b/tests/replace.rs
@@ -0,0 +1,132 @@
+macro_rules! replace(
+ ($name:ident, $which:ident, $re:expr,
+ $search:expr, $replace:expr, $result:expr) => (
+ #[test]
+ fn $name() {
+ let re = regex!($re);
+ assert_eq!(re.$which(text!($search), $replace), text!($result));
+ }
+ );
+);
+
+replace!(first, replace, r"[0-9]", "age: 26", t!("Z"), "age: Z6");
+replace!(plus, replace, r"[0-9]+", "age: 26", t!("Z"), "age: Z");
+replace!(all, replace_all, r"[0-9]", "age: 26", t!("Z"), "age: ZZ");
+replace!(
+ groups,
+ replace,
+ r"(?-u)(\S+)\s+(\S+)",
+ "w1 w2",
+ t!("$2 $1"),
+ "w2 w1"
+);
+replace!(
+ double_dollar,
+ replace,
+ r"(?-u)(\S+)\s+(\S+)",
+ "w1 w2",
+ t!("$2 $$1"),
+ "w2 $1"
+);
+// replace!(adjacent_index, replace,
+// r"([^aeiouy])ies$", "skies", t!("$1y"), "sky");
+replace!(
+ named,
+ replace_all,
+ r"(?-u)(?P<first>\S+)\s+(?P<last>\S+)(?P<space>\s*)",
+ "w1 w2 w3 w4",
+ t!("$last $first$space"),
+ "w2 w1 w4 w3"
+);
+replace!(
+ trim,
+ replace_all,
+ "^[ \t]+|[ \t]+$",
+ " \t trim me\t \t",
+ t!(""),
+ "trim me"
+);
+replace!(number_hypen, replace, r"(.)(.)", "ab", t!("$1-$2"), "a-b");
+// replace!(number_underscore, replace, r"(.)(.)", "ab", t!("$1_$2"), "a_b");
+replace!(
+ simple_expand,
+ replace_all,
+ r"(?-u)(\w) (\w)",
+ "a b",
+ t!("$2 $1"),
+ "b a"
+);
+replace!(
+ literal_dollar1,
+ replace_all,
+ r"(?-u)(\w+) (\w+)",
+ "a b",
+ t!("$$1"),
+ "$1"
+);
+replace!(
+ literal_dollar2,
+ replace_all,
+ r"(?-u)(\w+) (\w+)",
+ "a b",
+ t!("$2 $$c $1"),
+ "b $c a"
+);
+replace!(
+ no_expand1,
+ replace,
+ r"(?-u)(\S+)\s+(\S+)",
+ "w1 w2",
+ no_expand!("$2 $1"),
+ "$2 $1"
+);
+replace!(
+ no_expand2,
+ replace,
+ r"(?-u)(\S+)\s+(\S+)",
+ "w1 w2",
+ no_expand!("$$1"),
+ "$$1"
+);
+use_!(Captures);
+replace!(
+ closure_returning_reference,
+ replace,
+ r"([0-9]+)",
+ "age: 26",
+ |captures: &Captures| {
+ match_text!(captures.get(1).unwrap())[0..1].to_owned()
+ },
+ "age: 2"
+);
+replace!(
+ closure_returning_value,
+ replace,
+ r"[0-9]+",
+ "age: 26",
+ |_captures: &Captures| t!("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",
+ t!(""),
+ "bar"
+);
+
+// See https://github.com/rust-lang/regex/issues/393
+replace!(single_empty_match, replace, r"^", "bar", t!("foo"), "foobar");
+
+// See https://github.com/rust-lang/regex/issues/399
+replace!(
+ capture_longest_possible_name,
+ replace_all,
+ r"(.)",
+ "b",
+ t!("${1}a $1a"),
+ "ba "
+);