fluentasserts.core.evaluation 42/42(100%) 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
270
280
290
300
310
320
330
340
350
367292
3762
380
390
407230
410
420
430
440
450
460
470
480
490
500
510
520
530
540
550
560
570
580
590
600
610
620
630
640
650
660
670
680
690
700
710
720
730
740
750
760
770
780
79368
800
810
820
830
846495
850
860
870
886495
890
900
910
92111
93111
94111
950
960
970
986395
996395
1006395
1016395
1020
103100
1040
1050
10678
1070
1080
109100
1100
1110
1120
1130
1140
1150
1161
1170
1180
1192
1200
1211
1221
1230
1240
1250
1260
1270
1281
1290
1300
1313
1320
1331
1341
1350
1360
1370
1386523
1390
1406523
1410
1420
1430
14437
1450
1460
1470
1480
1490
1503
1510
1520
1530
1546523
1550
1560
1570
1582060
1590
1600
1610
1621
1632
1640
1650
1660
1670
1681
1691
1700
1710
1720
1730
1741
1751
1760
1770
1780
1790
1801
1811
1820
1830
1840
1850
1860
1870
1880
1891
1900
1911
1921
1931
1940
module fluentasserts.core.evaluation; import std.datetime; import std.typecons; import std.traits; import std.conv; import std.range; import std.array; import std.algorithm : map; import fluentasserts.core.serializers; import fluentasserts.core.results; import fluentasserts.core.base : TestException; /// struct ValueEvaluation { /// The exception thrown during evaluation Throwable throwable; /// Time needed to evaluate the value Duration duration; /// Serialized value as string string strValue; /// Human readable value string niceValue; /// The name of the type before it was converted to string string[] typeNames; /// Other info about the value string[string] meta; string typeName() @safe nothrow { if(typeNames.length == 0) { return "unknown"; } return typeNames[0]; } } /// class Evaluation { /// The id of the current evaluation size_t id; /// The value that will be validated ValueEvaluation currentValue; /// The expected value that we will use to perform the comparison ValueEvaluation expectedValue; /// The operation name string operationName; /// True if the operation result needs to be negated to have a successful result bool isNegated; /// The nice message printed to the user MessageResult message; /// The source code where the assert is located SourceResult source; /// Results generated during evaluation IResult[] results; /// The throwable generated by the evaluation Throwable throwable; /// True when the evaluation is done bool isEvaluated; } /// auto evaluate(T)(lazy T testData) @trusted if(isInputRange!T && !isArray!T && !isAssociativeArray!T) { return evaluate(testData.array); } /// auto evaluate(T)(lazy T testData) @trusted if(!isInputRange!T || isArray!T || isAssociativeArray!T) { auto begin = Clock.currTime; alias Result = Tuple!(T, "value", ValueEvaluation, "evaluation"); try { auto value = testData; alias TT = typeof(value); static if(isCallable!T) { if(value !is null) { begin = Clock.currTime; value(); } } auto duration = Clock.currTime - begin; auto serializedValue = SerializerRegistry.instance.serialize(value); auto niceValue = SerializerRegistry.instance.niceValue(value); return Result(value, ValueEvaluation(null, duration, serializedValue, niceValue, extractTypes!TT )); } catch(Throwable t) { T result; static if(isCallable!T) { result = testData; } return Result(result, ValueEvaluation(t, Clock.currTime - begin, result.to!string, result.to!string, extractTypes!T )); } } /// evaluate a lazy value should capture an exception unittest { int value() { throw new Exception("message"); } auto result = evaluate(value); assert(result.evaluation.throwable !is null); assert(result.evaluation.throwable.msg == "message"); } /// evaluate should capture an exception thrown by a callable unittest { void value() { throw new Exception("message"); } auto result = evaluate(&value); assert(result.evaluation.throwable !is null); assert(result.evaluation.throwable.msg == "message"); } string[] extractTypes(T)() if((!isArray!T && !isAssociativeArray!T) || isSomeString!T) { string[] types; types ~= unqualString!T; static if(is(T == class)) { static foreach(Type; BaseClassesTuple!T) { types ~= unqualString!Type; } } static if(is(T == interface) || is(T == class)) { static foreach(Type; InterfacesTuple!T) { types ~= unqualString!Type; } } return types; } string[] extractTypes(T: U[], U)() if(isArray!T && !isSomeString!T) { return extractTypes!(U).map!(a => a ~ "[]").array; } string[] extractTypes(T: U[K], U, K)() { string k = unqualString!(K); return extractTypes!(U).map!(a => a ~ "[" ~ k ~ "]").array; } /// It can get the type of a string unittest { auto result = extractTypes!string; assert(result == ["string"]); } /// It can get the type of a string list unittest { auto result = extractTypes!(string[]); assert(result == ["string[]"]); } /// It can get the type of a string assoc array unittest { auto result = extractTypes!(string[string]); assert(result == ["string[string]"]); } /// It can get all types of a class unittest { interface I {} class T : I {} auto result = extractTypes!(T[]); assert(result[0] == "fluentasserts.core.evaluation.__unittest_L185_C1.T[]"); assert(result[1] == "object.Object[]"); assert(result[2] == "fluentasserts.core.evaluation.__unittest_L185_C1.I[]"); }