fluentasserts.core.operations.contain 93/94(98%) line coverage

      
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
20129
210
22129
230
24129
25129
260
27129
28208
290
3097
3118
3218
330
340
350
3678
370
3832
3915
400
4115
427
430
440
4515
460
4715
480
4915
508
510
520
530
548
550
567
570
580
590
607
610
620
6315
6415
650
6615
670
680
690
700
71129
720
730
740
750
76166
770
78166
790
80166
81166
820
83166
84227
850
8689
8741
8841
890
900
910
92205
930
9477
9538
9638
970
980
990
1000
101166
1020
1030
1040
1050
106142
1070
108142
1090
110142
111142
1120
113142
1140
115142
116142
117142
1180
119142
120142
1210
122142
1233
1240
1250
126142
12769
1280
1290
130142
131176
1320
13374
13423
1350
1360
13723
1380
1390
1400
141112
1420
14368
14422
1450
1460
1470
1480
149142
1500
1510
1520
1530
15459
1550
15659
15733
1580
1590
1600
16133
1620
1630
16426
1650
1660
1670
1680
16926
1700
1710
17259
17359
1740
1750
1760
1770
17838
1790
18038
18119
1820
1830
1840
18519
1860
18719
1880
1890
1900
1910
19219
1930
1940
19538
19638
1970
1980
1990
20059
2010
20259
20327
2040
2050
20659
2070
20859
2090
2100
2110
21238
2130
21438
21520
2160
2170
21838
2190
22038
2210
2220
2230
224184
2250
2260
227184
2280
229184
230159
2310
2320
2330
234184
2350
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; } /// IResult[] contain(ref Evaluation evaluation) @safe nothrow { Lifecycle.instance.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.currentValue, missingValues); try results ~= new ExpectedActualResult(createResultMessage(evaluation.expectedValue, expectedPieces), testData); catch(Exception) {} } } 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; Lifecycle.instance.addText(" "); if(presentValues.length == 1) { try Lifecycle.instance.addValue(presentValues[0]); catch(Exception e) { Lifecycle.instance.addText(" some value "); } Lifecycle.instance.addText(" is present in "); } else { try Lifecycle.instance.addValue(presentValues.to!string); catch(Exception e) { Lifecycle.instance.addText(" some values "); } Lifecycle.instance.addText(" are present in "); } Lifecycle.instance.addValue(evaluation.currentValue.strValue); Lifecycle.instance.addText("."); try results ~= new ExpectedActualResult(message, testData); catch(Exception) {} } } return results; } /// IResult[] arrayContain(ref Evaluation evaluation) @safe nothrow { Lifecycle.instance.addText("."); IResult[] results = []; auto expectedPieces = evaluation.expectedValue.strValue.parseList.cleanString; auto testData = evaluation.currentValue.strValue.parseList.cleanString; if(!evaluation.isNegated) { auto missingValues = expectedPieces.filter!(a => !testData.canFind(a)).array; if(missingValues.length > 0) { addLifecycleMessage(evaluation.currentValue, missingValues); try results ~= new ExpectedActualResult(createResultMessage(evaluation.expectedValue, expectedPieces), evaluation.currentValue.strValue); catch(Exception) {} } } else { auto presentValues = expectedPieces.filter!(a => testData.canFind(a)).array; if(presentValues.length > 0) { addNegatedLifecycleMessage(evaluation.currentValue, presentValues); try results ~= new ExpectedActualResult(createNegatedResultMessage(evaluation.expectedValue, expectedPieces), evaluation.currentValue.strValue); catch(Exception) {} } } return results; } /// IResult[] arrayContainOnly(ref Evaluation evaluation) @safe nothrow { Lifecycle.instance.addText("."); IResult[] results = []; auto expectedPieces = evaluation.expectedValue.strValue.parseList.cleanString; auto testData = evaluation.currentValue.strValue.parseList.cleanString; auto comparison = ListComparison!string(testData, expectedPieces); auto missing = comparison.missing; auto extra = comparison.extra; auto common = comparison.common; 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) {} try results ~= new ExtraMissingResult(strExtra, strMissing); catch(Exception) {} } } 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) {} } } return results; } /// void addLifecycleMessage(ValueEvaluation currentValue, string[] missingValues) @safe nothrow { Lifecycle.instance.addText(" "); if(missingValues.length == 1) { try Lifecycle.instance.addValue(missingValues[0]); catch(Exception e) { Lifecycle.instance.addText(" some value "); } Lifecycle.instance.addText(" is missing from "); } else { try { Lifecycle.instance.addValue(missingValues.niceJoin(currentValue.typeName)); } catch(Exception e) { Lifecycle.instance.addText(" some values "); } Lifecycle.instance.addText(" are missing from "); } Lifecycle.instance.addValue(currentValue.strValue); Lifecycle.instance.addText("."); } /// void addNegatedLifecycleMessage(ValueEvaluation currentValue, string[] presentValues) @safe nothrow { Lifecycle.instance.addText(" "); if(presentValues.length == 1) { try Lifecycle.instance.addValue(presentValues[0]); catch(Exception e) { Lifecycle.instance.addText(" some value "); } Lifecycle.instance.addText(" is present in "); } else { try Lifecycle.instance.addValue(presentValues.niceJoin(currentValue.typeName)); catch(Exception e) { Lifecycle.instance.addText(" some values "); } Lifecycle.instance.addText(" are present in "); } Lifecycle.instance.addValue(currentValue.strValue); Lifecycle.instance.addText("."); } string createResultMessage(ValueEvaluation expectedValue, string[] expectedPieces) @safe nothrow { string message = "to contain "; if(expectedPieces.length > 1) { message ~= "all "; } message ~= expectedValue.strValue; return message; } 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 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; }