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

      
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
2031
2131
2231
230
240
250
2611
2711
2811
290
3011
313
320
338
340
350
360
370
3812
3912
400
4112
420
4312
4412
450
460
470
480
490
506
516
526
530
546
556
560
570
580
590
600
611
620
632
642
652
660
670
683
692
700
710
722
732
742
750
763
772
780
790
802
812
822
830
840
850
860
871
881
891
901
910
921
931
941
950
962
972
980
992
1002
1010
1023
1032
1040
1050
1062
1072
1082
1090
1103
1112
1120
1130
1142
1152
1162
1170
1180
1190
1200
1210
1221
1231
1241
1250
1261
1271
1281
1290
1302
1312
1320
1332
1340
1353
1362
1370
1380
1392
1402
1412
1420
1433
1442
1450
1460
1472
1482
1492
1500
1510
1520
1530
1541
1550
1561
1571
1580
1590
1602
1610
1622
1630
1641
1650
1660
1672
1680
1692
1701
1710
1720
1732
1740
1750
1760
1770
1781
1790
1800
1813
1823
1830
1840
1850
1861
1870
1882
1892
1900
1913
1922
1930
1940
1952
1960
1973
1982
1990
2000
2012
2020
2030
2040
2050
2060
2071
2080
2093
2102
2110
2120
2132
2140
2153
2162
2170
2180
2192
2200
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; struct ShouldObject(T) { private { T testData; ValueEvaluation valueEvaluation; } 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__) { 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__) { 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__) { addMessage(" equal "); addValue("`" ~ U.stringof ~ "`"); beginCheck; return result(testData == instance, [] , new ExpectedActualResult(( expectedValue ? "" : "not " ) ~ instance.to!string, testData.to!string), file, line); } } /// 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)`."); }