fluentasserts.core.evaluation 85/91(93%) 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
360
370
380
396749
4062
410
420
436687
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
790
800
810
82288
830
840
850
860
876031
880
890
900
916031
920
930
940
95111
96111
97111
980
990
1000
1015931
1025931
1035931
1045931
1050
106100
1070
1080
10978
1100
1110
112100
1130
1140
1150
1160
1170
1180
1191
1200
1210
1222
1230
1241
1251
1260
1270
1280
1290
1300
1311
1320
1330
1343
1350
1361
1371
1380
1390
1400
1416065
1420
1436065
1440
1450
1460
14789
1480
1490
1500
1510
1520
1533
1540
1550
1560
1576065
1580
1590
1600
1611869
1620
1630
1640
16511
16622
1670
1680
1690
1700
1711
1721
1730
1740
1750
1760
1771
1781
1790
1800
1810
1820
1831
1841
1850
1860
1870
1880
1890
1900
1910
1921
1930
1941
1951
1961
1970
1980
1990
2000
2010
2020
2030
2040
2050
2060
2070
2080
2090
2100
2110
212966
2130
2140
2150
21610
2170
2187300
2190
2200
2210
2220
2230
2240
2250
2260
2270
2280
2290
2307521
2317521
2327521
2330
2340
2350
2360
2372467
2380
2392467
2402246
2410
2420
243221
2440
2450
2469
2470
2489
2492
2500
2510
2520
253219
2540
2550
2560
2570
2580
2590
260651
2610
2620
2630
2640
2652
2660
2672
2682
2690
2700
2710
272219
2730
2740
2750
2760
2770
2783
2790
2800
2810
28257
2830
2840
2850
2862
2870
2880
2890
2900
2910
2920
2930
2940
2950
2960
2970
2981
2991
3000
3010
3020
3031
3041
3051
3061
3070
3080
3090
3100
3110
3120
3130
3140
3150
3160
317976
318976
319976
3200
3210
3220
323101
3240
325101
3260
3270
3280
329101
3300
3310
3320
33314
3340
3350
3360
3370
3386
3390
3400
3413035
3420
343793
3440
3450
3460
3470
3480
3490
3500
3510
3520
3530
3540
3550
3560
3570
3580
3590
3600
36110
36210
3630
36410
36530
3660
3670
36810
3690
3700
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, sort; 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; /// Proxy object holding the evaluated value to help doing better comparisions EquableValue proxyValue; /// 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, equableValue(value, niceValue), 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, equableValue(result, 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_L188_C1.T[]", `Expected: ` ~ result[0]); assert(result[1] == "object.Object[]", `Expected: ` ~ result[1] ); assert(result[2] == "fluentasserts.core.evaluation.__unittest_L188_C1.I[]", `Expected: ` ~ result[2] ); } /// A proxy type that allows to compare the native values interface EquableValue { @safe nothrow: bool isEqualTo(EquableValue value); EquableValue[] toArray(); string toString(); EquableValue generalize(); string getSerialized(); } /// Wraps a value into equable value EquableValue equableValue(T)(T value, string serialized) { static if(isArray!T && !isSomeString!T) { return new ArrayEquable!T(value, serialized); } else static if(isInputRange!T && !isSomeString!T) { return new ArrayEquable!T(value.array, serialized); } else static if(isAssociativeArray!T) { return new AssocArrayEquable!T(value, serialized); } else { return new ObjectEquable!T(value, serialized); } } /// class ObjectEquable(T) : EquableValue { private { T value; string serialized; } @trusted nothrow: this(T value, string serialized) { this.value = value; this.serialized = serialized; } bool isEqualTo(EquableValue otherEquable) { try { auto other = cast(ObjectEquable) otherEquable; if(other !is null) { return value == other.value; } auto generalized = otherEquable.generalize; static if(is(T == class)) { auto otherGeneralized = cast(ObjectEquable!Object) generalized; if(otherGeneralized !is null) { return value == otherGeneralized.value; } } return serialized == otherEquable.getSerialized; } catch(Exception) { return false; } } string getSerialized() { return serialized; } EquableValue generalize() { static if(is(T == class)) { auto obj = cast(Object) value; if(obj !is null) { return new ObjectEquable!Object(obj, serialized); } } return new ObjectEquable!string(serialized, serialized); } EquableValue[] toArray() { static if(__traits(hasMember, T, "byValue") && !__traits(hasMember, T, "byKeyValue")) { try { return value.byValue.map!(a => a.equableValue(SerializerRegistry.instance.serialize(a))).array; } catch(Exception) {} } return [ this ]; } override string toString() { return "Equable." ~ serialized; } override int opCmp (Object o) { return -1; } } /// an object with byValue method should return an array with all elements unittest { class TestObject { auto byValue() { auto items = [1, 2]; return items.inputRangeObject; } } auto value = equableValue(new TestObject(), "[1, 2]").toArray; assert(value.length == 2, "invalid length"); assert(value[0].toString == "Equable.1", value[0].toString ~ " != Equable.1"); assert(value[1].toString == "Equable.2", value[1].toString ~ " != Equable.2"); } /// class ArrayEquable(U: T[], T) : EquableValue { private { T[] values; string serialized; } @safe nothrow: this(T[] values, string serialized) { this.values = values; this.serialized = serialized; } bool isEqualTo(EquableValue otherEquable) { auto other = cast(ArrayEquable!U) otherEquable; if(other is null) { return false; } return serialized == other.serialized; } string getSerialized() { return serialized; } @trusted EquableValue[] toArray() { static if(is(T == void)) { return []; } else { try { auto newList = values.map!(a => equableValue(a, SerializerRegistry.instance.niceValue(a))).array; return cast(EquableValue[]) newList; } catch(Exception) { return []; } } } EquableValue generalize() { return this; } override string toString() { return serialized; } } /// class AssocArrayEquable(U: T[V], T, V) : ArrayEquable!(string[], string) { this(T[V] values, string serialized) { auto sortedKeys = values.keys.sort; auto sortedValues = sortedKeys .map!(a => SerializerRegistry.instance.niceValue(a) ~ `: ` ~ SerializerRegistry.instance.niceValue(values[a])) .array; super(sortedValues, serialized); } }