1 module dmocks.util; 2 3 public import dmocks.interval; 4 import std.conv; 5 6 string nullableToString(T)(T obj) 7 { 8 if (obj is null) 9 return "<null>"; 10 return obj.to!string; 11 } 12 13 void debugLog(T...)(lazy T args) @trusted nothrow 14 { 15 debug (dmocks) 16 { 17 try 18 { 19 import std.stdio; 20 21 writefln(args); 22 } 23 catch (Exception ex) 24 { 25 assert(false, "Could not write to error log"); 26 } 27 } 28 } 29 30 template IsConcreteClass(T) 31 { 32 static if ((is(T == class)) && (!__traits(isAbstractClass, T))) 33 { 34 const bool IsConcreteClass = true; 35 } 36 else 37 { 38 const bool IsConcreteClass = false; 39 } 40 } 41 42 class InvalidOperationException : Exception 43 { 44 this(string file = __FILE__, size_t line = __LINE__) 45 { 46 super(typeof(this).stringof ~ "The requested operation is not valid.", file, line); 47 } 48 49 this(string msg, string file = __FILE__, size_t line = __LINE__) 50 { 51 super(typeof(this).stringof ~ msg, file, line); 52 } 53 } 54 55 /** 56 * Thrown when an expectation was violated during unittest execution. 57 */ 58 public class ExpectationViolationError : Error 59 { 60 this(string msg, string file = __FILE__, size_t line = __LINE__) 61 { 62 super(msg, file, line); 63 } 64 } 65 66 /** 67 * Thrown when an expectation violation was found during mocker verification. 68 */ 69 public class ExpectationViolationException : Exception 70 { 71 this(string msg, string file = __FILE__, size_t line = __LINE__) 72 { 73 super(msg, file, line); 74 } 75 } 76 77 public class MocksSetupException : Exception 78 { 79 this(string msg, string file = __FILE__, size_t line = __LINE__) 80 { 81 super(typeof(this).stringof ~ ": " ~ msg); 82 } 83 } 84 85 public string yellow(string text) 86 { 87 if (enableAnsiEscape) 88 { 89 return "\x1b[93m" ~ text ~ "\x1b[0m"; 90 } 91 return text; 92 } 93 94 private bool enableAnsiEscape() 95 { 96 version (Posix) 97 { 98 import core.sys.posix.unistd : isatty; 99 100 return isatty(1 /* stdout */) ? true : false; 101 } 102 else 103 { 104 return false; 105 } 106 }