fluentasserts.core.callable 0/0(100%) line coverage

      
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
200
210
220
230
240
250
260
270
280
290
300
310
320
330
340
350
360
370
380
390
400
410
420
430
440
450
460
470
480
490
500
510
520
530
540
550
560
570
580
590
600
610
620
630
640
650
660
670
680
690
700
710
720
730
740
750
760
770
780
790
800
810
820
830
840
850
860
870
880
890
900
910
920
930
940
950
960
970
980
990
1000
1010
1020
1030
1040
1050
1060
1070
1080
1090
1100
1110
1120
1130
1140
1150
1160
1170
1180
1190
1200
1210
1220
1230
1240
1250
1260
1270
1280
1290
1300
1310
1320
1330
1340
1350
1360
1370
1380
1390
1400
1410
1420
1430
1440
1450
1460
1470
1480
1490
1500
1510
1520
1530
1540
1550
1560
1570
1580
1590
1600
1610
1620
1630
1640
1650
1660
1670
1680
1690
1700
1710
1720
1730
1740
1750
1760
1770
1780
1790
1800
1810
1820
1830
1840
1850
1860
1870
1880
1890
1900
1910
1920
1930
1940
1950
1960
1970
1980
1990
2000
2010
2020
2030
2040
2050
2060
2070
2080
2090
2100
2110
2120
2130
2140
2150
2160
2170
2180
2190
2200
2210
2220
2230
2240
2250
2260
2270
2280
2290
2300
2310
2320
2330
2340
2350
2360
module fluentasserts.core.callable; public import fluentasserts.core.base; import std.string; import std.datetime; import std.conv; import std.traits; import fluentasserts.core.results; @safe: struct ShouldCallable(T) { private { T callable; } mixin ShouldCommons; mixin ShouldThrowableCommons; this(lazy T callable) { auto result = callable.evaluate; valueEvaluation = result.evaluation; this.callable = result.value; } auto haveExecutionTime(string file = __FILE__, size_t line = __LINE__) { validateException; auto tmpShould = ShouldBaseType!Duration(evaluate(valueEvaluation.duration)).forceMessage(" have execution time"); return tmpShould; } auto beNull(string file = __FILE__, size_t line = __LINE__) { validateException; addMessage(" be "); addValue("null"); beginCheck; bool isNull = callable is null; string expected; static if(isDelegate!callable) { string actual = callable.ptr.to!string; } else { string actual = (cast(void*)callable).to!string; } if(expectedValue) { expected = "null"; } else { expected = "not null"; } return result(isNull, [], new ExpectedActualResult(expected, actual), file, line); } } /// Should be able to catch any exception unittest { ({ throw new Exception("test"); }).should.throwAnyException.msg.should.equal("test"); } /// Should be able to catch any assert unittest { ({ assert(false, "test"); }).should.throwSomething.withMessage.equal("test"); } /// Should be able to use with message without a custom assert unittest { ({ assert(false, "test"); }).should.throwSomething.withMessage("test"); } /// Should be able to catch a certain exception type unittest { class CustomException : Exception { this(string msg, string fileName = "", size_t line = 0, Throwable next = null) { super(msg, fileName, line, next); } } ({ throw new CustomException("test"); }).should.throwException!CustomException.withMessage.equal("test"); bool hasException; try { ({ throw new Exception("test"); }).should.throwException!CustomException.withMessage.equal("test"); } catch(TestException t) { hasException = true; t.msg.should.contain(" }) should throw a `CustomException`. An exception of type `object.Exception` saying `test` was thrown."); } hasException.should.equal(true).because("we want to catch a CustomException not an Exception"); } /// Should be able to retrieve a typed version of a custom exception unittest { class CustomException : Exception { int data; this(int data, string msg, string fileName = "", size_t line = 0, Throwable next = null) { super(msg, fileName, line, next); this.data = data; } } auto thrown = ({ throw new CustomException(2, "test"); }).should.throwException!CustomException.original; thrown.should.not.beNull; thrown.should.instanceOf!CustomException; thrown.msg.should.equal("test"); thrown.data.should.equal(2); } /// Should print a nice message for exception message asserts @trusted unittest { class CustomException : Exception { this(string msg, string fileName = "", size_t line = 0, Throwable next = null) { super(msg, fileName, line, next); } } Throwable t; try { ({ throw new CustomException("test"); }).should.throwException!CustomException.withMessage.equal("other"); } catch(Throwable e) { t = e; } t.should.not.beNull; t.msg.should.startWith("({\n throw new CustomException(\"test\");\n }) should throw a `CustomException` with message equal `other`. `test` is not equal to `other`."); } /// Should fail if an exception is not thrown unittest { auto thrown = false; try { ({ }).should.throwAnyException; } catch(TestException e) { thrown = true; e.msg.split("\n")[0].should.equal("({ }) should throw any exception. Nothing was thrown."); } thrown.should.equal(true); } /// Should fail if an exception is not expected unittest { auto thrown = false; try { ({ throw new Exception("test"); }).should.not.throwAnyException; } catch(TestException e) { thrown = true; e.msg.split("\n")[2].should.equal(" }) should not throw any exception. An exception of type `object.Exception` saying `test` was thrown."); } thrown.should.equal(true); } @("Should be able to benchmark some code") unittest { ({ }).should.haveExecutionTime.lessThan(1.seconds); } @("Should fail on benchmark timeout") unittest { import core.thread; TestException exception = null; try { ({ Thread.sleep(2.msecs); }).should.haveExecutionTime.lessThan(1.msecs); } catch(TestException e) { exception = e; } exception.should.not.beNull.because("we wait 20 milliseconds"); exception.msg.should.startWith("({\n Thread.sleep(2.msecs);\n }) should have execution time less than `1 ms`."); } /// It should check if a delagate is null unittest { void delegate() action; action.should.beNull; ({ }).should.not.beNull; auto msg = ({ action.should.not.beNull; }).should.throwException!TestException.msg; msg.should.startWith("action should not be null."); msg.should.contain("Expected:not null"); msg.should.contain("Actual:null"); msg = ({ ({ }).should.beNull; }).should.throwException!TestException.msg; msg.should.startWith("({ }) should be null."); msg.should.contain("Expected:null\n"); msg.should.not.contain("Actual:null\n"); }