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

      
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
20130
21347
220
23130
24130
250
260
270
284
290
302
310
320
330
344
354
364
370
384
390
404
410
420
432
440
452
460
470
484
492
500
512
520
530
544
550
560
570
580
590
600
613
621
631
640
650
660
670
683
691
700
710
720
730
740
753
761
770
780
790
800
810
820
831
841
851
860
870
880
893
901
910
920
930
941
950
963
971
980
990
1001
1012
1020
1030
1042
1050
1060
1070
1080
1090
1101
1110
1121
1131
1140
1151
1160
1170
1180
1193
1201
1210
1220
1232
1242
1252
1262
1270
1280
1290
1300
1310
1321
1331
1341
1350
1360
1370
1381
1390
1400
1413
1421
1430
1440
1451
1460
1470
1482
1492
1500
1510
1520
1530
1540
1551
1560
1572
1580
1591
1602
1610
1620
1632
1640
1650
1660
1670
1680
1691
1700
1713
1721
1730
1740
1751
1762
1770
1780
1792
1800
1810
1820
1830
1840
1852
1860
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; struct ShouldCallable(T) { private { T callable; ValueEvaluation valueEvaluation; } 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__) { auto tmpShould = ShouldBaseType!Duration(evaluate(valueEvaluation.duration)).forceMessage(" have execution time"); return tmpShould; } auto beNull(string file = __FILE__, size_t line = __LINE__) { 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 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"); }