Skip to content

Commit

Permalink
allow systime comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
gedaiu committed Apr 4, 2021
1 parent 0abc51a commit a0c7808
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 116 deletions.
6 changes: 6 additions & 0 deletions source/fluentasserts/core/lifecycle.d
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,15 @@ static this() {
Registry.instance.register!(Duration, Duration)("lessThan", &lessThanDuration);
Registry.instance.register!(Duration, Duration)("below", &lessThanDuration);

Registry.instance.register!(SysTime, SysTime)("lessThan", &lessThanSysTime);
Registry.instance.register!(SysTime, SysTime)("below", &lessThanSysTime);

Registry.instance.register!(Duration, Duration)("greaterThan", &greaterThanDuration);
Registry.instance.register!(Duration, Duration)("above", &greaterThanDuration);

Registry.instance.register!(SysTime, SysTime)("greaterThan", &greaterThanSysTime);
Registry.instance.register!(SysTime, SysTime)("above", &greaterThanSysTime);

Registry.instance.register("string", "string", "equal", &equal);
Registry.instance.register("bool", "bool", "equal", &equal);

Expand Down
57 changes: 28 additions & 29 deletions source/fluentasserts/core/operations/greaterThan.d
Original file line number Diff line number Diff line change
Expand Up @@ -28,36 +28,10 @@ IResult[] greaterThan(T)(ref Evaluation evaluation) @safe nothrow {

auto result = currentValue > expectedValue;

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

if(result) {
return [];
}

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

IResult[] results = [];

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


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


return results;
return greaterThanResults(result, evaluation.expectedValue.strValue, evaluation.currentValue.strValue, evaluation);
}


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

Expand All @@ -78,6 +52,31 @@ IResult[] greaterThanDuration(ref Evaluation evaluation) @safe nothrow {

auto result = currentValue > expectedValue;

return greaterThanResults(result, niceExpectedValue, niceCurrentValue, evaluation);
}

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

SysTime expectedValue;
SysTime currentValue;
string niceExpectedValue;
string niceCurrentValue;

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

auto result = currentValue > expectedValue;

return greaterThanResults(result, evaluation.expectedValue.strValue, evaluation.currentValue.strValue, evaluation);
}

private IResult[] greaterThanResults(bool result, string niceExpectedValue, string niceCurrentValue, ref Evaluation evaluation) @safe nothrow {
if(evaluation.isNegated) {
result = !result;
}
Expand All @@ -103,4 +102,4 @@ IResult[] greaterThanDuration(ref Evaluation evaluation) @safe nothrow {
evaluation.message.addText(".");

return results;
}
}
52 changes: 27 additions & 25 deletions source/fluentasserts/core/operations/lessThan.d
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,10 @@ IResult[] lessThan(T)(ref Evaluation evaluation) @safe nothrow {

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 than ");
results ~= new ExpectedActualResult("greater than or equal to " ~ evaluation.expectedValue.niceValue, evaluation.currentValue.niceValue);
} else {
evaluation.message.addText(" is greater than or equal to ");
results ~= new ExpectedActualResult("less than " ~ evaluation.expectedValue.niceValue, evaluation.currentValue.niceValue);
}

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

return results;
return lessThanResults(result, evaluation.expectedValue.strValue, evaluation.currentValue.strValue, evaluation);
}

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

Expand All @@ -75,6 +52,31 @@ IResult[] lessThanDuration(ref Evaluation evaluation) @safe nothrow {

auto result = currentValue < expectedValue;

return lessThanResults(result, niceExpectedValue, niceCurrentValue, evaluation);
}

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

SysTime expectedValue;
SysTime currentValue;
string niceExpectedValue;
string niceCurrentValue;

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

auto result = currentValue < expectedValue;

return lessThanResults(result, evaluation.expectedValue.strValue, evaluation.currentValue.strValue, evaluation);
}

