PageRenderTime 53ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/src/sys/java/fan/sys/Test.java

https://bitbucket.org/bedlaczech/fan-1.0
Java | 247 lines | 187 code | 31 blank | 29 comment | 55 complexity | 88efb391828fb46272294495f3967023 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. // 1 Jan 06 Brian Frank Creation
  7. //
  8. package fan.sys;
  9. import java.util.*;
  10. import fanx.util.*;
  11. /**
  12. * Test is the base class of unit tests.
  13. */
  14. public class Test
  15. extends FanObj
  16. {
  17. //////////////////////////////////////////////////////////////////////////
  18. // Constructors
  19. //////////////////////////////////////////////////////////////////////////
  20. public static void make$(Test t) {}
  21. //////////////////////////////////////////////////////////////////////////
  22. // Object Overrides
  23. //////////////////////////////////////////////////////////////////////////
  24. public Type typeof()
  25. {
  26. return Sys.TestType;
  27. }
  28. //////////////////////////////////////////////////////////////////////////
  29. // Lifecycle
  30. //////////////////////////////////////////////////////////////////////////
  31. public Method curTestMethod()
  32. {
  33. if (curTestMethod == null)
  34. throw Err.make("No test currently executing for " + typeof());
  35. return curTestMethod;
  36. }
  37. public void setup() {}
  38. public void teardown() {}
  39. //////////////////////////////////////////////////////////////////////////
  40. // Verify
  41. //////////////////////////////////////////////////////////////////////////
  42. public void verify(boolean cond) { verify(cond, null); }
  43. public void verify(boolean cond, String msg)
  44. {
  45. if (!cond) fail(msg);
  46. verifyCount++;
  47. }
  48. public void verifyFalse(boolean cond) { verifyFalse(cond, null); }
  49. public void verifyFalse(boolean cond, String msg)
  50. {
  51. if (cond) fail(msg);
  52. verifyCount++;
  53. }
  54. public void verifyNull(Object a) { verifyNull(a, null); }
  55. public void verifyNull(Object a, String msg)
  56. {
  57. if (a != null)
  58. {
  59. if (msg == null) msg = s(a) + " is not null";
  60. fail(msg);
  61. }
  62. verifyCount++;
  63. }
  64. public void verifyNotNull(Object a) { verifyNotNull(a, null); }
  65. public void verifyNotNull(Object a, String msg)
  66. {
  67. if (a == null)
  68. {
  69. if (msg == null) msg = "is null";
  70. fail(msg);
  71. }
  72. verifyCount++;
  73. }
  74. public void verifyEq(Object expected, Object actual) { verifyEq(expected, actual, null); }
  75. public void verifyEq(Object expected, Object actual, String msg)
  76. {
  77. if (!OpUtil.compareEQ(expected, actual))
  78. {
  79. // if we have two multi-line strings display line in error
  80. if (expected instanceof String && actual instanceof String)
  81. {
  82. List eLines = FanStr.splitLines((String)expected);
  83. List aLines = FanStr.splitLines((String)actual);
  84. if (eLines.sz() > 1 || aLines.sz() > 1)
  85. {
  86. if (eLines.sz() != aLines.sz())
  87. {
  88. msg = "Expected " + eLines.sz() + " lines, actual " + aLines.sz() + " lines";
  89. }
  90. else
  91. {
  92. for (int i=0; i<eLines.sz(); ++i)
  93. {
  94. if (!eLines.get(i).equals(aLines.get(i)))
  95. {
  96. msg = "Line " + (i+1) + ": " + FanStr.toCode((String)eLines.get(i)) + " != " + FanStr.toCode((String)aLines.get(i));
  97. break;
  98. }
  99. }
  100. }
  101. }
  102. }
  103. if (msg == null) msg = s(expected) + " != " + s(actual);
  104. fail(msg);
  105. }
  106. if (expected != null && actual != null)
  107. {
  108. if (hash(expected) != hash(actual))
  109. {
  110. fail("Equal but different hash codes: " +
  111. expected + " (0x" + FanInt.toHex(hash(expected)) + ") ?= " +
  112. actual + " (0x" + FanInt.toHex(hash(actual)) + ")");
  113. }
  114. }
  115. verifyCount++;
  116. }
  117. public void verifyNotEq(Object expected, Object actual) { verifyNotEq(expected, actual, null); }
  118. public void verifyNotEq(Object expected, Object actual, String msg)
  119. {
  120. if (!OpUtil.compareNE(expected, actual))
  121. {
  122. if (msg == null) msg = s(expected) + " == " + s(actual);
  123. fail(msg);
  124. }
  125. verifyCount++;
  126. }
  127. public void verifySame(Object expected, Object actual) { verifySame(expected, actual, null); }
  128. public void verifySame(Object expected, Object actual, String msg)
  129. {
  130. if (expected != actual)
  131. {
  132. if (msg == null) msg = s(expected) + " !== " + s(actual);
  133. fail(msg);
  134. }
  135. verifyCount++;
  136. }
  137. public void verifyNotSame(Object expected, Object actual) { verifyNotSame(expected, actual, null); }
  138. public void verifyNotSame(Object expected, Object actual, String msg)
  139. {
  140. if (expected == actual)
  141. {
  142. if (msg == null) msg = s(expected) + " === " + s(actual);
  143. fail(msg);
  144. }
  145. verifyCount++;
  146. }
  147. public void verifyType(Object obj, Type t)
  148. {
  149. verifyEq(Type.of(obj), t);
  150. }
  151. public void verifyErr(Type errType, Func f)
  152. {
  153. try
  154. {
  155. f.call(this);
  156. }
  157. catch (Err e)
  158. {
  159. if (verbose) System.out.println(" verifyErr: " + e);
  160. if (e.typeof() == errType) { verifyCount++; return; }
  161. fail(e.typeof() + " thrown, expected " + errType);
  162. }
  163. catch (Throwable e)
  164. {
  165. if (verbose) System.out.println(" verifyErr: " + e);
  166. Err err = Err.make(e);
  167. if (err.typeof() == errType) { verifyCount++; return; }
  168. fail(e.toString() + " thrown, expected " + errType);
  169. }
  170. fail("No err thrown, expected " + errType);
  171. }
  172. public void fail() { fail(null); }
  173. public void fail(String msg)
  174. {
  175. throw err(msg);
  176. }
  177. private RuntimeException err(String msg)
  178. {
  179. if (msg == null)
  180. return TestErr.make("Test failed");
  181. else
  182. return TestErr.make("Test failed: " + msg);
  183. }
  184. static String s(Object obj)
  185. {
  186. if (obj == null) return "null";
  187. if (obj instanceof String) return FanStr.toCode((String)obj) + " [sys::Str]";
  188. if (obj instanceof List) return ((List)obj).of().toString() + obj;
  189. return toStr(obj) + " [" + FanObj.typeof(obj) + "]";
  190. }
  191. //////////////////////////////////////////////////////////////////////////
  192. // Utils
  193. //////////////////////////////////////////////////////////////////////////
  194. public Object trap(String name, List args)
  195. {
  196. if (name.equals("verifyCount")) return Long.valueOf(verifyCount);
  197. return super.trap(name, args);
  198. }
  199. public File tempDir()
  200. {
  201. if (tempDir == null)
  202. {
  203. tempDir = Env.cur().tempDir().plus(Uri.fromStr("test/"), false);
  204. tempDir.delete();
  205. tempDir.create();
  206. }
  207. return tempDir;
  208. }
  209. //////////////////////////////////////////////////////////////////////////
  210. // Fields
  211. //////////////////////////////////////////////////////////////////////////
  212. public int verifyCount;
  213. public static boolean verbose;
  214. public Method curTestMethod;
  215. File tempDir;
  216. }