1020304050607080901001101201301401501601701801902011621116221162302411625116261162702802911630116311163203303403503611637038039040041042943044945946947048049950951952053954055056057058959060961062063064065966067968969970071072973974975076977078079080081982083984085086087268882688909013491134922519309413495096097268980990100010101021341030104134105741062910701082910914110141110112011301142911515116151170118011901202912101222912301246012515126012701281341290 module fluentasserts.core.operations.between; import fluentasserts.core.results; import fluentasserts.core.evaluation; import fluentasserts.core.lifecycle; import std.conv; import std.datetime; version(unittest) { import fluentasserts.core.expect; } static immutable betweenDescription = "Asserts that the target is a number or a date greater than or equal to the given number or date start, " ~ "and less than or equal to the given number or date finish respectively. However, it's often best to assert that the target is equal to its expected value."; /// IResult[] between(T)(ref Evaluation evaluation) @safe nothrow { evaluation.message.addText(" and "); evaluation.message.addValue(evaluation.expectedValue.meta["1"]); evaluation.message.addText(". "); T currentValue; T limit1; T limit2; try { currentValue = evaluation.currentValue.strValue.to!T; limit1 = evaluation.expectedValue.strValue.to!T; limit2 = evaluation.expectedValue.meta["1"].to!T; } catch(Exception e) { return [ new MessageResult("Can't convert the values to " ~ T.stringof) ]; } return betweenResults(currentValue, limit1, limit2, evaluation); } /// IResult[] betweenDuration(ref Evaluation evaluation) @safe nothrow { evaluation.message.addText(" and "); Duration currentValue; Duration limit1; Duration limit2; try { currentValue = dur!"nsecs"(evaluation.currentValue.strValue.to!size_t); limit1 = dur!"nsecs"(evaluation.expectedValue.strValue.to!size_t); limit2 = dur!"nsecs"(evaluation.expectedValue.meta["1"].to!size_t); evaluation.message.addValue(limit2.to!string); } catch(Exception e) { return [ new MessageResult("Can't convert the values to Duration") ]; } evaluation.message.addText(". "); return betweenResults(currentValue, limit1, limit2, evaluation); } /// IResult[] betweenSysTime(ref Evaluation evaluation) @safe nothrow { evaluation.message.addText(" and "); SysTime currentValue; SysTime limit1; SysTime limit2; try { currentValue = SysTime.fromISOExtString(evaluation.currentValue.strValue); limit1 = SysTime.fromISOExtString(evaluation.expectedValue.strValue); limit2 = SysTime.fromISOExtString(evaluation.expectedValue.meta["1"]); evaluation.message.addValue(limit2.toISOExtString); } catch(Exception e) { return [ new MessageResult("Can't convert the values to Duration") ]; } evaluation.message.addText(". "); return betweenResults(currentValue, limit1, limit2, evaluation); } private IResult[] betweenResults(T)(T currentValue, T limit1, T limit2, ref Evaluation evaluation) { T min = limit1 < limit2 ? limit1 : limit2; T max = limit1 > limit2 ? limit1 : limit2; auto isLess = currentValue <= min; auto isGreater = currentValue >= max; auto isBetween = !isLess && !isGreater; string interval; try { interval = "a value " ~ (evaluation.isNegated ? "outside" : "inside") ~ " (" ~ min.to!string ~ ", " ~ max.to!string ~ ") interval"; } catch(Exception) { interval = "a value " ~ (evaluation.isNegated ? "outside" : "inside") ~ " the interval"; } IResult[] results = []; if(!evaluation.isNegated) { if(!isBetween) { evaluation.message.addValue(evaluation.currentValue.niceValue); if(isGreater) { evaluation.message.addText(" is greater than or equal to "); try evaluation.message.addValue(max.to!string); catch(Exception) {} } if(isLess) { evaluation.message.addText(" is less than or equal to "); try evaluation.message.addValue(min.to!string); catch(Exception) {} } evaluation.message.addText("."); results ~= new ExpectedActualResult(interval, evaluation.currentValue.niceValue); } } else if(isBetween) { results ~= new ExpectedActualResult(interval, evaluation.currentValue.niceValue); } return results; }
module fluentasserts.core.operations.between; import fluentasserts.core.results; import fluentasserts.core.evaluation; import fluentasserts.core.lifecycle; import std.conv; import std.datetime; version(unittest) { import fluentasserts.core.expect; } static immutable betweenDescription = "Asserts that the target is a number or a date greater than or equal to the given number or date start, " ~ "and less than or equal to the given number or date finish respectively. However, it's often best to assert that the target is equal to its expected value."; /// IResult[] between(T)(ref Evaluation evaluation) @safe nothrow { evaluation.message.addText(" and "); evaluation.message.addValue(evaluation.expectedValue.meta["1"]); evaluation.message.addText(". "); T currentValue; T limit1; T limit2; try { currentValue = evaluation.currentValue.strValue.to!T; limit1 = evaluation.expectedValue.strValue.to!T; limit2 = evaluation.expectedValue.meta["1"].to!T; } catch(Exception e) { return [ new MessageResult("Can't convert the values to " ~ T.stringof) ]; } return betweenResults(currentValue, limit1, limit2, evaluation); } /// IResult[] betweenDuration(ref Evaluation evaluation) @safe nothrow { evaluation.message.addText(" and "); Duration currentValue; Duration limit1; Duration limit2; try { currentValue = dur!"nsecs"(evaluation.currentValue.strValue.to!size_t); limit1 = dur!"nsecs"(evaluation.expectedValue.strValue.to!size_t); limit2 = dur!"nsecs"(evaluation.expectedValue.meta["1"].to!size_t); evaluation.message.addValue(limit2.to!string); } catch(Exception e) { return [ new MessageResult("Can't convert the values to Duration") ]; } evaluation.message.addText(". "); return betweenResults(currentValue, limit1, limit2, evaluation); } /// IResult[] betweenSysTime(ref Evaluation evaluation) @safe nothrow { evaluation.message.addText(" and "); SysTime currentValue; SysTime limit1; SysTime limit2; try { currentValue = SysTime.fromISOExtString(evaluation.currentValue.strValue); limit1 = SysTime.fromISOExtString(evaluation.expectedValue.strValue); limit2 = SysTime.fromISOExtString(evaluation.expectedValue.meta["1"]); evaluation.message.addValue(limit2.toISOExtString); } catch(Exception e) { return [ new MessageResult("Can't convert the values to Duration") ]; } evaluation.message.addText(". "); return betweenResults(currentValue, limit1, limit2, evaluation); } private IResult[] betweenResults(T)(T currentValue, T limit1, T limit2, ref Evaluation evaluation) { T min = limit1 < limit2 ? limit1 : limit2; T max = limit1 > limit2 ? limit1 : limit2; auto isLess = currentValue <= min; auto isGreater = currentValue >= max; auto isBetween = !isLess && !isGreater; string interval; try { interval = "a value " ~ (evaluation.isNegated ? "outside" : "inside") ~ " (" ~ min.to!string ~ ", " ~ max.to!string ~ ") interval"; } catch(Exception) { interval = "a value " ~ (evaluation.isNegated ? "outside" : "inside") ~ " the interval"; } IResult[] results = []; if(!evaluation.isNegated) { if(!isBetween) { evaluation.message.addValue(evaluation.currentValue.niceValue); if(isGreater) { evaluation.message.addText(" is greater than or equal to "); try evaluation.message.addValue(max.to!string); catch(Exception) {} } if(isLess) { evaluation.message.addText(" is less than or equal to "); try evaluation.message.addValue(min.to!string); catch(Exception) {} } evaluation.message.addText("."); results ~= new ExpectedActualResult(interval, evaluation.currentValue.niceValue); } } else if(isBetween) { results ~= new ExpectedActualResult(interval, evaluation.currentValue.niceValue); } return results; }