Skip to content

Commit

Permalink
Enable objects for contain and contain only
Browse files Browse the repository at this point in the history
  • Loading branch information
Szabo Bogdan committed Oct 8, 2021
1 parent e5ae31b commit 08a475a
Show file tree
Hide file tree
Showing 8 changed files with 289 additions and 52 deletions.
34 changes: 19 additions & 15 deletions source/fluentasserts/core/array.d
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,29 @@ struct ListComparison(Type) {
this.maxRelDiff = maxRelDiff;
}

private long findIndex(T[] list, T element) {
static if(std.traits.isNumeric!(T)) {
return list.countUntil!(a => approxEqual(element, a, maxRelDiff));
} else static if(is(T == EquableValue)) {
foreach(index, a; list) {
if(a.isEqualTo(element)) {
return index;
}
}

return -1;
} else {
return list.countUntil(element);
}
}

T[] missing() @trusted {
T[] result;

auto tmpList = list.dup;

foreach(element; referenceList) {
static if(std.traits.isNumeric!(T)) {
auto index = tmpList.countUntil!(a => approxEqual(element, a, maxRelDiff));
} else {
auto index = tmpList.countUntil(element);
}
auto index = this.findIndex(tmpList, element);

if(index == -1) {
result ~= element;
Expand All @@ -76,11 +88,7 @@ struct ListComparison(Type) {
auto tmpReferenceList = referenceList.dup;

foreach(element; list) {
static if(isFloatingPoint!(T)) {
auto index = tmpReferenceList.countUntil!(a => approxEqual(element, a, maxRelDiff));
} else {
auto index = tmpReferenceList.countUntil(element);
}
auto index = this.findIndex(tmpReferenceList, element);

if(index == -1) {
result ~= element;
Expand All @@ -102,11 +110,7 @@ struct ListComparison(Type) {
break;
}

static if(isFloatingPoint!(T)) {
auto index = tmpList.countUntil!(a => approxEqual(element, a, maxRelDiff));
} else {
auto index = tmpList.countUntil(element);
}
auto index = this.findIndex(tmpList, element);

if(index >= 0) {
result ~= element;
Expand Down
8 changes: 8 additions & 0 deletions source/fluentasserts/core/base.d
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,14 @@ mixin template ShouldCommons()
messages ~= Message(true, msg);
}

void addValue(EquableValue msg) {
if(mesageCheckIndex != 0) {
return;
}

messages ~= Message(true, msg.getSerialized);
}

void beginCheck() {
if(mesageCheckIndex != 0) {
return;
Expand Down
21 changes: 10 additions & 11 deletions source/fluentasserts/core/evaluation.d
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ interface EquableValue {

/// Wraps a value into equable value
EquableValue equableValue(T)(T value, string serialized) {

static if(isArray!T && !isSomeString!T) {
return new ArrayEquable!T(value, serialized);
} else static if(isInputRange!T && !isSomeString!T) {
Expand All @@ -227,12 +226,12 @@ class ObjectEquable(T) : EquableValue {
string serialized;
}

this(T value, string serialized) {
this.value = value;
this.serialized = serialized;
}

@trusted nothrow:
this(T value, string serialized) {
this.value = value;
this.serialized = serialized;
}

bool isEqualTo(EquableValue otherEquable) {
try {
auto other = cast(ObjectEquable) otherEquable;
Expand Down Expand Up @@ -294,12 +293,12 @@ class ArrayEquable(U: T[], T) : EquableValue {
string serialized;
}

this(T[] values, string serialized) {
this.values = values;
this.serialized = serialized;
}

@safe nothrow:
this(T[] values, string serialized) {
this.values = values;
this.serialized = serialized;
}

bool isEqualTo(EquableValue otherEquable) {
auto other = cast(ArrayEquable!U) otherEquable;

Expand Down
6 changes: 4 additions & 2 deletions source/fluentasserts/core/lifecycle.d
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ static this() {
Registry.instance.register("object.Object[]", "object.Object[]", "containOnly", &arrayContainOnly);
}


Registry.instance.register("*[]", "*[]", "contain", &arrayContain);
Registry.instance.register("*[]", "*[]", "containOnly", &arrayContainOnly);

static foreach(Type1; StringTypes) {
Registry.instance.register(Type1.stringof ~ "[]", "void[]", "equal", &arrayEqual);

Expand All @@ -118,8 +122,6 @@ static this() {

Registry.instance.register(Type1.stringof, Type2.stringof ~ "[]", "contain", &contain);
Registry.instance.register(Type1.stringof, Type2.stringof, "contain", &contain);
Registry.instance.register(Type1.stringof ~ "[]", Type2.stringof, "contain", &arrayContain);
Registry.instance.register(Type1.stringof ~ "[]", Type2.stringof ~ "[]", "contain", &arrayContain);
Registry.instance.register(Type1.stringof ~ "[]", Type2.stringof ~ "[]", "containOnly", &arrayContainOnly);

Registry.instance.register(Type1.stringof, Type2.stringof, "startWith", &startWith);
Expand Down
106 changes: 85 additions & 21 deletions source/fluentasserts/core/operations/contain.d
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ IResult[] contain(ref Evaluation evaluation) @safe nothrow {
if(missingValues.length > 0) {
addLifecycleMessage(evaluation, missingValues);
try results ~= new ExpectedActualResult(createResultMessage(evaluation.expectedValue, expectedPieces), testData);
catch(Exception) {}
catch(Exception e) {
results ~= e.toResults;
return results;
}
}
} else {
auto presentValues = expectedPieces.filter!(a => testData.canFind(a)).array;
Expand Down Expand Up @@ -64,37 +67,46 @@ IResult[] contain(ref Evaluation evaluation) @safe nothrow {
evaluation.message.addText(".");

try results ~= new ExpectedActualResult(message, testData);
catch(Exception) {}
catch(Exception e) {
results ~= e.toResults;
return results;
}
}
}

return results;
}

///
IResult[] arrayContain(ref Evaluation evaluation) @safe nothrow {
IResult[] arrayContain(ref Evaluation evaluation) @trusted nothrow {
evaluation.message.addText(".");

IResult[] results = [];

auto expectedPieces = evaluation.expectedValue.strValue.parseList.cleanString;
auto testData = evaluation.currentValue.strValue.parseList.cleanString;
auto expectedPieces = evaluation.expectedValue.proxyValue.toArray;
auto testData = evaluation.currentValue.proxyValue.toArray;

if(!evaluation.isNegated) {
auto missingValues = expectedPieces.filter!(a => !testData.canFind(a)).array;
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) {}
catch(Exception e) {
results ~= e.toResults;
return results;
}
}
} else {
auto presentValues = expectedPieces.filter!(a => testData.canFind(a)).array;
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) {}
catch(Exception e) {
results ~= e.toResults;
return results;
}
}
}

Expand All @@ -107,14 +119,24 @@ IResult[] arrayContainOnly(ref Evaluation evaluation) @safe nothrow {

IResult[] results = [];

auto expectedPieces = evaluation.expectedValue.strValue.parseList.cleanString;
auto testData = evaluation.currentValue.strValue.parseList.cleanString;
auto expectedPieces = evaluation.expectedValue.proxyValue.toArray;
auto testData = evaluation.currentValue.proxyValue.toArray;

auto comparison = ListComparison!EquableValue(testData, expectedPieces);

auto comparison = ListComparison!string(testData, expectedPieces);
EquableValue[] missing;
EquableValue[] extra;
EquableValue[] common;

auto missing = comparison.missing;
auto extra = comparison.extra;
auto common = comparison.common;
try {
missing = comparison.missing;
extra = comparison.extra;
common = comparison.common;
} catch(Exception e) {
results ~= e.toResults;

return results;
}

string strExtra = "";
string strMissing = "";
Expand All @@ -132,17 +154,26 @@ IResult[] arrayContainOnly(ref Evaluation evaluation) @safe nothrow {

if(!isSuccess) {
try results ~= new ExpectedActualResult("", testData.niceJoin(evaluation.currentValue.typeName));
catch(Exception) {}
catch(Exception e) {
results ~= e.toResults;
return results;
}

try results ~= new ExtraMissingResult(strExtra, strMissing);
catch(Exception) {}
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) {}
catch(Exception e) {
results ~= e.toResults;
return results;
}
}
}

Expand All @@ -154,15 +185,15 @@ void addLifecycleMessage(ref Evaluation evaluation, string[] missingValues) @saf
evaluation.message.addText(" ");

if(missingValues.length == 1) {
try evaluation.message.addValue(missingValues[0]); catch(Exception e) {
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 e) {
} catch(Exception) {
evaluation.message.addText(" some values ");
}

Expand All @@ -173,6 +204,13 @@ void addLifecycleMessage(ref Evaluation evaluation, string[] missingValues) @saf
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(" ");
Expand All @@ -196,6 +234,13 @@ void addNegatedLifecycleMessage(ref Evaluation evaluation, string[] presentValue
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 ";

Expand All @@ -208,6 +253,13 @@ string createResultMessage(ValueEvaluation expectedValue, string[] expectedPiece
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 ";

Expand All @@ -220,6 +272,13 @@ string createNegatedResultMessage(ValueEvaluation expectedValue, string[] expect
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 = "";

Expand All @@ -232,4 +291,9 @@ string niceJoin(string[] values, string typeName = "") @safe nothrow {
} catch(Exception) {}

return result;
}
}

string niceJoin(EquableValue[] values, string typeName = "") @safe nothrow {
return values.map!(a => a.getSerialized.cleanString).array.niceJoin(typeName);
}

10 changes: 8 additions & 2 deletions source/fluentasserts/core/results.d
Original file line number Diff line number Diff line change
Expand Up @@ -561,13 +561,11 @@ class ExpectedActualResult : IResult {
KeyResult!"Actual" actual;
}


this(string title, string expected, string actual) nothrow @safe {
this.title = title;
this(expected, actual);
}


this(string expected, string actual) nothrow @safe {
this.expected = new KeyResult!"Expected"(expected);
this.actual = new KeyResult!"Actual"(actual);
Expand Down Expand Up @@ -1635,3 +1633,11 @@ unittest {
abs:2,3
abc:3`);
}

IResult[] toResults(Exception e) nothrow @trusted {
try {
return [ new MessageResult(e.message.to!string) ];
} catch(Exception) {
return [ new MessageResult("Unknown error!") ];
}
}
Loading

0 comments on commit 08a475a

Please sign in to comment.