102030405060708090100110120130140150160170180190200210220230240250260270280290300310320330340350360370380390400410420430440450460470480490500510520530540550560570580590600610620630640650660670680690700710720730740750760770780790800810820830840850860870880890900910920930940950960970980990100010101020103010401050106010701080109011001110112011301140115011601170118011901200121012201230124012501260127012801290130013101320133013401350136013701380139014001410142014301440145014601470148014901500151015201530154015501560157015801590160016101620163016401650166016701680169017001710172017301740175017601770178017901800181018201830184018501860187018801890190019101920193019401950196019701980199020002010202020302040205020602070208020902100211021202130214021502160217021802190220022102220223022402250226022702280229023002310232023302340235023602370238023902400241024202430244024502460247024802490 module fluentasserts.core.basetype; public import fluentasserts.core.base; import fluentasserts.core.results; import std.string; import std.conv; import std.algorithm; /// When there is a lazy number that throws an it should throw that exception unittest { int someLazyInt() { throw new Exception("This is it."); } ({ someLazyInt.should.equal(3); }).should.throwAnyException.withMessage("This is it."); ({ someLazyInt.should.be.greaterThan(3); }).should.throwAnyException.withMessage("This is it."); ({ someLazyInt.should.be.lessThan(3); }).should.throwAnyException.withMessage("This is it."); ({ someLazyInt.should.be.between(3, 4); }).should.throwAnyException.withMessage("This is it."); ({ someLazyInt.should.be.approximately(3, 4); }).should.throwAnyException.withMessage("This is it."); } @("numbers equal") unittest { ({ 5.should.equal(5); 5.should.not.equal(6); }).should.not.throwAnyException; auto msg = ({ 5.should.equal(6); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("5 should equal 6. 5 is not equal to 6."); msg = ({ 5.should.not.equal(5); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("5 should not equal 5. 5 is equal to 5."); } @("bools equal") unittest { ({ true.should.equal(true); true.should.not.equal(false); }).should.not.throwAnyException; auto msg = ({ true.should.equal(false); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("true should equal false."); msg.split("\n")[2].strip.should.equal("Expected:false"); msg.split("\n")[3].strip.should.equal("Actual:true"); msg = ({ true.should.not.equal(true); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("true should not equal true."); msg.split("\n")[2].strip.should.equal("Expected:not true"); msg.split("\n")[3].strip.should.equal("Actual:true"); } @("numbers greater than") unittest { ({ 5.should.be.greaterThan(4); 5.should.not.be.greaterThan(6); 5.should.be.above(4); 5.should.not.be.above(6); }).should.not.throwAnyException; auto msg = ({ 5.should.be.greaterThan(5); 5.should.be.above(5); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("5 should be greater than 5. 5 is less than or equal to 5."); msg = ({ 5.should.not.be.greaterThan(4); 5.should.not.be.above(4); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("5 should not be greater than 4. 5 is greater than 4."); } @("numbers less than") unittest { ({ 5.should.be.lessThan(6); 5.should.not.be.lessThan(4); 5.should.be.below(6); 5.should.not.be.below(4); }).should.not.throwAnyException; auto msg = ({ 5.should.be.lessThan(4); 5.should.be.below(4); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("5 should be less than 4. 5 is greater than or equal to 4."); msg.split("\n")[2].strip.should.equal("Expected:less than 4"); msg.split("\n")[3].strip.should.equal("Actual:5"); msg = ({ 5.should.not.be.lessThan(6); 5.should.not.be.below(6); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("5 should not be less than 6. 5 is less than 6."); } @("numbers between") unittest { ({ 5.should.be.between(4, 6); 5.should.be.between(6, 4); 5.should.not.be.between(5, 6); 5.should.not.be.between(4, 5); 5.should.be.within(4, 6); 5.should.be.within(6, 4); 5.should.not.be.within(5, 6); 5.should.not.be.within(4, 5); }).should.not.throwAnyException; auto msg = ({ 5.should.be.between(5, 6); 5.should.be.within(5, 6); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("5 should be between 5 and 6. 5 is less than or equal to 5."); msg.split("\n")[2].strip.should.equal("Expected:a value inside (5, 6) interval"); msg.split("\n")[3].strip.should.equal("Actual:5"); msg = ({ 5.should.be.between(4, 5); 5.should.be.within(4, 5); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("5 should be between 4 and 5. 5 is greater than or equal to 5."); msg.split("\n")[2].strip.should.equal("Expected:a value inside (4, 5) interval"); msg.split("\n")[3].strip.should.equal("Actual:5"); msg = ({ 5.should.not.be.between(4, 6); 5.should.not.be.within(4, 6); }).should.throwException!TestException.msg; msg.split("\n")[0].strip.should.equal("5 should not be between 4 and 6."); msg.split("\n")[2].strip.should.equal("Expected:a value outside (4, 6) interval"); msg.split("\n")[3].strip.should.equal("Actual:5"); msg = ({ 5.should.not.be.between(6, 4); 5.should.not.be.within(6, 4); }).should.throwException!TestException.msg; msg.split("\n")[0].strip.should.equal("5 should not be between 6 and 4."); msg.split("\n")[2].strip.should.equal("Expected:a value outside (4, 6) interval"); msg.split("\n")[3].strip.should.equal("Actual:5"); } /// numbers approximately unittest { ({ (10f/3f).should.be.approximately(3, 0.34); (10f/3f).should.not.be.approximately(3, 0.1); }).should.not.throwAnyException; auto msg = ({ (10f/3f).should.be.approximately(3, 0.1); }).should.throwException!TestException.msg; msg.split("\n")[0].strip.should.contain("(10f/3f) should be approximately 3±0.1."); msg.split("\n")[2].strip.should.contain("Expected:3±0.1"); msg.split("\n")[3].strip.should.contain("Actual:3.33333"); msg = ({ (10f/3f).should.not.be.approximately(3, 0.34); }).should.throwException!TestException.msg; msg.split("\n")[0].strip.should.contain("(10f/3f) should not be approximately 3±0.34."); msg.split("\n")[2].strip.should.contain("Expected:not 3±0.34"); msg.split("\n")[3].strip.should.contain("Actual:3.33333"); } /// should throw exceptions for delegates that return basic types unittest { int value() { throw new Exception("not implemented value"); } void voidValue() { throw new Exception("nothing here"); } void noException() { } value().should.throwAnyException.withMessage.equal("not implemented value"); voidValue().should.throwAnyException.withMessage.equal("nothing here"); 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); thrown = false; try { voidValue().should.not.throwAnyException; } catch(TestException e) { thrown = true; e.msg.split("\n")[0].should.equal("voidValue() should not throw any exception. `object.Exception` saying `nothing here` was thrown."); } thrown.should.equal(true); } /// it should compile const comparison unittest { const actual = 42; actual.should.equal(42); }
module fluentasserts.core.basetype; public import fluentasserts.core.base; import fluentasserts.core.results; import std.string; import std.conv; import std.algorithm; /// When there is a lazy number that throws an it should throw that exception unittest { int someLazyInt() { throw new Exception("This is it."); } ({ someLazyInt.should.equal(3); }).should.throwAnyException.withMessage("This is it."); ({ someLazyInt.should.be.greaterThan(3); }).should.throwAnyException.withMessage("This is it."); ({ someLazyInt.should.be.lessThan(3); }).should.throwAnyException.withMessage("This is it."); ({ someLazyInt.should.be.between(3, 4); }).should.throwAnyException.withMessage("This is it."); ({ someLazyInt.should.be.approximately(3, 4); }).should.throwAnyException.withMessage("This is it."); } @("numbers equal") unittest { ({ 5.should.equal(5); 5.should.not.equal(6); }).should.not.throwAnyException; auto msg = ({ 5.should.equal(6); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("5 should equal 6. 5 is not equal to 6."); msg = ({ 5.should.not.equal(5); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("5 should not equal 5. 5 is equal to 5."); } @("bools equal") unittest { ({ true.should.equal(true); true.should.not.equal(false); }).should.not.throwAnyException; auto msg = ({ true.should.equal(false); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("true should equal false."); msg.split("\n")[2].strip.should.equal("Expected:false"); msg.split("\n")[3].strip.should.equal("Actual:true"); msg = ({ true.should.not.equal(true); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("true should not equal true."); msg.split("\n")[2].strip.should.equal("Expected:not true"); msg.split("\n")[3].strip.should.equal("Actual:true"); } @("numbers greater than") unittest { ({ 5.should.be.greaterThan(4); 5.should.not.be.greaterThan(6); 5.should.be.above(4); 5.should.not.be.above(6); }).should.not.throwAnyException; auto msg = ({ 5.should.be.greaterThan(5); 5.should.be.above(5); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("5 should be greater than 5. 5 is less than or equal to 5."); msg = ({ 5.should.not.be.greaterThan(4); 5.should.not.be.above(4); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("5 should not be greater than 4. 5 is greater than 4."); } @("numbers less than") unittest { ({ 5.should.be.lessThan(6); 5.should.not.be.lessThan(4); 5.should.be.below(6); 5.should.not.be.below(4); }).should.not.throwAnyException; auto msg = ({ 5.should.be.lessThan(4); 5.should.be.below(4); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("5 should be less than 4. 5 is greater than or equal to 4."); msg.split("\n")[2].strip.should.equal("Expected:less than 4"); msg.split("\n")[3].strip.should.equal("Actual:5"); msg = ({ 5.should.not.be.lessThan(6); 5.should.not.be.below(6); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("5 should not be less than 6. 5 is less than 6."); } @("numbers between") unittest { ({ 5.should.be.between(4, 6); 5.should.be.between(6, 4); 5.should.not.be.between(5, 6); 5.should.not.be.between(4, 5); 5.should.be.within(4, 6); 5.should.be.within(6, 4); 5.should.not.be.within(5, 6); 5.should.not.be.within(4, 5); }).should.not.throwAnyException; auto msg = ({ 5.should.be.between(5, 6); 5.should.be.within(5, 6); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("5 should be between 5 and 6. 5 is less than or equal to 5."); msg.split("\n")[2].strip.should.equal("Expected:a value inside (5, 6) interval"); msg.split("\n")[3].strip.should.equal("Actual:5"); msg = ({ 5.should.be.between(4, 5); 5.should.be.within(4, 5); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("5 should be between 4 and 5. 5 is greater than or equal to 5."); msg.split("\n")[2].strip.should.equal("Expected:a value inside (4, 5) interval"); msg.split("\n")[3].strip.should.equal("Actual:5"); msg = ({ 5.should.not.be.between(4, 6); 5.should.not.be.within(4, 6); }).should.throwException!TestException.msg; msg.split("\n")[0].strip.should.equal("5 should not be between 4 and 6."); msg.split("\n")[2].strip.should.equal("Expected:a value outside (4, 6) interval"); msg.split("\n")[3].strip.should.equal("Actual:5"); msg = ({ 5.should.not.be.between(6, 4); 5.should.not.be.within(6, 4); }).should.throwException!TestException.msg; msg.split("\n")[0].strip.should.equal("5 should not be between 6 and 4."); msg.split("\n")[2].strip.should.equal("Expected:a value outside (4, 6) interval"); msg.split("\n")[3].strip.should.equal("Actual:5"); } /// numbers approximately unittest { ({ (10f/3f).should.be.approximately(3, 0.34); (10f/3f).should.not.be.approximately(3, 0.1); }).should.not.throwAnyException; auto msg = ({ (10f/3f).should.be.approximately(3, 0.1); }).should.throwException!TestException.msg; msg.split("\n")[0].strip.should.contain("(10f/3f) should be approximately 3±0.1."); msg.split("\n")[2].strip.should.contain("Expected:3±0.1"); msg.split("\n")[3].strip.should.contain("Actual:3.33333"); msg = ({ (10f/3f).should.not.be.approximately(3, 0.34); }).should.throwException!TestException.msg; msg.split("\n")[0].strip.should.contain("(10f/3f) should not be approximately 3±0.34."); msg.split("\n")[2].strip.should.contain("Expected:not 3±0.34"); msg.split("\n")[3].strip.should.contain("Actual:3.33333"); } /// should throw exceptions for delegates that return basic types unittest { int value() { throw new Exception("not implemented value"); } void voidValue() { throw new Exception("nothing here"); } void noException() { } value().should.throwAnyException.withMessage.equal("not implemented value"); voidValue().should.throwAnyException.withMessage.equal("nothing here"); 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); thrown = false; try { voidValue().should.not.throwAnyException; } catch(TestException e) { thrown = true; e.msg.split("\n")[0].should.equal("voidValue() should not throw any exception. `object.Exception` saying `nothing here` was thrown."); } thrown.should.equal(true); } /// it should compile const comparison unittest { const actual = 42; actual.should.equal(42); }