1 module mocked.tests.readme; 2 3 import mocked; 4 import unit_threaded.attrs : ShouldFail; 5 6 unittest 7 { 8 static class Dependency 9 { 10 string authorOf(string phrase) 11 { 12 return null; 13 } 14 } 15 16 enum string phrase = "[T]he meaning of a word is its use in the language."; 17 enum string expected = "L. Wittgenstein"; 18 19 Mocker mocker; 20 auto builder = mocker.mock!Dependency; 21 22 builder.expect.authorOf(phrase).returns(expected); 23 24 auto dependency = builder.get; 25 26 assert(dependency.authorOf(phrase) == expected); 27 } 28 29 unittest 30 { 31 import std.math : abs; 32 33 static class Dependency 34 { 35 public void call(float) 36 { 37 } 38 } 39 40 // This function is used to compare two floating point numbers that don't 41 // match exactly. 42 alias approxComparator = (float a, float b) { 43 return abs(a - b) <= 0.1; 44 }; 45 Configure!approxComparator mocker; 46 auto builder = mocker.mock!Dependency; 47 48 builder.expect.call(1.01); 49 50 auto mock = builder.get; 51 52 mock.call(1.02); 53 } 54 55 unittest 56 { 57 static class Dependency 58 { 59 bool isTrue() 60 { 61 return true; 62 } 63 } 64 Mocker mocker; 65 auto mock = mocker.mock!Dependency; 66 mock.expect.isTrue.passThrough; 67 68 assert(mock.get.isTrue); 69 } 70 71 unittest 72 { 73 Mocker mocker; 74 auto mock = mocker.mock!Object; 75 mock.expect.toString.returns("in abstracto"); 76 77 assert(mock.get.toString == "in abstracto"); 78 } 79 80 unittest 81 { 82 import std.exception : assertThrown; 83 84 Mocker mocker; 85 auto mock = mocker.mock!Object; 86 mock.expect.toString.throws!Exception(""); 87 88 assertThrown!Exception(mock.get.toString); 89 } 90 91 unittest 92 { 93 static bool flag = false; 94 95 static class Dependency 96 { 97 void setFlag(bool flag) 98 { 99 } 100 } 101 Mocker mocker; 102 auto mock = mocker.mock!Dependency; 103 mock.expect.setFlag.action((value) { flag = value; }); 104 105 mock.get.setFlag(true); 106 107 assert(flag); 108 } 109 110 @ShouldFail("See warning in the action section in the README") 111 unittest 112 { 113 static class Dependency 114 { 115 void setFlag(bool flag) 116 { 117 } 118 } 119 120 Mocker mocker; 121 auto mock = mocker.mock!Dependency; 122 123 foreach (action; [true, false]) 124 { 125 mock.expect.setFlag.action((value) { assert(action == value); }); 126 } 127 128 mock.get.setFlag(true); 129 mock.get.setFlag(false); 130 } 131 132 unittest 133 { 134 enum string expected = "Three times you must say it, then."; 135 Mocker mocker; 136 137 auto builder = mocker.mock!Object; 138 builder.expect.toString.returns(expected).repeat(3); 139 // Or: builder.expect.toString.returns(expected).repeatAny; 140 141 auto mock = builder.get; 142 143 assert(mock.toString() == expected); 144 assert(mock.toString() == expected); 145 assert(mock.toString() == expected); 146 } 147 148 unittest 149 { 150 static class Dependency 151 { 152 void say(string) 153 { 154 } 155 } 156 Mocker mocker; 157 auto mock = mocker.mock!Dependency; 158 mock.expect.say("Naturam expelles furca, tamen usque recurret. (Horace)"); 159 // or mock.expect.say(); to ignore the arguments 160 161 mock.get.say("Naturam expelles furca, tamen usque recurret. (Horace)"); 162 } 163 164 unittest 165 { 166 enum string vergil = "tu ne cede malis, sed contra audentior ito."; 167 enum string plotinus = "neque est alter hijus universi locus, quam anima"; 168 static class Dependency 169 { 170 string authorOf(string) 171 { 172 return null; 173 } 174 } 175 Mocker mocker; 176 auto stub = mocker.stub!Dependency; 177 stub.stub.authorOf(vergil).returns("Vergil"); 178 stub.stub.authorOf(plotinus).returns("Plotinus"); 179 180 assert(stub.get.authorOf(vergil) == "Vergil"); 181 assert(stub.get.authorOf(plotinus) == "Plotinus"); 182 } 183 184 unittest 185 { 186 static class Dependency 187 { 188 void callFirst() 189 { 190 } 191 192 void callSecond() 193 { 194 } 195 } 196 Mocker mocker; 197 auto mock = mocker.mock!Dependency.unordered; 198 199 mock.expect.callFirst; 200 mock.expect.callSecond; 201 202 mock.get.callSecond; 203 mock.get.callFirst; 204 }