Clean up formatting
[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                 struct testcase { 
19                         public string original, pattern, replacement, expected;
20                         public testcase (string o, string p, string r, string e)
21                         {
22                                 original = o;
23                                 pattern = p;
24                                 replacement = r;
25                                 expected = e;
26                         }
27                 }
28                 
29                 static testcase [] tests = {
30                         //      original        pattern                 replacement             expected
31                         new testcase ("text",   "x",                    "y",                    "teyt"          ),
32                         new testcase ("text",   "x",                    "$",                    "te$t"          ),
33                         new testcase ("text",   "x",                    "$1",                   "te$1t"         ),
34                         new testcase ("text",   "x",                    "${1}",                 "te${1}t"       ),
35                         new testcase ("text",   "x",                    "$5",                   "te$5t"         ),
36                         new testcase ("te(x)t", "x",                    "$5",                   "te($5)t"       ),
37                         new testcase ("text",   "x",                    "${5",                  "te${5t"        ),
38                         new testcase ("text",   "x",                    "${foo",                "te${foot"      ),
39                         new testcase ("text",   "(x)",                  "$5",                   "te$5t"         ),
40                         new testcase ("text",   "(x)",                  "$1",                   "text"          ),
41                         new testcase ("text",   "e(x)",                 "$1",                   "txt"           ),
42                         new testcase ("text",   "e(x)",                 "$5",                   "t$5t"          ),
43                         new testcase ("text",   "e(x)",                 "$4",                   "t$4t"          ),
44                         new testcase ("text",   "e(x)",                 "$3",                   "t$3t"          ),
45                         new testcase ("text",   "e(x)",                 "${1}",                 "txt"           ),
46                         new testcase ("text",   "e(x)",                 "${3}",                 "t${3}t"        ),
47                         new testcase ("text",   "e(x)",                 "${1}${3}",             "tx${3}t"       ),
48                         new testcase ("text",   "e(x)",                 "${1}${name}",          "tx${name}t"    ),
49                         new testcase ("text",   "e(?<foo>x)",           "${1}${name}",          "tx${name}t"    ),
50                         new testcase ("text",   "e(?<foo>x)",           "${1}${foo}",           "txxt"          ),
51                         new testcase ("text",   "e(?<foo>x)",           "${goll}${foo}",        "t${goll}xt"    ),
52                         new testcase ("text",   "e(?<foo>x)",           "${goll${foo}",         "t${gollxt"     ),
53                         new testcase ("text",   "e(?<foo>x)",           "${goll${foo}}",        "t${gollx}t"    ),
54                         new testcase ("text",   "e(?<foo>x)",           "$${foo}}",             "t${foo}}t"     ),
55                         new testcase ("text",   "e(?<foo>x)",           "${${foo}}",            "t${x}t"        ),
56                         new testcase ("text",   "e(?<foo>x)",           "$${foo}}",             "t${foo}}t"     ),
57                         new testcase ("text",   "e(?<foo>x)",           "$${bfoo}}",            "t${bfoo}}t"    ),
58                         new testcase ("text",   "e(?<foo>x)",           "$${foo}}",             "t${foo}}t"     ),
59                         new testcase ("text",   "e(?<foo>x)",           "$${foo}",              "t${foo}t"      ),
60                         new testcase ("text",   "e(?<foo>x)",           "$$",                   "t$t"           ),
61                         new testcase ("text",   "(?<foo>e)(?<foo>x)",   "${foo}$1$2",           "txx$2t"        ),
62                         new testcase ("text",   "(e)(?<foo>x)",         "${foo}$1$2",           "txext"         ),
63                         new testcase ("text",   "(?<foo>e)(x)",         "${foo}$1$2",           "texet"         ),
64                         new testcase ("text",   "(e)(?<foo>x)",         "${foo}$1$2$+",         "txexxt"        ),
65                         new testcase ("text",   "(?<foo>e)(x)",         "${foo}$1$2$+",         "texeet"        ),
66                         new testcase ("314 1592 65358",         @"\d\d\d\d|\d\d\d", "a",        "a a a8"        ),
67                         new testcase ("2 314 1592 65358",       @"\d\d\d\d|\d\d\d", "a",        "2 a a a8"      ),
68                         new testcase ("<i>am not</i>",          "<(.+?)>",      "[$0:$1]",      "[<i>:i]am not[</i>:/i]"),
69                 };
70
71                 [Test]
72                 public void ReplaceTests ()
73                 {
74                         string result;
75                         int i = 0;
76                         foreach (testcase test in tests) {
77                                 try {
78                                         result = Regex.Replace (test.original, test.pattern, test.replacement);
79                                         Assert.AreEqual (result, test.expected, "rr#{0}: {1} ~ s,{2},{3},", i,
80                                                          test.original, test.pattern, test.replacement);
81                                 } catch (Exception e) {
82                                         Assert.Fail ("rr#{0}: Exception thrown", i);
83                                 }
84                                 ++i;
85                         }
86                 }
87         }
88 }