fluentasserts.core.objects 113/113(100%) line coverage

      
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
200
2134
2234
2334
240
250
260
2712
280
2911
3011
3111
320
3311
343
350
368
370
380
390
400
4113
420
4312
4412
450
4612
470
4812
4912
500
510
520
530
540
557
560
576
586
596
600
616
626
630
640
650
660
670
680
691
703
710
720
733
742
750
760
773
782
790
800
813
822
830
840
850
860
870
881
890
902
912
922
930
940
953
962
970
980
992
1002
1012
1020
1033
1042
1050
1060
1072
1082
1092
1100
1110
1120
1130
1141
1151
1161
1171
1180
1191
1201
1211
1220
1232
1242
1250
1262
1272
1280
1293
1302
1310
1320
1332
1342
1352
1360
1373
1382
1390
1400
1412
1422
1432
1440
1450
1460
1470
1481
1491
1501
1510
1521
1531
1541
1550
1562
1572
1580
1592
1600
1613
1622
1630
1640
1652
1662
1672
1680
1693
1702
1710
1720
1732
1742
1752
1760
1770
1780
1790
1801
1810
1821
1831
1840
1850
1862
1870
1882
1890
1901
1910
1920
1932
1940
1952
1961
1970
1980
1992
2000
2010
2020
2030
2041
2050
2060
2073
2083
2090
2100
2110
2121
2130
2142
2152
2160
2173
2182
2190
2200
2212
2220
2233
2242
2250
2260
2272
2280
2290
2300
2310
2320
2331
2340
2353
2362
2370
2380
2392
2400
2413
2422
2430
2440
2452
2460
module fluentasserts.core.objects; public import fluentasserts.core.base; import fluentasserts.core.results; import std.string; import std.stdio; import std.traits; import std.conv; @safe: struct ShouldObject(T) { private { T testData; } mixin ShouldCommons; mixin ShouldThrowableCommons; this(U)(U value) { this.valueEvaluation = value.evaluation; this.testData = value.value; } auto beNull(const string file = __FILE__, const size_t line = __LINE__) @trusted { validateException; addMessage(" be "); addValue("null"); beginCheck; if(expectedValue) { return result(testData is null, cast(IResult) new ExpectedActualResult("null", "a `" ~ T.stringof ~ "` instance"), file, line); } else { return result(testData is null, cast(IResult) new ExpectedActualResult("a `" ~ T.stringof ~ "` instance", "null"), file, line); } } auto instanceOf(U)(const string file = __FILE__, const size_t line = __LINE__) @trusted { validateException; addValue(" instance of `" ~ U.stringof ~ "`"); beginCheck; U castedObject = cast(U) testData; return result(castedObject !is null, cast(IResult) new ExpectedActualResult(( expectedValue ? "" : "not " ) ~ "a `" ~ U.stringof ~ "` instance", "a `" ~ T.stringof ~ "` instance"), file, line); } auto equal(U)(U instance, const string file = __FILE__, const size_t line = __LINE__) @trusted { validateException; addMessage(" equal "); addValue("`" ~ U.stringof ~ "`"); beginCheck; return result(testData == instance, [] , new ExpectedActualResult(( expectedValue ? "" : "not " ) ~ instance.to!string, testData.to!string), file, line); } } /// When there is a lazy object that throws an it should throw that exception unittest { Object someLazyObject() { throw new Exception("This is it."); } ({ someLazyObject.should.not.beNull; }).should.throwAnyException.withMessage("This is it."); ({ someLazyObject.should.be.instanceOf!Object; }).should.throwAnyException.withMessage("This is it."); ({ someLazyObject.should.equal(new Object); }).should.throwAnyException.withMessage("This is it."); } /// object beNull unittest { Object o = null; ({ o.should.beNull; (new Object).should.not.beNull; }).should.not.throwAnyException; auto msg = ({ o.should.not.beNull; }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("o should not be null."); msg.split("\n")[2].strip.should.equal("Expected:a `Object` instance"); msg.split("\n")[3].strip.should.equal("Actual:null"); msg = ({ (new Object).should.beNull; }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("(new Object) should be null."); msg.split("\n")[2].strip.should.equal("Expected:null"); msg.split("\n")[3].strip.strip.should.equal("Actual:a `Object` instance"); } /// object instanceOf unittest { class BaseClass { } class ExtendedClass : BaseClass { } class SomeClass { } class OtherClass { } auto someObject = new SomeClass; auto otherObject = new OtherClass; auto extendedObject = new ExtendedClass; someObject.should.be.instanceOf!SomeClass; extendedObject.should.be.instanceOf!BaseClass; someObject.should.not.be.instanceOf!OtherClass; someObject.should.not.be.instanceOf!BaseClass; auto msg = ({ otherObject.should.be.instanceOf!SomeClass; }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("otherObject should be instance of `SomeClass`."); msg.split("\n")[2].strip.should.equal("Expected:a `SomeClass` instance"); msg.split("\n")[3].strip.should.equal("Actual:a `OtherClass` instance"); msg = ({ otherObject.should.not.be.instanceOf!OtherClass; }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("otherObject should not be instance of `OtherClass`."); msg.split("\n")[2].strip.should.equal("Expected:not a `OtherClass` instance"); msg.split("\n")[3].strip.should.equal("Actual:a `OtherClass` instance"); } /// object instanceOf interface unittest { interface MyInterface { } class BaseClass : MyInterface { } class OtherClass { } auto someObject = new BaseClass; MyInterface someInterface = new BaseClass; auto otherObject = new OtherClass; someInterface.should.be.instanceOf!MyInterface; someInterface.should.be.instanceOf!BaseClass; someObject.should.be.instanceOf!MyInterface; auto msg = ({ otherObject.should.be.instanceOf!MyInterface; }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("otherObject should be instance of `MyInterface`."); msg.split("\n")[2].strip.should.equal("Expected:a `MyInterface` instance"); msg.split("\n")[3].strip.should.equal("Actual:a `OtherClass` instance"); msg = ({ someObject.should.not.be.instanceOf!MyInterface; }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("someObject should not be instance of `MyInterface`."); msg.split("\n")[2].strip.should.equal("Expected:not a `MyInterface` instance"); msg.split("\n")[3].strip.should.equal("Actual:a `BaseClass` instance"); } /// should throw exceptions for delegates that return basic types unittest { class SomeClass { } SomeClass value() { throw new Exception("not implemented"); } SomeClass noException() { return null; } value().should.throwAnyException.withMessage.equal("not implemented"); bool thrown; try { noException.should.throwAnyException; } catch (TestException e) { e.msg.should.startWith("noException should throw any exception. Nothing was thrown."); thrown = true; } thrown.should.equal(true); } /// object equal unittest { class TestEqual { private int value; this(int value) { this.value = value; } } auto instance = new TestEqual(1); instance.should.equal(instance); instance.should.not.equal(new TestEqual(1)); auto msg = ({ instance.should.not.equal(instance); }).should.throwException!TestException.msg; msg.should.startWith("instance should not equal `TestEqual`."); msg = ({ instance.should.equal(new TestEqual(1)); }).should.throwException!TestException.msg; msg.should.startWith("instance should equal `TestEqual`."); } /// null object comparison unittest { Object nullObject; auto msg = ({ nullObject.should.equal(new Object); }).should.throwException!TestException.msg; msg.should.startWith("nullObject should equal `Object`."); msg = ({ (new Object).should.equal(null); }).should.throwException!TestException.msg; msg.should.startWith("(new Object) should equal `typeof(null)`."); }