5 // Raja R Harinath <rharinath@novell.com>
7 // (C) 2005, Novell Inc.
10 using System.Text.RegularExpressions;
12 using NUnit.Framework;
14 namespace MonoTests.System.Text.RegularExpressions {
17 public class RegexReplaceTest {
25 public string original, pattern, replacement, expected;
26 public direction direction;
27 public testcase (string o, string p, string r, string e, direction d)
35 public testcase (string o, string p, string r, string e) : this (o, p, r, e, direction.Both) {}
38 static testcase [] tests = {
39 // original pattern replacement expected
40 new testcase ("text", "x", "y", "teyt" ),
41 new testcase ("text", "x", "$", "te$t" ),
42 new testcase ("text", "x", "$1", "te$1t" ),
43 new testcase ("text", "x", "${1}", "te${1}t" ),
44 new testcase ("text", "x", "$5", "te$5t" ),
45 new testcase ("te(x)t", "x", "$5", "te($5)t" ),
46 new testcase ("text", "x", "${5", "te${5t" ),
47 new testcase ("text", "x", "${foo", "te${foot" ),
48 new testcase ("text", "(x)", "$5", "te$5t" ),
49 new testcase ("text", "(x)", "$1", "text" ),
50 new testcase ("text", "e(x)", "$1", "txt" ),
51 new testcase ("text", "e(x)", "$5", "t$5t" ),
52 new testcase ("text", "e(x)", "$4", "t$4t" ),
53 new testcase ("text", "e(x)", "$3", "t$3t" ),
54 new testcase ("text", "e(x)", "${1}", "txt" ),
55 new testcase ("text", "e(x)", "${3}", "t${3}t" ),
56 new testcase ("text", "e(x)", "${1}${3}", "tx${3}t" ),
57 new testcase ("text", "e(x)", "${1}${name}", "tx${name}t" ),
58 new testcase ("text", "e(?<foo>x)", "${1}${name}", "tx${name}t" ),
59 new testcase ("text", "e(?<foo>x)", "${1}${foo}", "txxt" ),
60 new testcase ("text", "e(?<foo>x)", "${goll}${foo}", "t${goll}xt" ),
61 new testcase ("text", "e(?<foo>x)", "${goll${foo}", "t${gollxt" ),
62 new testcase ("text", "e(?<foo>x)", "${goll${foo}}", "t${gollx}t" ),
63 new testcase ("text", "e(?<foo>x)", "$${foo}}", "t${foo}}t" ),
64 new testcase ("text", "e(?<foo>x)", "${${foo}}", "t${x}t" ),
65 new testcase ("text", "e(?<foo>x)", "$${foo}}", "t${foo}}t" ),
66 new testcase ("text", "e(?<foo>x)", "$${bfoo}}", "t${bfoo}}t" ),
67 new testcase ("text", "e(?<foo>x)", "$${foo}}", "t${foo}}t" ),
68 new testcase ("text", "e(?<foo>x)", "$${foo}", "t${foo}t" ),
69 new testcase ("text", "e(?<foo>x)", "$$", "t$t" ),
70 new testcase ("text", "(?<foo>e)(?<foo>x)", "${foo}$1$2", "txx$2t", direction.LTR),
71 new testcase ("text", "(?<foo>e)(?<foo>x)", "${foo}$1$2", "tee$2t", direction.RTL),
72 new testcase ("text", "(e)(?<foo>x)", "${foo}$1$2", "txext" ),
73 new testcase ("text", "(?<foo>e)(x)", "${foo}$1$2", "texet" ),
74 new testcase ("text", "(e)(?<foo>x)", "${foo}$1$2$+", "txexxt" ),
75 new testcase ("text", "(?<foo>e)(x)", "${foo}$1$2$+", "texeet" ),
76 new testcase ("314 1592 65358", @"\d\d\d\d|\d\d\d", "a", "a a a8", direction.LTR),
77 new testcase ("314 1592 65358", @"\d\d\d\d|\d\d\d", "a", "a a 6a", direction.RTL),
78 new testcase ("2 314 1592 65358", @"\d\d\d\d|\d\d\d", "a", "2 a a a8", direction.LTR),
79 new testcase ("2 314 1592 65358", @"\d\d\d\d|\d\d\d", "a", "2 a a 6a", direction.RTL),
80 new testcase ("<i>am not</i>", "<(.+?)>", "[$0:$1]", "[<i>:i]am not[</i>:/i]"),
81 new testcase ("texts", "(?<foo>e)(x)", "${foo}$1$2$_", "texetextsts" ),
82 new testcase ("texts", "(?<foo>e)(x)", "${foo}$1$2$`", "texetts" ),
83 new testcase ("texts", "(?<foo>e)(x)", "${foo}$1$2$'", "texetsts" ),
84 new testcase ("texts", "(?<foo>e)(x)", "${foo}$1$2$&", "texeexts" ),
85 //new testcase ("F2345678910L71", @"(F)(2)(3)(4)(5)(6)(?<S>7)(8)(9)(10)(L)\11", "${S}$11$1", "77F1" ),
86 new testcase ("F2345678910L71", @"(F)(2)(3)(4)(5)(6)(7)(8)(9)(10)(L)\11", "${S}$11$1", "F2345678910L71" ),
87 new testcase ("F2345678910LL1", @"(F)(2)(3)(4)(5)(6)(7)(8)(9)(10)(L)\11", "${S}$11$1", "${S}LF1", /*FIXME: this should work in RTL direction too */ direction.LTR),
88 new testcase ("a", "a", @"\\", @"\\"), // bug #317092
89 new testcase ("a", "^", "x", "xa"), // bug #324390
93 public void ReplaceTests ()
97 foreach (testcase test in tests) {
98 if ((test.direction & direction.LTR) == direction.LTR) {
100 result = Regex.Replace (test.original, test.pattern, test.replacement);
101 } catch (Exception e) {
102 Assert.Fail ("rr#{0}ltr Exception thrown: " + e.ToString (), i);
104 Assert.AreEqual (test.expected, result, "rr#{0}ltr: {1} ~ s,{2},{3},", i,
105 test.original, test.pattern, test.replacement);
108 if ((test.direction & direction.RTL) == direction.RTL) {
110 result = Regex.Replace (test.original, test.pattern, test.replacement, RegexOptions.RightToLeft);
111 } catch (Exception e) {
112 Assert.Fail ("rr#{0}rtl Exception thrown: " + e.ToString (), i);
114 Assert.AreEqual (test.expected, result, "rr#{0}rtl: {1} ~ s,{2},{3},", i,
115 test.original, test.pattern, test.replacement);
121 static string substitute (Match m)
123 switch (m.Value.Substring(2, m.Length - 3)) {
133 static string resolve (string val, Regex r)
135 MatchEvaluator ev = new MatchEvaluator (substitute);
136 while (r.IsMatch (val))
137 val = r.Replace (val, ev);
142 public void EvaluatorTests ()
144 string test = "@(foo) @(hello)";
145 string regex = "\\@\\([^\\)]*?\\)";
146 Assert.AreEqual ("bar world", resolve (test, new Regex (regex)), "ltr");
147 Assert.AreEqual ("bar world", resolve (test, new Regex (regex, RegexOptions.RightToLeft)), "rtl");