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 {
24 char[] c = { (char)32, (char)8212, (char)32 };
25 string s = new String(c);
26 Assert.IsTrue (Regex.IsMatch(s, s), "char");
30 public void Unescape ()
32 string inString = @"\a\b\t\r\v\f\n\e\02400\x231\cC\ufffff\*";
33 char [] c = { (char)7, (char)8, (char)9, (char)13,
34 (char)11, (char)12, (char)10, (char)27, (char) 20,
35 (char)48, (char)48, (char)35, (char)49,
36 (char)3, (char)65535, (char)102, (char)42
38 string expectedString = new String(c);
39 string outString = Regex.Unescape(inString);
41 Assert.AreEqual (outString, expectedString, "unescape");
47 Regex email = new Regex ("(?<user>[^@]+)@(?<domain>.+)");
50 m = email.Match ("mono@go-mono.com");
52 Assert.IsTrue (m.Success, "#m01");
53 Assert.AreEqual ("mono", m.Groups ["user"].Value, "#m02");
54 Assert.AreEqual ("go-mono.com", m.Groups ["domain"].Value, "#m03");
56 m = email.Match ("mono.bugs@go-mono.com");
57 Assert.IsTrue (m.Success, "m04");
58 Assert.AreEqual ("mono.bugs", m.Groups ["user"].Value, "#m05");
59 Assert.AreEqual ("go-mono.com", m.Groups ["domain"].Value, "#m06");
63 "Two little dragons lived in the forest\n" +
64 "They spent their days collecting honey suckle,\n" +
65 "And eating curds and whey\n" +
66 "Until an evil sorcer came along\n" +
67 "And chased my dragon friends away";
69 struct MatchCollectionTrial {
70 public readonly string name;
71 public readonly string text;
72 public readonly string regex;
73 public readonly string [] matches;
74 public MatchCollectionTrial (string name, string text, string regex, string [] matches)
79 this.matches = matches;
83 static readonly MatchCollectionTrial [] trials = {
84 new MatchCollectionTrial ("word", "the fat cat ate the rat", "(?<word>\\w+)",
85 new string [] { "the", "fat", "cat", "ate", "the", "rat" }),
86 new MatchCollectionTrial ("digit", "0 1 2 3 4 5 6a7b8c9d10", "(?<digit>\\d+)",
87 new string [] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }),
88 new MatchCollectionTrial ("line", story, "(?<line>.+)",
89 new string [] { "Two little dragons lived in the forest",
90 "They spent their days collecting honey suckle,",
91 "And eating curds and whey",
92 "Until an evil sorcer came along",
93 "And chased my dragon friends away" }),
94 new MatchCollectionTrial ("nonwhite", "ab 12 cde 456 fghi .,\niou", "(?<nonwhite>\\S+)",
95 new string [] { "ab", "12", "cde", "456", "fghi", ".,", "iou" }),
96 new MatchCollectionTrial ("nondigit", "ab0cd1ef2", "(?<nondigit>\\D+)",
97 new string [] { "ab", "cd", "ef" })
100 static void runTrial (MatchCollectionTrial t)
106 static void runTrial (MatchCollectionTrial t, bool rtl)
111 string name = t.name;
115 int len = t.matches.Length;
116 Regex r = new Regex (t.regex, rtl ? RegexOptions.RightToLeft : RegexOptions.None);
118 // Incremental mode -- this access
119 mc = r.Matches (t.text);
120 for (i = 0; i < len; ++i)
121 Assert.AreEqual (mc [i].Value, t.matches [rtl ? len - i - 1 : i], "{0}:this:{1}", name, i);
122 Assert.AreEqual (i, mc.Count, "{0}:this:count", name);
124 // Incremental mode -- enumerator
125 mc = r.Matches (t.text);
127 foreach (Match m in mc) {
128 Assert.AreEqual (m.Value, t.matches [rtl ? len - i - 1 : i], "{0}:enum:{1}", name, i);
131 Assert.AreEqual (i, len, "{0}:enum:count", name);
134 Random rng = new Random ();
135 for (int j = 0; j < len * 5; ++j) {
137 Assert.AreEqual (mc [i].Value, t.matches [rtl ? len - i - 1 : i], "{0}:random{1}:{2}", name, j, i);
140 // Non-incremental mode
141 mc = r.Matches (t.text);
142 Assert.AreEqual (mc.Count, len);
144 foreach (Match m in mc) {
145 Assert.AreEqual (m.Value, t.matches [rtl ? len - i - 1 : i], "{0}:nienum:{1}", name, i);
148 for (i = 0; i < len; ++i)
149 Assert.AreEqual (mc [i].Value, t.matches [rtl ? len - i - 1 : i], "{0}:nithis:{1}", name, i);
153 public void Matches ()
158 foreach (MatchCollectionTrial t in trials)