Advertisement
Guest User

Untitled

a guest
Jul 15th, 2013
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 1.12 KB | None | 0 0
  1. import std.stdio;
  2. import std.typetuple;
  3.  
  4. enum ctfe;
  5.  
  6. class AssertException : Exception
  7. {
  8.     this (string message, string file = __FILE__, size_t line = __LINE__)
  9.     {
  10.         super(message, file, line);
  11.     }
  12. }
  13.  
  14. void myAssert (bool value, string file = __FILE__, size_t line = __LINE__)
  15. {
  16.     if (!value)
  17.         throw new AssertException("Assertion failed", file, line);
  18. }
  19.  
  20. void bar (int a)
  21. {
  22.     myAssert(a == 3);
  23. }
  24.  
  25. @ctfe unittest
  26. {
  27.     bar(4);
  28. }
  29.  
  30. int runTest (alias test) ()
  31. {
  32.     try
  33.         test();
  34.  
  35.     catch (AssertException e)
  36.     {
  37.         assert(0); // should print some info here put writeln isn't ctfe-able
  38.         return 1;
  39.     }
  40.  
  41.     return 0;
  42. }
  43.  
  44. template Tuple (T...)
  45. {
  46.     alias Tuple = T;
  47. }
  48.  
  49. int runCtfeUnitTests ()
  50. {
  51.     int result;
  52.  
  53.     foreach (test ; __traits(getUnitTests, mixin(__MODULE__)))
  54.     {
  55.         alias attributes = Tuple!(__traits(getAttributes, test));
  56.  
  57.         if (staticIndexOf!(ctfe, attributes) != -1)
  58.             result = runTest!(test);
  59.     }
  60.  
  61.     return result;
  62. }
  63.  
  64. enum result = runCtfeUnitTests();
  65.  
  66. int main ()
  67. {
  68.     return result;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement