fluentasserts.core.callable 56/56(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
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
952
961
970
980
991
1000
1013
1021
1030
1040
1051
1062
1070
1082
1090
1100
1110
1120
1130
1140
1151
1161
1170
1181
1190
1200
1210
1222
1231
1240
1250
1262
1272
1282
1290
1300
1310
1320
1331
1340
1352
1360
1371
1382
1390
1400
1412
1420
1430
1440
1450
1461
1470
1483
1491
1500
1510
1521
1532
1540
1550
1562
1570
1580
1590
1600
1612
1620
1630
1640
1650
1660
1670
1680
1690
1701
1710
1720
1732
1741
1750
1760
1771
1780
1790
1802
1812
1820
1830
1840
1850
1861
1872
1880
1892
1900
1912
1922
1930
1940
1952
1962
1972
1980
1993
2002
2010
2020
2032
2042
2052
2060
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("test"); bool hasException; try { ({ throw new Exception("test"); }).should.throwException!CustomException.withMessage("test"); } catch(TestException t) { hasException = true; t.msg.should.contain(" }) should throw exception with message equal \"test\". `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.thrown; thrown.should.not.beNull; thrown.msg.should.equal("test"); (cast(CustomException) thrown).data.should.equal(2); } /// 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. No exception 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. `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 delegate 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"); }