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
223269
233269
240
253269
263269
273269
283269
290
300
313269
320
333269
341326
350
361943
370
380
390
400
410
423269
430
443269
452
460
470
480
495510
505510
515510
520
530
540
558779
560
578779
583267
593267
600
613267
623205
633205
6462
650
660
670
680
693267
700
710
720
730
74475
750
760
770
78475
790
800
810
8211
8311
840
850
860
8721
8821
890
900
910
92951
93951
940
950
960
970
981007
990
1000
1010
1020
103567
104567
1050
1060
1070
1080
109619
110619
1110
112619
1130
1140
1150
1160
11764
1180
1190
1200
1210
122489
123489
1240
125489
1260
1270
1280
1297
1307
1310
1320
1330
1340
1351635
1360
1370
1380
1390
140267
1410
1420
1430
1440
14570
1460
1470
1480
1490
15063
1510
1520
1530
1540
15532
1560
1570
1580
15961
1600
1610
1620
1630
16455
1650
1660
1670
1680
16930
1700
1710
1720
1730
17490
1750
1760
1770
1780
17938
1800
1810
1820
183124
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
2203331
22164
2220
2230
2243331
2250
2260
2270
2280
22986
2300
23186
2320
2330
2340
2350
2363213
2370
2380
2396426
2400
2415217
2421002
2430
2440
2453213
2460
2470
2480
2490
2506822
2510
2520
2530
2543213
2550
2560
2570
2580
2590
260453
261453
2620
2630
264453
265449
2660
2674
2680
2690
270436
271436
2720
2731
2741
2750
2760
277453
2780
2790
2800
2810
2825709
2830
2840
2850
2860
2873270
2880
289116418
29026652
2913269
2923269
2930
2940
29523383
29665
29765
2980
2990
30024788
3011470
3021470
3031470
3040
3050
30621848
3070
3080
3093270
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"); }