PageRenderTime 50ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/src/sys/java/fanx/tools/Fant.java

https://bitbucket.org/bedlaczech/fan-1.0
Java | 340 lines | 317 code | 9 blank | 14 comment | 13 complexity | 16da9573c7299158141a2fa63eb7a11d 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. // 25 Dec 05 Brian Frank Creation
  7. //
  8. package fanx.tools;
  9. import java.io.File;
  10. import java.util.*;
  11. import fan.sys.*;
  12. import fan.sys.List;
  13. /**
  14. * Fant is the command line used to run Fantom unit tests.
  15. */
  16. public class Fant
  17. {
  18. //////////////////////////////////////////////////////////////////////////
  19. // Test
  20. //////////////////////////////////////////////////////////////////////////
  21. public int test(String[] patterns, boolean verbose)
  22. {
  23. Sys.boot();
  24. long t1 = System.currentTimeMillis();
  25. for (int i=0; i<patterns.length; ++i)
  26. {
  27. int r = test(patterns[i], verbose);
  28. if (r != 0) return r;
  29. }
  30. long t2 = System.currentTimeMillis();
  31. System.out.println();
  32. System.out.println("Time: " + (t2-t1) + "ms");
  33. System.out.println();
  34. if (failureNames.size() > 0)
  35. {
  36. System.out.println("Failed:");
  37. for (int i=0; i<failureNames.size(); ++i)
  38. System.out.println(" " + failureNames.get(i));
  39. System.out.println();
  40. }
  41. System.out.println("***");
  42. System.out.println("*** " + (failures == 0 ? "All tests passed!" : failures + " FAILURES") + " [" + testCount + " tests, " + methodCount + " methods, " + totalVerifyCount + " verifies] ");
  43. System.out.println("***");
  44. return failures;
  45. }
  46. public int test(String pattern, boolean verbose)
  47. {
  48. // first try as file name
  49. File file = new File(pattern);
  50. if (file.exists() && pattern.toLowerCase().endsWith(".fan") && !file.isDirectory())
  51. {
  52. Pod pod = Fan.compileScript(file, null);
  53. if (pod == null) return 1;
  54. test(pod.name(), verbose);
  55. return 0;
  56. }
  57. if (pattern.contains("/") || pattern.contains("\\"))
  58. throw Err.make("Unknown file: " + pattern);
  59. if (pattern.equals("*"))
  60. {
  61. List pods = Pod.list();
  62. for (int i=0; i<pods.sz(); ++i)
  63. {
  64. Pod pod = (Pod)pods.get(i);
  65. test(pod.name(), verbose);
  66. }
  67. return 0;
  68. }
  69. if (pattern.equals("sys") || pattern.startsWith("sys::"))
  70. {
  71. System.out.println();
  72. if (pattern.equals("sys"))
  73. pattern = "";
  74. else
  75. pattern = pattern.substring(5);
  76. if (!fanx.test.Test.test(pattern)) failures++;
  77. totalVerifyCount += fanx.test.Test.totalVerified;
  78. return 0;
  79. }
  80. StringTokenizer st = new StringTokenizer(pattern, ":.");
  81. String podName = st.nextToken();
  82. String testName = st.hasMoreTokens() ? st.nextToken() : "*";
  83. String methodName = st.hasMoreTokens() ? st.nextToken() : "*";
  84. System.out.println();
  85. Pod pod = Pod.find(podName, true);
  86. Type[] tests = tests(pod, testName);
  87. for (int i=0; i<tests.length; ++i)
  88. {
  89. Type testType = tests[i];
  90. Method[] methods = methods(testType, methodName);
  91. for (int j=0; j<methods.length; ++j)
  92. {
  93. String name = testType.qname() + "." + methods[j].name();
  94. System.out.println("-- Run: " + name + "...");
  95. System.out.flush();
  96. int verifyCount = runTest(tests[i], methods[j]);
  97. if (verifyCount < 0)
  98. {
  99. failures++;
  100. failureNames.add(name);
  101. }
  102. else
  103. {
  104. System.out.println(" Pass: " + name + " [" + verifyCount + "]");
  105. methodCount++;
  106. totalVerifyCount += verifyCount;
  107. }
  108. }
  109. testCount++;
  110. }
  111. return 0;
  112. }
  113. private Type[] tests(Pod pod, String testName)
  114. {
  115. // named test
  116. if (!testName.equals("*")) return new Type[] { pod.type(testName, true) };
  117. // all types which subclass Test
  118. List all = pod.types();
  119. ArrayList acc = new ArrayList();
  120. for (int i=0; i<all.sz(); ++i)
  121. {
  122. Type x = (Type)all.get(i);
  123. if (x.is(Sys.TestType) && !x.isAbstract()) acc.add(x);
  124. }
  125. return (Type[])acc.toArray(new Type[acc.size()]);
  126. }
  127. private Method[] methods(Type type, String methodName)
  128. {
  129. // named test
  130. if (!methodName.equals("*")) return new Method[] { type.method(methodName, true) };
  131. // all methods which start with "test"
  132. List all = type.methods();
  133. ArrayList acc = new ArrayList();
  134. for (int i=0; i<all.sz(); ++i)
  135. {
  136. Method m = (Method)all.get(i);
  137. if (m.name().startsWith("test") && !m.isAbstract()) acc.add(m);
  138. }
  139. return (Method[])acc.toArray(new Method[acc.size()]);
  140. // what would this look like in Fan?
  141. // if (methodName == "*") return [ type.slot(testMethod, true) ]
  142. // return type.methods.filter {|m| return m.name.startsWith("test") && !m.isAbstract }
  143. }
  144. private int runTest(Type type, Method method)
  145. {
  146. Method setup = type.method("setup", true);
  147. Method teardown = type.method("teardown", true);
  148. Test test = null;
  149. List args = null;
  150. try
  151. {
  152. test = (Test)type.make();
  153. args = new List(Sys.ObjType, new Object[] {test});
  154. }
  155. catch (Throwable e)
  156. {
  157. System.out.println();
  158. System.out.println("ERROR: Cannot make test " + type);
  159. if (e instanceof Err)
  160. ((Err)e).trace();
  161. else
  162. e.printStackTrace();
  163. return -1;
  164. }
  165. try
  166. {
  167. test.curTestMethod = method;
  168. setup.callList(args);
  169. method.callList(args);
  170. return test.verifyCount;
  171. }
  172. catch (Throwable e)
  173. {
  174. System.out.println();
  175. System.out.println("TEST FAILED");
  176. if (e instanceof Err)
  177. ((Err)e).trace();
  178. else
  179. e.printStackTrace();
  180. return -1;
  181. }
  182. finally
  183. {
  184. try
  185. {
  186. if (args != null) teardown.callList(args);
  187. }
  188. catch (Throwable e)
  189. {
  190. if (e instanceof Err)
  191. ((Err)e).trace();
  192. else
  193. e.printStackTrace();
  194. }
  195. test.curTestMethod = null;
  196. }
  197. }
  198. //////////////////////////////////////////////////////////////////////////
  199. // Run
  200. //////////////////////////////////////////////////////////////////////////
  201. /**
  202. * Main entry point for compiler.
  203. */
  204. public int run(String[] args)
  205. {
  206. try
  207. {
  208. boolean self = false;
  209. boolean verbose = false;
  210. boolean js = false;
  211. ArrayList targets = new ArrayList();
  212. if (args.length == 0) { help(); return -1; }
  213. // process args
  214. for (int i=0; i<args.length; ++i)
  215. {
  216. String a = args[i].intern();
  217. if (a.length() == 0) continue;
  218. if (a == "-help" || a == "-h" || a == "-?")
  219. {
  220. help();
  221. return -1;
  222. }
  223. if (a == "-version")
  224. {
  225. Fan.version("Fantom Test");
  226. return -1;
  227. }
  228. else if (a == "-v")
  229. {
  230. verbose = true;
  231. fan.sys.Test.verbose = true;
  232. fanx.test.Test.verbose = true;
  233. }
  234. else if (a == "-js")
  235. {
  236. js = true;
  237. }
  238. else if (a == "-all")
  239. {
  240. targets.add("*");
  241. }
  242. else if (a.charAt(0) == '-')
  243. {
  244. System.out.println("WARNING: Unknown option " + a);
  245. }
  246. else
  247. {
  248. targets.add(a);
  249. }
  250. }
  251. if (targets.size() == 0) { help(); return -1; }
  252. String[] t = (String[])targets.toArray(new String[targets.size()]);
  253. if (js)
  254. return new Fan().execute("compilerJs::TestRunner", t);
  255. else
  256. return test(t, verbose);
  257. }
  258. catch (Throwable e)
  259. {
  260. e.printStackTrace();
  261. return -1;
  262. }
  263. }
  264. /**
  265. * Dump help usage.
  266. */
  267. void help()
  268. {
  269. System.out.println("Fantom Test");
  270. System.out.println("Usage:");
  271. System.out.println(" fant [options] -all");
  272. System.out.println(" fant [options] <pod> [pod]*");
  273. System.out.println(" fant [options] <pod>::<test>");
  274. System.out.println(" fant [options] <pod>::<test>.<method>");
  275. System.out.println(" fant [options] filename");
  276. System.out.println("Options:");
  277. System.out.println(" -help, -h, -? print usage help");
  278. System.out.println(" -version print version");
  279. System.out.println(" -v verbose mode");
  280. System.out.println(" -all test all pods");
  281. System.out.println(" -js test JavaScript environment");
  282. }
  283. //////////////////////////////////////////////////////////////////////////
  284. // Main
  285. //////////////////////////////////////////////////////////////////////////
  286. /** Used by fan "[java]fanx.tools::Fant.fanMain" */
  287. public static void fanMain() throws Exception
  288. {
  289. List args = Env.cur().args();
  290. main((String[])args.toArray(new String[args.sz()]));
  291. }
  292. public static void main(final String[] args)
  293. throws Exception
  294. {
  295. System.exit(new Fant().run(args));
  296. }
  297. //////////////////////////////////////////////////////////////////////////
  298. // Fields
  299. //////////////////////////////////////////////////////////////////////////
  300. int testCount = 0;
  301. int methodCount = 0;
  302. int totalVerifyCount = 0;
  303. int failures = 0;
  304. ArrayList failureNames = new ArrayList();
  305. }