Welcome to mirror list, hosted at ThFree Co, Russian Federation.

RegexBugs.cs « System.Text.RegularExpressions « Test « System « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e65648542f512f50db23cd6c03eded0d0eea08ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//
// MonoTests.System.Text.RegularExpressions misc. test cases
//
// Authors:
// 	Gonzalo Paniagua Javier (gonzalo@ximian.com)
//
// (c) Copyright 2003,2004 Novell, Inc. (http://www.novell.com)
//

using NUnit.Framework;
using System;
using System.Text.RegularExpressions;

namespace MonoTests.System.Text.RegularExpressions
{
	[TestFixture]
	public class RegexBugs : Assertion
	{
		[Test]
		public void SplitGroup () // bug51146
		{
		        string [] splitResult = new Regex ("-").Split ("a-bcd-e-fg");
			string [] expected = new string [] {"a", "bcd", "e", "fg"};
			int length = expected.Length;
			int i;
			AssertEquals ("#01", length, splitResult.Length);
			for (i = 0; i < length; i++)
				AssertEquals ("#02 " + i, expected [i], splitResult [i]);
			
			splitResult = new Regex ("(-)").Split ("a-bcd-e-fg");
			expected = new string [] {"a", "-", "bcd", "-", "e", "-", "fg"};
			length = expected.Length;
			AssertEquals ("#03", length, splitResult.Length);
			for (i = 0; i < length; i++)
				AssertEquals ("#04 " + i, expected [i], splitResult [i]);

			splitResult = new Regex ("(-)b(c)").Split ("a-bcd-e-fg");
			expected = new string [] {"a", "-", "c", "d-e-fg" };
			length = expected.Length;
			AssertEquals ("#04", length, splitResult.Length);
			for (i = 0; i < length; i++)
				AssertEquals ("#05 " + i, expected [i], splitResult [i]);
				
			splitResult = new Regex ("-").Split ("a-bcd-e-fg-");
			expected = new string [] {"a", "bcd", "e", "fg", ""};
			length = expected.Length;
			AssertEquals ("#06", length, splitResult.Length);
			for (i = 0; i < length; i++)
				AssertEquals ("#07 " + i, expected [i], splitResult [i]);
		}

		[Test]
		public void MathEmptyGroup () // bug 42529
		{
			string str = "Match something from here.";

			AssertEquals ("MEG #01", false, Regex.IsMatch(str, @"(something|dog)$"));
			AssertEquals ("MEG #02", true, Regex.IsMatch(str, @"(|something|dog)$"));
			AssertEquals ("MEG #03", true, Regex.IsMatch(str, @"(something||dog)$"));
			AssertEquals ("MEG #04", true, Regex.IsMatch(str, @"(something|dog|)$"));

			AssertEquals ("MEG #05", true, Regex.IsMatch(str, @"(something|dog)*"));
			AssertEquals ("MEG #06", true, Regex.IsMatch(str, @"(|something|dog)*"));
			AssertEquals ("MEG #07", true, Regex.IsMatch(str, @"(something||dog)*"));
			AssertEquals ("MEG #08", true, Regex.IsMatch(str, @"(something|dog|)*"));

			AssertEquals ("MEG #09", true, Regex.IsMatch(str, @"(something|dog)*$"));
			AssertEquals ("MEG #10", true, Regex.IsMatch(str, @"(|something|dog)*$"));
			AssertEquals ("MEG #11", true, Regex.IsMatch(str, @"(something||dog)*$"));
			AssertEquals ("MEG #12", true, Regex.IsMatch(str, @"(something|dog|)*$"));

		}

		[Test]
		public void Braces () // bug 52924
		{
			// Before the fix, the next line throws an exception
			Regex regVar = new Regex(@"{\w+}");
			Match m = regVar.Match ("{   }");
			AssertEquals ("BR #01", false, m.Success);
		}

