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.

544 lines
14KB

  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. #region Obsolete
  330. /// <summary>
  331. /// Precondition check.
  332. /// </summary>
  333. [Obsolete("Set Check.UseAssertions = true and then call Check.Require")]
  334. [Conditional("DBC_CHECK_ALL"),
  335. Conditional("DBC_CHECK_INVARIANT"),
  336. Conditional("DBC_CHECK_POSTCONDITION"),
  337. Conditional("DBC_CHECK_PRECONDITION")]
  338. public static void RequireTrace(bool assertion, string message)
  339. {
  340. Trace.Assert(assertion, "Precondition: " + message);
  341. }
  342. /// <summary>
  343. /// Precondition check.
  344. /// </summary>
  345. [Obsolete("Set Check.UseAssertions = true and then call Check.Require")]
  346. [Conditional("DBC_CHECK_ALL"),
  347. Conditional("DBC_CHECK_INVARIANT"),
  348. Conditional("DBC_CHECK_POSTCONDITION"),
  349. Conditional("DBC_CHECK_PRECONDITION")]
  350. public static void RequireTrace(bool assertion)
  351. {
  352. Trace.Assert(assertion, "Precondition failed.");
  353. }
  354. /// <summary>
  355. /// Postcondition check.
  356. /// </summary>
  357. [Obsolete("Set Check.UseAssertions = true and then call Check.Ensure")]
  358. [Conditional("DBC_CHECK_ALL"),
  359. Conditional("DBC_CHECK_INVARIANT"),
  360. Conditional("DBC_CHECK_POSTCONDITION")]
  361. public static void EnsureTrace(bool assertion, string message)
  362. {
  363. Trace.Assert(assertion, "Postcondition: " + message);
  364. }
  365. /// <summary>
  366. /// Postcondition check.
  367. /// </summary>
  368. [Obsolete("Set Check.UseAssertions = true and then call Check.Ensure")]
  369. [Conditional("DBC_CHECK_ALL"),
  370. Conditional("DBC_CHECK_INVARIANT"),
  371. Conditional("DBC_CHECK_POSTCONDITION")]
  372. public static void EnsureTrace(bool assertion)
  373. {
  374. Trace.Assert(assertion, "Postcondition failed.");
  375. }
  376. /// <summary>
  377. /// Invariant check.
  378. /// </summary>
  379. [Obsolete("Set Check.UseAssertions = true and then call Check.Invariant")]
  380. [Conditional("DBC_CHECK_ALL"),
  381. Conditional("DBC_CHECK_INVARIANT")]
  382. public static void InvariantTrace(bool assertion, string message)
  383. {
  384. Trace.Assert(assertion, "Invariant: " + message);
  385. }
  386. /// <summary>
  387. /// Invariant check.
  388. /// </summary>
  389. [Obsolete("Set Check.UseAssertions = true and then call Check.Invariant")]
  390. [Conditional("DBC_CHECK_ALL"),
  391. Conditional("DBC_CHECK_INVARIANT")]
  392. public static void InvariantTrace(bool assertion)
  393. {
  394. Trace.Assert(assertion, "Invariant failed.");
  395. }
  396. /// <summary>
  397. /// Assertion check.
  398. /// </summary>
  399. [Obsolete("Set Check.UseAssertions = true and then call Check.Assert")]
  400. [Conditional("DBC_CHECK_ALL")]
  401. public static void AssertTrace(bool assertion, string message)
  402. {
  403. Trace.Assert(assertion, "Assertion: " + message);
  404. }
  405. /// <summary>
  406. /// Assertion check.
  407. /// </summary>
  408. [Obsolete("Set Check.UseAssertions = true and then call Check.Assert")]
  409. [Conditional("DBC_CHECK_ALL")]
  410. public static void AssertTrace(bool assertion)
  411. {
  412. Trace.Assert(assertion, "Assertion failed.");
  413. }
  414. #endregion // Obsolete
  415. } // End Check
  416. #region Exceptions
  417. /// <summary>
  418. /// Exception raised when a contract is broken.
  419. /// Catch this exception type if you wish to differentiate between
  420. /// any DesignByContract exception and other runtime exceptions.
  421. ///
  422. /// </summary>
  423. public class DesignByContractException : ApplicationException
  424. {
  425. protected DesignByContractException() {}
  426. protected DesignByContractException(string message) : base(message) {}
  427. protected DesignByContractException(string message, Exception inner) : base(message, inner) {}
  428. }
  429. /// <summary>
  430. /// Exception raised when a precondition fails.
  431. /// </summary>
  432. public class PreconditionException : DesignByContractException
  433. {
  434. /// <summary>
  435. /// Precondition Exception.
  436. /// </summary>
  437. public PreconditionException() {}
  438. /// <summary>
  439. /// Precondition Exception.
  440. /// </summary>
  441. public PreconditionException(string message) : base(message) {}
  442. /// <summary>
  443. /// Precondition Exception.
  444. /// </summary>
  445. public PreconditionException(string message, Exception inner) : base(message, inner) {}
  446. }
  447. /// <summary>
  448. /// Exception raised when a postcondition fails.
  449. /// </summary>
  450. public class PostconditionException : DesignByContractException
  451. {
  452. /// <summary>
  453. /// Postcondition Exception.
  454. /// </summary>
  455. public PostconditionException() {}
  456. /// <summary>
  457. /// Postcondition Exception.
  458. /// </summary>
  459. public PostconditionException(string message) : base(message) {}
  460. /// <summary>
  461. /// Postcondition Exception.
  462. /// </summary>
  463. public PostconditionException(string message, Exception inner) : base(message, inner) {}
  464. }
  465. /// <summary>
  466. /// Exception raised when an invariant fails.
  467. /// </summary>
  468. public class InvariantException : DesignByContractException
  469. {
  470. /// <summary>
  471. /// Invariant Exception.
  472. /// </summary>
  473. public InvariantException() {}
  474. /// <summary>
  475. /// Invariant Exception.
  476. /// </summary>
  477. public InvariantException(string message) : base(message) {}
  478. /// <summary>
  479. /// Invariant Exception.
  480. /// </summary>
  481. public InvariantException(string message, Exception inner) : base(message, inner) {}
  482. }
  483. /// <summary>
  484. /// Exception raised when an assertion fails.
  485. /// </summary>
  486. public class AssertionException : DesignByContractException
  487. {
  488. /// <summary>
  489. /// Assertion Exception.
  490. /// </summary>
  491. public AssertionException() {}
  492. /// <summary>
  493. /// Assertion Exception.
  494. /// </summary>
  495. public AssertionException(string message) : base(message) {}
  496. /// <summary>
  497. /// Assertion Exception.
  498. /// </summary>
  499. public AssertionException(string message, Exception inner) : base(message, inner) {}
  500. }
  501. #endregion // Exception classes
  502. } // End Design By Contract