fluentasserts.core.string 32/48(66%) 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
31223
320
33223
34223
35223
36223
370
38223
390
40223
410
420
43223
440
450
460
470
480
490
500
51223
520
530
540
550
563
570
583
593
603
613
620
633
648
652
660
670
680
690
700
710
722
730
743
751
760
770
780
790
800
810
821
830
840
850
860
8731
880
8931
9031
9131
9231
930
9431
9531
960
9731
980
990
10031
1010
1020
1030
1040
10562
1060
10731
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
1510
1520
1530
1540
1550
1560
1570
1580
1590
1600
1610
1620
1630
1640
1650
1660
1670
1680
1690
1700
1710
1720
1730
1740
1750
1760
1770
1780
1790
1800
1810
1820
1830
1840
1850
1860
1870
1880
1890
1900
1910
1920
1930
1940
1950
1960
1970
1980
1990
2000
2010
2020
2030
2040
2050
2060
2070
2080
2090
2100
2110
2120
2130
2140
2150
2160
2170
2180
2190
2200
2210
2220
2230
2240
2250
2260
2270
2280
2290
2300
2310
2320
2330
2340
2350
2360
2370
2380
2390
2400
2410
2420
2430
2440
2450
2460
2470
2480
2490
2500
2510
2520
2530
2540
2550
2560
2570
2580
2590
2600
2610
2620
2630
2640
2650
2660
2670
2680
2690
2700
2710
2720
2730
2740
2750
2760
2770
2780
2790
2800
2810
2820
2830
2840
2850
2860
2870
2880
2890
2900
2910
2920
2930
2940
2950
2960
2970
2980
2990
3000
3010
3020
3030
3040
3050
3060
3070
3080
3090
3100
3110
3120
3130
3140
3150
3160
3170
3180
3190
3200
3210
3220
3230
3240
3250
3260
3270
3280
3290
3300
3310
3320
3330
3340
3350
3360
3370
3380
3390
3400
3410
3420
3430
3440
3450
3460
3470
3480
3490
3500
3510
3520
3530
3540
3550
3560
3570
3580
3590
3600
3610
3620
3630
3640
3650
3660
3670
3680
3690
3700
3710
3720
3730
3740
3750
3760
3770
3780
3790
3800
3810
3820
3830
3840
3850
3860
3870
3880
3890
3900
3910
3920
3930
3940
3950
3960
3970
3980
3990
4000
4010
4020
4030
4040
4050
4060
4070
4080
4090
4100
4110
4120
4130
4140
4150
4160
4170
4180
4190
4200
4210
4220
4230
4240
4250
4260
4270
4280
4290
4300
4310
4320
4330
4340
4350
4360
4370
4380
4390
4400
4410
4420
4430
4440
4450
4460
4470
4480
4490
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); }