Mirror of Svelto.ECS because we're a fan of it
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DesignByContract.cs 11KB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. // from: http://www.codeproject.com/Articles/1863/Design-by-Contract-Framework
  2. // Provides support for Design By Contract
  3. // as described by Bertrand Meyer in his seminal book,
  4. // Object-Oriented Software Construction (2nd Ed) Prentice Hall 1997
  5. // (See chapters 11 and 12).
  6. //
  7. // See also Building Bug-free O-O Software: An Introduction to Design by Contract
  8. // http://www.eiffel.com/doc/manuals/technology/contract/
  9. //
  10. // The following conditional compilation symbols are supported:
  11. //
  12. // These suggestions are based on Bertrand Meyer's Object-Oriented Software Construction (2nd Ed) p393
  13. //
  14. // DBC_CHECK_ALL - Check assertions - implies checking preconditions, postconditions and invariants
  15. // DBC_CHECK_INVARIANT - Check invariants - implies checking preconditions and postconditions
  16. // DBC_CHECK_POSTCONDITION - Check postconditions - implies checking preconditions
  17. // DBC_CHECK_PRECONDITION - Check preconditions only, e.g., in Release build
  18. //
  19. // A suggested default usage scenario is the following:
  20. //
  21. // #if DEBUG
  22. // #define DBC_CHECK_ALL
  23. // #else
  24. // #define DBC_CHECK_PRECONDITION
  25. // #endif
  26. //
  27. // Alternatively, you can define these in the project properties dialog.
  28. #if UNITY_EDITOR || ROBO_TEST_BUILD
  29. #define DBC_CHECK_ALL
  30. #endif
  31. using System;
  32. using System.Diagnostics;
  33. namespace DesignByContract
  34. {
  35. /// <summary>
  36. /// Design By Contract Checks.
  37. ///
  38. /// Each method generates an exception or
  39. /// a trace assertion statement if the contract is broken.
  40. /// </summary>
  41. /// <remarks>
  42. /// This example shows how to call the Require method.
  43. /// Assume DBC_CHECK_PRECONDITION is defined.
  44. /// <code>
  45. /// public void Test(int x)
  46. /// {
  47. /// try
  48. /// {
  49. /// Check.Require(x > 1, "x must be > 1");
  50. /// }
  51. /// catch (System.Exception ex)
  52. /// {
  53. /// Console.WriteLine(ex.ToString());
  54. /// }
  55. /// }
  56. /// </code>
  57. /// If you wish to use trace assertion statements, intended for Debug scenarios,
  58. /// rather than exception handling then set
  59. ///
  60. /// <code>Check.UseAssertions = true</code>
  61. ///
  62. /// You can specify this in your application entry point and maybe make it
  63. /// dependent on conditional compilation flags or configuration file settings, e.g.,
  64. /// <code>
  65. /// #if DBC_USE_ASSERTIONS
  66. /// Check.UseAssertions = true;
  67. /// #endif
  68. /// </code>
  69. /// You can direct output to a Trace listener. For example, you could insert
  70. /// <code>
  71. /// Trace.Listeners.Clear();
  72. /// Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
  73. /// </code>
  74. ///
  75. /// or direct output to a file or the Event Log.
  76. ///
  77. /// (Note: For ASP.NET clients use the Listeners collection
  78. /// of the Debug, not the Trace, object and, for a Release build, only exception-handling
  79. /// is possible.)
  80. /// </remarks>
  81. ///
  82. public sealed class Check
  83. {
  84. #region Interface
  85. /// <summary>
  86. /// Precondition check.
  87. /// </summary>
  88. [Conditional("DBC_CHECK_ALL"),
  89. Conditional("DBC_CHECK_INVARIANT"),
  90. Conditional("DBC_CHECK_POSTCONDITION"),
  91. Conditional("DBC_CHECK_PRECONDITION")]
  92. public static void Require(bool assertion, string message)
  93. {
  94. if (UseExceptions)
  95. {
  96. if (!assertion)
  97. throw new PreconditionException(message);
  98. }
  99. else
  100. {
  101. Trace.Assert(assertion, "Precondition: " + message);
  102. }
  103. }
  104. /// <summary>
  105. /// Precondition check.
  106. /// </summary>
  107. [Conditional("DBC_CHECK_ALL"),
  108. Conditional("DBC_CHECK_INVARIANT"),
  109. Conditional("DBC_CHECK_POSTCONDITION"),
  110. Conditional("DBC_CHECK_PRECONDITION")]
  111. public static void Require(bool assertion, string message, Exception inner)
  112. {
  113. if (UseExceptions)
  114. {
  115. if (!assertion)
  116. throw new PreconditionException(message, inner);
  117. }
  118. else
  119. {
  120. Trace.Assert(assertion, "Precondition: " + message);
  121. }
  122. }
  123. /// <summary>
  124. /// Precondition check.
  125. /// </summary>
  126. [Conditional("DBC_CHECK_ALL"),
  127. Conditional("DBC_CHECK_INVARIANT"),
  128. Conditional("DBC_CHECK_POSTCONDITION"),
  129. Conditional("DBC_CHECK_PRECONDITION")]
  130. public static void Require(bool assertion)
  131. {
  132. if (UseExceptions)
  133. {
  134. if (!assertion)
  135. throw new PreconditionException("Precondition failed.");
  136. }
  137. else
  138. {
  139. Trace.Assert(assertion, "Precondition failed.");
  140. }
  141. }
  142. /// <summary>
  143. /// Postcondition check.
  144. /// </summary>
  145. [Conditional("DBC_CHECK_ALL"),
  146. Conditional("DBC_CHECK_INVARIANT"),
  147. Conditional("DBC_CHECK_POSTCONDITION")]
  148. public static void Ensure(bool assertion, string message)
  149. {
  150. if (UseExceptions)
  151. {
  152. if (!assertion)
  153. throw new PostconditionException(message);
  154. }
  155. else
  156. {
  157. Trace.Assert(assertion, "Postcondition: " + message);
  158. }
  159. }
  160. /// <summary>
  161. /// Postcondition check.
  162. /// </summary>
  163. [Conditional("DBC_CHECK_ALL"),
  164. Conditional("DBC_CHECK_INVARIANT"),
  165. Conditional("DBC_CHECK_POSTCONDITION")]
  166. public static void Ensure(bool assertion, string message, Exception inner)
  167. {
  168. if (UseExceptions)
  169. {
  170. if (!assertion)
  171. throw new PostconditionException(message, inner);
  172. }
  173. else
  174. {
  175. Trace.Assert(assertion, "Postcondition: " + message);
  176. }
  177. }
  178. /// <summary>
  179. /// Postcondition check.
  180. /// </summary>
  181. [Conditional("DBC_CHECK_ALL"),
  182. Conditional("DBC_CHECK_INVARIANT"),
  183. Conditional("DBC_CHECK_POSTCONDITION")]
  184. public static void Ensure(bool assertion)
  185. {
  186. if (UseExceptions)
  187. {
  188. if (!assertion)
  189. throw new PostconditionException("Postcondition failed.");
  190. }
  191. else
  192. {
  193. Trace.Assert(assertion, "Postcondition failed.");
  194. }
  195. }
  196. /// <summary>
  197. /// Invariant check.
  198. /// </summary>
  199. [Conditional("DBC_CHECK_ALL"),
  200. Conditional("DBC_CHECK_INVARIANT")]
  201. public static void Invariant(bool assertion, string message)
  202. {
  203. if (UseExceptions)
  204. {
  205. if (!assertion)
  206. throw new InvariantException(message);
  207. }
  208. else
  209. {
  210. Trace.Assert(assertion, "Invariant: " + message);
  211. }
  212. }
  213. /// <summary>
  214. /// Invariant check.
  215. /// </summary>
  216. [Conditional("DBC_CHECK_ALL"),
  217. Conditional("DBC_CHECK_INVARIANT")]
  218. public static void Invariant(bool assertion, string message, Exception inner)
  219. {
  220. if (UseExceptions)
  221. {
  222. if (!assertion)
  223. throw new InvariantException(message, inner);
  224. }
  225. else
  226. {
  227. Trace.Assert(assertion, "Invariant: " + message);
  228. }
  229. }
  230. /// <summary>
  231. /// Invariant check.
  232. /// </summary>
  233. [Conditional("DBC_CHECK_ALL"),
  234. Conditional("DBC_CHECK_INVARIANT")]
  235. public static void Invariant(bool assertion)
  236. {
  237. if (UseExceptions)
  238. {
  239. if (!assertion)
  240. throw new InvariantException("Invariant failed.");
  241. }
  242. else
  243. {
  244. Trace.Assert(assertion, "Invariant failed.");
  245. }
  246. }
  247. /// <summary>
  248. /// Assertion check.
  249. /// </summary>
  250. [Conditional("DBC_CHECK_ALL")]
  251. public static void Assert(bool assertion, string message)
  252. {
  253. if (UseExceptions)
  254. {
  255. if (!assertion)
  256. throw new AssertionException(message);
  257. }
  258. else
  259. {
  260. Trace.Assert(assertion, "Assertion: " + message);
  261. }
  262. }
  263. /// <summary>
  264. /// Assertion check.
  265. /// </summary>
  266. [Conditional("DBC_CHECK_ALL")]
  267. public static void Assert(bool assertion, string message, Exception inner)
  268. {
  269. if (UseExceptions)
  270. {
  271. if (!assertion)
  272. throw new AssertionException(message, inner);
  273. }
  274. else
  275. {
  276. Trace.Assert(assertion, "Assertion: " + message);
  277. }
  278. }
  279. /// <summary>
  280. /// Assertion check.
  281. /// </summary>
  282. [Conditional("DBC_CHECK_ALL")]
  283. public static void Assert(bool assertion)
  284. {
  285. if (UseExceptions)
  286. {
  287. if (!assertion)
  288. throw new AssertionException("Assertion failed.");
  289. }
  290. else
  291. {
  292. Trace.Assert(assertion, "Assertion failed.");
  293. }
  294. }
  295. /// <summary>
  296. /// Set this if you wish to use Trace Assert statements
  297. /// instead of exception handling.
  298. /// (The Check class uses exception handling by default.)
  299. /// </summary>
  300. public static bool UseAssertions
  301. {
  302. get
  303. {
  304. return useAssertions;
  305. }
  306. set
  307. {
  308. useAssertions = value;
  309. }
  310. }
  311. #endregion // Interface
  312. #region Implementation
  313. // No creation
  314. private Check() {}
  315. /// <summary>
  316. /// Is exception handling being used?
  317. /// </summary>
  318. private static bool UseExceptions
  319. {
  320. get
  321. {
  322. return !useAssertions;
  323. }
  324. }
  325. // Are trace assertion statements being used?
  326. // Default is to use exception handling.
  327. private static bool useAssertions = false;
  328. #endregion // Implementation
  329. } // End Check
  330. internal class Trace
  331. {
  332. internal static void Assert(bool assertion, string v)
  333. {
  334. #if NETFX_CORE
  335. System.Diagnostics.Contracts.Contract.Assert(assertion, v);
  336. #else
  337. System.Diagnostics.Trace.Assert(assertion, v);
  338. #endif
  339. }
  340. }
  341. #region Exceptions
  342. /// <summary>
  343. /// Exception raised when a contract is broken.
  344. /// Catch this exception type if you wish to differentiate between
  345. /// any DesignByContract exception and other runtime exceptions.
  346. ///
  347. /// </summary>
  348. public class DesignByContractException : ApplicationException
  349. {
  350. protected DesignByContractException() {}
  351. protected DesignByContractException(string message) : base(message) {}
  352. protected DesignByContractException(string message, Exception inner) : base(message, inner) {}
  353. }
  354. /// <summary>
  355. /// Exception raised when a precondition fails.
  356. /// </summary>
  357. public class PreconditionException : DesignByContractException
  358. {
  359. /// <summary>
  360. /// Precondition Exception.
  361. /// </summary>
  362. public PreconditionException() {}
  363. /// <summary>
  364. /// Precondition Exception.
  365. /// </summary>
  366. public PreconditionException(string message) : base(message) {}
  367. /// <summary>
  368. /// Precondition Exception.
  369. /// </summary>
  370. public PreconditionException(string message, Exception inner) : base(message, inner) {}
  371. }
  372. /// <summary>
  373. /// Exception raised when a postcondition fails.
  374. /// </summary>
  375. public class PostconditionException : DesignByContractException
  376. {
  377. /// <summary>
  378. /// Postcondition Exception.
  379. /// </summary>
  380. public PostconditionException() {}
  381. /// <summary>
  382. /// Postcondition Exception.
  383. /// </summary>
  384. public PostconditionException(string message) : base(message) {}
  385. /// <summary>
  386. /// Postcondition Exception.
  387. /// </summary>
  388. public PostconditionException(string message, Exception inner) : base(message, inner) {}
  389. }
  390. /// <summary>
  391. /// Exception raised when an invariant fails.
  392. /// </summary>
  393. public class InvariantException : DesignByContractException
  394. {
  395. /// <summary>
  396. /// Invariant Exception.
  397. /// </summary>
  398. public InvariantException() {}
  399. /// <summary>
  400. /// Invariant Exception.
  401. /// </summary>
  402. public InvariantException(string message) : base(message) {}
  403. /// <summary>
  404. /// Invariant Exception.
  405. /// </summary>
  406. public InvariantException(string message, Exception inner) : base(message, inner) {}
  407. }
  408. /// <summary>
  409. /// Exception raised when an assertion fails.
  410. /// </summary>
  411. public class AssertionException : DesignByContractException
  412. {
  413. /// <summary>
  414. /// Assertion Exception.
  415. /// </summary>
  416. public AssertionException() {}
  417. /// <summary>
  418. /// Assertion Exception.
  419. /// </summary>
  420. public AssertionException(string message) : base(message) {}
  421. /// <summary>
  422. /// Assertion Exception.
  423. /// </summary>
  424. public AssertionException(string message, Exception inner) : base(message, inner) {}
  425. }
  426. #endregion // Exception classes
  427. } // End Design By Contract