2005-02-27 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System / Test / System.Text.RegularExpressions / RegexBugs.cs
1 //
2 // MonoTests.System.Text.RegularExpressions misc. test cases
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (c) Copyright 2003,2004 Novell, Inc. (http://www.novell.com)
8 //
9
10 using NUnit.Framework;
11 using System;
12 using System.Text.RegularExpressions;
13
14 namespace MonoTests.System.Text.RegularExpressions
15 {
16         [TestFixture]
17         public class RegexBugs : Assertion
18         {
19                 [Test]
20                 public void SplitGroup () // bug51146
21                 {
22                         string [] splitResult = new Regex ("-").Split ("a-bcd-e-fg");
23                         string [] expected = new string [] {"a", "bcd", "e", "fg"};
24                         int length = expected.Length;
25                         int i;
26                         AssertEquals ("#01", length, splitResult.Length);
27                         for (i = 0; i < length; i++)
28                                 AssertEquals ("#02 " + i, expected [i], splitResult [i]);
29                         
30                         splitResult = new Regex ("(-)").Split ("a-bcd-e-fg");
31                         expected = new string [] {"a", "-", "bcd", "-", "e", "-", "fg"};
32                         length = expected.Length;
33                         AssertEquals ("#03", length, splitResult.Length);
34                         for (i = 0; i < length; i++)
35                                 AssertEquals ("#04 " + i, expected [i], splitResult [i]);
36
37                         splitResult = new Regex ("(-)b(c)").Split ("a-bcd-e-fg");
38                         expected = new string [] {"a", "-", "c", "d-e-fg" };
39                         length = expected.Length;
40                         AssertEquals ("#04", length, splitResult.Length);
41                         for (i = 0; i < length; i++)
42                                 AssertEquals ("#05 " + i, expected [i], splitResult [i]);
43                                 
44                         splitResult = new Regex ("-").Split ("a-bcd-e-fg-");
45                         expected = new string [] {"a", "bcd", "e", "fg", ""};
46                         length = expected.Length;
47                         AssertEquals ("#06", length, splitResult.Length);
48                         for (i = 0; i < length; i++)
49                                 AssertEquals ("#07 " + i, expected [i], splitResult [i]);
50                 }
51
52                 [Test]
53                 public void MathEmptyGroup () // bug 42529
54                 {
55                         string str = "Match something from here.";
56
57                         AssertEquals ("MEG #01", false, Regex.IsMatch(str, @"(something|dog)$"));
58                         AssertEquals ("MEG #02", true, Regex.IsMatch(str, @"(|something|dog)$"));
59                         AssertEquals ("MEG #03", true, Regex.IsMatch(str, @"(something||dog)$"));
60                         AssertEquals ("MEG #04", true, Regex.IsMatch(str, @"(something|dog|)$"));
61
62                         AssertEquals ("MEG #05", true, Regex.IsMatch(str, @"(something|dog)*"));
63                         AssertEquals ("MEG #06", true, Regex.IsMatch(str, @"(|something|dog)*"));
64                         AssertEquals ("MEG #07", true, Regex.IsMatch(str, @"(something||dog)*"));
65                         AssertEquals ("MEG #08", true, Regex.IsMatch(str, @"(something|dog|)*"));
66
67                         AssertEquals ("MEG #09", true, Regex.IsMatch(str, @"(something|dog)*$"));
68                         AssertEquals ("MEG #10", true, Regex.IsMatch(str, @"(|something|dog)*$"));
69                         AssertEquals ("MEG #11", true, Regex.IsMatch(str, @"(something||dog)*$"));
70                         AssertEquals ("MEG #12", true, Regex.IsMatch(str, @"(something|dog|)*$"));
71
72                 }
73
74                 [Test]
75                 public void Braces () // bug 52924
76                 {
77                         // Before the fix, the next line throws an exception
78                         Regex regVar = new Regex(@"{\w+}");
79                         Match m = regVar.Match ("{   }");
80                         AssertEquals ("BR #01", false, m.Success);
81                 }
82
83                 [Test]
84                 public void WhiteSpaceGroupped () // bug 71077
85                 {
86                         string s = "\n";
87                         string p = @"[\s\S]";   // =Category.Any
88
89                         AssertEquals ("WSG#1", true, Regex.IsMatch (s, p));
90                 }
91
92                 [Test]
93                 public void RangeIgnoreCase() // bug 45976
94                 {
95                         string str = "AAABBBBAAA" ;
96                         AssertEquals("RIC #01", true, Regex.IsMatch(str, @"[A-F]+", RegexOptions.IgnoreCase));
97                         AssertEquals("RIC #02", true, Regex.IsMatch(str, @"[a-f]+", RegexOptions.IgnoreCase));
98                         AssertEquals("RIC #03", true, Regex.IsMatch(str, @"[A-Fa-f]+", RegexOptions.IgnoreCase));
99                         AssertEquals("RIC #04", true, Regex.IsMatch(str, @"[AB]+", RegexOptions.IgnoreCase));
100                         AssertEquals("RIC #05", true, Regex.IsMatch(str, @"[A-B]+", RegexOptions.IgnoreCase));
101
102                         str = "AaaBBBaAa" ;
103                         AssertEquals("RIC #06", true, Regex.IsMatch(str, @"[A-F]+", RegexOptions.IgnoreCase));
104                         AssertEquals("RIC #07", true, Regex.IsMatch(str, @"[a-f]+", RegexOptions.IgnoreCase));
105                         AssertEquals("RIC #08", true, Regex.IsMatch(str, @"[A-Fa-f]+", RegexOptions.IgnoreCase));
106                         AssertEquals("RIC #09", true, Regex.IsMatch(str, @"[AB]+", RegexOptions.IgnoreCase));
107                         AssertEquals("RIC #10", true, Regex.IsMatch(str, @"[A-B]+", RegexOptions.IgnoreCase));
108
109                         str = "Aaa[";
110                         AssertEquals("RIC #11", true, Regex.IsMatch(str, @"[A-a]+", RegexOptions.IgnoreCase));
111                         
112                         str = "Ae";
113                         Assert("RIC #12", Regex.IsMatch(str, @"[A-a]+", RegexOptions.IgnoreCase));
114
115                 }
116
117                 [Test]
118                 public void Escape0 () // bug54797
119                 {
120                         Regex r = new Regex(@"^[\s\0]*$");
121                         AssertEquals ("E0-1", true, r.Match(" \0").Success);
122                 }
123
124                 [Test()]
125                 public void MultipleMatches()
126                 {
127                         Regex regex = new Regex (@"^(?'path'.*(\\|/)|(/|\\))(?'file'.*)$");
128                         Match match = regex.Match (@"d:\Temp\SomeDir\SomeDir\bla.xml");
129                                                                                            
130                         AssertEquals ("MM #01", 5, match.Groups.Count);
131                                                                                            
132                         AssertEquals ("MM #02", "1", regex.GroupNameFromNumber(1));
133                         AssertEquals ("MM #03", "2", regex.GroupNameFromNumber(2));
134                         AssertEquals ("MM #04", "path", regex.GroupNameFromNumber(3));
135                         AssertEquals ("MM #05", "file", regex.GroupNameFromNumber(4));
136                                                                                            
137                         AssertEquals ("MM #06", "\\", match.Groups[1].Value);
138                         AssertEquals ("MM #07", "", match.Groups[2].Value);
139                         AssertEquals ("MM #08", @"d:\Temp\SomeDir\SomeDir\", match.Groups[3].Value);
140                         AssertEquals ("MM #09", "bla.xml", match.Groups[4].Value);
141                 }
142
143                 [Test] 
144                 public void SameNameGroups () // First problem in fixing bug #56000
145                 {
146                         string rex = "link\\s*rel\\s*=\\s*[\"']?alternate[\"']?\\s*";
147                         rex += "type\\s*=\\s*[\"']?text/xml[\"']?\\s*href\\s*=\\s*(?:\"(?<1>[^\"]*)\"|'(?<1>[^']*)'|(?<1>\\S+))";
148                         Regex rob = new Regex (rex, RegexOptions.IgnoreCase);
149                 }
150                 
151                 [Test]
152                 public void UndefinedGroup () // bug 52890
153                 {
154                         Regex regex = new Regex( "[A-Za-z_0-9]" );
155                         Match m = regex.Match( "123456789abc" );
156                         Group g = m.Groups["not_defined"];
157                         AssertNotNull ("#0", g);
158                         AssertEquals ("#1", 0, g.Index);
159                         AssertEquals ("#2", 0, g.Length);
160                         AssertEquals ("#3", "", g.Value);
161                         Assert ("#4", !g.Success);
162                         AssertNotNull ("#5", g.Captures);
163                         AssertEquals ("#6", 0, g.Captures.Count);
164                 }
165
166                 [Test]
167                 public void Quantifiers1 ()
168                 {
169                         Regex re = new Regex ("[\\w\\W]{8,32}");
170                         Match m = re.Match (new string ('1', 7));
171                         AssertEquals ("#01", false, m.Success);
172                 }
173
174                 [Test]
175                 public void Quantifiers2 ()
176                 {
177                         Regex re = new Regex ("[\\w\\W]{8,32}");
178                         Match m = re.Match (new string ('1', 8));
179                         AssertEquals ("#01", true, m.Success);
180                 }
181
182                 [Test]
183                 public void Quantifiers3 ()
184                 {
185                         Regex re = new Regex ("[\\w\\W]{8,32}");
186                         Match m = re.Match (new string ('1', 16));
187                         AssertEquals ("#01", true, m.Success);
188                 }
189
190                 [Test]
191                 public void Quantifiers4 ()
192                 {
193                         Regex re = new Regex ("[\\w\\W]{8,32}");
194                         Match m = re.Match (new string ('1', 32));
195                         AssertEquals ("#01", true, m.Success);
196                 }
197
198                 [Test]
199                 public void Quantifiers5 ()
200                 {
201                         Regex re = new Regex ("[\\w\\W]{8,32}");
202                         Match m = re.Match (new string ('1', 33));
203                         AssertEquals ("#01", true, m.Success);
204                 }
205
206                 [Test]
207                 public void CategoryAndNegated () // Was a regression after first attemp to fix 59150.
208                 {
209                         string text = "<?xml version=\"1.0\"?>";
210                         Regex re = new Regex ("<\\s*(\\/?)\\s*([\\s\\S]*?)\\s*(\\/?)\\s*>");
211                         text = re.Replace (text, "{blue:&lt;$1}{maroon:$2}{blue:$3&gt;}");
212                         AssertEquals ("#01", "{blue:&lt;}{maroon:?xml version=\"1.0\"?}{blue:&gt;}", text);
213                 }
214         
215                 [Test]
216                 public void BackSpace ()
217                 {
218                         string text = "Go, \bNo\bGo" ;
219                         Regex re = new Regex(@"\b[\b]");
220                         text = re.Replace(text, " ");
221                         AssertEquals("#01", "Go, \bNo Go", text);
222                 }
223
224                 [Test]
225                 public void ReplaceNegOneAndStartat ()
226                 {
227                         string text = "abcdeeee";
228                         Regex re = new Regex("e+");
229                         text = re.Replace(text, "e", -1, 4);
230                         AssertEquals("#01", "abcde", text);
231                 }
232
233                 [Test]
234                 //[Ignore] You may want to ignore this if the bugs gets back
235                 public void SplitInfiniteLoop () // bug 57274
236                 {
237                         string ss = "a b c d e";
238                         string [] words = Regex.Split (ss, "[ \t\n\r]*");
239                         AssertEquals ("#01Length", 11, words.Length);
240                         AssertEquals ("#00", "", words [0]);
241                         AssertEquals ("#01", "a", words [1]);
242                         AssertEquals ("#02", "", words [2]);
243                         AssertEquals ("#03", "b", words [3]);
244                         AssertEquals ("#04", "", words [4]);
245                         AssertEquals ("#05", "c", words [5]);
246                         AssertEquals ("#06", "", words [6]);
247                         AssertEquals ("#07", "d", words [7]);
248                         AssertEquals ("#08", "", words [8]);
249                         AssertEquals ("#09", "e", words [9]);
250                         AssertEquals ("#10", "", words [10]);
251
252                 }
253
254                 [Test]
255                 public void CaseAndSearch () // bug 69065
256                 {
257                         string test1 =  @"\f!E   ZWEITBAD :REGLER-PARAMETER 20.10.2004  SEITE   1";
258                         string test2 =  @" REGLER-PARAMETER ";
259                         string test3 =  @"REGLER-PARAMETER ";
260                         Regex x = new Regex ("REGLER-PARAMETER",RegexOptions.IgnoreCase|RegexOptions.Compiled);
261
262                         Match m = x.Match (test1);
263                         AssertEquals ("#01", true, m.Success);
264
265                         m = x.Match (test2);
266                         AssertEquals ("#02", true, m.Success);
267
268                         m = x.Match (test3);
269                         AssertEquals ("#03", true, m.Success);
270                 }
271
272                 [Test]
273                 public void QuantifiersParseError () // bug 69193
274                 {
275                         Regex x = new Regex ("{1,a}");
276                         x = new Regex ("{a,1}");
277                         x = new Regex ("{a}");
278                         x = new Regex ("{,a}");
279                 }
280         }
281 }
282