102030405060708090100110120130140150160170180190200210225562355624025556265562755628556290300315563203355634235036554370380390400410425564304455645046047048049601506015160152053054055115756057115758556595566006155662548635486486506606706806955670071072073074075076077078079080081082083084085086087088089090091092093094095096097098099010001010102010331104311050106010701080109711071110112711301140115011601170118011901200121012201230124012501260127012801297130713101320133013401350136013701380139014001410142014301440145014601470148014901500151015201530154015501560157015801590160016101620163016401650166016701680169017001710172017301740175017601770178017901800181018201830184018501860187818801890190019101920193019401950196019701980199020002010202020302040205020602070208020902100211021202130214021502160217021802190220556221022202230224556225022602270228022982300231823202330234023502360237023802390240024102420243024402450246024702480249025002510252025302540255025602570258025902600261026202630264026502660267026802690270027102720273027402750276027702780279028002810282028302840285028602875562880289139642903074291556292556293029402952518296029702980299030025643014630246303463040305030624723070308030955631003110312031303140315031603170 module fluentasserts.core.expect; import fluentasserts.core.lifecycle; import fluentasserts.core.evaluation; import fluentasserts.core.results; import fluentasserts.core.serializers; import std.traits; import std.string; import std.uni; import std.conv; /// @safe struct Expect { private { Evaluation evaluation; int refCount; } this(ValueEvaluation value, const string fileName, const size_t line, string prependText = null) @trusted { this.evaluation = new Evaluation(); evaluation.id = Lifecycle.instance.beginEvaluation(value); evaluation.currentValue = value; evaluation.message = new MessageResult(); evaluation.source = new SourceResult(fileName, line); try { auto sourceValue = evaluation.source.getValue; if(sourceValue == "") { evaluation.message.startWith(evaluation.currentValue.niceValue); } else { evaluation.message.startWith(sourceValue); } } catch(Exception) { evaluation.message.startWith(evaluation.currentValue.strValue); } evaluation.message.addText(" should"); if(prependText) { evaluation.message.addText(prependText); } } this(ref return scope Expect another) { this.evaluation = another.evaluation; this.refCount = another.refCount + 1; } ~this() { refCount--; if(refCount < 0) { evaluation.message.addText(" "); evaluation.message.addText(evaluation.operationName.toNiceOperation); if(evaluation.expectedValue.niceValue) { evaluation.message.addText(" "); evaluation.message.addValue(evaluation.expectedValue.niceValue); } else if(evaluation.expectedValue.strValue) { evaluation.message.addText(" "); evaluation.message.addValue(evaluation.expectedValue.strValue); } Lifecycle.instance.endEvaluation(evaluation); } } string msg(const size_t line = __LINE__, const string file = __FILE__) @trusted { if(this.thrown is null) { throw new Exception("There were no thrown exceptions", file, line); } return this.thrown.message.to!string; } Expect withMessage(const size_t line = __LINE__, const string file = __FILE__) { addOperationName("withMessage"); return this; } Expect withMessage(string message, const size_t line = __LINE__, const string file = __FILE__) { addOperationName("withMessage"); return this.equal(message); } Throwable thrown() { Lifecycle.instance.endEvaluation(evaluation); return evaluation.throwable; } /// Expect to() { return this; } /// Expect be () { evaluation.message.addText(" be"); return this; } /// Expect not() { evaluation.isNegated = !evaluation.isNegated; evaluation.message.addText(" not"); return this; } /// auto throwAnyException() { return opDispatch!"throwAnyException"; } /// Expect throwException(Type)() { this.evaluation.expectedValue.meta["exceptionType"] = fullyQualifiedName!Type; this.evaluation.expectedValue.meta["throwableType"] = fullyQualifiedName!Type; return opDispatch!"throwException"(fullyQualifiedName!Type); } auto because(string reason) { evaluation.message.prependText("Because " ~ reason ~ ", "); return this; } /// auto equal(T)(T value) { return opDispatch!"equal"(value); } /// auto contain(T)(T value) { return opDispatch!"contain"(value); } /// auto greaterThan(T)(T value) { return opDispatch!"greaterThan"(value); } /// auto greaterOrEqualTo(T)(T value) { return opDispatch!"greaterOrEqualTo"(value); } /// auto above(T)(T value) { return opDispatch!"above"(value); } /// auto lessThan(T)(T value) { return opDispatch!"lessThan"(value); } /// auto lessOrEqualTo(T)(T value) { return opDispatch!"lessOrEqualTo"(value); } /// auto below(T)(T value) { return opDispatch!"below"(value); } /// auto startWith(T)(T value) { return opDispatch!"startWith"(value); } /// auto endWith(T)(T value) { return opDispatch!"endWith"(value); } auto containOnly(T)(T value) { return opDispatch!"containOnly"(value); } auto beNull() { return opDispatch!"beNull"; } auto instanceOf(Type)() { return opDispatch!"instanceOf"(fullyQualifiedName!Type); } auto approximately(T, U)(T value, U range) { return opDispatch!"approximately"(value, range); } auto between(T, U)(T value, U range) { return opDispatch!"between"(value, range); } auto within(T, U)(T value, U range) { return opDispatch!"within"(value, range); } void inhibit() { this.refCount = int.max; } auto haveExecutionTime() { this.inhibit; auto result = expect(evaluation.currentValue.duration, evaluation.source.file, evaluation.source.line, " have execution time"); return result; } void addOperationName(string value) { if(this.evaluation.operationName) { this.evaluation.operationName ~= "."; } this.evaluation.operationName ~= value; } /// Expect opDispatch(string methodName)() { addOperationName(methodName); return this; } /// Expect opDispatch(string methodName, Params...)(Params params) if(Params.length > 0) { addOperationName(methodName); static if(Params.length > 0) { auto expectedValue = params[0].evaluate.evaluation; foreach(key, value; evaluation.expectedValue.meta) { expectedValue.meta[key] = value; } evaluation.expectedValue = expectedValue; } static if(Params.length >= 1) { static foreach (i, Param; Params) { () @trusted { evaluation.expectedValue.meta[i.to!string] = SerializerRegistry.instance.serialize(params[i]); } (); } } return this; } } /// Expect expect(void delegate() callable, const string file = __FILE__, const size_t line = __LINE__, string prependText = null) @trusted { ValueEvaluation value; value.typeNames = [ "callable" ]; try { if(callable !is null) { callable(); } else { value.typeNames = ["null"]; } } catch(Exception e) { value.throwable = e; value.meta["Exception"] = "yes"; } catch(Throwable t) { value.throwable = t; value.meta["Throwable"] = "yes"; } return Expect(value, file, line, prependText); } /// 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); } /// string toNiceOperation(string value) @safe nothrow { string newValue; foreach(index, ch; value) { if(index == 0) { newValue ~= ch.toLower; continue; } if(ch == '.') { newValue ~= ' '; continue; } if(ch.isUpper && value[index - 1].isLower) { newValue ~= ' '; newValue ~= ch.toLower; continue; } newValue ~= ch; } return newValue; } /// toNiceOperation converts to a nice and readable string unittest { expect("".toNiceOperation).to.equal(""); expect("a.b".toNiceOperation).to.equal("a b"); expect("aB".toNiceOperation).to.equal("a b"); }
module fluentasserts.core.expect; import fluentasserts.core.lifecycle; import fluentasserts.core.evaluation; import fluentasserts.core.results; import fluentasserts.core.serializers; import std.traits; import std.string; import std.uni; import std.conv; /// @safe struct Expect { private { Evaluation evaluation; int refCount; } this(ValueEvaluation value, const string fileName, const size_t line, string prependText = null) @trusted { this.evaluation = new Evaluation(); evaluation.id = Lifecycle.instance.beginEvaluation(value); evaluation.currentValue = value; evaluation.message = new MessageResult(); evaluation.source = new SourceResult(fileName, line); try { auto sourceValue = evaluation.source.getValue; if(sourceValue == "") { evaluation.message.startWith(evaluation.currentValue.niceValue); } else { evaluation.message.startWith(sourceValue); } } catch(Exception) { evaluation.message.startWith(evaluation.currentValue.strValue); } evaluation.message.addText(" should"); if(prependText) { evaluation.message.addText(prependText); } } this(ref return scope Expect another) { this.evaluation = another.evaluation; this.refCount = another.refCount + 1; } ~this() { refCount--; if(refCount < 0) { evaluation.message.addText(" "); evaluation.message.addText(evaluation.operationName.toNiceOperation); if(evaluation.expectedValue.niceValue) { evaluation.message.addText(" "); evaluation.message.addValue(evaluation.expectedValue.niceValue); } else if(evaluation.expectedValue.strValue) { evaluation.message.addText(" "); evaluation.message.addValue(evaluation.expectedValue.strValue); } Lifecycle.instance.endEvaluation(evaluation); } } string msg(const size_t line = __LINE__, const string file = __FILE__) @trusted { if(this.thrown is null) { throw new Exception("There were no thrown exceptions", file, line); } return this.thrown.message.to!string; } Expect withMessage(const size_t line = __LINE__, const string file = __FILE__) { addOperationName("withMessage"); return this; } Expect withMessage(string message, const size_t line = __LINE__, const string file = __FILE__) { addOperationName("withMessage"); return this.equal(message); } Throwable thrown() { Lifecycle.instance.endEvaluation(evaluation); return evaluation.throwable; } /// Expect to() { return this; } /// Expect be () { evaluation.message.addText(" be"); return this; } /// Expect not() { evaluation.isNegated = !evaluation.isNegated; evaluation.message.addText(" not"); return this; } /// auto throwAnyException() { return opDispatch!"throwAnyException"; } /// Expect throwException(Type)() { this.evaluation.expectedValue.meta["exceptionType"] = fullyQualifiedName!Type; this.evaluation.expectedValue.meta["throwableType"] = fullyQualifiedName!Type; return opDispatch!"throwException"(fullyQualifiedName!Type); } auto because(string reason) { evaluation.message.prependText("Because " ~ reason ~ ", "); return this; } /// auto equal(T)(T value) { return opDispatch!"equal"(value); } /// auto contain(T)(T value) { return opDispatch!"contain"(value); } /// auto greaterThan(T)(T value) { return opDispatch!"greaterThan"(value); } /// auto greaterOrEqualTo(T)(T value) { return opDispatch!"greaterOrEqualTo"(value); } /// auto above(T)(T value) { return opDispatch!"above"(value); } /// auto lessThan(T)(T value) { return opDispatch!"lessThan"(value); } /// auto lessOrEqualTo(T)(T value) { return opDispatch!"lessOrEqualTo"(value); } /// auto below(T)(T value) { return opDispatch!"below"(value); } /// auto startWith(T)(T value) { return opDispatch!"startWith"(value); } /// auto endWith(T)(T value) { return opDispatch!"endWith"(value); } auto containOnly(T)(T value) { return opDispatch!"containOnly"(value); } auto beNull() { return opDispatch!"beNull"; } auto instanceOf(Type)() { return opDispatch!"instanceOf"(fullyQualifiedName!Type); } auto approximately(T, U)(T value, U range) { return opDispatch!"approximately"(value, range); } auto between(T, U)(T value, U range) { return opDispatch!"between"(value, range); } auto within(T, U)(T value, U range) { return opDispatch!"within"(value, range); } void inhibit() { this.refCount = int.max; } auto haveExecutionTime() { this.inhibit; auto result = expect(evaluation.currentValue.duration, evaluation.source.file, evaluation.source.line, " have execution time"); return result; } void addOperationName(string value) { if(this.evaluation.operationName) { this.evaluation.operationName ~= "."; } this.evaluation.operationName ~= value; } /// Expect opDispatch(string methodName)() { addOperationName(methodName); return this; } /// Expect opDispatch(string methodName, Params...)(Params params) if(Params.length > 0) { addOperationName(methodName); static if(Params.length > 0) { auto expectedValue = params[0].evaluate.evaluation; foreach(key, value; evaluation.expectedValue.meta) { expectedValue.meta[key] = value; } evaluation.expectedValue = expectedValue; } static if(Params.length >= 1) { static foreach (i, Param; Params) { () @trusted { evaluation.expectedValue.meta[i.to!string] = SerializerRegistry.instance.serialize(params[i]); } (); } } return this; } } /// Expect expect(void delegate() callable, const string file = __FILE__, const size_t line = __LINE__, string prependText = null) @trusted { ValueEvaluation value; value.typeNames = [ "callable" ]; try { if(callable !is null) { callable(); } else { value.typeNames = ["null"]; } } catch(Exception e) { value.throwable = e; value.meta["Exception"] = "yes"; } catch(Throwable t) { value.throwable = t; value.meta["Throwable"] = "yes"; } return Expect(value, file, line, prependText); } /// 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); } /// string toNiceOperation(string value) @safe nothrow { string newValue; foreach(index, ch; value) { if(index == 0) { newValue ~= ch.toLower; continue; } if(ch == '.') { newValue ~= ' '; continue; } if(ch.isUpper && value[index - 1].isLower) { newValue ~= ' '; newValue ~= ch.toLower; continue; } newValue ~= ch; } return newValue; } /// toNiceOperation converts to a nice and readable string unittest { expect("".toNiceOperation).to.equal(""); expect("a.b".toNiceOperation).to.equal("a b"); expect("aB".toNiceOperation).to.equal("a b"); }