fluentasserts.core.expect 105/109(96%) line coverage

      
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
200
210
223522
233522
240
253522
263522
273522
283522
290
300
313522
320
333522
341458
350
362064
370
380
390
400
410
423522
430
443522
452
460
470
480
495883
505883
515883
520
530
540
559405
560
579405
583520
593520
600
613520
623458
633458
6462
650
660
670
680
693520
700
710
720
730
74517
750
760
770
78517
790
800
810
8211
8311
840
850
860
8721
8821
890
900
910
921035
931035
940
950
960
970
981091
990
1000
1010
1020
103557
104557
1050
1060
1070
1080
109665
110665
1110
112665
1130
1140
1150
1160
11764
1180
1190
1200
1210
122531
123531
1240
125531
1260
1270
1280
1297
1307
1310
1320
1330
1340
1351801
1360
1370
1380
1390
140305
1410
1420
1430
1440
14570
1460
1470
1480
1490
15055
1510
1520
1530
1540
15530
1560
1570
1580
15961
1600
1610
1620
1630
16455
1650
1660
1670
1680
16930
1700
1710
1720
1730
17490
1750
1760
1770
1780
17937
1800
1810
1820
183142
1840
1850
1860
18717
1880
1890
1900
19161
1920
1930
1940
19564
1960
1970
1980
199104
2000
2010
2020
20330
2040
2050
2060
2072
2080
2090
2100
2112
2120
2134
2140
2152
2160
2170
2180
2190
2203584
22164
2220
2230
2243584
2250
2260
2270
2280
22986
2300
23186
2320
2330
2340
2350
2363466
2370
2380
2396932
2400
2415638
2421086
2430
2440
2453466
2460
2470
2480
2490
2507328
2510
2520
2530
2543466
2550
2560
2570
2580
2590
260495
261495
2620
2630
264495
265491
2660
2674
2680
2690
270478
271478
2720
2731
2741
2750
2760
277495
2780
2790
2800
2810
2826131
2830
2840
2850
2860
2873523
2880
289124125
29028389
2913522
2923522
2930
2940
29524867
29665
29765
2980
2990
30026307
3011505
3021505
3031505
3040
3050
30623297
3070
3080
3093523
3100
3110
3120
3130
3142
3152
3162
3170
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"); }