1 module mocked.option; 2 3 import std.stdio; 4 import std.format; 5 import std.meta; 6 import std.traits; 7 8 /** 9 * Takes template parameter $(D_PARAM F), which is used to compare two objects 10 * of some type. 11 * 12 * $(D_PARAM F) should be a callable accepting exactly 2 arguments of the same 13 * type a returning a $(D_KEYWORD bool). 14 * 15 * Params: 16 * F = The actual comparator. 17 */ 18 struct Comparator(F...) 19 if (F.length == 1 20 && isCallable!(F[0]) 21 && is(ReturnType!F == bool) 22 && allSameType!(Parameters!F) 23 && Parameters!F.length == 2) 24 { 25 private alias T = Parameters!F[0]; 26 private alias compare = F[0]; 27 } 28 29 /** 30 * Compile-time mocker options. 31 * 32 * Params: 33 * Args = Option list. 34 */ 35 struct Options(Args...) 36 { 37 /** 38 * Fallback method that tests whether $(D_PARAM a) is equal to $(D_PARAM b). 39 * It simply does $(D_KEYWORD ==) comparison. 40 * 41 * Params: 42 * a = Left-hand side operand. 43 * b = Right-hand side operand. 44 * 45 * Returns: $(D_KEYWORD true) if `a == b`, $(D_KEYWORD false) otherwise. 46 */ 47 bool equal(T)(T a, T b) 48 { 49 return a == b; 50 } 51 52 static foreach (Arg; Args) 53 { 54 /** 55 * Overloaded, configurable method that tests whether $(D_PARAM a) is 56 * equal to $(D_PARAM b). 57 * 58 * Params: 59 * a = Left-hand side operand. 60 * b = Right-hand side operand. 61 * 62 * Returns: $(D_KEYWORD true) if `a == b`, $(D_KEYWORD false) otherwise. 63 */ 64 bool equal(Arg.T a, Arg.T b) 65 { 66 return Arg.compare(a, b); 67 } 68 } 69 }