fluentasserts.core.basetype 115/123(93%) 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
680
690
700
710
720
730
740
750
760
770
780
790
800
810
820
830
840
850
860
870
880
890
900
910
920
930
940
950
960
970
980
990
1000
1010
1020
1030
1040
1050
1060
1070
1080
1090
1100
1110
1120
1130
1140
1150
1160
1170
1180
1190
1200
1210
1220
1230
1240
1250
1260
1270
1280
1290
1300
1310
1320
1330
1340
1350
1360
1370
1380
1390
1400
1410
1420
1430
1440
1450
1460
1470
1480
1490
1500
1515
1520
1530
1542
1552
1560
1570
1582
1592
1600
1610
1622
1632
1640
1650
1662
1672
1680
1690
1702
1712
1720
1730
1740
1750
1760
1772
1782
1792
1800
1810
1823
1832
1840
1850
1862
1870
1883
1892
1900
1910
1922
1930
1940
1950
1960
1972
1982
1992
2000
2010
2023
2032
2040
2050
2062
2072
2082
2090
2103
2112
2120
2130
2142
2152
2162
2170
2180
2190
2200
2212
2222
2232
2240
2252
2262
2270
2280
2293
2302
2310
2320
2330
2342
2350
2363
2372
2380
2390
2400
2412
2420
2430
2440
2450
2462
2472
2482
2490
2502
2512
2520
2530
2543
2552
2560
2570
2580
2592
2602
2612
2620
2633
2642
2650
2660
2670
2682
2690
2700
2710
2720
2732
2742
2752
2762
2772
2780
2792
2802
2812
2822
2830
2840
2853
2862
2870
2880
2890
2902
2912
2922
2930
2943
2952
2960
2970
2980
2992
3002
3012
3020
3033
3042
3050
3060
3070
3082
3092
3102
3110
3123
3132
3140
3150
3160
3172
3182
3192
3200
3210
3220
3230
3242
3252
3262
3270
3280
3293
3302
3310
3320
3332
3342
3352
3360
3373
3382
3390
3400
3412
3422
3432
3440
3450
3460
3470
3480
3491
3500
3510
3520
3532
3540
3550
3560
3570
3582
3592
3600
3611
3620
3630
3642
3650
3662
3671
3680
3692
3700
3711
3720
3730
3742
3750
3761
3772
3780
3790
3802
3810
3820
3830
3840
3851
3862
3870
module fluentasserts.core.basetype; public import fluentasserts.core.base; import fluentasserts.core.results; import std.string; import std.conv; import std.algorithm; @trusted: struct ShouldBaseType(T) { private const T testData; this(U)(U value) { valueEvaluation = value.evaluation; testData = value.value; } mixin ShouldCommons; mixin ShouldThrowableCommons; alias above = typeof(this).greaterThan; alias below = typeof(this).lessThan; alias within = typeof(this).between; auto equal(const T someValue, const string file = __FILE__, const size_t line = __LINE__) { validateException; addMessage(" equal `"); addValue(someValue.to!string); addMessage("`"); beginCheck; auto isSame = testData == someValue; static if(is( T == bool )) { auto expected = expectedValue ? someValue.to!string : (!someValue).to!string; } else { auto expected = expectedValue ? someValue.to!string : ("not " ~ someValue.to!string); } return result(isSame, new ExpectedActualResult(expected, testData.to!string), file, line); } auto greaterThan()(const T someValue, const string file = __FILE__, const size_t line = __LINE__) if(!is(T == bool)) { validateException; addMessage(" greater than `"); addValue(someValue.to!string); addMessage("`"); beginCheck; auto isGreater = testData > someValue; auto mode = isGreater ? "greater than" : "less than or equal to"; auto expectedMode = isGreater ? "less than or equal to" : "greater than"; Message[] msg = [ Message(false, "`"), Message(true, testData.to!string), Message(false, "` is " ~ mode ~ " `"), Message(true, someValue.to!string), Message(false, "`.") ]; return result(isGreater, msg, new ExpectedActualResult(expectedMode ~ " `" ~ someValue.to!string ~ "`", testData.to!string), file, line); } auto lessThan()(const T someValue, const string file = __FILE__, const size_t line = __LINE__) if(!is(T == bool)) { validateException; addMessage(" less than `"); addValue(someValue.to!string); addMessage("`"); beginCheck; auto isLess = testData < someValue; Message[] msg = [ Message(false, "`"), Message(true, testData.to!string), Message(false, isLess ? "` is less than `" : "` is greater or equal to `"), Message(true, someValue.to!string), Message(false, "`.") ]; auto expectedMode = isLess ? "greater or equal to" : "less than"; return result(isLess, msg, new ExpectedActualResult(expectedMode ~ " `" ~ someValue.to!string ~ "`", testData.to!string), file, line); } auto between()(const T limit1, const T limit2, const string file = __FILE__, const size_t line = __LINE__) if(!is(T == bool)) { validateException; T min = limit1 < limit2 ? limit1 : limit2; T max = limit1 > limit2 ? limit1 : limit2; addMessage(" between `"); addValue(min.to!string); addMessage("` and `"); addValue(max.to!string); addMessage("`"); beginCheck; auto isLess = testData <= min; auto isGreater = testData >= max; auto isBetween = !isLess && !isGreater; Message[] msg; auto interval = "a value " ~ (expectedValue ? "inside" : "outside") ~ " (" ~ min.to!string ~ ", " ~ max.to!string ~ ") interval"; if(expectedValue) { msg ~= [ Message(false, "`"), Message(true, testData.to!string), Message(false, "`") ]; if(isLess) { msg ~= [ Message(false, " is less than or equal to `"), Message(true, min.to!string), Message(false, "`.") ]; } if(isGreater) { msg ~= [ Message(false, " is greater than or equal to `"), Message(true, max.to!string), Message(false, "`.") ]; } } return result(isBetween, msg, new ExpectedActualResult(interval, testData.to!string), file, line); } auto approximately()(const T someValue, const T delta, const string file = __FILE__, const size_t line = __LINE__) if(!is(T == bool)) { validateException; addMessage(" equal `"); addValue(someValue.to!string ~ "±" ~ delta.to!string); addMessage("`"); beginCheck; return between(someValue - delta, someValue + delta, file, line); } } /// When there is a lazy number that throws an it should throw that exception unittest { int someLazyInt() { throw new Exception("This is it."); } ({ someLazyInt.should.equal(3); }).should.throwAnyException.withMessage("This is it."); ({ someLazyInt.should.be.greaterThan(3); }).should.throwAnyException.withMessage("This is it."); ({ someLazyInt.should.be.lessThan(3); }).should.throwAnyException.withMessage("This is it."); ({ someLazyInt.should.be.between(3, 4); }).should.throwAnyException.withMessage("This is it."); ({ someLazyInt.should.be.approximately(3, 4); }).should.throwAnyException.withMessage("This is it."); } @("numbers equal") unittest { ({ 5.should.equal(5); 5.should.not.equal(6); }).should.not.throwAnyException; auto msg = ({ 5.should.equal(6); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("5 should equal 6. 5 is not equal to 6."); msg = ({ 5.should.not.equal(5); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("5 should not equal 5. 5 is equal to 5."); } @("bools equal") unittest { ({ true.should.equal(true); true.should.not.equal(false); }).should.not.throwAnyException; auto msg = ({ true.should.equal(false); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("true should equal false."); msg.split("\n")[2].strip.should.equal("Expected:false"); msg.split("\n")[3].strip.should.equal("Actual:true"); msg = ({ true.should.not.equal(true); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("true should not equal true."); msg.split("\n")[2].strip.should.equal("Expected:not true"); msg.split("\n")[3].strip.should.equal("Actual:true"); } @("numbers greater than") unittest { ({ 5.should.be.greaterThan(4); 5.should.not.be.greaterThan(6); 5.should.be.above(4); 5.should.not.be.above(6); }).should.not.throwAnyException; auto msg = ({ 5.should.be.greaterThan(5); 5.should.be.above(5); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("5 should be greater than 5. 5 is less than or equal to 5."); msg = ({ 5.should.not.be.greaterThan(4); 5.should.not.be.above(4); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("5 should not be greater than 4. 5 is greater than 4."); } @("numbers less than") unittest { ({ 5.should.be.lessThan(6); 5.should.not.be.lessThan(4); 5.should.be.below(6); 5.should.not.be.below(4); }).should.not.throwAnyException; auto msg = ({ 5.should.be.lessThan(4); 5.should.be.below(4); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("5 should be less than 4. 5 is greater than or equal to 4."); msg.split("\n")[2].strip.should.equal("Expected:less than 4"); msg.split("\n")[3].strip.should.equal("Actual:5"); msg = ({ 5.should.not.be.lessThan(6); 5.should.not.be.below(6); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("5 should not be less than 6. 5 is less than 6."); } @("numbers between") unittest { ({ 5.should.be.between(4, 6); 5.should.be.between(6, 4); 5.should.not.be.between(5, 6); 5.should.not.be.between(4, 5); 5.should.be.within(4, 6); 5.should.be.within(6, 4); 5.should.not.be.within(5, 6); 5.should.not.be.within(4, 5); }).should.not.throwAnyException; auto msg = ({ 5.should.be.between(5, 6); 5.should.be.within(5, 6); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("5 should be between 5 and 6. 5 is less than or equal to 5."); msg.split("\n")[2].strip.should.equal("Expected:a value inside (5, 6) interval"); msg.split("\n")[3].strip.should.equal("Actual:5"); msg = ({ 5.should.be.between(4, 5); 5.should.be.within(4, 5); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("5 should be between 4 and 5. 5 is greater than or equal to 5."); msg.split("\n")[2].strip.should.equal("Expected:a value inside (4, 5) interval"); msg.split("\n")[3].strip.should.equal("Actual:5"); msg = ({ 5.should.not.be.between(4, 6); 5.should.not.be.within(4, 6); }).should.throwException!TestException.msg; msg.split("\n")[0].strip.should.equal("5 should not be between 4 and 6."); msg.split("\n")[2].strip.should.equal("Expected:a value outside (4, 6) interval"); msg.split("\n")[3].strip.should.equal("Actual:5"); msg = ({ 5.should.not.be.between(6, 4); 5.should.not.be.within(6, 4); }).should.throwException!TestException.msg; msg.split("\n")[0].strip.should.equal("5 should not be between 6 and 4."); msg.split("\n")[2].strip.should.equal("Expected:a value outside (4, 6) interval"); msg.split("\n")[3].strip.should.equal("Actual:5"); } /// numbers approximately unittest { ({ (10f/3f).should.be.approximately(3, 0.34); (10f/3f).should.not.be.approximately(3, 0.1); }).should.not.throwAnyException; auto msg = ({ (10f/3f).should.be.approximately(3, 0.1); }).should.throwException!TestException.msg; msg.split("\n")[0].strip.should.equal("(10f/3f) should be approximately 3±0.1."); msg.split("\n")[2].strip.should.equal("Expected:3±0.1"); msg.split("\n")[3].strip.should.equal("Actual:3.33333"); msg = ({ (10f/3f).should.not.be.approximately(3, 0.34); }).should.throwException!TestException.msg; msg.split("\n")[0].strip.should.equal("(10f/3f) should not be approximately 3±0.34."); msg.split("\n")[2].strip.should.equal("Expected:not 3±0.34"); msg.split("\n")[3].strip.should.equal("Actual:3.33333"); } /// should throw exceptions for delegates that return basic types unittest { int value() { throw new Exception("not implemented value"); } void voidValue() { throw new Exception("nothing here"); } void noException() { } value().should.throwAnyException.withMessage.equal("not implemented value"); voidValue().should.throwAnyException.withMessage.equal("nothing here"); bool thrown; try { noException.should.throwAnyException; } catch (TestException e) { e.msg.should.startWith("noException should throw any exception. No exception was thrown."); thrown = true; } thrown.should.equal(true); thrown = false; try { voidValue().should.not.throwAnyException; } catch(TestException e) { thrown = true; e.msg.split("\n")[0].should.equal("voidValue() should not throw any exception. `object.Exception` saying `nothing here` was thrown."); } thrown.should.equal(true); } /// it should compile const comparison unittest { const actual = 42; actual.should.equal(42); }