RegexReplace.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //
  2. // RegexReplace.cs
  3. //
  4. // Author:
  5. // Raja R Harinath <[email protected]>
  6. //
  7. // (C) 2005, Novell Inc.
  8. using System;
  9. using System.Text.RegularExpressions;
  10. using NUnit.Framework;
  11. namespace MonoTests.System.Text.RegularExpressions {
  12. [TestFixture]
  13. public class RegexReplaceTest {
  14. struct testcase {
  15. public string original, pattern, replacement, expected;
  16. public testcase (string o, string p, string r, string e)
  17. {
  18. original = o;
  19. pattern = p;
  20. replacement = r;
  21. expected = e;
  22. }
  23. }
  24. static testcase [] tests = {
  25. // original pattern replacement expected
  26. new testcase ("text", "x", "y", "teyt" ),
  27. new testcase ("text", "x", "$", "te$t" ),
  28. new testcase ("text", "x", "$1", "te$1t" ),
  29. new testcase ("text", "x", "${1}", "te${1}t" ),
  30. new testcase ("text", "x", "$5", "te$5t" ),
  31. new testcase ("te(x)t", "x", "$5", "te($5)t" ),
  32. new testcase ("text", "x", "${5", "te${5t" ),
  33. new testcase ("text", "x", "${foo", "te${foot" ),
  34. new testcase ("text", "(x)", "$5", "te$5t" ),
  35. new testcase ("text", "(x)", "$1", "text" ),
  36. new testcase ("text", "e(x)", "$1", "txt" ),
  37. new testcase ("text", "e(x)", "$5", "t$5t" ),
  38. new testcase ("text", "e(x)", "$4", "t$4t" ),
  39. new testcase ("text", "e(x)", "$3", "t$3t" ),
  40. new testcase ("text", "e(x)", "${1}", "txt" ),
  41. new testcase ("text", "e(x)", "${3}", "t${3}t" ),
  42. new testcase ("text", "e(x)", "${1}${3}", "tx${3}t" ),
  43. new testcase ("text", "e(x)", "${1}${name}", "tx${name}t" ),
  44. new testcase ("text", "e(?<foo>x)", "${1}${name}", "tx${name}t" ),
  45. new testcase ("text", "e(?<foo>x)", "${1}${foo}", "txxt" ),
  46. new testcase ("text", "e(?<foo>x)", "${goll}${foo}", "t${goll}xt" ),
  47. new testcase ("text", "e(?<foo>x)", "${goll${foo}", "t${gollxt" ),
  48. new testcase ("text", "e(?<foo>x)", "${goll${foo}}", "t${gollx}t" ),
  49. new testcase ("text", "e(?<foo>x)", "$${foo}}", "t${foo}}t" ),
  50. new testcase ("text", "e(?<foo>x)", "${${foo}}", "t${x}t" ),
  51. new testcase ("text", "e(?<foo>x)", "$${foo}}", "t${foo}}t" ),
  52. new testcase ("text", "e(?<foo>x)", "$${bfoo}}", "t${bfoo}}t" ),
  53. new testcase ("text", "e(?<foo>x)", "$${foo}}", "t${foo}}t" ),
  54. new testcase ("text", "e(?<foo>x)", "$${foo}", "t${foo}t" ),
  55. new testcase ("text", "e(?<foo>x)", "$$", "t$t" ),
  56. new testcase ("text", "(?<foo>e)(?<foo>x)", "${foo}$1$2", "txx$2t" ),
  57. new testcase ("text", "(e)(?<foo>x)", "${foo}$1$2", "txext" ),
  58. new testcase ("text", "(?<foo>e)(x)", "${foo}$1$2", "texet" ),
  59. new testcase ("text", "(e)(?<foo>x)", "${foo}$1$2$+", "txexxt" ),
  60. new testcase ("text", "(?<foo>e)(x)", "${foo}$1$2$+", "texeet" ),
  61. new testcase ("314 1592 65358", @"\d\d\d\d|\d\d\d", "a", "a a a8" ),
  62. new testcase ("2 314 1592 65358", @"\d\d\d\d|\d\d\d", "a", "2 a a a8" ),
  63. new testcase ("<i>am not</i>", "<(.+?)>", "[$0:$1]", "[<i>:i]am not[</i>:/i]"),
  64. };
  65. [Test]
  66. public void ReplaceTests ()
  67. {
  68. string result;
  69. int i = 0;
  70. foreach (testcase test in tests) {
  71. try {
  72. result = Regex.Replace (test.original, test.pattern, test.replacement);
  73. Assert.AreEqual (result, test.expected, "rr#{0}: {1} ~ s,{2},{3},", i,
  74. test.original, test.pattern, test.replacement);
  75. } catch (Exception e) {
  76. Assert.Fail ("rr#{0}: Exception thrown", i);
  77. }
  78. ++i;
  79. }
  80. }
  81. }
  82. }