**** Merged from MCS ****
[mono-project.git] / mcs / class / System / Test / System.Text.RegularExpressions / RegexBugs.cs
blob26c8f2baf47a8aafa7f2c44843866ab6f7053631
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 //
10 using NUnit.Framework;
11 using System;
12 using System.Text.RegularExpressions;
14 namespace MonoTests.System.Text.RegularExpressions
16 [TestFixture]
17 public class RegexBugs : Assertion
19 [Test]
20 public void SplitGroup () // bug51146
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]);
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]);
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]);
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]);
52 [Test]
53 public void MathEmptyGroup () // bug 42529
55 string str = "Match something from here.";
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|)$"));
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|)*"));
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|)*$"));
74 [Test]
75 public void Braces () // bug 52924
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);
83 [Test]
84 public void RangeIgnoreCase() // bug 45976
86 string str = "AAABBBBAAA" ;
87 AssertEquals("RIC #01", true, Regex.IsMatch(str, @"[A-F]+", RegexOptions.IgnoreCase));
88 AssertEquals("RIC #02", true, Regex.IsMatch(str, @"[a-f]+", RegexOptions.IgnoreCase));
89 AssertEquals("RIC #03", true, Regex.IsMatch(str, @"[A-Fa-f]+", RegexOptions.IgnoreCase));
90 AssertEquals("RIC #04", true, Regex.IsMatch(str, @"[AB]+", RegexOptions.IgnoreCase));
91 AssertEquals("RIC #05", true, Regex.IsMatch(str, @"[A-B]+", RegexOptions.IgnoreCase));
93 str = "AaaBBBaAa" ;
94 AssertEquals("RIC #06", true, Regex.IsMatch(str, @"[A-F]+", RegexOptions.IgnoreCase));
95 AssertEquals("RIC #07", true, Regex.IsMatch(str, @"[a-f]+", RegexOptions.IgnoreCase));
96 AssertEquals("RIC #08", true, Regex.IsMatch(str, @"[A-Fa-f]+", RegexOptions.IgnoreCase));
97 AssertEquals("RIC #09", true, Regex.IsMatch(str, @"[AB]+", RegexOptions.IgnoreCase));
98 AssertEquals("RIC #10", true, Regex.IsMatch(str, @"[A-B]+", RegexOptions.IgnoreCase));
100 str = "Aaa[";
101 AssertEquals("RIC #11", true, Regex.IsMatch(str, @"[A-a]+", RegexOptions.IgnoreCase));
103 str = "Ae";
104 Assert("RIC #12", Regex.IsMatch(str, @"[A-a]+", RegexOptions.IgnoreCase));
108 [Test]
109 public void Escape0 () // bug54797
111 Regex r = new Regex(@"^[\s\0]*$");
112 AssertEquals ("E0-1", true, r.Match(" \0").Success);
115 [Test()]
116 public void MultipleMatches()
118 Regex regex = new Regex (@"^(?'path'.*(\\|/)|(/|\\))(?'file'.*)$");
119 Match match = regex.Match (@"d:\Temp\SomeDir\SomeDir\bla.xml");
121 AssertEquals ("MM #01", 5, match.Groups.Count);
123 AssertEquals ("MM #02", "1", regex.GroupNameFromNumber(1));
124 AssertEquals ("MM #03", "2", regex.GroupNameFromNumber(2));
125 AssertEquals ("MM #04", "path", regex.GroupNameFromNumber(3));
126 AssertEquals ("MM #05", "file", regex.GroupNameFromNumber(4));
128 AssertEquals ("MM #06", "\\", match.Groups[1].Value);
129 AssertEquals ("MM #07", "", match.Groups[2].Value);
130 AssertEquals ("MM #08", @"d:\Temp\SomeDir\SomeDir\", match.Groups[3].Value);
131 AssertEquals ("MM #09", "bla.xml", match.Groups[4].Value);
134 [Test]
135 public void SameNameGroups () // First problem in fixing bug #56000
137 string rex = "link\\s*rel\\s*=\\s*[\"']?alternate[\"']?\\s*";
138 rex += "type\\s*=\\s*[\"']?text/xml[\"']?\\s*href\\s*=\\s*(?:\"(?<1>[^\"]*)\"|'(?<1>[^']*)'|(?<1>\\S+))";
139 Regex rob = new Regex (rex, RegexOptions.IgnoreCase);
142 [Test]
143 public void UndefinedGroup () // bug 52890
145 Regex regex = new Regex( "[A-Za-z_0-9]" );
146 Match m = regex.Match( "123456789abc" );
147 Group g = m.Groups["not_defined"];
148 AssertNotNull ("#0", g);
149 AssertEquals ("#1", 0, g.Index);
150 AssertEquals ("#2", 0, g.Length);
151 AssertEquals ("#3", "", g.Value);
152 Assert ("#4", !g.Success);
153 AssertNotNull ("#5", g.Captures);
154 AssertEquals ("#6", 0, g.Captures.Count);
157 [Test]
158 public void Quantifiers1 ()
160 Regex re = new Regex ("[\\w\\W]{8,32}");
161 Match m = re.Match (new string ('1', 7));
162 AssertEquals ("#01", false, m.Success);
165 [Test]
166 public void Quantifiers2 ()
168 Regex re = new Regex ("[\\w\\W]{8,32}");
169 Match m = re.Match (new string ('1', 8));
170 AssertEquals ("#01", true, m.Success);
173 [Test]
174 public void Quantifiers3 ()
176 Regex re = new Regex ("[\\w\\W]{8,32}");
177 Match m = re.Match (new string ('1', 16));
178 AssertEquals ("#01", true, m.Success);
181 [Test]
182 public void Quantifiers4 ()
184 Regex re = new Regex ("[\\w\\W]{8,32}");
185 Match m = re.Match (new string ('1', 32));
186 AssertEquals ("#01", true, m.Success);
189 [Test]
190 public void Quantifiers5 ()
192 Regex re = new Regex ("[\\w\\W]{8,32}");
193 Match m = re.Match (new string ('1', 33));
194 AssertEquals ("#01", true, m.Success);
197 [Test]
198 public void CategoryAndNegated () // Was a regression after first attemp to fix 59150.
200 string text = "<?xml version=\"1.0\"?>";
201 Regex re = new Regex ("<\\s*(\\/?)\\s*([\\s\\S]*?)\\s*(\\/?)\\s*>");
202 text = re.Replace (text, "{blue:&lt;$1}{maroon:$2}{blue:$3&gt;}");
203 AssertEquals ("#01", "{blue:&lt;}{maroon:?xml version=\"1.0\"?}{blue:&gt;}", text);
206 [Test]
207 public void BackSpace ()
209 string text = "Go, \bNo\bGo" ;
210 Regex re = new Regex(@"\b[\b]");
211 text = re.Replace(text, " ");
212 AssertEquals("#01", "Go, \bNo Go", text);
215 [Test]
216 public void ReplaceNegOneAndStartat ()
218 string text = "abcdeeee";
219 Regex re = new Regex("e+");
220 text = re.Replace(text, "e", -1, 4);
221 AssertEquals("#01", "abcde", text);