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

      
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
200
2128
2228
230
240
25421
26421
27421
280
290
300
31360
320
33359
34359
35359
36359
370
38359
390
40359
410
420
43359
440
450
460
470
480
490
500
51359
520
530
540
550
565
570
584
594
604
614
620
634
646
652
660
670
680
690
700
710
722
730
746
752
760
770
780
790
800
810
822
830
840
850
860
8727
880
8926
9026
9126
9226
930
9426
9526
960
9726
980
990
10026
1010
1020
1030
1040
10552
1060
10726
1080
1090
1100
1116
1120
1135
1145
1155
1165
1170
1185
1195
1200
1215
1220
1230
1245
1250
1260
1270
1280
12910
1300
1315
1320
1330
1340
13536
1360
13735
13835
13935
14035
1410
14235
14335
1440
14535
1460
1470
14835
1490
1500
1510
1520
15370
1540
15535
1560
1570
1580
15913
1600
16112
16212
16312
16412
1650
16612
1670
1680
1696
1700
1716
1720
1730
17412
1750
1760
17712
1780
1790
1800
1810
18224
1830
18412
1850
1860
1870
1880
1890
1901
1916
1920
1930
1943
1952
1960
1970
1983
1992
2000
2010
2023
2032
2040
2050
2063
2072
2080
2090
2103
2112
2120
2130
2143
2152
2160
2170
2180
2190
2200
2212
2222
2230
2240
2253
2262
2270
2280
2292
2302
2312
2320
2332
2342
2350
2360
2373
2382
2390
2400
2412
2422
2432
2440
2452
2462
2470
2480
2493
2502
2510
2520
2532
2542
2552
2560
2572
2582
2590
2600
2613
2622
2630
2640
2652
2662
2672
2680
2690
2700
2710
2722
2732
2740
2750
2763
2772
2780
2790
2802
2812
2822
2830
2842
2852
2860
2870
2883
2892
2900
2910
2922
2932
2942
2950
2962
2972
2980
2990
3003
3012
3020
3030
3042
3052
3062
3070
3082
3092
3100
3110
3123
3132
3140
3150
3162
3172
3182
3190
3200
3210
3220
3232
3242
3252
3260
3270
3282
3292
3302
3310
3320
3332
3342
3352
3360
3370
3383
3392
3400
3410
3422
3432
3442
3450
3463
3472
3480
3490
3502
3512
3522
3530
3543
3552
3560
3570
3582
3592
3602
3610
3623
3632
3640
3650
3662
3672
3682
3690
3703
3712
3720
3730
3742
3752
3762
3770
3783
3792
3800
3810
3822
3832
3842
3850
3860
3870
3880
3892
3902
3910
3920
3932
3942
3950
3960
3973
3982
3990
4000
4012
4020
4033
4042
4050
4060
4072
4080
4093
4101
4112
4120
4130
4142
4152
4162
4170
4180
4190
4200
4211
4221
4230
4240
4252
4260
4272
4280
4291
4300
4312
4320
4332
4341
4350
4360
4372
4380
4390
4400
4410
4421
4431
4440
4452
4462
4470
4482
4492
4500
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; @safe: struct ShouldString { private { const string testData; } 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__) @trusted { validateException; 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__) { validateException; 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__) { validateException; 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__) { validateException; 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__) { validateException; 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__) { validateException; 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); } } /// When there is a lazy string that throws an it should throw that exception unittest { string someLazyString() { throw new Exception("This is it."); } ({ someLazyString.should.equal(""); }).should.throwAnyException.withMessage("This is it."); ({ someLazyString.should.contain(""); }).should.throwAnyException.withMessage("This is it."); ({ someLazyString.should.contain([""]); }).should.throwAnyException.withMessage("This is it."); ({ someLazyString.should.contain(' '); }).should.throwAnyException.withMessage("This is it."); ({ someLazyString.should.startWith(" "); }).should.throwAnyException.withMessage("This is it."); ({ someLazyString.should.endWith(" "); }).should.throwAnyException.withMessage("This is it."); } @("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); }