Skip to content

Commit

Permalink
fix broken tests and add the either lib as dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
Bogdan Szabo committed Feb 23, 2024
1 parent bb15273 commit 70b5ec1
Show file tree
Hide file tree
Showing 8 changed files with 309 additions and 127 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ test/unit-threaded/unit-threaded-example
test/disabledDiffResult/disabled-diff-result-example
test/disabledMessageResult/disabled-message-result-example
test/disabledSourceResult/disabled-source-result-example
fluent-asserts-test-unittest
13 changes: 7 additions & 6 deletions dub.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
"Szabo Bogdan"
],
"description": "Fluent assertions done right",
"copyright": "Copyright © 2022, Szabo Bogdan",
"copyright": "Copyright © 2023, Szabo Bogdan",
"license": "MIT",
"homepage": "http://fluentasserts.szabobogdan.com/",
"dependencies": {
"libdparse": "~>0.20.0",
"either": "~>1.1.3",
"ddmp": "~>0.0.1-0.dev.3",
"unit-threaded": {
"version": "*",
Expand All @@ -26,11 +27,11 @@
]
},
{
"name": "trial",
"sourcePaths": [
"source",
"test/operations"
]
"name": "unittest",
"targetType": "library",
"dependencies": {
"silly": "~>1.2.0-dev.2"
}
}
]
}
9 changes: 2 additions & 7 deletions source/fluentasserts/core/base.d
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,6 @@ struct Result {
}
}

struct Message {
bool isValue;
string text;
}

mixin template DisabledShouldThrowableCommons() {
auto throwSomething(string file = __FILE__, size_t line = __LINE__) {
static assert("`throwSomething` does not work for arrays and ranges");
Expand Down Expand Up @@ -491,13 +486,13 @@ auto should(T)(lazy T testData, const string file = __FILE__, const size_t line
}
}

@("because")
/// "because" adds a text before the assert message
unittest {
auto msg = ({
true.should.equal(false).because("of test reasons");
}).should.throwException!TestException.msg;

msg.split("\n")[0].should.equal("Because of test reasons, true should equal false.");
msg.split("\n")[0].should.equal("Because of test reasons, true should equal false. ");
}

struct Assert {
Expand Down
12 changes: 6 additions & 6 deletions source/fluentasserts/core/basetype.d
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ unittest {
5.should.equal(6);
}).should.throwException!TestException.msg;

msg.split("\n")[0].should.equal("5 should equal 6. 5 is not equal to 6.");
msg.split("\n")[0].should.equal("5 should equal 6. 5 is not equal to 6. ");

msg = ({
5.should.not.equal(5);
}).should.throwException!TestException.msg;

msg.split("\n")[0].should.equal("5 should not equal 5. 5 is equal to 5.");
msg.split("\n")[0].should.equal("5 should not equal 5. 5 is equal to 5. ");
}

