102030405060708090100110120130140150160170180190200210220233624025362602736283629030363172320333434035036037038039040041042543044245046047048049050051052053054055056057058059060061062063064065066067068069070071072073074075076077078079080368108208308408588608788808989089109289313294095896097098099010001010102010301040105010601070108010901100111011201130114011501168117011801190120012111220123112401251126112701281129013011311132113301340135113611371138013901400141014201430144114511460147114801490150015111520153015401551156315701581159016001610162016301640165016601670168016901700171017201730174017501760177017801790180018101820183118401850186018701880189019001910192019301940195019601970198019902000201020202030204020502060207020802090210021102120213021402150216021702180219022002210222022302240225022602270228022902300231023202330234023502360237023802390240024102420243024402450246024702480249025002510252025302540255025602570258025902600261026202630264026502660267026802690270027102720273027402750276027702780279028002810282028302840285028602870288028902900291029202930294029502960297029802990300030103020 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); }
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); }