Skip to content

Commit

Permalink
add the lessOrEqualTo operation
Browse files Browse the repository at this point in the history
  • Loading branch information
gedaiu committed Apr 3, 2021
1 parent e49e970 commit 1794c29
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 2 deletions.
4 changes: 2 additions & 2 deletions source/fluentasserts/core/expect.d
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ import std.conv;
}

///
auto lessOrEqualThan(T)(T value) {
return opDispatch!"lessOrEqualThan"(value);
auto lessOrEqualTo(T)(T value) {
return opDispatch!"lessOrEqualTo"(value);
}

///
Expand Down
2 changes: 2 additions & 0 deletions source/fluentasserts/core/lifecycle.d
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import fluentasserts.core.operations.greaterThan;
import fluentasserts.core.operations.greaterOrEqualTo;
import fluentasserts.core.operations.instanceOf;
import fluentasserts.core.operations.lessThan;
import fluentasserts.core.operations.lessOrEqualTo;
import fluentasserts.core.operations.registry;
import fluentasserts.core.operations.startWith;
import fluentasserts.core.operations.throwable;
Expand Down Expand Up @@ -52,6 +53,7 @@ static this() {
Registry.instance.register(Type.stringof, Type.stringof, "greaterThan", &greaterThan!Type);
Registry.instance.register(Type.stringof, Type.stringof, "above", &greaterThan!Type);

Registry.instance.register(Type.stringof, Type.stringof, "lessOrEqualTo", &lessOrEqualTo!Type);
Registry.instance.register(Type.stringof, Type.stringof, "lessThan", &lessThan!Type);
Registry.instance.register(Type.stringof, Type.stringof, "below", &lessThan!Type);

Expand Down
56 changes: 56 additions & 0 deletions source/fluentasserts/core/operations/lessOrEqualTo.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
module fluentasserts.core.operations.lessOrEqualTo;

import fluentasserts.core.results;
import fluentasserts.core.evaluation;

import fluentasserts.core.lifecycle;

import std.conv;
import std.datetime;

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

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

T expectedValue;
T currentValue;

try {
expectedValue = evaluation.expectedValue.strValue.to!T;
currentValue = evaluation.currentValue.strValue.to!T;
} catch(Exception e) {
return [ new MessageResult("Can't convert the values to " ~ T.stringof) ];
}

auto result = currentValue <= expectedValue;

if(evaluation.isNegated) {
result = !result;
}

if(result) {
return [];
}

evaluation.message.addText(" ");
evaluation.message.addValue(evaluation.currentValue.niceValue);

IResult[] results = [];

if(evaluation.isNegated) {
evaluation.message.addText(" is less or equal to ");
results ~= new ExpectedActualResult("greater than " ~ evaluation.expectedValue.niceValue, evaluation.currentValue.niceValue);
} else {
evaluation.message.addText(" is greater than ");
results ~= new ExpectedActualResult("less or equal to " ~ evaluation.expectedValue.niceValue, evaluation.currentValue.niceValue);
}

evaluation.message.addValue(evaluation.expectedValue.niceValue);
evaluation.message.addText(".");

return results;
}
56 changes: 56 additions & 0 deletions test/operations/lessOrEqualTo.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
module test.operations.lessOrEqualTo;

import fluentasserts.core.expect;
import fluent.asserts;

import trial.discovery.spec;

import std.string;
import std.conv;
import std.meta;
import std.datetime;

alias s = Spec!({
alias NumericTypes = AliasSeq!(byte, ubyte, short, ushort, int, uint, long, ulong, float, double, real);

static foreach(Type; NumericTypes) {
describe("using " ~ Type.stringof ~ " values", {
Type smallValue;
Type largeValue;

before({
smallValue = cast(Type) 40;
largeValue = cast(Type) 50;
});

it("should be able to compare two values", {
expect(smallValue).to.be.lessOrEqualTo(largeValue);
expect(smallValue).to.be.lessOrEqualTo(smallValue);
});

it("should be able to compare two values using negation", {
expect(largeValue).not.to.be.lessOrEqualTo(smallValue);
});

it("should throw a detailed error when the comparison fails", {
auto msg = ({
expect(largeValue).to.be.lessOrEqualTo(smallValue);
}).should.throwException!TestException.msg;

msg.split("\n")[0].should.equal(largeValue.to!string ~ " should be less or equal to " ~ smallValue.to!string ~ ". " ~ largeValue.to!string ~ " is greater than " ~ smallValue.to!string ~ ".");
msg.split("\n")[2].strip.should.equal("Expected:less or equal to " ~ smallValue.to!string);
msg.split("\n")[3].strip.should.equal("Actual:" ~ largeValue.to!string);
});

it("should throw a detailed error when the negated comparison fails", {
auto msg = ({
expect(smallValue).not.to.be.lessOrEqualTo(largeValue);
}).should.throwException!TestException.msg;

msg.split("\n")[0].should.equal(smallValue.to!string ~ " should not be less or equal to " ~ largeValue.to!string ~ ". " ~ smallValue.to!string ~ " is less or equal to " ~ largeValue.to!string ~ ".");
msg.split("\n")[2].strip.should.equal("Expected:greater than " ~ largeValue.to!string);
msg.split("\n")[3].strip.should.equal("Actual:" ~ smallValue.to!string);
});
});
}
});

0 comments on commit 1794c29

Please sign in to comment.