@("bools equal")
/// bools equal
unittest {
({
true.should.equal(true);
Expand All @@ -65,20 +65,20 @@ unittest {
true.should.equal(false);
}).should.throwException!TestException.msg;

msg.split("\n")[0].should.equal("true should equal false.");
msg.split("\n")[0].should.equal("true should equal false. ");
msg.split("\n")[2].strip.should.equal("Expected:false");
msg.split("\n")[3].strip.should.equal("Actual:true");

msg = ({
true.should.not.equal(true);
}).should.throwException!TestException.msg;

msg.split("\n")[0].should.equal("true should not equal true.");
msg.split("\n")[0].should.equal("true should not equal true. ");
msg.split("\n")[2].strip.should.equal("Expected:not true");
msg.split("\n")[3].strip.should.equal("Actual:true");
}

@("numbers greater than")
/// numbers greater than
unittest {
({
5.should.be.greaterThan(4);
Expand Down
198 changes: 198 additions & 0 deletions source/fluentasserts/core/message.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
module fluentasserts.core.message;

import std.string;
import ddmp.diff;
import fluentasserts.core.results;
import std.algorithm;
import std.conv;

@safe:

/// Glyphs used to display special chars in the results
struct ResultGlyphs {
static {
/// Glyph for the tab char
string tab;

/// Glyph for the \r char
string carriageReturn;

/// Glyph for the \n char
string newline;

/// Glyph for the space char
string space;

/// Glyph for the \0 char
string nullChar;

/// Glyph that indicates the error line
string sourceIndicator;

/// Glyph that sepparates the line number
string sourceLineSeparator;

/// Glyph for the diff begin indicator
string diffBegin;

/// Glyph for the diff end indicator
string diffEnd;

/// Glyph that marks an inserted text in diff
string diffInsert;

/// Glyph that marks deleted text in diff
string diffDelete;
}

/// Set the default values. The values are
static resetDefaults() {
version(windows) {
ResultGlyphs.tab = `\t`;
ResultGlyphs.carriageReturn = `\r`;
ResultGlyphs.newline = `\n`;
ResultGlyphs.space = ` `;
ResultGlyphs.nullChar = ``;
} else {
ResultGlyphs.tab = `¤`;
ResultGlyphs.carriageReturn = ``;
ResultGlyphs.newline = ``;
ResultGlyphs.space = ``;
ResultGlyphs.nullChar = `\0`;
}

ResultGlyphs.sourceIndicator = ">";
ResultGlyphs.sourceLineSeparator = ":";

ResultGlyphs.diffBegin = "[";
ResultGlyphs.diffEnd = "]";
ResultGlyphs.diffInsert = "+";
ResultGlyphs.diffDelete = "-";
}
}

struct Message {
enum Type {
info,
value,
title,
category,
insert,
delete_
}

Type type;
string text;

this(Type type, string text) nothrow {
this.type = type;

if(type == Type.value || type == Type.insert || type == Type.delete_) {
this.text = text
.replace("\r", ResultGlyphs.carriageReturn)
.replace("\n", ResultGlyphs.newline)
.replace("\0", ResultGlyphs.nullChar)
.replace("\t", ResultGlyphs.tab);
} else {
this.text = text;
}
}

string toString() nothrow inout {
switch(type) {
case Type.title:
return "\n\n" ~ text ~ "\n";
case Type.insert:
return "[-" ~ text ~ "]";
case Type.delete_:
return "[+" ~ text ~ "]";
case Type.category:
return "\n" ~ text ~ "";
default:
return text;
}
}
}

IResult[] toException(ref EvaluationResult result) nothrow {
if(result.messages.length == 0) {
return [];
}

return [ new EvaluationResultInstance(result) ];
}

struct EvaluationResult {
private {
immutable(Message)[] messages;
}

void add(immutable(Message) message) nothrow {
messages ~= message;
}

string toString() nothrow {
string result;

foreach (message; messages) {
result ~= message.toString;
}

return result;
}

void print(ResultPrinter printer) nothrow {
foreach (message; messages) {
printer.print(message);
}
}
}

static immutable actualTitle = Message(Message.Type.category, "Actual:");

void addResult(ref EvaluationResult result, string value) nothrow @trusted {
result.add(actualTitle);

result.add(Message(Message.Type.value, value));
}


static immutable expectedTitle = Message(Message.Type.category, "Expected:");
static immutable expectedNot = Message(Message.Type.info, "not ");

void addExpected(ref EvaluationResult result, bool isNegated, string value) nothrow @trusted {
result.add(expectedTitle);

if(isNegated) {
result.add(expectedNot);
}

result.add(Message(Message.Type.value, value));
}


static immutable diffTitle = Message(Message.Type.title, "Diff:");

void addDiff(ref EvaluationResult result, string actual, string expected) nothrow @trusted {
result.add(diffTitle);

try {
auto diffResult = diff_main(expected, actual);

foreach(diff; diffResult) {
if(diff.operation == Operation.EQUAL) {
result.add(Message(Message.Type.info, diff.text));
}

if(diff.operation == Operation.INSERT) {
result.add(Message(Message.Type.insert, diff.text));
}

if(diff.operation == Operation.DELETE) {
result.add(Message(Message.Type.delete_, diff.text));
}
}
} catch(Exception e) {
return;
}
}
26 changes: 17 additions & 9 deletions source/fluentasserts/core/operations/equal.d
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@ import fluentasserts.core.results;
import fluentasserts.core.evaluation;

import fluentasserts.core.lifecycle;
import fluentasserts.core.message;

version(unittest) {
import fluentasserts.core.expect;
}

static immutable equalDescription = "Asserts that the target is strictly == equal to the given val.";

static immutable isEqualTo = Message(Message.Type.info, " is equal to ");
static immutable isNotEqualTo = Message(Message.Type.info, " is not equal to ");
static immutable endSentence = Message(Message.Type.info, ". ");

///
IResult[] equal(ref Evaluation evaluation) @safe nothrow {
evaluation.message.addText(".");
EvaluationResult evaluationResult;

evaluation.message.add(endSentence);

bool result = evaluation.currentValue.strValue == evaluation.expectedValue.strValue;

Expand All @@ -32,22 +39,23 @@ IResult[] equal(ref Evaluation evaluation) @safe nothrow {
IResult[] results = [];

if(evaluation.currentValue.typeName != "bool") {
evaluation.message.addText(" ");
evaluation.message.addValue(evaluation.currentValue.strValue);
evaluation.message.add(Message(Message.Type.value, evaluation.currentValue.strValue));

if(evaluation.isNegated) {
evaluation.message.addText(" is equal to ");
evaluation.message.add(isEqualTo);
} else {
evaluation.message.addText(" is not equal to ");
evaluation.message.add(isNotEqualTo);
}

evaluation.message.addValue(evaluation.expectedValue.strValue);
evaluation.message.addText(".");
evaluation.message.add(Message(Message.Type.value, evaluation.expectedValue.strValue));
evaluation.message.add(endSentence);

evaluationResult.addDiff(evaluation.expectedValue.strValue, evaluation.currentValue.strValue);
try results ~= new DiffResult(evaluation.expectedValue.strValue, evaluation.currentValue.strValue); catch(Exception) {}
}

try results ~= new ExpectedActualResult((evaluation.isNegated ? "not " : "") ~ evaluation.expectedValue.strValue, evaluation.currentValue.strValue); catch(Exception) {}
evaluationResult.addExpected(evaluation.isNegated, evaluation.expectedValue.strValue);
evaluationResult.addResult(evaluation.currentValue.strValue);

return results;
return evaluationResult.toException;
}
Loading

0 comments on commit 70b5ec1

Please sign in to comment.