fluentasserts.core.string 191/191(100%) line coverage

      
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
2010
2110
220
230
24341
25341
26341
270
280
290
30269
31269
32269
33269
340
35269
360
37269
380
390
40269
410
420
430
440
450
460
470
48269
490
500
510
520
534
544
554
564
570
584
596
602
610
620
630
640
650
660
672
680
696
702
710
720
730
740
750
760
772
780
790
800
810
8226
8326
8426
8526
860
8726
8826
890
9026
910
920
9326
940
950
960
970
9852
990
10026
1010
1020
1030
1045
1055
1065
1075
1080
1095
1105
1110
1125
1130
1140
1155
1160
1170
1180
1190
12010
1210
1225
1230
1240
1250
12633
12733
12833
12933
1300
13133
13233
1330
13433
1350
1360
13733
1380
1390
1400
1410
14266
1430
14433
1450
1460
1470
14812
14912
15012
15112
1520
15312
1540
1550
1566
1570
1586
1590
1600
16112
1620
1630
16412
1650
1660
1670
1680
16924
1700
17112
1720
1730
1740
1750
1760
1772
1782
1790
1800
1813
1822
1830
1840
1852
1862
1872
1880
1892
1902
1910
1920
1933
1942
1950
1960
1972
1982
1992
2000
2012
2022
2030
2040
2053
2062
2070
2080
2092
2102
2112
2120
2132
2142
2150
2160
2173
2182
2190
2200
2212
2222
2232
2240
2250
2260
2270
2282
2292
2300
2310
2323
2332
2340
2350
2362
2372
2382
2390
2402
2412
2420
2430
2443
2452
2460
2470
2482
2492
2502
2510
2522
2532
2540
2550
2563
2572
2580
2590
2602
2612
2622
2630
2642
2652
2660
2670
2683
2692
2700
2710
2722
2732
2742
2750
2760
2770
2780
2792
2802
2812
2820
2830
2842
2852
2862
2870
2880
2892
2902
2912
2920
2930
2943
2952
2960
2970
2982
2992
3002
3010
3023
3032
3040
3050
3062
3072
3082
3090
3103
3112
3120
3130
3142
3152
3162
3170
3183
3192
3200
3210
3222
3232
3242
3250
3263
3272
3280
3290
3302
3312
3322
3330
3343
3352
3360
3370
3382
3392
3402
3410
3420
3430
3440
3452
3462
3470
3480
3492
3502
3510
3520
3533
3542
3550
3560
3572
3580
3593
3602
3610
3620
3632
3640
3653
3661
3672
3680
3690
3702
3712
3722
3730
3740
3750
3760
3771
3781
3790
3800
3812
3820
3832
3840
3851
3860
3872
3880
3892
3901
3910
3920
3932
3940
3950
3960
3970
3981
3991
4000
4012
4022
4030
4042
4052
4060
module fluentasserts.core.string; public import fluentasserts.core.base; import fluentasserts.core.results; import std.string; import std.conv; import std.algorithm; import std.array; struct ShouldString { private { const string testData; ValueEvaluation valueEvaluation; } mixin ShouldCommons; mixin ShouldThrowableCommons; this(string value) { testData = value; } this(U)(U value) { valueEvaluation = value.evaluation; testData = value.value; } auto equal(const string someString, const string file = __FILE__, const size_t line = __LINE__) { addMessage(" equal `"); addValue(someString.to!string); addMessage("`"); beginCheck; auto isSame = testData == someString; Message[] msg = [ Message(false, "`"), Message(true, testData), Message(false, "` is" ~ (expectedValue ? " not" : "") ~ " equal to `"), Message(true, someString), Message(false, "`.") ]; version(DisableDiffResult) { return result(isSame, msg, cast(IResult[])[ new ExpectedActualResult(someString, testData) ], file, line); } else { return result(isSame, msg, cast(IResult[])[ new DiffResult(someString, testData), new ExpectedActualResult(someString, testData) ], file, line); } } auto contain(const string[] someStrings, const string file = __FILE__, const size_t line = __LINE__) { addMessage(" contain `"); addValue(someStrings.to!string); addMessage("`"); beginCheck; if(expectedValue) { auto missingValues = someStrings.filter!(a => testData.indexOf(a) == -1).array; Message[] msg = [ Message(true, missingValues.to!string), Message(false, " are missing from `"), Message(true, testData), Message(false, "`.") ]; return result(missingValues.length == 0, msg, new ExpectedActualResult("to contain all " ~ someStrings.to!string, testData), file, line); } else { auto presentValues = someStrings.filter!(a => testData.indexOf(a) != -1).array; Message[] msg = [ Message(true, presentValues.to!string), Message(false, " are present in `"), Message(true, testData), Message(false, "`.") ]; return result(presentValues.length != 0, msg, new ExpectedActualResult("to not contain any " ~ someStrings.to!string, testData), file, line); } } auto contain(const string someString, const string file = __FILE__, const size_t line = __LINE__) { addMessage(" contain `"); addValue(someString); addMessage("`"); beginCheck; auto index = testData.indexOf(someString); auto isPresent = index >= 0; Message[] msg = [ Message(false, "`"), Message(true, someString), Message(false, expectedValue ? "` is missing from `" : "` is present in `"), Message(true, testData), Message(false, "`.") ]; auto mode = expectedValue ? "to contain" : "to not contain"; return result(isPresent, msg, new ExpectedActualResult(mode ~ " `" ~ someString ~ "`", testData), file, line); } auto contain(const char someChar, const string file = __FILE__, const size_t line = __LINE__) { addMessage(" contain `"); addValue(someChar.to!string); addMessage("`"); beginCheck; auto index = testData.indexOf(someChar); auto isPresent = index >= 0; Message[] msg = [ Message(false, "`"), Message(true, someChar.to!string), Message(false, isPresent ? "` is present in `" : "` is not present in `"), Message(true, testData), Message(false, "`.") ]; auto mode = expectedValue ? "to contain" : "to not contain"; return result(isPresent, msg, new ExpectedActualResult(mode ~ " `" ~ someChar ~ "`", testData), file, line); } auto startWith(T)(const T someString, const string file = __FILE__, const size_t line = __LINE__) { addMessage(" start with `"); addValue(someString.to!string); addMessage("`"); beginCheck; auto index = testData.indexOf(someString); auto doesStartWith = index == 0; Message[] msg = [ Message(false, "`"), Message(true, testData.to!string), Message(false, expectedValue ? "` does not start with `" : "` does start with `"), Message(true, someString.to!string), Message(false, "`.") ]; auto mode = expectedValue ? "to start with " : "to not start with "; return result(doesStartWith, msg, new ExpectedActualResult(mode ~ "`" ~ someString.to!string ~ "`", testData), file, line); } auto endWith(T)(const T someString, const string file = __FILE__, const size_t line = __LINE__) { addMessage(" end with `"); addValue(someString.to!string); addMessage("`"); beginCheck; auto index = testData.lastIndexOf(someString); static if(is(T == string)) { auto doesEndWith = index == testData.length - someString.length; } else { auto doesEndWith = index == testData.length - 1; } Message[] msg = [ Message(false, "`"), Message(true, testData.to!string), Message(false, expectedValue ? "` does not end with `" : "` does end with `"), Message(true, someString.to!string), Message(false, "`.") ]; auto mode = expectedValue ? "to end with " : "to not end with "; return result(doesEndWith, msg, new ExpectedActualResult(mode ~ "`" ~ someString.to!string ~ "`", testData), file, line); } } @("string startWith") unittest { ({ "test string".should.startWith("test"); }).should.not.throwAnyException; auto msg = ({ "test string".should.startWith("other"); }).should.throwException!TestException.msg; msg.split("\n")[0].should.contain("`test string` does not start with `other`"); msg.split("\n")[2].strip.should.equal("Expected:to start with `other`"); msg.split("\n")[3].strip.should.equal("Actual:test string"); ({ "test string".should.not.startWith("other"); }).should.not.throwAnyException; msg = ({ "test string".should.not.startWith("test"); }).should.throwException!TestException.msg; msg.split("\n")[0].should.contain("`test string` does start with `test`"); msg.split("\n")[2].strip.should.equal("Expected:to not start with `test`"); msg.split("\n")[3].strip.should.equal("Actual:test string"); ({ "test string".should.startWith('t'); }).should.not.throwAnyException; msg = ({ "test string".should.startWith('o'); }).should.throwException!TestException.msg; msg.split("\n")[0].should.contain("`test string` does not start with `o`"); msg.split("\n")[2].strip.should.equal("Expected:to start with `o`"); msg.split("\n")[3].strip.should.equal("Actual:test string"); ({ "test string".should.not.startWith('o'); }).should.not.throwAnyException; msg = ({ "test string".should.not.startWith('t'); }).should.throwException!TestException.msg; msg.split("\n")[0].should.contain("`test string` does start with `t`"); msg.split("\n")[2].strip.should.equal("Expected:to not start with `t`"); msg.split("\n")[3].strip.should.equal("Actual:test string"); } @("string endWith") unittest { ({ "test string".should.endWith("string"); }).should.not.throwAnyException; auto msg = ({ "test string".should.endWith("other"); }).should.throwException!TestException.msg; msg.split("\n")[0].should.contain("`test string` does not end with `other`"); msg.split("\n")[2].strip.should.equal("Expected:to end with `other`"); msg.split("\n")[3].strip.should.equal("Actual:test string"); ({ "test string".should.not.endWith("other"); }).should.not.throwAnyException; msg = ({ "test string".should.not.endWith("string"); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("\"test string\" should not end with `string`. `test string` does end with `string`."); msg.split("\n")[2].strip.should.equal("Expected:to not end with `string`"); msg.split("\n")[3].strip.should.equal("Actual:test string"); ({ "test string".should.endWith('g'); }).should.not.throwAnyException; msg = ({ "test string".should.endWith('t'); }).should.throwException!TestException.msg; msg.split("\n")[0].should.contain("`test string` does not end with `t`"); msg.split("\n")[2].strip.should.equal("Expected:to end with `t`"); msg.split("\n")[3].strip.should.equal("Actual:test string"); ({ "test string".should.not.endWith('w'); }).should.not.throwAnyException; msg = ({ "test string".should.not.endWith('g'); }).should.throwException!TestException.msg; msg.split("\n")[0].should.contain("`test string` does end with `g`"); msg.split("\n")[2].strip.should.equal("Expected:to not end with `g`"); msg.split("\n")[3].strip.should.equal("Actual:test string"); } @("string contain") unittest { ({ "test string".should.contain(["string", "test"]); "test string".should.not.contain(["other", "message"]); }).should.not.throwAnyException; ({ "test string".should.contain("string"); "test string".should.not.contain("other"); }).should.not.throwAnyException; ({ "test string".should.contain('s'); "test string".should.not.contain('z'); }).should.not.throwAnyException; auto msg = ({ "test string".should.contain(["other", "message"]); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("\"test string\" should contain `[\"other\", \"message\"]`. [\"other\", \"message\"] are missing from `test string`."); msg.split("\n")[2].strip.should.equal("Expected:to contain all [\"other\", \"message\"]"); msg.split("\n")[3].strip.should.equal("Actual:test string"); msg = ({ "test string".should.not.contain(["test", "string"]); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("\"test string\" should not contain `[\"test\", \"string\"]`. [\"test\", \"string\"] are present in `test string`."); msg.split("\n")[2].strip.should.equal("Expected:to not contain any [\"test\", \"string\"]"); msg.split("\n")[3].strip.should.equal("Actual:test string"); msg = ({ "test string".should.contain("other"); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("\"test string\" should contain `other`. `other` is missing from `test string`."); msg.split("\n")[2].strip.should.equal("Expected:to contain `other`"); msg.split("\n")[3].strip.should.equal("Actual:test string"); msg = ({ "test string".should.not.contain("test"); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("\"test string\" should not contain `test`. `test` is present in `test string`."); msg.split("\n")[2].strip.should.equal("Expected:to not contain `test`"); msg.split("\n")[3].strip.should.equal("Actual:test string"); msg = ({ "test string".should.contain('o'); }).should.throwException!TestException.msg; msg.split("\n")[0].should.contain("`o` is not present in `test string`"); msg.split("\n")[2].strip.should.equal("Expected:to contain `o`"); msg.split("\n")[3].strip.should.equal("Actual:test string"); msg = ({ "test string".should.not.contain('t'); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("\"test string\" should not contain `t`. `t` is present in `test string`."); msg.split("\n")[2].strip.should.equal("Expected:to not contain `t`"); msg.split("\n")[3].strip.should.equal("Actual:test string"); } @("string equal") unittest { ({ "test string".should.equal("test string"); }).should.not.throwAnyException; ({ "test string".should.not.equal("test"); }).should.not.throwAnyException; auto msg = ({ "test string".should.equal("test"); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("\"test string\" should equal `test`. `test string` is not equal to `test`."); msg = ({ "test string".should.not.equal("test string"); }).should.throwException!TestException.msg; msg.split("\n")[0].should.equal("\"test string\" should not equal `test string`. `test string` is equal to `test string`."); msg = ({ ubyte[] data = [115, 111, 109, 101, 32, 100, 97, 116, 97, 0, 0]; data.assumeUTF.to!string.should.equal("some data"); }).should.throwException!TestException.msg; msg.should.contain(`Actual:some data\0\0`); msg.should.contain("data.assumeUTF.to!string should equal `some data`. `some data\\0\\0` is not equal to `some data`."); msg.should.contain(`some data[+\0\0]`); } /// should throw exceptions for delegates that return basic types unittest { string value() { throw new Exception("not implemented"); } string 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); } @("const string equal") unittest { const string constValue = "test string"; immutable string immutableValue = "test string"; constValue.should.equal("test string"); immutableValue.should.equal("test string"); "test string".should.equal(constValue); "test string".should.equal(immutableValue); }