fluentasserts.core.lifecycle 110/112(98%) line coverage

      
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
200
210
220
230
240
250
260
271
281
290
301
310
321
331
341
350
360
3720
3820
3920
400
410
420
4311
4411
450
4611
4711
480
4911
5011
510
520
530
5420
550
560
57400
58400
59400
600
61400
62400
63400
640
65400
66400
67400
680
69400
700
710
720
730
743
750
760
779
789
790
809
819
829
839
840
859
869
870
880
890
901
911
920
930
943
953
963
970
980
991
1001
1010
1021
1031
1041
1050
1060
1070
1080
1090
1100
1110
1120
1130
1140
1150
1160
1170
1180
1190
1200
1210
1220
1230
1240
1250
1260
1270
1280
1290
1300
1310
1324105
1330
1340
1350
1360
1376502
1380
1390
1400
1410
1422397
1430
1442397
1452397
1460
1472397
1480
1492397
1502397
1512397
1520
1530
1542397
1550
1560
1570
1580
1592391
1602391
1610
1620
1630
1640
1652419
1662419
1672419
1680
1690
1700
1710
1722910
1730
1742910
175513
1760
1770
1782910
1790
1800
1810
1820
1832397
1842397
1852397
1860
1870
1882397
1890
1902397
1911089
1920
1931308
1940
1950
1960
1970
1980
1992397
2000
2012397
2020
2030
2040
2050
2064
2070
2084
2090
2100
2110
2120
21313764
2146
2150
2160
21713764
21813764
2190
2200
2210
2220
2233124
2243124
2250
2260
2270
2280
2296502
2300
2316502
2326502
2330
23410607
2350
2362397
2372397
2380
2392397
2402391
2412391
2420
2430
2442397
2452397
2460
2470
2482397
249434
2500
2510
2520
2532397
2542397
2550
2562397
25715
2580
2590
2602397
2610
2620
2630
2642397
2650
2660
2670
2680
2690
2700
2710
2720
2730
2740
2750
2760
2776502
27815
2790
2800
2816487
2826063
2830
2840
285424
2860
287424
288424
2890
2900
291424
2920
293424
2940
2950
2960
2976502
2980
2990
module fluentasserts.core.lifecycle; import fluentasserts.core.base; import fluentasserts.core.evaluation; import fluentasserts.core.operations.approximately; import fluentasserts.core.operations.arrayEqual; import fluentasserts.core.operations.between; import fluentasserts.core.operations.contain; import fluentasserts.core.operations.endWith; import fluentasserts.core.operations.equal; import fluentasserts.core.operations.greaterThan; import fluentasserts.core.operations.lessThan; import fluentasserts.core.operations.registry; import fluentasserts.core.operations.startWith; import fluentasserts.core.operations.throwable; import fluentasserts.core.results; import fluentasserts.core.serializers; import std.meta; import std.conv; alias BasicNumericTypes = AliasSeq!(byte, ubyte, short, ushort, int, uint, long, ulong, float, double, real); alias NumericTypes = AliasSeq!(byte, ubyte, short, ushort, int, uint, long, ulong, float, double, real, ifloat, idouble, ireal, cfloat, cdouble, creal, char, wchar, dchar); alias StringTypes = AliasSeq!(string, wstring, dstring); static this() { SerializerRegistry.instance = new SerializerRegistry; Lifecycle.instance = new Lifecycle; ResultGlyphs.resetDefaults; Registry.instance = new Registry(); Registry.instance.register("string", "string", "equal", &equal); Registry.instance.register("bool", "bool", "equal", &equal); static foreach(Type; NumericTypes) { Registry.instance.register(Type.stringof, Type.stringof, "equal", &equal); Registry.instance.register(Type.stringof ~ "[]", Type.stringof ~ "[]", "equal", &arrayEqual); Registry.instance.register(Type.stringof ~ "[]", "void[]", "equal", &arrayEqual); } static foreach(Type; BasicNumericTypes) { Registry.instance.register(Type.stringof, Type.stringof, "greaterThan", &greaterThan!Type); Registry.instance.register(Type.stringof, Type.stringof, "above", &greaterThan!Type); Registry.instance.register(Type.stringof, Type.stringof, "lessThan", &lessThan!Type); Registry.instance.register(Type.stringof, Type.stringof, "below", &lessThan!Type); Registry.instance.register(Type.stringof, Type.stringof, "between", &between!Type); Registry.instance.register(Type.stringof, Type.stringof, "within", &between!Type); } static foreach(Type1; NumericTypes) { Registry.instance.register(Type1.stringof ~ "[]", "void[]", "approximately", &approximately); static foreach(Type2; NumericTypes) { Registry.instance.register(Type1.stringof, Type2.stringof, "equal", &equal); Registry.instance.register(Type1.stringof ~ "[]", Type2.stringof ~ "[]", "equal", &arrayEqual); Registry.instance.register(Type1.stringof ~ "[]", "void[]", "equal", &arrayEqual); Registry.instance.register(Type1.stringof ~ "[]", Type2.stringof ~ "[]", "contain", &arrayContain); Registry.instance.register(Type1.stringof ~ "[]", "void[]", "contain", &arrayContain); Registry.instance.register(Type1.stringof ~ "[]", Type2.stringof, "contain", &arrayContain); Registry.instance.register(Type1.stringof ~ "[]", Type2.stringof ~ "[]", "containOnly", &arrayContainOnly); Registry.instance.register(Type1.stringof ~ "[]", "void[]", "containOnly", &arrayContainOnly); Registry.instance.register(Type1.stringof ~ "[]", Type2.stringof ~ "[]", "approximately", &approximately); Registry.instance.register(Type1.stringof, Type2.stringof, "approximately", &approximately); } } static foreach(Type1; StringTypes) { Registry.instance.register(Type1.stringof ~ "[]", "void[]", "equal", &arrayEqual); static foreach(Type2; StringTypes) { Registry.instance.register(Type1.stringof, Type2.stringof, "equal", &equal); Registry.instance.register(Type1.stringof ~ "[]", Type2.stringof ~ "[]", "equal", &arrayEqual); Registry.instance.register(Type1.stringof, Type2.stringof ~ "[]", "contain", &contain); Registry.instance.register(Type1.stringof, Type2.stringof, "contain", &contain); Registry.instance.register(Type1.stringof ~ "[]", Type2.stringof ~ "[]", "contain", &arrayContain); Registry.instance.register(Type1.stringof ~ "[]", Type2.stringof ~ "[]", "containOnly", &arrayContainOnly); Registry.instance.register(Type1.stringof, Type2.stringof, "startWith", &startWith); Registry.instance.register(Type1.stringof, Type2.stringof, "endWith", &endWith); } } Registry.instance.register("*[]", "*[]", "equal", &arrayEqual); Registry.instance.register("*", "*", "equal", &equal); static foreach(Type; StringTypes) { Registry.instance.register(Type.stringof, "char", "contain", &contain); Registry.instance.register(Type.stringof, "char", "startWith", &startWith); Registry.instance.register(Type.stringof, "char", "endWith", &endWith); } Registry.instance.register("callable", "", "throwAnyException", &throwAnyException); Registry.instance.register("callable", "", "throwException", &throwException); Registry.instance.register("*", "*", "throwAnyException", &throwAnyException); Registry.instance.register("*", "*", "throwException", &throwException); Registry.instance.register("*", "*", "throwAnyException.withMessage.equal", &throwAnyExceptionWithMessage); } /// The assert lifecycle @safe class Lifecycle { /// Global instance for the assert lifecicle static Lifecycle instance; private { Evaluation evaluation; /// The nice message printed to the user MessageResult message; /// The source code where the assert is located SourceResult sourceResult; /// int assertIndex; /// int totalAsserts; } void incAssertIndex() { assertIndex++; } /// Checks if an assert operation was set bool hasOperation() { return evaluation.operationName != ""; } /// Method called when a new value is evaluated Lifecycle beginEvaluation(ValueEvaluation value) @safe nothrow { assert(assertIndex >= 0, "assert index is `" ~ assertIndex.to!string ~ "`. It must be >= 0."); totalAsserts++; assertIndex++; evaluation = Evaluation(); if(assertIndex == 1) { evaluation.currentValue = value; message = new MessageResult(); } return this; } /// Method called when the oracle value is known Lifecycle compareWith(ValueEvaluation value) @safe nothrow { evaluation.expectedValue = value; return this; } /// Method called when the comparison operation is known Lifecycle usingOperation(string operationName) @safe nothrow { assert(evaluation.operationName == "", "Operation name is already set to `" ~ evaluation.operationName ~ "`"); evaluation.operationName = operationName; return this; } /// Method called when the operation result needs to be negated to be true Lifecycle usingNegation(bool value) { evaluation.isNegated = value; if(value) { addText(" not"); } return this; } /// Method called when the assert location is known Lifecycle atSourceLocation(const string fileName, const size_t line) @safe nothrow { evaluation.fileName = fileName; evaluation.line = line; sourceResult = new SourceResult(fileName, line); try { auto value = sourceResult.getValue; if(value == "") { message.startWith(evaluation.currentValue.strValue); } else { message.startWith(value); } } catch(Exception) { message.startWith(evaluation.currentValue.strValue); } addText(" should"); return this; } /// Lifecycle prependText(string text) @safe nothrow { message.prependText(text); return this; } /// Lifecycle addText(string text) @safe nothrow { if(text == "throwAnyException") { text = "throw any exception"; } message.addText(text); return this; } /// Lifecycle addValue(string value) @safe nothrow { message.addValue(value); return this; } /// EvaluationResult endEvaluation() @trusted { EvaluationResult result; assertIndex--; assert(assertIndex >= 0, "assert index is `" ~ assertIndex.to!string ~ "`. It must be >= 0."); if(assertIndex > 0) return result; addText(" "); addText(evaluation.operationName); if(evaluation.expectedValue.strValue) { addText(" "); addValue(evaluation.expectedValue.strValue); } result.message = message; result.results = Registry.instance.handle(evaluation); version(DisableSourceResult) {} else { if(result.results.length > 0) { result.results ~= sourceResult; } } result.fileName = evaluation.fileName; result.line = evaluation.line; if(evaluation.currentValue.throwable !is null) { result.throwable = evaluation.currentValue.throwable; } if(evaluation.expectedValue.throwable !is null) { result.throwable = evaluation.currentValue.throwable; } return result; } } /// @safe struct EvaluationResult { MessageResult message; IResult[] results; string fileName; size_t line; Throwable throwable; void perform() { if(throwable !is null) { throw throwable; } if(results.length == 0) { return; } IResult[] all; if(message !is null) { all ~= message; } all ~= results; throw new TestException(all, fileName, line); } ~this() { this.perform; } }