/src/System.Text.RegularExpressions/tests/RegexMatchValueTests.cs

https://github.com/EricWhiteDev/corefx · C# · 226 lines · 183 code · 25 blank · 18 comment · 31 complexity · 66c2e28e50c3e65cdc2756db5c4dba7a MD5 · raw file

  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Text.RegularExpressions;
  6. using Xunit;
  7. public class RegexMatchValueTests
  8. {
  9. [Fact]
  10. public static void MatchValue()
  11. {
  12. //////////// Global Variables used for all tests
  13. int iCountErrors = 0;
  14. int iCountTestcases = 0;
  15. String strLoc = "Loc_000oo";
  16. Regex rgx1;
  17. Match mtch1;
  18. String strInput;
  19. String strExpected;
  20. GroupCollection grpc1;
  21. CaptureCollection capc1;
  22. String[] arrGroupExp =
  23. {
  24. "aaabbcccccccccc", "aaa", "bb", "cccccccccc"
  25. }
  26. ;
  27. String[] arrGroupExp1 =
  28. {
  29. "abracadabra", "abra", "cad"
  30. }
  31. ;
  32. String[] arrCaptureExp =
  33. {
  34. "aaabbcccccccccc", "aaa", "bb", "cccccccccc"
  35. }
  36. ;
  37. String[] arrCaptureExp1 =
  38. {
  39. "abracad", "abra"
  40. }
  41. ;
  42. try
  43. {
  44. ///////////////////////// START TESTS ////////////////////////////
  45. ///////////////////////////////////////////////////////////////////
  46. //[] We are testing the Value property of Match, Group and Capture here. Value is the more semantic equivalent of
  47. //the current ToString
  48. //trim leading and trailing white spaces
  49. //my answer to the csharp alias, Regex.Replace(strInput, @"\s*(.*?)\s*$", "${1}") works fine, Even Freidl gives it
  50. //a solution, albeit not a very fast one
  51. iCountTestcases++;
  52. rgx1 = new Regex(@"\s*(.*?)\s*$");
  53. strInput = " Hello World ";
  54. mtch1 = rgx1.Match(strInput);
  55. if (mtch1.Success)
  56. {
  57. strExpected = strInput;
  58. if (!strExpected.Equals(mtch1.Value))
  59. {
  60. iCountErrors++;
  61. Console.WriteLine("Err_759ed! The expected value was not returned, Expected - {0} Returned - {1}", strExpected, mtch1.Value);
  62. }
  63. }
  64. //[]for Groups and Captures
  65. iCountTestcases++;
  66. rgx1 = new Regex(@"(?<A1>a*)(?<A2>b*)(?<A3>c*)");
  67. strInput = "aaabbccccccccccaaaabc";
  68. mtch1 = rgx1.Match(strInput);
  69. if (mtch1.Success)
  70. {
  71. strExpected = "aaabbcccccccccc";
  72. if (!strExpected.Equals(mtch1.Value))
  73. {
  74. iCountErrors++;
  75. Console.WriteLine("Err_745fg! The expected value was not returned, Expected - {0} Returned - {1}", strExpected, mtch1.Value);
  76. }
  77. grpc1 = mtch1.Groups;
  78. if (grpc1.Count != 4)
  79. {
  80. iCountErrors++;
  81. Console.WriteLine("Err_67fdaq! The expected value was not returned, Expected - 4 Returned - {0}", grpc1.Count);
  82. }
  83. for (int i = 0; i < grpc1.Count; i++)
  84. {
  85. if (!arrGroupExp[i].Equals(grpc1[i].Value))
  86. {
  87. iCountErrors++;
  88. Console.WriteLine("Err_00213534_{2}! The expected value was not returned, Expected - {0} Returned - {1}", arrGroupExp[i], grpc1[i].Value, i);
  89. }
  90. //Group has a Captures property too
  91. capc1 = grpc1[i].Captures;
  92. if (capc1.Count != 1)
  93. {
  94. iCountErrors++;
  95. Console.WriteLine("Err_0834esfd! The expected value was not returned, Expected - 1 Returned - {0}", capc1.Count);
  96. }
  97. if (!arrGroupExp[i].Equals(capc1[0].Value))
  98. {
  99. iCountErrors++;
  100. Console.WriteLine("Err_974535! The expected value was not returned, Expected - {0} Returned - {1}", arrGroupExp[i], capc1[0].Value);
  101. }
  102. }
  103. capc1 = mtch1.Captures;
  104. if (capc1.Count != 1)
  105. {
  106. iCountErrors++;
  107. Console.WriteLine("Err_863trfg! The expected value was not returned, Expected - 1 Returned - {0}", capc1.Count);
  108. }
  109. strExpected = "aaabbcccccccccc";
  110. if (!strExpected.Equals(capc1[0].Value))
  111. {
  112. iCountErrors++;
  113. Console.WriteLine("Err_97463dfsg! The expected value was not returned, Expected - {0} Returned - {1}", strExpected, capc1[0].Value);
  114. }
  115. }
  116. //Another example - given by Brad Merril in an article on RegularExpressions
  117. iCountTestcases++;
  118. rgx1 = new Regex(@"(abra(cad)?)+");
  119. strInput = "abracadabra1abracadabra2abracadabra3";
  120. mtch1 = rgx1.Match(strInput);
  121. while (mtch1.Success)
  122. {
  123. strExpected = "abracadabra";
  124. if (!strExpected.Equals(mtch1.Value))
  125. {
  126. iCountErrors++;
  127. Console.WriteLine("Err_7342wsdg! The expected value was not returned, Expected - {0} Returned - {1}", strExpected, mtch1.Value);
  128. }
  129. grpc1 = mtch1.Groups;
  130. if (grpc1.Count != 3)
  131. {
  132. iCountErrors++;
  133. Console.WriteLine("Err_9745fgg! The expected value was not returned, Expected - 4 Returned - {0}", grpc1.Count);
  134. }
  135. for (int i = 0; i < grpc1.Count; i++)
  136. {
  137. if (!arrGroupExp1[i].Equals(grpc1[i].Value))
  138. {
  139. iCountErrors++;
  140. Console.WriteLine("Err_8756fdg! The expected value was not returned, Expected - {0} Returned - {1}", arrGroupExp1[i], grpc1[i].Value, i);
  141. }
  142. //Group has a Captures property too
  143. capc1 = grpc1[i].Captures;
  144. switch (i)
  145. {
  146. case 1:
  147. if (capc1.Count != 2)
  148. {
  149. iCountErrors++;
  150. Console.WriteLine("Err_9745fgs! The expected value was not returned, Expected - 1 Returned - {0}", capc1.Count);
  151. }
  152. for (int j = 0; j < capc1.Count; j++)
  153. {
  154. if (!arrCaptureExp1[j].Equals(capc1[j].Value))
  155. {
  156. iCountErrors++;
  157. Console.WriteLine("Err_97453sdf! The expected value was not returned, Expected - {0} Returned - {1}", arrGroupExp[j], capc1[j].Value);
  158. }
  159. }
  160. break;
  161. case 2:
  162. if (capc1.Count != 1)
  163. {
  164. iCountErrors++;
  165. Console.WriteLine("Err_8473rsg! The expected value was not returned, Expected - 1 Returned - {0}", capc1.Count);
  166. }
  167. strExpected = "cad";
  168. if (!strExpected.Equals(capc1[0].Value))
  169. {
  170. iCountErrors++;
  171. Console.WriteLine("Err_9743rfsfg! The expected value was not returned, Expected - {0} Returned - {1}", strExpected, capc1[0].Value);
  172. }
  173. break;
  174. }
  175. }
  176. capc1 = mtch1.Captures;
  177. if (capc1.Count != 1)
  178. {
  179. iCountErrors++;
  180. Console.WriteLine("Err_7453fgds! The expected value was not returned, Expected - 1 Returned - {0}", capc1.Count);
  181. }
  182. strExpected = "abracadabra";
  183. if (!strExpected.Equals(capc1[0].Value))
  184. {
  185. iCountErrors++;
  186. Console.WriteLine("Err_0753rdg! The expected value was not returned, Expected - {0} Returned - {1}", strExpected, capc1[0].Value);
  187. }
  188. mtch1 = mtch1.NextMatch();
  189. }
  190. ///////////////////////////////////////////////////////////////////
  191. /////////////////////////// END TESTS /////////////////////////////
  192. }
  193. catch (Exception exc_general)
  194. {
  195. ++iCountErrors;
  196. Console.WriteLine("Error Err_8888yyy! strLoc==" + strLoc + ", exc_general==\n" + exc_general.ToString());
  197. }
  198. //// Finish Diagnostics
  199. Assert.Equal(0, iCountErrors);
  200. }
  201. }