PageRenderTime 55ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/src/sys/dotnet/fanx/tools/Fant.cs

https://bitbucket.org/bedlaczech/fan-1.0
C# | 287 lines | 223 code | 29 blank | 35 comment | 50 complexity | dd345921770d166923acf2a4cd4b185c MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. //
  2. // Copyright (c) 2006, Brian Frank and Andy Frank
  3. // Licensed under the Academic Free License version 3.0
  4. //
  5. // History:
  6. // 4 Jan 06 Andy Frank Creation
  7. //
  8. using System.Collections;
  9. using Fan.Sys;
  10. using FanSysTest = Fan.Sys.Test;
  11. namespace Fanx.Tools
  12. {
  13. /// <summary>
  14. /// Fant .NET runtime.
  15. /// </summary>
  16. ///
  17. public class Fant : Tool
  18. {
  19. //////////////////////////////////////////////////////////////////////////
  20. // Test
  21. //////////////////////////////////////////////////////////////////////////
  22. public int test(string[] patterns, bool verbose)
  23. {
  24. long t1 = System.Environment.TickCount;
  25. for (int i=0; i<patterns.Length; i++)
  26. test(patterns[i], verbose);
  27. long t2 = System.Environment.TickCount;
  28. writeLine("");
  29. writeLine("Time: " + (t2-t1) + "ms");
  30. writeLine("");
  31. foreach (Method m in failedMethods)
  32. writeLine(" -- failed: " + m.qname());
  33. writeLine("");
  34. writeLine("***");
  35. writeLine("*** " + (failures == 0 ? "All tests passed!" : failures + " FAILURES") + " [" + testCount + " tests, " + methodCount + " methods, " + totalVerifyCount + " verifies] ");
  36. writeLine("***");
  37. return failures;
  38. }
  39. public void test(string pattern, bool verbose)
  40. {
  41. if (pattern == "*")
  42. {
  43. List pods = Pod.list();
  44. for (int i=0; i<pods.sz(); i++)
  45. {
  46. Pod p = (Pod)pods.get(i);
  47. test(p.name(), verbose);
  48. }
  49. return;
  50. }
  51. if (pattern == "sys" || pattern.StartsWith("sys::"))
  52. {
  53. writeLine("");
  54. //if (pattern == "sys")
  55. // pattern = "";
  56. //else
  57. // pattern = pattern.Substring(5);
  58. if (!Fanx.Test.Test.RunTests(null)) failures++;
  59. totalVerifyCount += Fanx.Test.Test.totalVerified;
  60. return;
  61. }
  62. string[] s = pattern.Split(new char[] { ':', '.' });
  63. string podName = s[0];
  64. string testName = (s.Length > 2) ? s[2] : "*";
  65. string methodName = (s.Length > 3) ? s[3] : "*";
  66. writeLine("");
  67. Pod pod = Pod.doFind(podName, true, null);
  68. Type[] t = tests(pod, testName);
  69. for (int i=0; i<t.Length; i++)
  70. {
  71. Method[] m = methods(t[i], methodName);
  72. for (int j=0; j<m.Length; j++)
  73. {
  74. writeLine("-- Run: " + m[j] + "...");
  75. int verifyCount = runTest(t[i], m[j]);
  76. if (verifyCount < 0) { failures++; failedMethods.Add(m[j]); continue; }
  77. writeLine(" Pass: " + m[j] + " [" + verifyCount + "]");
  78. methodCount++;
  79. totalVerifyCount += verifyCount;
  80. }
  81. testCount++;
  82. }
  83. }
  84. private Type[] tests(Pod pod, string testName)
  85. {
  86. // named test
  87. if (testName != "*") return new Type[] { pod.type(testName, true) };
  88. // all types which subclass Test
  89. List all = pod.types();
  90. ArrayList acc = new ArrayList();
  91. for (int i=0; i<all.sz(); i++)
  92. {
  93. Type x = (Type)all.get(i);
  94. if (x.@is(Sys.TestType) && !x.isAbstract()) acc.Add(x);
  95. }
  96. return (Type[])acc.ToArray(System.Type.GetType("Fan.Sys.Type"));
  97. }
  98. private Method[] methods(Type type, string methodName)
  99. {
  100. // named test
  101. if (methodName != "*") return new Method[] { type.method(methodName, true) };
  102. // all methods which start with "test"
  103. List all = type.methods();
  104. ArrayList acc = new ArrayList();
  105. for (int i=0; i<all.sz(); i++)
  106. {
  107. Method m = (Method)all.get(i);
  108. if (m.name().StartsWith("test") && !m.isAbstract()) acc.Add(m);
  109. }
  110. return (Method[])acc.ToArray(System.Type.GetType("Fan.Sys.Method"));
  111. }
  112. private int runTest(Type type, Method method)
  113. {
  114. Method setup = type.method("setup", true);
  115. Method teardown = type.method("teardown", true);
  116. FanSysTest test = null;
  117. List args = null;
  118. try
  119. {
  120. test = (FanSysTest)type.make();
  121. args = new List(Sys.ObjType, new object[] {test});
  122. }
  123. catch (System.Exception e)
  124. {
  125. System.Console.WriteLine();
  126. System.Console.WriteLine("ERROR: Cannot make test " + type);
  127. if (e is Err.Val)
  128. ((Err.Val)e).err().trace();
  129. else
  130. Err.dumpStack(e);
  131. return -1;
  132. }
  133. try
  134. {
  135. test.m_curTestMethod = method;
  136. setup.callList(args);
  137. method.callList(args);
  138. return test.verifyCount;
  139. }
  140. catch (System.Exception e)
  141. {
  142. //System.Console.WriteLine(" -- " + e.GetType() + " -- ");
  143. System.Console.WriteLine();
  144. System.Console.WriteLine("TEST FAILED");
  145. if (e is Err.Val)
  146. ((Err.Val)e).err().trace();
  147. else
  148. Err.dumpStack(e);
  149. return -1;
  150. }
  151. finally
  152. {
  153. try
  154. {
  155. if (args != null) teardown.callList(args);
  156. }
  157. catch (System.Exception e)
  158. {
  159. Err.dumpStack(e);
  160. }
  161. test.m_curTestMethod = null;
  162. }
  163. }
  164. //////////////////////////////////////////////////////////////////////////
  165. // Run Methods
  166. //////////////////////////////////////////////////////////////////////////
  167. public static int run(string reserved)
  168. {
  169. try
  170. {
  171. sysInit(reserved);
  172. SysProps.putProperty("fan.appDir", "$home/tmp/test/");
  173. //bool self = false;
  174. bool verbose = false;
  175. ArrayList targets = new ArrayList();
  176. string[] args = Tool.getArgv();
  177. if (args.Length == 0) { help(); return -1; }
  178. // process args
  179. for (int i=0; i<args.Length; i++)
  180. {
  181. string a = args[i];
  182. if (a.Length == 0) continue;
  183. if (a == "-help" || a == "-h" || a == "-?")
  184. {
  185. help();
  186. return -1;
  187. }
  188. if (a == "-version")
  189. {
  190. Fan.version("Fantom Test");
  191. return -1;
  192. }
  193. else if (a == "-v")
  194. {
  195. verbose = true;
  196. FanSysTest.verbose = true;
  197. //fanx.test.Test.verbose = true;
  198. }
  199. else if (a == "-all")
  200. {
  201. targets.Add("*");
  202. }
  203. else if (a[0] == '-')
  204. {
  205. writeLine("WARNING: Unknown option " + a);
  206. }
  207. else
  208. {
  209. targets.Add(a);
  210. }
  211. }
  212. if (targets.Count == 0) { help(); return -1; }
  213. string[] t = (string[])targets.ToArray(System.Type.GetType("System.String"));
  214. return new Fant().test(t, verbose);
  215. }
  216. catch (System.Exception e)
  217. {
  218. Err.dumpStack(e);
  219. return -1;
  220. }
  221. }
  222. static void help()
  223. {
  224. writeLine("Fantom Test");
  225. writeLine("Usage:");
  226. writeLine(" fant [options] -all");
  227. writeLine(" fant [options] <pod> [pod]*");
  228. writeLine(" fant [options] <pod>::<test>");
  229. writeLine(" fant [options] <pod>::<test>.<method>");
  230. writeLine("Note:");
  231. writeLine(" You can use * to indicate wildcard for all pods");
  232. writeLine("Options:");
  233. writeLine(" -help, -h, -? print usage help");
  234. writeLine(" -version print version");
  235. writeLine(" -v verbose mode");
  236. writeLine(" -all test all pods");
  237. }
  238. static void writeLine(string s)
  239. {
  240. System.Console.WriteLine(s);
  241. }
  242. static Fant()
  243. {
  244. // .NET will only create one new thread every 500ms, which will
  245. // throw off all the timing in testSys, so if we're running under
  246. // fant, create a larger initial thread pool to work around that.
  247. System.Threading.ThreadPool.SetMinThreads(10, 10);
  248. }
  249. //////////////////////////////////////////////////////////////////////////
  250. // Fields
  251. //////////////////////////////////////////////////////////////////////////
  252. int testCount = 0;
  253. int methodCount = 0;
  254. int totalVerifyCount = 0;
  255. int failures = 0;
  256. ArrayList failedMethods = new ArrayList();
  257. }
  258. }