RegexTest.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // assembly: System_test
  3. // namespace: MonoTests.System.Text.RegularExpressions
  4. // file: RegexTest.cs
  5. //
  6. // Authors:
  7. // Juraj Skripsky ([email protected])
  8. //
  9. // (c) 2003 Juraj Skripsky
  10. using System;
  11. using System.Text.RegularExpressions;
  12. using NUnit.Framework;
  13. namespace MonoTests.System.Text.RegularExpressions {
  14. [TestFixture]
  15. public class RegexTest {
  16. [Test]
  17. public void Simple ()
  18. {
  19. char[] c = { (char)32, (char)8212, (char)32 };
  20. string s = new String(c);
  21. Assert.IsTrue (Regex.IsMatch(s, s), "char");
  22. }
  23. [Test]
  24. public void Unescape ()
  25. {
  26. string inString = @"\a\b\t\r\v\f\n\e\02400\x231\cC\ufffff\*";
  27. char [] c = { (char)7, (char)8, (char)9, (char)13,
  28. (char)11, (char)12, (char)10, (char)27, (char) 20,
  29. (char)48, (char)48, (char)35, (char)49,
  30. (char)3, (char)65535, (char)102, (char)42
  31. };
  32. string expectedString = new String(c);
  33. string outString = Regex.Unescape(inString);
  34. Assert.AreEqual (outString, expectedString, "unescape");
  35. }
  36. [Test]
  37. public void Match1 ()
  38. {
  39. Regex email = new Regex ("(?<user>[^@]+)@(?<domain>.+)");
  40. Match m;
  41. m = email.Match ("[email protected]");
  42. Assert.IsTrue (m.Success, "#m01");
  43. Assert.AreEqual ("mono", m.Groups ["user"].Value, "#m02");
  44. Assert.AreEqual ("go-mono.com", m.Groups ["domain"].Value, "#m03");
  45. m = email.Match ("[email protected]");
  46. Assert.IsTrue (m.Success, "m04");
  47. Assert.AreEqual ("mono.bugs", m.Groups ["user"].Value, "#m05");
  48. Assert.AreEqual ("go-mono.com", m.Groups ["domain"].Value, "#m06");
  49. }
  50. static string story =
  51. "Two little dragons lived in the forest\n" +
  52. "They spent their days collecting honey suckle,\n" +
  53. "And eating curds and whey\n" +
  54. "Until an evil sorcer came along\n" +
  55. "And chased my dragon friends away";
  56. struct MatchCollectionTrial {
  57. public readonly string name;
  58. public readonly string text;
  59. public readonly string regex;
  60. public readonly string [] matches;
  61. public MatchCollectionTrial (string name, string text, string regex, string [] matches)
  62. {
  63. this.name = name;
  64. this.text = text;
  65. this.regex = regex;
  66. this.matches = matches;
  67. }
  68. }
  69. static readonly MatchCollectionTrial [] trials = {
  70. new MatchCollectionTrial ("word", "the fat cat ate the rat", "(?<word>\\w+)",
  71. new string [] { "the", "fat", "cat", "ate", "the", "rat" }),
  72. new MatchCollectionTrial ("digit", "0 1 2 3 4 5 6a7b8c9d10", "(?<digit>\\d+)",
  73. new string [] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }),
  74. new MatchCollectionTrial ("line", story, "(?<line>.+)",
  75. new string [] { "Two little dragons lived in the forest",
  76. "They spent their days collecting honey suckle,",
  77. "And eating curds and whey",
  78. "Until an evil sorcer came along",
  79. "And chased my dragon friends away" }),
  80. new MatchCollectionTrial ("nonwhite", "ab 12 cde 456 fghi .,\niou", "(?<nonwhite>\\S+)",
  81. new string [] { "ab", "12", "cde", "456", "fghi", ".,", "iou" }),
  82. new MatchCollectionTrial ("nondigit", "ab0cd1ef2", "(?<nondigit>\\D+)",
  83. new string [] { "ab", "cd", "ef" })
  84. };
  85. static void runTrial (MatchCollectionTrial t)
  86. {
  87. runTrial (t, false);
  88. runTrial (t, true);
  89. }
  90. static void runTrial (MatchCollectionTrial t, bool rtl)
  91. {
  92. int i;
  93. MatchCollection mc;
  94. string name = t.name;
  95. if (rtl)
  96. name += "-rtl";
  97. int len = t.matches.Length;
  98. Regex r = new Regex (t.regex, rtl ? RegexOptions.RightToLeft : RegexOptions.None);
  99. // Incremental mode -- this access
  100. mc = r.Matches (t.text);
  101. for (i = 0; i < len; ++i)
  102. Assert.AreEqual (mc [i].Value, t.matches [rtl ? len - i - 1 : i], "{0}:this:{1}", name, i);
  103. Assert.AreEqual (i, mc.Count, "{0}:this:count", name);
  104. // Incremental mode -- enumerator
  105. mc = r.Matches (t.text);
  106. i = 0;
  107. foreach (Match m in mc) {
  108. Assert.AreEqual (m.Value, t.matches [rtl ? len - i - 1 : i], "{0}:enum:{1}", name, i);
  109. ++i;
  110. }
  111. Assert.AreEqual (i, len, "{0}:enum:count", name);
  112. // random mode
  113. Random rng = new Random ();
  114. for (int j = 0; j < len * 5; ++j) {
  115. i = rng.Next (len);
  116. Assert.AreEqual (mc [i].Value, t.matches [rtl ? len - i - 1 : i], "{0}:random{1}:{2}", name, j, i);
  117. }
  118. // Non-incremental mode
  119. mc = r.Matches (t.text);
  120. Assert.AreEqual (mc.Count, len);
  121. i = 0;
  122. foreach (Match m in mc) {
  123. Assert.AreEqual (m.Value, t.matches [rtl ? len - i - 1 : i], "{0}:nienum:{1}", name, i);
  124. ++i;
  125. }
  126. for (i = 0; i < len; ++i)
  127. Assert.AreEqual (mc [i].Value, t.matches [rtl ? len - i - 1 : i], "{0}:nithis:{1}", name, i);
  128. }
  129. [Test]
  130. public void Matches ()
  131. {
  132. int i;
  133. MatchCollection mc;
  134. Regex r;
  135. foreach (MatchCollectionTrial t in trials)
  136. runTrial (t);
  137. }
  138. }
  139. }