1 module fluentasserts.core..string; 2 3 public import fluentasserts.core.base; 4 import fluentasserts.core.results; 5 6 import std..string; 7 import std.conv; 8 import std.algorithm; 9 import std.array; 10 11 /// When there is a lazy string that throws an it should throw that exception 12 unittest { 13 string someLazyString() { 14 throw new Exception("This is it."); 15 } 16 17 ({ 18 someLazyString.should.equal(""); 19 }).should.throwAnyException.withMessage("This is it."); 20 21 ({ 22 someLazyString.should.contain(""); 23 }).should.throwAnyException.withMessage("This is it."); 24 25 ({ 26 someLazyString.should.contain([""]); 27 }).should.throwAnyException.withMessage("This is it."); 28 29 ({ 30 someLazyString.should.contain(' '); 31 }).should.throwAnyException.withMessage("This is it."); 32 33 ({ 34 someLazyString.should.startWith(" "); 35 }).should.throwAnyException.withMessage("This is it."); 36 37 ({ 38 someLazyString.should.endWith(" "); 39 }).should.throwAnyException.withMessage("This is it."); 40 } 41 42 @("string startWith") 43 unittest { 44 ({ 45 "test string".should.startWith("test"); 46 }).should.not.throwAnyException; 47 48 auto msg = ({ 49 "test string".should.startWith("other"); 50 }).should.throwException!TestException.msg; 51 52 msg.split("\n")[0].should.contain(`"test string" does not start with "other"`); 53 msg.split("\n")[2].strip.should.equal(`Expected:to start with "other"`); 54 msg.split("\n")[3].strip.should.equal(`Actual:"test string"`); 55 56 ({ 57 "test string".should.not.startWith("other"); 58 }).should.not.throwAnyException; 59 60 msg = ({ 61 "test string".should.not.startWith("test"); 62 }).should.throwException!TestException.msg; 63 64 msg.split("\n")[0].should.contain(`"test string" starts with "test"`); 65 msg.split("\n")[2].strip.should.equal(`Expected:to not start with "test"`); 66 msg.split("\n")[3].strip.should.equal(`Actual:"test string"`); 67 68 ({ 69 "test string".should.startWith('t'); 70 }).should.not.throwAnyException; 71 72 msg = ({ 73 "test string".should.startWith('o'); 74 }).should.throwException!TestException.msg; 75 76 msg.split("\n")[0].should.contain(`"test string" does not start with 'o'`); 77 msg.split("\n")[2].strip.should.equal("Expected:to start with 'o'"); 78 msg.split("\n")[3].strip.should.equal(`Actual:"test string"`); 79 80 ({ 81 "test string".should.not.startWith('o'); 82 }).should.not.throwAnyException; 83 84 msg = ({ 85 "test string".should.not.startWith('t'); 86 }).should.throwException!TestException.msg; 87 88 msg.split("\n")[0].should.contain(`"test string" starts with 't'`); 89 msg.split("\n")[2].strip.should.equal(`Expected:to not start with 't'`); 90 msg.split("\n")[3].strip.should.equal(`Actual:"test string"`); 91 } 92 93 @("string endWith") 94 unittest { 95 ({ 96 "test string".should.endWith("string"); 97 }).should.not.throwAnyException; 98 99 auto msg = ({ 100 "test string".should.endWith("other"); 101 }).should.throwException!TestException.msg; 102 103 msg.split("\n")[0].should.contain(`"test string" does not end with "other"`); 104 msg.split("\n")[2].strip.should.equal(`Expected:to end with "other"`); 105 msg.split("\n")[3].strip.should.equal(`Actual:"test string"`); 106 107 ({ 108 "test string".should.not.endWith("other"); 109 }).should.not.throwAnyException; 110 111 msg = ({ 112 "test string".should.not.endWith("string"); 113 }).should.throwException!TestException.msg; 114 115 msg.split("\n")[0].should.equal(`"test string" should not end with "string". "test string" ends with "string".`); 116 msg.split("\n")[2].strip.should.equal(`Expected:to not end with "string"`); 117 msg.split("\n")[3].strip.should.equal(`Actual:"test string"`); 118 119 ({ 120 "test string".should.endWith('g'); 121 }).should.not.throwAnyException; 122 123 msg = ({ 124 "test string".should.endWith('t'); 125 }).should.throwException!TestException.msg; 126 127 msg.split("\n")[0].should.contain(`"test string" does not end with 't'`); 128 msg.split("\n")[2].strip.should.equal("Expected:to end with 't'"); 129 msg.split("\n")[3].strip.should.equal(`Actual:"test string"`); 130 131 ({ 132 "test string".should.not.endWith('w'); 133 }).should.not.throwAnyException; 134 135 msg = ({ 136 "test string".should.not.endWith('g'); 137 }).should.throwException!TestException.msg; 138 139 msg.split("\n")[0].should.contain(`"test string" ends with 'g'`); 140 msg.split("\n")[2].strip.should.equal("Expected:to not end with 'g'"); 141 msg.split("\n")[3].strip.should.equal(`Actual:"test string"`); 142 } 143 144 @("string contain") 145 unittest { 146 ({ 147 "test string".should.contain(["string", "test"]); 148 "test string".should.not.contain(["other", "message"]); 149 }).should.not.throwAnyException; 150 151 ({ 152 "test string".should.contain("string"); 153 "test string".should.not.contain("other"); 154 }).should.not.throwAnyException; 155 156 ({ 157 "test string".should.contain('s'); 158 "test string".should.not.contain('z'); 159 }).should.not.throwAnyException; 160 161 auto msg = ({ 162 "test string".should.contain(["other", "message"]); 163 }).should.throwException!TestException.msg; 164 165 msg.split("\n")[0].should.equal(`"test string" should contain ["other", "message"]. ["other", "message"] are missing from "test string".`); 166 msg.split("\n")[2].strip.should.equal(`Expected:to contain all ["other", "message"]`); 167 msg.split("\n")[3].strip.should.equal("Actual:test string"); 168 169 msg = ({ 170 "test string".should.not.contain(["test", "string"]); 171 }).should.throwException!TestException.msg; 172 173 msg.split("\n")[0].should.equal(`"test string" should not contain ["test", "string"]. ["test", "string"] are present in "test string".`); 174 msg.split("\n")[2].strip.should.equal(`Expected:to not contain any ["test", "string"]`); 175 msg.split("\n")[3].strip.should.equal("Actual:test string"); 176 177 msg = ({ 178 "test string".should.contain("other"); 179 }).should.throwException!TestException.msg; 180 181 msg.split("\n")[0].should.equal(`"test string" should contain "other". other is missing from "test string".`); 182 msg.split("\n")[2].strip.should.equal(`Expected:to contain "other"`); 183 msg.split("\n")[3].strip.should.equal("Actual:test string"); 184 185 msg = ({ 186 "test string".should.not.contain("test"); 187 }).should.throwException!TestException.msg; 188 189 msg.split("\n")[0].should.equal(`"test string" should not contain "test". test is present in "test string".`); 190 msg.split("\n")[2].strip.should.equal(`Expected:to not contain "test"`); 191 msg.split("\n")[3].strip.should.equal("Actual:test string"); 192 193 msg = ({ 194 "test string".should.contain('o'); 195 }).should.throwException!TestException.msg; 196 197 msg.split("\n")[0].should.contain(`o is missing from "test string"`); 198 msg.split("\n")[2].strip.should.equal("Expected:to contain 'o'"); 199 msg.split("\n")[3].strip.should.equal("Actual:test string"); 200 201 msg = ({ 202 "test string".should.not.contain('t'); 203 }).should.throwException!TestException.msg; 204 205 msg.split("\n")[0].should.equal(`"test string" should not contain 't'. t is present in "test string".`); 206 msg.split("\n")[2].strip.should.equal("Expected:to not contain 't'"); 207 msg.split("\n")[3].strip.should.equal("Actual:test string"); 208 } 209 210 @("string equal") 211 unittest { 212 ({ 213 "test string".should.equal("test string"); 214 }).should.not.throwAnyException; 215 216 ({ 217 "test string".should.not.equal("test"); 218 }).should.not.throwAnyException; 219 220 auto msg = ({ 221 "test string".should.equal("test"); 222 }).should.throwException!TestException.msg; 223 224 msg.split("\n")[0].should.equal(`"test string" should equal "test". "test string" is not equal to "test".`); 225 226 msg = ({ 227 "test string".should.not.equal("test string"); 228 }).should.throwException!TestException.msg; 229 230 msg.split("\n")[0].should.equal(`"test string" should not equal "test string". "test string" is equal to "test string".`); 231 232 msg = ({ 233 ubyte[] data = [115, 111, 109, 101, 32, 100, 97, 116, 97, 0, 0]; 234 data.assumeUTF.to!string.should.equal("some data"); 235 }).should.throwException!TestException.msg; 236 237 msg.should.contain(`Actual:"some data\0\0"`); 238 msg.should.contain(`data.assumeUTF.to!string should equal "some data". "some data\0\0" is not equal to "some data".`); 239 msg.should.contain(`some data[+\0\0]`); 240 } 241 242 /// should throw exceptions for delegates that return basic types 243 unittest { 244 string value() { 245 throw new Exception("not implemented"); 246 } 247 248 value().should.throwAnyException.withMessage.equal("not implemented"); 249 250 string noException() { return null; } 251 bool thrown; 252 253 try { 254 noException.should.throwAnyException; 255 } catch(TestException e) { 256 e.msg.should.startWith("noException should throw any exception. No exception was thrown."); 257 thrown = true; 258 } 259 260 thrown.should.equal(true); 261 } 262 263 @("const string equal") 264 unittest { 265 const string constValue = "test string"; 266 immutable string immutableValue = "test string"; 267 268 constValue.should.equal("test string"); 269 immutableValue.should.equal("test string"); 270 271 "test string".should.equal(constValue); 272 "test string".should.equal(immutableValue); 273 }