private IResult[] lessThanResults(bool result, string niceExpectedValue, string niceCurrentValue, ref Evaluation evaluation) @safe nothrow {
if(evaluation.isNegated) {
result = !result;
}
Expand Down
17 changes: 16 additions & 1 deletion source/fluentasserts/core/serializers.d
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ class SerializerRegistry {
}
} else static if(is(Unqual!T == Duration)) {
result = value.total!"nsecs".to!string;
} else static if(is(Unqual!T == SysTime)) {
result = value.toISOExtString;
} else {
result = value.to!string;
}
Expand Down Expand Up @@ -133,7 +135,9 @@ class SerializerRegistry {
}

string niceValue(T)(T value) {
static if(is(Unqual!T == Duration)) {
static if(is(Unqual!T == SysTime)) {
return value.toISOExtString;
} else static if(is(Unqual!T == Duration)) {
return value.to!string;
} else {
return serialize(value);
Expand Down Expand Up @@ -261,6 +265,17 @@ unittest {
SerializerRegistry.instance.serialize(ich).should.equal("'a'");
}

/// It should serialize a SysTime
unittest {
SysTime val = SysTime.fromISOExtString("2010-07-04T07:06:12");
const SysTime cval = SysTime.fromISOExtString("2010-07-04T07:06:12");
immutable SysTime ival = SysTime.fromISOExtString("2010-07-04T07:06:12");

SerializerRegistry.instance.serialize(val).should.equal("2010-07-04T07:06:12");
SerializerRegistry.instance.serialize(cval).should.equal("2010-07-04T07:06:12");
SerializerRegistry.instance.serialize(ival).should.equal("2010-07-04T07:06:12");
}

/// It should serialize a string
unittest {
string str = "aaa";
Expand Down
22 changes: 21 additions & 1 deletion test/operations/equal.d
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import trial.discovery.spec;
import std.string;
import std.conv;
import std.meta;
import std.datetime;

alias s = Spec!({

Expand Down Expand Up @@ -115,7 +116,7 @@ alias s = Spec!({

it("should be able to compare that two bools that are not equal", {
expect(true).to.not.equal(false);
expect(true).to.not.equal(false);
expect(false).to.not.equal(true);
});

it("should throw a detailed error message when the two bools are not equal", {
Expand All @@ -128,4 +129,23 @@ alias s = Spec!({
msg[3].strip.should.equal("Actual:true");
});
});

describe("using durations", {
it("should compare two true values", {
expect(2.seconds).to.equal(2.seconds);
});

it("should be able to compare that two bools that are not equal", {
expect(2.seconds).to.not.equal(3.seconds);
expect(3.seconds).to.not.equal(2.seconds);
});

it("should throw a detailed error message when the two bools are not equal", {
auto msg = ({
expect(3.seconds).to.equal(2.seconds);
}).should.throwException!TestException.msg.split("\n");

msg[0].strip.should.equal("3 secs should equal 2 secs. 3000000000 is not equal to 2000000000.");
});
});
});
100 changes: 70 additions & 30 deletions test/operations/greaterThan.d
Original file line number Diff line number Diff line change
Expand Up @@ -66,42 +66,82 @@ alias s = Spec!({
}

describe("using Duration values", {
Duration smallValue;
Duration largeValue;
Duration smallValue;
Duration largeValue;

before({
smallValue = 40.seconds;
largeValue = 41.seconds;
});
before({
smallValue = 40.seconds;
largeValue = 41.seconds;
});

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

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

it("should throw a detailed error when the number is compared with itself", {
auto msg = ({
expect(smallValue).to.be.greaterThan(smallValue);
}).should.throwException!TestException.msg;
it("should throw a detailed error when the number is compared with itself", {
auto msg = ({
expect(smallValue).to.be.greaterThan(smallValue);
}).should.throwException!TestException.msg;

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

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

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

describe("using SysTime values", {
SysTime smallValue;
SysTime largeValue;

before({
smallValue = Clock.currTime;
largeValue = smallValue + 4.seconds;
});

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

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

it("should throw a detailed error when the number is compared with itself", {
auto msg = ({
expect(smallValue).to.be.greaterThan(smallValue);
}).should.throwException!TestException.msg;

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

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

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

0 comments on commit a0c7808

Please sign in to comment.