PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/sys/dotnet/fan/sys/Test.cs

https://bitbucket.org/bedlaczech/fan-1.0
C# | 223 lines | 164 code | 30 blank | 29 comment | 48 complexity | 574fdb23885926fcd91a3c78900b0f9c 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. // 20 Dec 06 Andy Frank Creation
  7. //
  8. using System;
  9. using System.Collections;
  10. using Fanx.Util;
  11. namespace Fan.Sys
  12. {
  13. /// <summary>
  14. /// Test is the base class of unit tests.
  15. /// </summary>
  16. public class Test : FanObj
  17. {
  18. //////////////////////////////////////////////////////////////////////////
  19. // Constructors
  20. //////////////////////////////////////////////////////////////////////////
  21. public static void make_(Test t) {}
  22. //////////////////////////////////////////////////////////////////////////
  23. // Obj Overrides
  24. //////////////////////////////////////////////////////////////////////////
  25. public override Type @typeof()
  26. {
  27. return Sys.TestType;
  28. }
  29. //////////////////////////////////////////////////////////////////////////
  30. // Lifecycle
  31. //////////////////////////////////////////////////////////////////////////
  32. public Method curTestMethod()
  33. {
  34. if (m_curTestMethod == null)
  35. throw Err.make("No test currently executing for " + @typeof()).val;
  36. return m_curTestMethod;
  37. }
  38. public virtual void setup() {}
  39. public virtual void teardown() {}
  40. //////////////////////////////////////////////////////////////////////////
  41. // verify
  42. //////////////////////////////////////////////////////////////////////////
  43. public void verify(bool cond) { verify(cond, null); }
  44. public void verify(bool cond, string msg)
  45. {
  46. if (!cond) fail(msg);
  47. verifyCount++;
  48. }
  49. public void verifyFalse(bool cond) { verifyFalse(cond, null); }
  50. public void verifyFalse(bool cond, string msg)
  51. {
  52. if (cond) fail(msg);
  53. verifyCount++;
  54. }
  55. public void verifyNull(object a) { verifyNull(a, null); }
  56. public void verifyNull(object a, string msg)
  57. {
  58. if (a != null)
  59. {
  60. if (msg == null) msg = s(a) + " is not null";
  61. fail(msg);
  62. }
  63. verifyCount++;
  64. }
  65. public void verifyNotNull(object a) { verifyNotNull(a, null); }
  66. public void verifyNotNull(object a, string msg)
  67. {
  68. if (a == null)
  69. {
  70. if (msg == null) msg = "is null";
  71. fail(msg);
  72. }
  73. verifyCount++;
  74. }
  75. public void verifyEq(object expected, object actual) { verifyEq(expected, actual, null); }
  76. public void verifyEq(object expected, object actual, string msg)
  77. {
  78. if (!OpUtil.compareEQ(expected, actual))
  79. {
  80. //if (msg == null) msg = s(expected) + " != " + s(actual);
  81. if (msg == null) msg = s(expected) +
  82. " [" + (expected != null ? expected.GetType().ToString() : "null") + "] != "
  83. + s(actual) + " [" + (actual != null ? actual.GetType().ToString() : "null") + "]";
  84. fail(msg);
  85. }
  86. if (expected != null && actual != null)
  87. {
  88. if (hash(expected) != hash(actual))
  89. {
  90. fail("Equal but different hash codes: " +
  91. expected + " (0x" + FanInt.toHex(hash(expected)) + ") ?= " +
  92. actual + " (0x" + FanInt.toHex(hash(actual)) + ")");
  93. }
  94. }
  95. verifyCount++;
  96. }
  97. public void verifyNotEq(object expected, object actual) { verifyNotEq(expected, actual, null); }
  98. public void verifyNotEq(object expected, object actual, string msg)
  99. {
  100. if (!OpUtil.compareNE(expected, actual))
  101. {
  102. if (msg == null) msg = s(expected) + " == " + s(actual);
  103. fail(msg);
  104. }
  105. verifyCount++;
  106. }
  107. public void verifySame(object expected, object actual) { verifySame(expected, actual, null); }
  108. public void verifySame(object expected, object actual, string msg)
  109. {
  110. if (expected != actual)
  111. {
  112. if (msg == null) msg = s(expected) + " !== " + s(actual);
  113. fail(msg);
  114. }
  115. verifyCount++;
  116. }
  117. public void verifyNotSame(object expected, object actual) { verifyNotSame(expected, actual, null); }
  118. public void verifyNotSame(object expected, object actual, string msg)
  119. {
  120. if (expected == actual)
  121. {
  122. if (msg == null) msg = s(expected) + " === " + s(actual);
  123. fail(msg);
  124. }
  125. verifyCount++;
  126. }
  127. public void verifyType(object obj, Type t)
  128. {
  129. verifyEq(Type.of(obj), t);
  130. }
  131. public void verifyErr(Type errType, Func f)
  132. {
  133. try
  134. {
  135. f.call(this);
  136. }
  137. catch (Err.Val e)
  138. {
  139. if (verbose) System.Console.WriteLine(" verifyErr: " + e);
  140. if (e.err().@typeof() == errType) { verifyCount++; return; }
  141. fail(e.err().@typeof() + " thrown, expected " + errType);
  142. }
  143. catch (System.Exception e)
  144. {
  145. if (verbose) System.Console.WriteLine(" verifyErr: " + e);
  146. Err err = Fan.Sys.Err.make(e);
  147. if (err.@typeof() == errType) { verifyCount++; return; }
  148. fail(e.GetType() + " thrown, expected " + errType);
  149. }
  150. fail("No err thrown, expected " + errType);
  151. }
  152. public void fail() { fail(null); }
  153. public void fail(string msg)
  154. {
  155. throw err(msg);
  156. }
  157. private Exception err(string msg)
  158. {
  159. if (msg == null)
  160. return Fan.Sys.TestErr.make("Test failed").val;
  161. else
  162. return Fan.Sys.TestErr.make("Test failed: " + msg).val;
  163. }
  164. private static string s(object obj)
  165. {
  166. if (obj == null) return "null";
  167. if (obj is string) return FanStr.toCode((string)obj);
  168. if (obj is List) return ((List)obj).of().ToString() + obj;
  169. return toStr(obj);
  170. }
  171. //////////////////////////////////////////////////////////////////////////
  172. // Utils
  173. //////////////////////////////////////////////////////////////////////////
  174. public File tempDir()
  175. {
  176. if (m_tempDir == null)
  177. {
  178. m_tempDir = Env.cur().tempDir().plus(Uri.fromStr("test/"), false);
  179. m_tempDir.delete();
  180. m_tempDir.create();
  181. }
  182. return m_tempDir;
  183. }
  184. //////////////////////////////////////////////////////////////////////////
  185. // Fields
  186. //////////////////////////////////////////////////////////////////////////
  187. internal static readonly Hashtable idMap = new Hashtable(); // qname -> Integer
  188. public int verifyCount;
  189. public static bool verbose;
  190. public Method m_curTestMethod;
  191. File m_tempDir = null;
  192. }
  193. }