fluentasserts.core.operations.contain 37/108(34%) line coverage

      
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
200
210
220
2336
240
2536
260
2736
2836
290
3036
3172
320
3334
340
350
360
370
380
390
400
410
425
430
442
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
8036
810
820
830
840
858
860
878
880
898
908
910
928
93132
940
958
960
970
980
990
1000
1010
1020
1030
1040
1050
1060
1070
1080
1090
1100
1110
1120
1130
1140
1150
1168
1170
1180
1190
1200
1211
1220
1231
1240
1251
1261
1270
1281
1290
1301
1311
1321
1330
1340
1351
1361
1371
1380
1390
1400
1410
1420
1430
1441
1451
1460
1471
1480
1490
1500
1511
1520
1530
1540
1551
1563
1570
1581
1590
1600
1610
1620
1630
1640
1650
1660
1670
1680
1690
1700
1710
1720
1730
1740
1750
1760
1770
1780
1790
1800
1810
1820
1831
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
module fluentasserts.core.operations.contain; import std.algorithm; import std.array; import std.conv; import fluentasserts.core.array; import fluentasserts.core.results; import fluentasserts.core.evaluation; import fluentasserts.core.serializers; import fluentasserts.core.lifecycle; version(unittest) { import fluentasserts.core.expect; } static immutable containDescription = "When the tested value is a string, it asserts that the given string val is a substring of the target. \n\n" ~ "When the tested value is an array, it asserts that the given val is inside the tested value."; /// IResult[] contain(ref Evaluation evaluation) @safe nothrow { evaluation.message.addText("."); IResult[] results = []; auto expectedPieces = evaluation.expectedValue.strValue.parseList.cleanString; auto testData = evaluation.currentValue.strValue.cleanString; if(!evaluation.isNegated) { auto missingValues = expectedPieces.filter!(a => !testData.canFind(a)).array; if(missingValues.length > 0) { addLifecycleMessage(evaluation, missingValues); try results ~= new ExpectedActualResult(createResultMessage(evaluation.expectedValue, expectedPieces), testData); catch(Exception e) { results ~= e.toResults; return results; } } } else { auto presentValues = expectedPieces.filter!(a => testData.canFind(a)).array; if(presentValues.length > 0) { string message = "to not contain "; if(presentValues.length > 1) { message ~= "any "; } message ~= evaluation.expectedValue.strValue; evaluation.message.addText(" "); if(presentValues.length == 1) { try evaluation.message.addValue(presentValues[0]); catch(Exception e) { evaluation.message.addText(" some value "); } evaluation.message.addText(" is present in "); } else { try evaluation.message.addValue(presentValues.to!string); catch(Exception e) { evaluation.message.addText(" some values "); } evaluation.message.addText(" are present in "); } evaluation.message.addValue(evaluation.currentValue.strValue); evaluation.message.addText("."); try results ~= new ExpectedActualResult(message, testData); catch(Exception e) { results ~= e.toResults; return results; } } } return results; } /// IResult[] arrayContain(ref Evaluation evaluation) @trusted nothrow { evaluation.message.addText("."); IResult[] results = []; auto expectedPieces = evaluation.expectedValue.proxyValue.toArray; auto testData = evaluation.currentValue.proxyValue.toArray; if(!evaluation.isNegated) { auto missingValues = expectedPieces.filter!(a => testData.filter!(b => b.isEqualTo(a)).empty).array; if(missingValues.length > 0) { addLifecycleMessage(evaluation, missingValues); try results ~= new ExpectedActualResult(createResultMessage(evaluation.expectedValue, expectedPieces), evaluation.currentValue.strValue); catch(Exception e) { results ~= e.toResults; return results; } } } else { auto presentValues = expectedPieces.filter!(a => !testData.filter!(b => b.isEqualTo(a)).empty).array; if(presentValues.length > 0) { addNegatedLifecycleMessage(evaluation, presentValues); try results ~= new ExpectedActualResult(createNegatedResultMessage(evaluation.expectedValue, expectedPieces), evaluation.currentValue.strValue); catch(Exception e) { results ~= e.toResults; return results; } } } return results; } /// IResult[] arrayContainOnly(ref Evaluation evaluation) @safe nothrow { evaluation.message.addText("."); IResult[] results = []; auto expectedPieces = evaluation.expectedValue.proxyValue.toArray; auto testData = evaluation.currentValue.proxyValue.toArray; auto comparison = ListComparison!EquableValue(testData, expectedPieces); EquableValue[] missing; EquableValue[] extra; EquableValue[] common; try { missing = comparison.missing; extra = comparison.extra; common = comparison.common; } catch(Exception e) { results ~= e.toResults; return results; } string strExtra = ""; string strMissing = ""; if(extra.length > 0) { strExtra = extra.niceJoin(evaluation.currentValue.typeName); } if(missing.length > 0) { strMissing = missing.niceJoin(evaluation.currentValue.typeName); } if(!evaluation.isNegated) { auto isSuccess = missing.length == 0 && extra.length == 0 && common.length == testData.length; if(!isSuccess) { try results ~= new ExpectedActualResult("", testData.niceJoin(evaluation.currentValue.typeName)); catch(Exception e) { results ~= e.toResults; return results; } try results ~= new ExtraMissingResult(strExtra, strMissing); catch(Exception e) { results ~= e.toResults; return results; } } } else { auto isSuccess = (missing.length != 0 || extra.length != 0) || common.length != testData.length; if(!isSuccess) { try results ~= new ExpectedActualResult("to not contain " ~ expectedPieces.niceJoin(evaluation.currentValue.typeName), testData.niceJoin(evaluation.currentValue.typeName)); catch(Exception e) { results ~= e.toResults; return results; } } } return results; } /// void addLifecycleMessage(ref Evaluation evaluation, string[] missingValues) @safe nothrow { evaluation.message.addText(" "); if(missingValues.length == 1) { try evaluation.message.addValue(missingValues[0]); catch(Exception) { evaluation.message.addText(" some value "); } evaluation.message.addText(" is missing from "); } else { try { evaluation.message.addValue(missingValues.niceJoin(evaluation.currentValue.typeName)); } catch(Exception) { evaluation.message.addText(" some values "); } evaluation.message.addText(" are missing from "); } evaluation.message.addValue(evaluation.currentValue.strValue); evaluation.message.addText("."); } /// void addLifecycleMessage(ref Evaluation evaluation, EquableValue[] missingValues) @safe nothrow { auto missing = missingValues.map!(a => a.getSerialized.cleanString).array; addLifecycleMessage(evaluation, missing); } /// void addNegatedLifecycleMessage(ref Evaluation evaluation, string[] presentValues) @safe nothrow { evaluation.message.addText(" "); if(presentValues.length == 1) { try evaluation.message.addValue(presentValues[0]); catch(Exception e) { evaluation.message.addText(" some value "); } evaluation.message.addText(" is present in "); } else { try evaluation.message.addValue(presentValues.niceJoin(evaluation.currentValue.typeName)); catch(Exception e) { evaluation.message.addText(" some values "); } evaluation.message.addText(" are present in "); } evaluation.message.addValue(evaluation.currentValue.strValue); evaluation.message.addText("."); } /// void addNegatedLifecycleMessage(ref Evaluation evaluation, EquableValue[] missingValues) @safe nothrow { auto missing = missingValues.map!(a => a.getSerialized).array; addNegatedLifecycleMessage(evaluation, missing); } string createResultMessage(ValueEvaluation expectedValue, string[] expectedPieces) @safe nothrow { string message = "to contain "; if(expectedPieces.length > 1) { message ~= "all "; } message ~= expectedValue.strValue; return message; } /// string createResultMessage(ValueEvaluation expectedValue, EquableValue[] missingValues) @safe nothrow { auto missing = missingValues.map!(a => a.getSerialized).array; return createResultMessage(expectedValue, missing); } string createNegatedResultMessage(ValueEvaluation expectedValue, string[] expectedPieces) @safe nothrow { string message = "to not contain "; if(expectedPieces.length > 1) { message ~= "any "; } message ~= expectedValue.strValue; return message; } /// string createNegatedResultMessage(ValueEvaluation expectedValue, EquableValue[] missingValues) @safe nothrow { auto missing = missingValues.map!(a => a.getSerialized).array; return createNegatedResultMessage(expectedValue, missing); } string niceJoin(string[] values, string typeName = "") @safe nothrow { string result = ""; try { result = values.to!string; if(!typeName.canFind("string")) { result = result.replace(`"`, ""); } } catch(Exception) {} return result; } string niceJoin(EquableValue[] values, string typeName = "") @safe nothrow { return values.map!(a => a.getSerialized.cleanString).array.niceJoin(typeName); }