fluentasserts.core.evaluation 3/28(10%) 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
391112
408
410
420
431104
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
820
830
840
850
860
870
880
890
900
910
920
930
940
950
960
970
980
990
1000
1010
1020
1030
1040
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
1320
1330
1340
1350
1360
1370
1380
1390
1400
1410
1420
1430
1440
1450
1460
1470
1480
1490
1500
1510
1520
1530
1540
1550
1560
1570
1580
1590
1600
1610
1620
1630
1640
1650
1660
1670
1680
1690
1700
1710
1720
1730
1740
1750
1760
1770
1780
1790
1800
1810
1820
1830
1840
1850
1860
1870
1880
1890
1900
1910
1920
1930
1940
1950
1960
1970
1980
1990
2000
2010
2020
2030
2040
2050
2060
2070
2080
2090
2100
2110
2120
2130
2140
2150
2160
2170
2180
2190
2200
2210
2220
2230
2240
2250
2260
2270
2280
2290
2300
2310
2320
2330
2340
2350
2360
2370
2380
2390
2400
2410
2420
2430
2440
2450
2460
2470
2480
2490
2500
2510
2520
2530
2540
2550
2560
2570
2580
2590
2600
2610
2620
2630
2640
2650
2660
2670
2680
2690
2700
2710
2720
2730
2740
2750
2760
2770
2780
2790
2800
2810
2820
2830
2840
2850
2860
2870
2880
2890
2900
2910
2920
2930
2940
2950
2960
2970
2980
2990
3000
3010
3020
3030
3040
3050
3060
3070
3080
3090
3100
3110
3120
3130
3140
3150
3160
3170
3180
3190
3200
3210
3220
3230
3240
3250
3260
3270
3280
3290
3300
3310
3320
3330
3340
3350
3360
3370
3380
3390
3400
3410
3420
3430
3440
3450
3460
3470
3480
3490
3500
3510
3520
3530
3540
3550
3560
3570
3580
3590
3600
3610
3620
3630
3640
3650
3660
3670
3680
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")) { 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); } }