Skip to content

Commit

Permalink
move location to the evaluation struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Bogdan Szabo committed Feb 23, 2024
1 parent 70b5ec1 commit b6c6bc1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 15 deletions.
34 changes: 27 additions & 7 deletions source/fluentasserts/core/evaluation.d
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ struct ValueEvaluation {
/// Other info about the value
string[string] meta;

/// The file name contining the evaluated value
string fileName;

/// The line number of the evaluated value
size_t line;

/// a custom text to be prepended to the value
string prependText;

string typeName() @safe nothrow {
if(typeNames.length == 0) {
return "unknown";
Expand Down Expand Up @@ -78,12 +87,12 @@ class Evaluation {
}

///
auto evaluate(T)(lazy T testData) @trusted if(isInputRange!T && !isArray!T && !isAssociativeArray!T) {
return evaluate(testData.array);
auto evaluate(T)(lazy T testData, const string file = __FILE__, const size_t line = __LINE__, string prependText = null) @trusted if(isInputRange!T && !isArray!T && !isAssociativeArray!T) {
return evaluate(testData.array, file, line, prependText);
}

///
auto evaluate(T)(lazy T testData) @trusted if(!isInputRange!T || isArray!T || isAssociativeArray!T) {
auto evaluate(T)(lazy T testData, const string file = __FILE__, const size_t line = __LINE__, string prependText = null) @trusted if(!isInputRange!T || isArray!T || isAssociativeArray!T) {
auto begin = Clock.currTime;
alias Result = Tuple!(T, "value", ValueEvaluation, "evaluation");

Expand All @@ -101,15 +110,26 @@ auto evaluate(T)(lazy T testData) @trusted if(!isInputRange!T || isArray!T || is
auto duration = Clock.currTime - begin;
auto serializedValue = SerializerRegistry.instance.serialize(value);
auto niceValue = SerializerRegistry.instance.niceValue(value);
return Result(value, ValueEvaluation(null, duration, serializedValue, equableValue(value, niceValue), niceValue, extractTypes!TT ));

auto valueEvaluation = ValueEvaluation(null, duration, serializedValue, equableValue(value, niceValue), niceValue, extractTypes!TT);
valueEvaluation.fileName = file;
valueEvaluation.line = line;
valueEvaluation.prependText = prependText;

return Result(value, valueEvaluation);
} catch(Throwable t) {
T result;

static if(isCallable!T) {
result = testData;
}

return Result(result, ValueEvaluation(t, Clock.currTime - begin, result.to!string, equableValue(result, result.to!string), result.to!string, extractTypes!T ));
auto valueEvaluation = ValueEvaluation(t, Clock.currTime - begin, result.to!string, equableValue(result, result.to!string), result.to!string, extractTypes!T);
valueEvaluation.fileName = file;
valueEvaluation.line = line;
valueEvaluation.prependText = prependText;

return Result(result, valueEvaluation);
}
}

Expand Down Expand Up @@ -191,9 +211,9 @@ unittest {

auto result = extractTypes!(T[]);

assert(result[0] == "fluentasserts.core.evaluation.__unittest_L188_C1.T[]", `Expected: ` ~ result[0]);
assert(result[0] == "fluentasserts.core.evaluation.__unittest_L208_C1.T[]", `Expected: "fluentasserts.core.evaluation.__unittest_L208_C1.T[]" got "` ~ result[0] ~ `"`);
assert(result[1] == "object.Object[]", `Expected: ` ~ result[1] );
assert(result[2] == "fluentasserts.core.evaluation.__unittest_L188_C1.I[]", `Expected: ` ~ result[2] );
assert(result[2] == "fluentasserts.core.evaluation.__unittest_L208_C1.I[]", `Expected: ` ~ result[2] );
}

/// A proxy type that allows to compare the native values
Expand Down
16 changes: 10 additions & 6 deletions source/fluentasserts/core/expect.d
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ import std.conv;
int refCount;
}

this(ValueEvaluation value, const string fileName, const size_t line, string prependText = null) @trusted {
this(ValueEvaluation value) @trusted {
this.evaluation = new Evaluation();

evaluation.id = Lifecycle.instance.beginEvaluation(value);
evaluation.currentValue = value;
evaluation.message = new MessageResult();
evaluation.source = new SourceResult(fileName, line);
evaluation.source = new SourceResult(value.fileName, value.line);

try {
auto sourceValue = evaluation.source.getValue;
Expand All @@ -41,8 +41,8 @@ import std.conv;

evaluation.message.addText(" should");

if(prependText) {
evaluation.message.addText(prependText);
if(value.prependText) {
evaluation.message.addText(value.prependText);
}
}

Expand Down Expand Up @@ -274,12 +274,16 @@ Expect expect(void delegate() callable, const string file = __FILE__, const size
value.meta["Throwable"] = "yes";
}

return Expect(value, file, line, prependText);
value.fileName = file;
value.line = line;
value.prependText = prependText;

return Expect(value);
}

///
Expect expect(T)(lazy T testedValue, const string file = __FILE__, const size_t line = __LINE__, string prependText = null) @trusted {
return Expect(testedValue.evaluate.evaluation, file, line, prependText);
return Expect(testedValue.evaluate(file, line, prependText).evaluation);
}

///
Expand Down
8 changes: 6 additions & 2 deletions source/fluentasserts/core/string.d
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,14 @@ unittest {

/// it shows null chars in the diff
unittest {
auto msg = ({
string msg;

try {
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;
} catch(TestException e) {
msg = e.message.to!string;
}

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".`);
Expand Down

0 comments on commit b6c6bc1

Please sign in to comment.