102030405060708090100110120130146150160172182190200212222230240252262270280292302310320332342350360372382390400410420430442452460470483492500510522532542550562572580590603612620630642652662670682692700710723732740750762772782790802812820830843852860870882892902910920930940952962970980993100210101020103210421052106010721082109011001113112211301140115211621172118011921202121012201233124212501260127212821292130013121322133013401353136213701380139214021412142014301440145014621472148214901500151215221532154015501562157215821590160016131622163016401652166216721680169317021710172017321742175217601773178217901800181218221832184018531862187018801892190219121920193319421950196019721982199220002013202220302040205220622072208020902100211021222132214021502162217221802190220322122220223022422250226322722280229023022310232323312342235023602372238223922400241024202430244024512460247024822490250125112520253025422550256225712580259026022610262026302640265126612670268226922700271227222730 module fluentasserts.core.string; public import fluentasserts.core.base; import fluentasserts.core.results; import std.string; import std.conv; import std.algorithm; import std.array; /// When there is a lazy string that throws an it should throw that exception unittest { string someLazyString() { throw new Exception("This is it."); } ({ someLazyString.should.equal(""); }).should.throwAnyException.withMessage("This is it."); ({ someLazyString.should.contain(""); }).should.throwAnyException.withMessage("This is it."); ({ someLazyString.should.contain([""]); }).should.throwAnyException.withMessage("This is it."); ({ someLazyString.should.contain(' '); }).should.throwAnyException.withMessage("This is it."); ({ someLazyString.should.startWith(" "); }).should.throwAnyException.withMessage("This is it."); ({ someLazyString.should.endWith(" "); }).should.throwAnyException.withMessage("This is it."); } @("string startWith") unittest { ({ "test string".should.startWith("test"); }).should.not.throwAnyException; auto msg = ({ "test string".should.startWith("other"); }).should.throwException!TestException.msg; msg.split("\n")[0].should.contain(`"test string" does not start with "other"`); msg.split("\n")[2].strip.should.equal(`Expected:to start with "other"`); msg.split("\n")[3].strip.should.equal(`Actual:"test string"`); ({ "test string".should.not.startWith("other"); }).should.not.throwAnyException; msg = ({ "test string".should.not.startWith("test"); }).should.throwException!TestException.msg; msg.split("\n")[0].should.contain(`"test string" starts with "test"`); msg.split("\n")[2].strip.should.equal(`Expected:to not start with "test"`); msg.split("\n")[3].strip.should.equal(`Actual:"test string"`); ({ "test string".should.startWith('t'); }).should.not.throwAnyException; msg = ({ "test string".should.startWith('o'); }).should.throwException!TestException.msg; msg.split("\n")[0].should.contain(`"test string" does not start with 'o'`); msg.split("\n")[2].strip.should.equal("Expected:to start with 'o'"); msg.split("\n")[3].strip.should.equal(`Actual:"test string"`); ({ "test string".should.not.startWith('o'); }).should.not.throwAnyException; msg = ({ "test string".should.not.startWith('t'); }).should.throwException!TestException.msg; msg.split("\n")[0].should.contain(`"test string" starts with 't'`); msg.split("\n")[2].strip.should.equal(`Expected:to not start with 't'`); msg.split("\n")[3].strip.should.equal(`Actual:"test string"`); } @("string endWith") unittest { ({ "test string".should.endWith("string"); }).should.not.throwAnyException; auto msg = ({ "test string".should.endWith("other"); }).should.throwException!TestException.msg; msg.split("\n")[0].should.contain(`"test string" does not end with "other"`); msg.split("\n")[2].strip.should.equal(`Expected:to end with "other"`); msg.split("\n")[3].strip.should.equal(`Actual:"test string"`); ({ "test string".should.not.endWith("other"); }).should.not.throwAnyException; msg = ({ "test string".should.not.endWith("string"); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal(`"test string" should not end with "string". "test string" ends with "string".`); msg.split("\n")[2].strip.should.equal(`Expected:to not end with "string"`); msg.split("\n")[3].strip.should.equal(`Actual:"test string"`); ({ "test string".should.endWith('g'); }).should.not.throwAnyException; msg = ({ "test string".should.endWith('t'); }).should.throwException!TestException.msg; msg.split("\n")[0].should.contain(`"test string" does not end with 't'`); msg.split("\n")[2].strip.should.equal("Expected:to end with 't'"); msg.split("\n")[3].strip.should.equal(`Actual:"test string"`); ({ "test string".should.not.endWith('w'); }).should.not.throwAnyException; msg = ({ "test string".should.not.endWith('g'); }).should.throwException!TestException.msg; msg.split("\n")[0].should.contain(`"test string" ends with 'g'`); msg.split("\n")[2].strip.should.equal("Expected:to not end with 'g'"); msg.split("\n")[3].strip.should.equal(`Actual:"test string"`); } @("string contain") unittest { ({ "test string".should.contain(["string", "test"]); "test string".should.not.contain(["other", "message"]); }).should.not.throwAnyException; ({ "test string".should.contain("string"); "test string".should.not.contain("other"); }).should.not.throwAnyException; ({ "test string".should.contain('s'); "test string".should.not.contain('z'); }).should.not.throwAnyException; auto msg = ({ "test string".should.contain(["other", "message"]); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal(`"test string" should contain ["other", "message"]. ["other", "message"] are missing from "test string".`); msg.split("\n")[2].strip.should.equal(`Expected:to contain all ["other", "message"]`); msg.split("\n")[3].strip.should.equal("Actual:test string"); msg = ({ "test string".should.not.contain(["test", "string"]); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal(`"test string" should not contain ["test", "string"]. ["test", "string"] are present in "test string".`); msg.split("\n")[2].strip.should.equal(`Expected:to not contain any ["test", "string"]`); msg.split("\n")[3].strip.should.equal("Actual:test string"); msg = ({ "test string".should.contain("other"); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal(`"test string" should contain "other". other is missing from "test string".`); msg.split("\n")[2].strip.should.equal(`Expected:to contain "other"`); msg.split("\n")[3].strip.should.equal("Actual:test string"); msg = ({ "test string".should.not.contain("test"); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal(`"test string" should not contain "test". test is present in "test string".`); msg.split("\n")[2].strip.should.equal(`Expected:to not contain "test"`); msg.split("\n")[3].strip.should.equal("Actual:test string"); msg = ({ "test string".should.contain('o'); }).should.throwException!TestException.msg; msg.split("\n")[0].should.contain(`o is missing from "test string"`); msg.split("\n")[2].strip.should.equal("Expected:to contain 'o'"); msg.split("\n")[3].strip.should.equal("Actual:test string"); msg = ({ "test string".should.not.contain('t'); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal(`"test string" should not contain 't'. t is present in "test string".`); msg.split("\n")[2].strip.should.equal("Expected:to not contain 't'"); msg.split("\n")[3].strip.should.equal("Actual:test string"); } @("string equal") unittest { ({ "test string".should.equal("test string"); }).should.not.throwAnyException; ({ "test string".should.not.equal("test"); }).should.not.throwAnyException; auto msg = ({ "test string".should.equal("test"); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal(`"test string" should equal "test". "test string" is not equal to "test".`); msg = ({ "test string".should.not.equal("test string"); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal(`"test string" should not equal "test string". "test string" is equal to "test string".`); msg = ({ ubyte[] data = [115, 111, 109, 101, 32, 100, 97, 116, 97, 0, 0]; data.assumeUTF.to!string.should.equal("some data"); }).should.throwException!TestException.msg; msg.should.contain(`Actual:"some data\0\0"`); msg.should.contain(`data.assumeUTF.to!string should equal "some data". "some data\0\0" is not equal to "some data".`); msg.should.contain(`some data[+\0\0]`); } /// should throw exceptions for delegates that return basic types unittest { string value() { throw new Exception("not implemented"); } value().should.throwAnyException.withMessage.equal("not implemented"); string noException() { return null; } bool thrown; try { noException.should.throwAnyException; } catch(TestException e) { e.msg.should.startWith("noException should throw any exception. No exception was thrown."); thrown = true; } thrown.should.equal(true); } @("const string equal") unittest { const string constValue = "test string"; immutable string immutableValue = "test string"; constValue.should.equal("test string"); immutableValue.should.equal("test string"); "test string".should.equal(constValue); "test string".should.equal(immutableValue); }
module fluentasserts.core.string; public import fluentasserts.core.base; import fluentasserts.core.results; import std.string; import std.conv; import std.algorithm; import std.array; /// When there is a lazy string that throws an it should throw that exception unittest { string someLazyString() { throw new Exception("This is it."); } ({ someLazyString.should.equal(""); }).should.throwAnyException.withMessage("This is it."); ({ someLazyString.should.contain(""); }).should.throwAnyException.withMessage("This is it."); ({ someLazyString.should.contain([""]); }).should.throwAnyException.withMessage("This is it."); ({ someLazyString.should.contain(' '); }).should.throwAnyException.withMessage("This is it."); ({ someLazyString.should.startWith(" "); }).should.throwAnyException.withMessage("This is it."); ({ someLazyString.should.endWith(" "); }).should.throwAnyException.withMessage("This is it."); } @("string startWith") unittest { ({ "test string".should.startWith("test"); }).should.not.throwAnyException; auto msg = ({ "test string".should.startWith("other"); }).should.throwException!TestException.msg; msg.split("\n")[0].should.contain(`"test string" does not start with "other"`); msg.split("\n")[2].strip.should.equal(`Expected:to start with "other"`); msg.split("\n")[3].strip.should.equal(`Actual:"test string"`); ({ "test string".should.not.startWith("other"); }).should.not.throwAnyException; msg = ({ "test string".should.not.startWith("test"); }).should.throwException!TestException.msg; msg.split("\n")[0].should.contain(`"test string" starts with "test"`); msg.split("\n")[2].strip.should.equal(`Expected:to not start with "test"`); msg.split("\n")[3].strip.should.equal(`Actual:"test string"`); ({ "test string".should.startWith('t'); }).should.not.throwAnyException; msg = ({ "test string".should.startWith('o'); }).should.throwException!TestException.msg; msg.split("\n")[0].should.contain(`"test string" does not start with 'o'`); msg.split("\n")[2].strip.should.equal("Expected:to start with 'o'"); msg.split("\n")[3].strip.should.equal(`Actual:"test string"`); ({ "test string".should.not.startWith('o'); }).should.not.throwAnyException; msg = ({ "test string".should.not.startWith('t'); }).should.throwException!TestException.msg; msg.split("\n")[0].should.contain(`"test string" starts with 't'`); msg.split("\n")[2].strip.should.equal(`Expected:to not start with 't'`); msg.split("\n")[3].strip.should.equal(`Actual:"test string"`); } @("string endWith") unittest { ({ "test string".should.endWith("string"); }).should.not.throwAnyException; auto msg = ({ "test string".should.endWith("other"); }).should.throwException!TestException.msg; msg.split("\n")[0].should.contain(`"test string" does not end with "other"`); msg.split("\n")[2].strip.should.equal(`Expected:to end with "other"`); msg.split("\n")[3].strip.should.equal(`Actual:"test string"`); ({ "test string".should.not.endWith("other"); }).should.not.throwAnyException; msg = ({ "test string".should.not.endWith("string"); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal(`"test string" should not end with "string". "test string" ends with "string".`); msg.split("\n")[2].strip.should.equal(`Expected:to not end with "string"`); msg.split("\n")[3].strip.should.equal(`Actual:"test string"`); ({ "test string".should.endWith('g'); }).should.not.throwAnyException; msg = ({ "test string".should.endWith('t'); }).should.throwException!TestException.msg; msg.split("\n")[0].should.contain(`"test string" does not end with 't'`); msg.split("\n")[2].strip.should.equal("Expected:to end with 't'"); msg.split("\n")[3].strip.should.equal(`Actual:"test string"`); ({ "test string".should.not.endWith('w'); }).should.not.throwAnyException; msg = ({ "test string".should.not.endWith('g'); }).should.throwException!TestException.msg; msg.split("\n")[0].should.contain(`"test string" ends with 'g'`); msg.split("\n")[2].strip.should.equal("Expected:to not end with 'g'"); msg.split("\n")[3].strip.should.equal(`Actual:"test string"`); } @("string contain") unittest { ({ "test string".should.contain(["string", "test"]); "test string".should.not.contain(["other", "message"]); }).should.not.throwAnyException; ({ "test string".should.contain("string"); "test string".should.not.contain("other"); }).should.not.throwAnyException; ({ "test string".should.contain('s'); "test string".should.not.contain('z'); }).should.not.throwAnyException; auto msg = ({ "test string".should.contain(["other", "message"]); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal(`"test string" should contain ["other", "message"]. ["other", "message"] are missing from "test string".`); msg.split("\n")[2].strip.should.equal(`Expected:to contain all ["other", "message"]`); msg.split("\n")[3].strip.should.equal("Actual:test string"); msg = ({ "test string".should.not.contain(["test", "string"]); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal(`"test string" should not contain ["test", "string"]. ["test", "string"] are present in "test string".`); msg.split("\n")[2].strip.should.equal(`Expected:to not contain any ["test", "string"]`); msg.split("\n")[3].strip.should.equal("Actual:test string"); msg = ({ "test string".should.contain("other"); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal(`"test string" should contain "other". other is missing from "test string".`); msg.split("\n")[2].strip.should.equal(`Expected:to contain "other"`); msg.split("\n")[3].strip.should.equal("Actual:test string"); msg = ({ "test string".should.not.contain("test"); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal(`"test string" should not contain "test". test is present in "test string".`); msg.split("\n")[2].strip.should.equal(`Expected:to not contain "test"`); msg.split("\n")[3].strip.should.equal("Actual:test string"); msg = ({ "test string".should.contain('o'); }).should.throwException!TestException.msg; msg.split("\n")[0].should.contain(`o is missing from "test string"`); msg.split("\n")[2].strip.should.equal("Expected:to contain 'o'"); msg.split("\n")[3].strip.should.equal("Actual:test string"); msg = ({ "test string".should.not.contain('t'); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal(`"test string" should not contain 't'. t is present in "test string".`); msg.split("\n")[2].strip.should.equal("Expected:to not contain 't'"); msg.split("\n")[3].strip.should.equal("Actual:test string"); } @("string equal") unittest { ({ "test string".should.equal("test string"); }).should.not.throwAnyException; ({ "test string".should.not.equal("test"); }).should.not.throwAnyException; auto msg = ({ "test string".should.equal("test"); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal(`"test string" should equal "test". "test string" is not equal to "test".`); msg = ({ "test string".should.not.equal("test string"); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal(`"test string" should not equal "test string". "test string" is equal to "test string".`); msg = ({ ubyte[] data = [115, 111, 109, 101, 32, 100, 97, 116, 97, 0, 0]; data.assumeUTF.to!string.should.equal("some data"); }).should.throwException!TestException.msg; msg.should.contain(`Actual:"some data\0\0"`); msg.should.contain(`data.assumeUTF.to!string should equal "some data". "some data\0\0" is not equal to "some data".`); msg.should.contain(`some data[+\0\0]`); } /// should throw exceptions for delegates that return basic types unittest { string value() { throw new Exception("not implemented"); } value().should.throwAnyException.withMessage.equal("not implemented"); string noException() { return null; } bool thrown; try { noException.should.throwAnyException; } catch(TestException e) { e.msg.should.startWith("noException should throw any exception. No exception was thrown."); thrown = true; } thrown.should.equal(true); } @("const string equal") unittest { const string constValue = "test string"; immutable string immutableValue = "test string"; constValue.should.equal("test string"); immutableValue.should.equal("test string"); "test string".should.equal(constValue); "test string".should.equal(immutableValue); }