fluentasserts.core.operations.throwable 99/113(87%) line coverage

      
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
162
172
180
190
200
210
220
230
246
250
266
276
280
2910
301
311
320
331
341
351
361
371
380
391
400
410
428
431
440
451
460
470
4813
491
501
510
521
530
541
550
560
576
580
596
600
610
620
630
640
652
660
670
680
690
701
710
721
730
740
752
760
771
780
791
801
811
820
830
841
850
860
870
880
891
902
910
920
930
940
951
960
971
980
990
1002
1010
1021
1030
1041
1051
1061
1071
1080
1090
1101
1110
1120
1130
1140
1151
1162
1170
1180
1190
1202
1210
1222
1230
1244
1250
1260
1270
1280
1290
1300
1310
1320
1330
1340
1350
1360
1372
1380
1390
1400
1410
1420
1436
1440
1450
1460
1470
1480
1490
1500
1510
1522
1530
1542
1550
1560
1570
1580
1594
1600
1614
1620
1634
1644
1650
16610
1671
1681
1690
1701
1711
1721
1731
1741
1750
1761
1770
1780
17910
1801
1811
1820
1831
1841
1851
1861
1871
1880
1891
1900
1910
1924
1930
1944
1950
1960
1970
1980
1991
2001
2010
2020
2030
2040
2050
2061
2070
2080
2091
2101
2110
2120
2131
2140
2151
2161
2171
2181
2190
2200
2211
2220
2230
2240
2250
2261
2271
2280
2290
2300
2310
2320
2331
2340
2350
2361
2371
2380
2390
2401
2410
2421
2431
2441
2451
2460
2470
2481
2490
module fluentasserts.core.operations.throwable; public import fluentasserts.core.base; import fluentasserts.core.results; import fluentasserts.core.lifecycle; import fluentasserts.core.expect; import fluentasserts.core.serializers; import std.string; import std.conv; import std.algorithm; import std.array; version(unittest) { class CustomException : Exception { this(string msg, string fileName = "", size_t line = 0, Throwable next = null) { super(msg, fileName, line, next); } } } /// IResult[] throwAnyException(ref Evaluation evaluation) @trusted nothrow { IResult[] results; Lifecycle.instance.addText(". "); auto thrown = evaluation.currentValue.throwable; if(evaluation.currentValue.throwable && evaluation.isNegated) { string message; try message = thrown.message.to!string; catch(Exception) {} Lifecycle.instance.addText("`"); Lifecycle.instance.addValue(thrown.classinfo.name); Lifecycle.instance.addText("` saying `"); Lifecycle.instance.addValue(message); Lifecycle.instance.addText("` was thrown."); try results ~= new ExpectedActualResult("No exception to be thrown", "`" ~ thrown.classinfo.name ~ "` saying `" ~ message ~ "`"); catch(Exception) {} } if(!thrown && !evaluation.isNegated) { Lifecycle.instance.addText("No exception was thrown."); try results ~= new ExpectedActualResult("Any exception to be thrown", "Nothing was thrown"); catch(Exception) {} } if(thrown && !evaluation.isNegated && "Throwable" in evaluation.currentValue.meta) { string message; try message = thrown.message.to!string; catch(Exception) {} Lifecycle.instance.addText("A `Throwable` saying `" ~ message ~ "` was thrown."); try results ~= new ExpectedActualResult("Any exception to be thrown", "A `Throwable` with message `" ~ message ~ "` was thrown"); catch(Exception) {} } evaluation.currentValue.throwable = null; return results; } /// It should be successfull when the function does not throw unittest { void test() {} expect({ test(); }).to.not.throwAnyException(); } /// It should fail when an exception is thrown and none is expected unittest { void test() { throw new Exception("Test exception"); } bool thrown; try { expect({ test(); }).to.not.throwAnyException(); } catch(TestException e) { thrown = true; assert(e.message.indexOf("should not throw any exception. `object.Exception` saying `Test exception` was thrown.") != -1); assert(e.message.indexOf("\n Expected:No exception to be thrown\n") != -1); assert(e.message.indexOf("\n Actual:`object.Exception` saying `Test exception`\n") != -1); } assert(thrown, "The exception was not thrown"); } /// It should be successfull when the function throws an expected exception unittest { void test() { throw new Exception("test"); } expect({ test(); }).to.throwAnyException; } /// It should not be successfull when the function throws a throwable and an exception is expected unittest { void test() { assert(false); } bool thrown; try { expect({ test(); }).to.throwAnyException; } catch(TestException e) { thrown = true; assert(e.message.indexOf("should throw any exception. A `Throwable` saying `Assertion failure` was thrown.") != -1); assert(e.message.indexOf("\n Expected:Any exception to be thrown\n") != -1); assert(e.message.indexOf("\n Actual:A `Throwable` with message `Assertion failure` was thrown\n") != -1); assert(e.file == "source/fluentasserts/core/operations/throwable.d"); } assert(thrown, "The exception was not thrown"); } /// It should be successfull when the function throws an expected exception unittest { void test() { throw new Exception("test"); } expect({ test(); }).to.throwAnyException; } IResult[] throwAnyExceptionWithMessage(ref Evaluation evaluation) @trusted nothrow { IResult[] results; auto thrown = evaluation.currentValue.throwable; if(evaluation.currentValue.throwable && evaluation.isNegated) { string message; try message = thrown.message.to!string; catch(Exception) {} Lifecycle.instance.addText("`"); Lifecycle.instance.addValue(thrown.classinfo.name); Lifecycle.instance.addText("` saying `"); Lifecycle.instance.addValue(message); Lifecycle.instance.addText("` was thrown."); try results ~= new ExpectedActualResult("No exception to be thrown", "`" ~ thrown.classinfo.name ~ "` saying `" ~ message ~ "`"); catch(Exception) {} } if(!thrown && !evaluation.isNegated) { Lifecycle.instance.addText("Nothing was thrown."); try results ~= new ExpectedActualResult("Any exception to be thrown", "Nothing was thrown"); catch(Exception) {} } if(thrown && !evaluation.isNegated && "Throwable" in evaluation.currentValue.meta) { string message; try message = thrown.message.to!string; catch(Exception) {} Lifecycle.instance.addText("A `Throwable` saying `" ~ message ~ "` was thrown."); try results ~= new ExpectedActualResult("Any exception to be thrown", "A `Throwable` with message `" ~ message ~ "` was thrown"); catch(Exception) {} } evaluation.currentValue.throwable = null; return results; } /// IResult[] throwException(ref Evaluation evaluation) @trusted nothrow { Lifecycle.instance.addText("."); string exceptionType = evaluation.expectedValue.strValue.cleanString; IResult[] results; auto thrown = evaluation.currentValue.throwable; if(thrown && evaluation.isNegated && thrown.classinfo.name == exceptionType) { string message; try message = thrown.message.to!string; catch(Exception) {} Lifecycle.instance.addText("`"); Lifecycle.instance.addValue(thrown.classinfo.name); Lifecycle.instance.addText("` saying `"); Lifecycle.instance.addValue(message); Lifecycle.instance.addText("` was thrown."); try results ~= new ExpectedActualResult("no `" ~ exceptionType ~ "` to be thrown", "`" ~ thrown.classinfo.name ~ "` saying `" ~ message ~ "`"); catch(Exception) {} } if(thrown && !evaluation.isNegated && thrown.classinfo.name != exceptionType) { string message; try message = thrown.message.to!string; catch(Exception) {} Lifecycle.instance.addText("`"); Lifecycle.instance.addValue(thrown.classinfo.name); Lifecycle.instance.addText("` saying `"); Lifecycle.instance.addValue(message); Lifecycle.instance.addText("` was thrown."); try results ~= new ExpectedActualResult(exceptionType, "`" ~ thrown.classinfo.name ~ "` saying `" ~ message ~ "`"); catch(Exception) {} } evaluation.currentValue.throwable = null; return results; } /// Should be able to catch a certain exception type unittest { expect({ throw new CustomException("test"); }).to.throwException!CustomException; } /// It should fail when an unexpected exception is thrown unittest { bool thrown; try { expect({ throw new Exception("test"); }).to.throwException!CustomException; } catch(TestException e) { thrown = true; assert(e.message.indexOf("should throwException \"fluentasserts.core.operations.throwable.CustomException\".`object.Exception` saying `test` was thrown.") != -1); assert(e.message.indexOf("\n Expected:fluentasserts.core.operations.throwable.CustomException\n") != -1); assert(e.message.indexOf("\n Actual:`object.Exception` saying `test`\n") != -1); assert(e.file == "source/fluentasserts/core/operations/throwable.d"); } assert(thrown, "The exception was not thrown"); } /// It should not fail when an exception is thrown and it is not expected unittest { expect({ throw new Exception("test"); }).to.not.throwException!CustomException; } /// It should fail when an different exception than the one checked is thrown unittest { bool thrown; try { expect({ throw new CustomException("test"); }).to.not.throwException!CustomException; } catch(TestException e) { thrown = true; assert(e.message.indexOf("should not throwException \"fluentasserts.core.operations.throwable.CustomException\".`fluentasserts.core.operations.throwable.CustomException` saying `test` was thrown.") != -1); assert(e.message.indexOf("\n Expected:no `fluentasserts.core.operations.throwable.CustomException` to be thrown\n") != -1); assert(e.message.indexOf("\n Actual:`fluentasserts.core.operations.throwable.CustomException` saying `test`\n") != -1); assert(e.file == "source/fluentasserts/core/operations/throwable.d"); } assert(thrown, "The exception was not thrown"); }