Merge pull request #2396 from akoeplinger/flaky-osx-socket-test
[mono.git] / mcs / class / System / Test / System.Text.RegularExpressions / RegexReplace.cs
1 //
2 // RegexReplace.cs
3 //
4 // Author:
5 //      Raja R Harinath <rharinath@novell.com>
6 //
7 // (C) 2005, Novell Inc.
8
9 using System;
10 using System.Text.RegularExpressions;
11
12 using NUnit.Framework;
13
14 namespace MonoTests.System.Text.RegularExpressions {
15
16         [TestFixture]
17         public class RegexReplaceTest {
18                 [Flags]
19                 enum direction {
20                         LTR = 1,
21                         RTL = 2,
22                         Both = 3
23                 }
24                 struct testcase {
25                         public string original, pattern, replacement, expected;
26                         public direction direction;
27                         public testcase (string o, string p, string r, string e, direction d)
28                         {
29                                 original = o;
30                                 pattern = p;
31                                 replacement = r;
32                                 expected = e;
33                                 direction = d;
34                         }
35                         public testcase (string o, string p, string r, string e) : this (o, p, r, e, direction.Both) {}
36                 }
37
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
90                 };
91
92                 [Test]
93                 public void ReplaceTests ()
94                 {
95                         string result = "";
96                         int i = 0;
97                         foreach (testcase test in tests) {
98                                 if ((test.direction & direction.LTR) == direction.LTR) {
99                                         try {
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);
103                                         }
104                                         Assert.AreEqual (test.expected, result, "rr#{0}ltr: {1} ~ s,{2},{3},", i,
105                                                          test.original, test.pattern, test.replacement);
106                                 }
107
108                                 if ((test.direction & direction.RTL) == direction.RTL) {
109                                         try {
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);
113                                         }
114                                         Assert.AreEqual (test.expected, result, "rr#{0}rtl: {1} ~ s,{2},{3},", i,
115                                                          test.original, test.pattern, test.replacement);
116                                 }
117                                 ++i;
118                         }
119                 }
120
121                 static string substitute (Match m)
122                 {
123                         switch (m.Value.Substring(2, m.Length - 3)) {
124                         case "foo": return "bar";
125                         case "hello": return "world";
126                         default: return "ERROR";
127                         }
128                 }
129
130                 static string resolve (string val, Regex r)
131                 {
132                         MatchEvaluator ev = new MatchEvaluator (substitute);
133                         while (r.IsMatch (val))
134                                 val = r.Replace (val, ev);
135                         return val;
136                 }
137
138                 [Test] // #321036
139                 public void EvaluatorTests ()
140                 {
141                         string test = "@(foo) @(hello)";
142                         string regex = "\\@\\([^\\)]*?\\)";
143                         Assert.AreEqual ("bar world", resolve (test, new Regex (regex)), "ltr");
144                         Assert.AreEqual ("bar world", resolve (test, new Regex (regex, RegexOptions.RightToLeft)), "rtl");
145                 }
146         }
147 }