]> rtime.felk.cvut.cz Git - wvtest.git/blob - dotnet/wvtest.cs
Clean up license information and add LICENSE file.
[wvtest.git] / dotnet / wvtest.cs
1 /*
2  * WvTest:
3  *   Copyright (C)2007-2009 Versabanq Innovations Inc. and contributors.
4  *       Licensed under the GNU Library General Public License, version 2.
5  *       See the included file named LICENSE for license information.
6  */
7 using System;
8 using System.Collections.Generic;
9 using System.Reflection;
10 using System.Runtime.Serialization;
11 using Wv;
12
13 // We put this in wvtest.cs since wvtest.cs should be able to compile all
14 // by itself, without relying on any other parts of wvdotnet.  On the other
15 // hand, it's perfectly fine for wvdotnet to have wvtest.cs in it.
16 namespace Wv
17 {
18     public static class WvReflection
19     {
20         public static IEnumerable<Type> find_types(Type attrtype)
21         {
22             foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies())
23             {
24                 foreach (Type t in a.GetTypes())
25                 {
26                     if (!t.IsDefined(attrtype, false))
27                         continue;
28                     
29                     yield return t;
30                 }
31             }
32         }
33         
34         public static IEnumerable<MethodInfo> find_methods(this Type t,
35                                                            Type attrtype)
36         {
37             foreach (MethodInfo m in t.GetMethods())
38             {
39                 if (!m.IsDefined(attrtype, false))
40                     continue;
41                 
42                 yield return m;
43             }
44         }
45     }
46 }
47
48 namespace Wv.Test
49 {
50     public class WvTest
51     {
52         struct TestInfo
53         {
54             public string name;
55             public Action cb;
56             
57             public TestInfo(string name, Action cb)
58                 { this.name = name; this.cb = cb; }
59         }
60         List<TestInfo> tests = new List<TestInfo>();
61
62         public int failures { get; private set; }
63         
64         public WvTest()
65         {
66             foreach (Type t in 
67                      WvReflection.find_types(typeof(TestFixtureAttribute)))
68             {
69                 foreach (MethodInfo m in 
70                          t.find_methods(typeof(TestAttribute)))
71                 {
72                     // The new t2, m2 are needed so that each delegate gets
73                     // its own copy of the variable.
74                     Type t2 = t;
75                     MethodInfo m2 = m;
76                     RegisterTest(String.Format("{0}/{1}",
77                                                t.Name, m.Name),
78                                  delegate() {
79                                      try {
80                                          m2.Invoke(Activator.CreateInstance(t2),
81                                                    null); 
82                                      } catch (TargetInvocationException e) {
83                                          throw e.InnerException;
84                                      }
85                                  });
86                 }
87             }
88         }
89
90         public void RegisterTest(string name, Action tc)
91         {
92             tests.Add(new TestInfo(name, tc));
93         }
94
95         public static void DoMain()
96         {
97             // Enough to run an entire test
98             Environment.Exit(new WvTest().Run());
99         }
100
101         public int Run()
102         {
103             string[] args = Environment.GetCommandLineArgs();
104             
105             if (args.Length <= 1)
106                 Console.WriteLine("WvTest: Running all tests");
107             else
108                 Console.WriteLine("WvTest: Running only selected tests");
109
110             foreach (TestInfo test in tests)
111             {
112                 string[] parts = test.name.Split(new char[] { '/' }, 2);
113                 
114                 bool runthis = (args.Length <= 1);
115                 foreach (string arg in args)
116                     if (parts[0].StartsWith(arg) || parts[1].StartsWith(arg))
117                         runthis = true;
118                 
119                 if (!runthis) continue;
120                 
121                 Console.WriteLine("\nTesting \"{0}\" in {1}:",
122                                   parts[1], parts[0]);
123
124                 try {
125                     test.cb();
126                 } catch (WvAssertionFailure) {
127                     failures++;
128                 } catch (Exception e) {
129                     Console.WriteLine(e.ToString());
130                     Console.WriteLine("! WvTest Exception received   FAILED");
131                     failures++;
132                 }
133             }
134             
135             Console.Out.WriteLine("Result: {0} failures.", failures);
136             
137             // Return a safe unix exit code
138             return failures > 0 ? 1 : 0;
139         }
140
141         public static bool booleanize(bool x)
142         {
143             return x;
144         }
145
146         public static bool booleanize(long x)
147         {
148             return x != 0;
149         }
150         
151         public static bool booleanize(ulong x)
152         {
153             return x != 0;
154         }
155         
156         public static bool booleanize(string s)
157         {
158             return s != null && s != "";
159         }
160         
161         public static bool booleanize(object o)
162         {
163             return o != null;
164         }
165         
166         static bool expect_fail = false;
167         public static void expect_next_failure()
168         {
169             expect_fail = true;
170         }
171         
172         public static bool test(bool ok, string file, int line, string s)
173         {
174             s = s.Replace("\n", "!");
175             s = s.Replace("\r", "!");
176             string suffix = "";
177             if (expect_fail)
178             {
179                 if (!ok)
180                     suffix = " (expected) ok";
181                 else
182                     suffix = " (expected fail!) FAILED";
183             }
184             Console.WriteLine("! {0}:{1,-5} {2,-40} {3}{4}",
185                               file, line, s,
186                               ok ? "ok" : "FAILED",
187                               suffix);
188             Console.Out.Flush();
189             expect_fail = false;
190
191             if (!ok)
192                 throw new WvAssertionFailure(String.Format("{0}:{1} {2}", file, line, s));
193
194             return ok;
195         }
196
197         public static void test_exception(string file, int line, string s)
198         {
199             Console.WriteLine("! {0}:{1,-5} {2,-40} {3}",
200                                          file, line, s, "EXCEPTION");
201             Console.Out.Flush();
202         }
203         
204         public static bool test_eq(bool cond1, bool cond2,
205                                    string file, int line,
206                                    string s1, string s2)
207         {
208             return test(cond1 == cond2, file, line,
209                 String.Format("[{0}] == [{1}] ({{{2}}} == {{{3}}})",
210                               cond1, cond2, s1, s2));
211         }
212         
213         public static bool test_eq(long cond1, long cond2,
214                                    string file, int line,
215                                    string s1, string s2)
216         {
217             return test(cond1 == cond2, file, line,
218                 String.Format("[{0}] == [{1}] ({{{2}}} == {{{3}}})",
219                               cond1, cond2, s1, s2));
220         }
221         
222         public static bool test_eq(ulong cond1, ulong cond2,
223                                    string file, int line,
224                                    string s1, string s2)
225         {
226             return test(cond1 == cond2, file, line,
227                 String.Format("[{0}] == [{1}] ({{{2}}} == {{{3}}})",
228                               cond1, cond2, s1, s2));
229         }
230         
231         public static bool test_eq(double cond1, double cond2,
232                                    string file, int line,
233                                    string s1, string s2)
234         {
235             return test(cond1 == cond2, file, line,
236                 String.Format("[{0}] == [{1}] ({{{2}}} == {{{3}}})",
237                               cond1, cond2, s1, s2));
238         }
239         
240         public static bool test_eq(decimal cond1, decimal cond2,
241                                    string file, int line,
242                                    string s1, string s2)
243         {
244             return test(cond1 == cond2, file, line,
245                 String.Format("[{0}] == [{1}] ({{{2}}} == {{{3}}})",
246                               cond1, cond2, s1, s2));
247         }
248         
249         public static bool test_eq(string cond1, string cond2,
250                                    string file, int line,
251                                    string s1, string s2)
252         {
253             return test(cond1 == cond2, file, line,
254                 String.Format("[{0}] == [{1}] ({{{2}}} == {{{3}}})",
255                               cond1, cond2, s1, s2));
256         }
257
258         // some objects can compare themselves to 'null', which is helpful.
259         // for example, DateTime.MinValue == null, but only through
260         // IComparable, not through IObject.
261         public static bool test_eq(IComparable cond1, IComparable cond2,
262                                    string file, int line,
263                                    string s1, string s2)
264         {
265             return test(cond1.CompareTo(cond2) == 0, file, line,
266                         String.Format("[{0}] == [{1}]", s1, s2));
267         }
268
269         public static bool test_eq(object cond1, object cond2,
270                                    string file, int line,
271                                    string s1, string s2)
272         {
273             return test(cond1 == cond2, file, line,
274                 String.Format("[{0}] == [{1}]", s1, s2));
275         }
276
277         public static bool test_ne(bool cond1, bool cond2,
278                                    string file, int line,
279                                    string s1, string s2)
280         {
281             return test(cond1 != cond2, file, line,
282                 String.Format("[{0}] != [{1}] ({{{2}}} != {{{3}}})",
283                               cond1, cond2, s1, s2));
284         }
285         
286         public static bool test_ne(long cond1, long cond2,
287                                    string file, int line,
288                                    string s1, string s2)
289         {
290             return test(cond1 != cond2, file, line,
291                 String.Format("[{0}] != [{1}] ({{{2}}} != {{{3}}})",
292                               cond1, cond2, s1, s2));
293         }
294         
295         public static bool test_ne(ulong cond1, ulong cond2,
296                                    string file, int line,
297                                    string s1, string s2)
298         {
299             return test(cond1 != cond2, file, line,
300                 String.Format("[{0}] != [{1}] ({{{2}}} != {{{3}}})",
301                               cond1, cond2, s1, s2));
302         }
303         
304         public static bool test_ne(double cond1, double cond2,
305                                    string file, int line,
306                                    string s1, string s2)
307         {
308             return test(cond1 != cond2, file, line,
309                 String.Format("[{0}] != [{1}] ({{{2}}} != {{{3}}})",
310                               cond1, cond2, s1, s2));
311         }
312         
313         public static bool test_ne(decimal cond1, decimal cond2,
314                                    string file, int line,
315                                    string s1, string s2)
316         {
317             return test(cond1 != cond2, file, line,
318                 String.Format("[{0}] != [{1}] ({{{2}}} != {{{3}}})",
319                               cond1, cond2, s1, s2));
320         }
321         
322         public static bool test_ne(string cond1, string cond2,
323                                    string file, int line,
324                                    string s1, string s2)
325         {
326             return test(cond1 != cond2, file, line,
327                 String.Format("[{0}] != [{1}] ({{{2}}} != {{{3}}})",
328                               cond1, cond2, s1, s2));
329         }
330         
331         // See notes for test_eq(IComparable,IComparable)
332         public static bool test_ne(IComparable cond1, IComparable cond2,
333                                    string file, int line,
334                                    string s1, string s2)
335         {
336             return test(cond1.CompareTo(cond2) != 0, file, line,
337                         String.Format("[{0}] != [{1}]", s1, s2));
338         }
339         
340         public static bool test_ne(object cond1, object cond2,
341                                    string file, int line,
342                                    string s1, string s2)
343         {
344             return test(cond1 != cond2, file, line,
345                 String.Format("[{0}] != [{1}]", s1, s2));
346         }
347     }
348
349     public class WvAssertionFailure : Exception
350     {
351         public WvAssertionFailure()
352             : base()
353         {
354         }
355
356         public WvAssertionFailure(string msg)
357             : base(msg)
358         {
359         }
360     }
361
362     // Placeholders for NUnit compatibility
363     public class TestFixtureAttribute : Attribute
364     {
365     }
366     public class TestAttribute : Attribute
367     {
368     }
369     [AttributeUsage(AttributeTargets.Method, AllowMultiple=true)]
370     public class CategoryAttribute : Attribute
371     {
372         public CategoryAttribute(string x)
373         {
374         }
375     }
376 }