                [Test]
                public void RangeIgnoreCase() // bug 45976
                {
                        string str = "AAABBBBAAA" ;
                        AssertEquals("RIC #01", true, Regex.IsMatch(str, @"[A-F]+", RegexOptions.IgnoreCase));
                        AssertEquals("RIC #02", true, Regex.IsMatch(str, @"[a-f]+", RegexOptions.IgnoreCase));
                        AssertEquals("RIC #03", true, Regex.IsMatch(str, @"[A-Fa-f]+", RegexOptions.IgnoreCase));
                        AssertEquals("RIC #04", true, Regex.IsMatch(str, @"[AB]+", RegexOptions.IgnoreCase));
                        AssertEquals("RIC #05", true, Regex.IsMatch(str, @"[A-B]+", RegexOptions.IgnoreCase));

                        str = "AaaBBBaAa" ;
                        AssertEquals("RIC #06", true, Regex.IsMatch(str, @"[A-F]+", RegexOptions.IgnoreCase));
                        AssertEquals("RIC #07", true, Regex.IsMatch(str, @"[a-f]+", RegexOptions.IgnoreCase));
                        AssertEquals("RIC #08", true, Regex.IsMatch(str, @"[A-Fa-f]+", RegexOptions.IgnoreCase));
                        AssertEquals("RIC #09", true, Regex.IsMatch(str, @"[AB]+", RegexOptions.IgnoreCase));
                        AssertEquals("RIC #10", true, Regex.IsMatch(str, @"[A-B]+", RegexOptions.IgnoreCase));

			str = "Aaa[";
			AssertEquals("RIC #11", true, Regex.IsMatch(str, @"[A-a]+", RegexOptions.IgnoreCase));
			
			str = "Ae";
			Assert("RIC #12", Regex.IsMatch(str, @"[A-a]+", RegexOptions.IgnoreCase));

                }

		[Test]
		public void Escape0 () // bug54797
		{
            		Regex r = new Regex(@"^[\s\0]*$");
			AssertEquals ("E0-1", true, r.Match(" \0").Success);
		}

        	[Test()]
        	public void MultipleMatches()
        	{
            		Regex regex = new Regex (@"^(?'path'.*(\\|/)|(/|\\))(?'file'.*)$");
            		Match match = regex.Match (@"d:\Temp\SomeDir\SomeDir\bla.xml");
                                                                                           
            		AssertEquals ("MM #01", 5, match.Groups.Count);
                                                                                           
            		AssertEquals ("MM #02", "1", regex.GroupNameFromNumber(1));
            		AssertEquals ("MM #03", "2", regex.GroupNameFromNumber(2));
            		AssertEquals ("MM #04", "path", regex.GroupNameFromNumber(3));
            		AssertEquals ("MM #05", "file", regex.GroupNameFromNumber(4));
                                                                                           
            		AssertEquals ("MM #06", "\\", match.Groups[1].Value);
            		AssertEquals ("MM #07", "", match.Groups[2].Value);
            		AssertEquals ("MM #08", @"d:\Temp\SomeDir\SomeDir\", match.Groups[3].Value);
            		AssertEquals ("MM #09", "bla.xml", match.Groups[4].Value);
        	}

		[Test] 
		public void SameNameGroups () // First problem in fixing bug #56000
		{
			string rex = "link\\s*rel\\s*=\\s*[\"']?alternate[\"']?\\s*";
			rex += "type\\s*=\\s*[\"']?text/xml[\"']?\\s*href\\s*=\\s*(?:\"(?<1>[^\"]*)\"|'(?<1>[^']*)'|(?<1>\\S+))";
			Regex rob = new Regex (rex, RegexOptions.IgnoreCase);
		}
		
		[Test]
		public void UndefinedGroup () // bug 52890
		{
			Regex regex = new Regex( "[A-Za-z_0-9]" );
			Match m = regex.Match( "123456789abc" );
			Group g = m.Groups["not_defined"];
			AssertNotNull ("#0", g);
			AssertEquals ("#1", 0, g.Index);
			AssertEquals ("#2", 0, g.Length);
			AssertEquals ("#3", "", g.Value);
			Assert ("#4", !g.Success);
			AssertNotNull ("#5", g.Captures);
			AssertEquals ("#6", 0, g.Captures.Count);
		}

		[Test]
		public void Quantifiers1 ()
		{
			Regex re = new Regex ("[\\w\\W]{8,32}");
			Match m = re.Match (new string ('1', 7));
			AssertEquals ("#01", false, m.Success);
		}

		[Test]
		public void Quantifiers2 ()
		{
			Regex re = new Regex ("[\\w\\W]{8,32}");
			Match m = re.Match (new string ('1', 8));
			AssertEquals ("#01", true, m.Success);
		}

		[Test]
		public void Quantifiers3 ()
		{
			Regex re = new Regex ("[\\w\\W]{8,32}");
			Match m = re.Match (new string ('1', 16));
			AssertEquals ("#01", true, m.Success);
		}

		[Test]
		public void Quantifiers4 ()
		{
			Regex re = new Regex ("[\\w\\W]{8,32}");
			Match m = re.Match (new string ('1', 32));
			AssertEquals ("#01", true, m.Success);
		}

		[Test]
		public void Quantifiers5 ()
		{
			Regex re = new Regex ("[\\w\\W]{8,32}");
			Match m = re.Match (new string ('1', 33));
			AssertEquals ("#01", true, m.Success);
		}
	}
}