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

      
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
200
210
22501
231461
240
25501
26501
270
280
290
300
312
320
334
340
352
360
370
380
390
404
410
424
434
444
450
464
470
484
490
500
512
520
532
540
550
564
572
580
592
600
610
624
630
640
650
660
670
683
691
701
710
720
730
740
753
761
770
780
790
800
810
823
831
840
850
860
870
880
890
901
911
920
930
940
953
961
970
980
990
1001
1010
1023
1031
1040
1050
1061
1072
1080
1090
1102
1110
1120
1130
1140
1150
1160
1171
1181
1190
1201
1210
1220
1230
1243
1251
1260
1270
1282
1292
1302
1312
1320
1330
1340
1350
1360
1371
1381
1390
1400
1410
1421
1430
1440
1453
1461
1470
1480
1491
1500
1510
1522
1532
1540
1550
1560
1570
1581
1590
1602
1610
1621
1632
1640
1650
1662
1670
1680
1690
1700
1711
1720
1733
1741
1750
1760
1771
1782
1790
1800
1812
1820
1830
1840
1850
1862
1870
1880
1890
1900
1910
1920
1930
1940
1951
1960
1970
1982
1991
2000
2010
2021
2030
2040
2052
2062
2070
2080
2090
2100
2111
2122
2130
2142
2150
2163
2172
2180
2190
2202
2212
2222
2230
2243
2252
2260
2270
2282
2292
2302
2310
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"); }