2 // assembly: System_test
3 // namespace: MonoTests.System.Text.RegularExpressions
7 // Juraj Skripsky (juraj@hotfeet.ch)
9 // (c) 2003 Juraj Skripsky
12 using System.Text.RegularExpressions;
14 using NUnit.Framework;
16 namespace MonoTests.System.Text.RegularExpressions {
19 public class RegexTest {
22 private int cache_initial_value;
25 public void FixtureSetUp ()
27 cache_initial_value = Regex.CacheSize;
31 public void TearDown ()
33 Regex.CacheSize = cache_initial_value;
39 char[] c = { (char)32, (char)8212, (char)32 };
40 string s = new String(c);
41 Assert.IsTrue (Regex.IsMatch(s, s), "char");
45 public void Unescape ()
47 string inString = @"\a\b\t\r\v\f\n\e\02400\x231\cC\ufffff\*";
48 char [] c = { (char)7, (char)8, (char)9, (char)13,
49 (char)11, (char)12, (char)10, (char)27, (char) 20,
50 (char)48, (char)48, (char)35, (char)49,
51 (char)3, (char)65535, (char)102, (char)42
53 string expectedString = new String(c);
54 string outString = Regex.Unescape(inString);
56 Assert.AreEqual (outString, expectedString, "unescape");
62 Regex email = new Regex ("(?<user>[^@]+)@(?<domain>.+)");
65 m = email.Match ("mono@go-mono.com");
67 Assert.IsTrue (m.Success, "#m01");
68 Assert.AreEqual ("mono", m.Groups ["user"].Value, "#m02");
69 Assert.AreEqual ("go-mono.com", m.Groups ["domain"].Value, "#m03");
71 m = email.Match ("mono.bugs@go-mono.com");
72 Assert.IsTrue (m.Success, "m04");
73 Assert.AreEqual ("mono.bugs", m.Groups ["user"].Value, "#m05");
74 Assert.AreEqual ("go-mono.com", m.Groups ["domain"].Value, "#m06");
78 "Two little dragons lived in the forest\n" +
79 "They spent their days collecting honey suckle,\n" +
80 "And eating curds and whey\n" +
81 "Until an evil sorcer came along\n" +
82 "And chased my dragon friends away";
84 struct MatchCollectionTrial {
85 public readonly string name;
86 public readonly string text;
87 public readonly string regex;
88 public readonly string [] matches;
89 public MatchCollectionTrial (string name, string text, string regex, string [] matches)
94 this.matches = matches;
98 static readonly MatchCollectionTrial [] trials = {
99 new MatchCollectionTrial ("word", "the fat cat ate the rat", "(?<word>\\w+)",
100 new string [] { "the", "fat", "cat", "ate", "the", "rat" }),
101 new MatchCollectionTrial ("digit", "0 1 2 3 4 5 6a7b8c9d10", "(?<digit>\\d+)",
102 new string [] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }),
103 new MatchCollectionTrial ("line", story, "(?<line>.+)",
104 new string [] { "Two little dragons lived in the forest",
105 "They spent their days collecting honey suckle,",
106 "And eating curds and whey",
107 "Until an evil sorcer came along",
108 "And chased my dragon friends away" }),
109 new MatchCollectionTrial ("nonwhite", "ab 12 cde 456 fghi .,\niou", "(?<nonwhite>\\S+)",
110 new string [] { "ab", "12", "cde", "456", "fghi", ".,", "iou" }),
111 new MatchCollectionTrial ("nondigit", "ab0cd1ef2", "(?<nondigit>\\D+)",
112 new string [] { "ab", "cd", "ef" })
115 static void runTrial (MatchCollectionTrial t)
121 static void runTrial (MatchCollectionTrial t, bool rtl)
126 string name = t.name;
130 int len = t.matches.Length;
131 Regex r = new Regex (t.regex, rtl ? RegexOptions.RightToLeft : RegexOptions.None);
133 // Incremental mode -- this access
134 mc = r.Matches (t.text);
135 for (i = 0; i < len; ++i)
136 Assert.AreEqual (mc [i].Value, t.matches [rtl ? len - i - 1 : i], "{0}:this:{1}", name, i);
137 Assert.AreEqual (i, mc.Count, "{0}:this:count", name);
139 // Incremental mode -- enumerator
140 mc = r.Matches (t.text);
142 foreach (Match m in mc) {
143 Assert.AreEqual (m.Value, t.matches [rtl ? len - i - 1 : i], "{0}:enum:{1}", name, i);
146 Assert.AreEqual (i, len, "{0}:enum:count", name);
149 Random rng = new Random ();
150 for (int j = 0; j < len * 5; ++j) {
152 Assert.AreEqual (mc [i].Value, t.matches [rtl ? len - i - 1 : i], "{0}:random{1}:{2}", name, j, i);
155 // Non-incremental mode
156 mc = r.Matches (t.text);
157 Assert.AreEqual (mc.Count, len);
159 foreach (Match m in mc) {
160 Assert.AreEqual (m.Value, t.matches [rtl ? len - i - 1 : i], "{0}:nienum:{1}", name, i);
163 for (i = 0; i < len; ++i)
164 Assert.AreEqual (mc [i].Value, t.matches [rtl ? len - i - 1 : i], "{0}:nithis:{1}", name, i);
168 public void Matches ()
173 foreach (MatchCollectionTrial t in trials)
178 public void CacheSize ()
180 Assert.AreEqual (15, Regex.CacheSize, "CacheSize");
182 Regex.CacheSize = Int32.MaxValue;
186 [ExpectedException (typeof (ArgumentOutOfRangeException))]
187 public void CacheSize_Negative ()
189 Regex.CacheSize = -1;
193 [ExpectedException (typeof (ArgumentOutOfRangeException))]
194 public void CacheSize_Min ()
196 Regex.CacheSize = Int32.MinValue;