test.operations.arrayEqual 4/5(80%) 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
390
400
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
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
28916
2900
2910
2922
2930
2942
2950
2962
2970
2980
2990
3000
3010
3020
module test.operations.arrayEqual; import fluentasserts.core.expect; import fluent.asserts; import fluentasserts.core.serializers; import trial.discovery.spec; import std.string; import std.conv; import std.meta; alias s = Spec!({ alias StringTypes = AliasSeq!(string, wstring, dstring); alias NumericTypes = AliasSeq!(byte, ubyte, short, ushort, int, uint, long, ulong, float, double, real /*, ifloat, idouble, ireal, cfloat, cdouble, creal*/); static foreach(Type; StringTypes) { describe("using an array of " ~ Type.stringof, { Type[] aList; Type[] anotherList; Type[] aListInOtherOrder; before({ aList = [ "a", "b", "c" ]; aListInOtherOrder = [ "c", "b", "a" ]; anotherList = [ "b", "c" ]; }); it("should compare two exact arrays", { expect(aList).to.equal(aList); }); it("should be able to compare that two arrays are not equal", { expect(aList).to.not.equal(aListInOtherOrder); expect(aList).to.not.equal(anotherList); expect(anotherList).to.not.equal(aList); }); it("should throw a detailed error message when the two arrays are not equal", { auto msg = ({ expect(aList).to.equal(anotherList); }).should.throwException!TestException.msg.split("\n"); msg[0].strip.should.equal(aList.to!string ~ " should equal " ~ anotherList.to!string ~ "."); msg[1].strip.should.equal("Diff:"); msg[2].strip.should.equal(`["[+a", "]b", "c"]`); msg[4].strip.should.equal("Expected:" ~ anotherList.to!string); msg[5].strip.should.equal("Actual:" ~ aList.to!string); }); it("should throw an error when the arrays have the same values in a different order", { auto msg = ({ expect(aList).to.equal(aListInOtherOrder); }).should.throwException!TestException.msg.split("\n"); msg[0].strip.should.equal(aList.to!string ~ " should equal " ~ aListInOtherOrder.to!string ~ "."); msg[1].strip.should.equal("Diff:"); msg[2].strip.should.equal(`["[-c][+a]", "b", "[-a][+c]"]`); msg[4].strip.should.equal("Expected:" ~ aListInOtherOrder.to!string); msg[5].strip.should.equal("Actual:" ~ aList.to!string); }); it("should throw an error when the arrays should not be equal", { auto msg = ({ expect(aList).not.to.equal(aList); }).should.throwException!TestException.msg.split("\n"); msg[0].strip.should.startWith(aList.to!string ~ " should not equal " ~ aList.to!string ~ "."); msg[2].strip.should.equal(`Expected:not ["a", "b", "c"]`); msg[3].strip.should.equal(`Actual:["a", "b", "c"]`); }); }); } describe("using an array of arrays", { int[][] aList; int[][] anotherList; int[][] aListInOtherOrder; before({ aList = [ [1], [2,2], [3,3,3] ]; aListInOtherOrder = [ [3,3,3], [2,2], [1] ]; anotherList = [ [2], [3] ]; }); it("should compare two exact arrays", { expect(aList).to.equal(aList); }); it("should be able to compare that two arrays are not equal", { expect(aList).to.not.equal(aListInOtherOrder); expect(aList).to.not.equal(anotherList); expect(anotherList).to.not.equal(aList); }); it("should throw a detailed error message when the two arrays are not equal", { auto msg = ({ expect(aList).to.equal(anotherList); }).should.throwException!TestException.msg.split("\n"); msg[0].strip.should.equal(aList.to!string ~ " should equal " ~ anotherList.to!string ~ "."); msg[1].strip.should.equal("Diff:"); msg[2].strip.should.equal(`[[[+1], [2, ]2], [[+3, 3, ]3]]`); msg[4].strip.should.equal("Expected:" ~ anotherList.to!string); msg[5].strip.should.equal("Actual:" ~ aList.to!string); }); it("should throw an error when the arrays have the same values in a different order", { auto msg = ({ expect(aList).to.equal(aListInOtherOrder); }).should.throwException!TestException.msg.split("\n"); msg[0].strip.should.equal(aList.to!string ~ " should equal " ~ aListInOtherOrder.to!string ~ "."); msg[1].strip.should.equal("Diff:"); msg[2].strip.should.equal(`[[[-3, 3, 3][+1]], [2, 2], [[-1][+3, 3, 3]]]`); msg[4].strip.should.equal("Expected:" ~ aListInOtherOrder.to!string); msg[5].strip.should.equal("Actual:" ~ aList.to!string); }); it("should throw an error when the arrays should not be equal", { auto msg = ({ expect(aList).not.to.equal(aList); }).should.throwException!TestException.msg.split("\n"); msg[0].strip.should.startWith(aList.to!string ~ " should not equal " ~ aList.to!string ~ "."); msg[2].strip.should.equal(`Expected:not [[1], [2, 2], [3, 3, 3]]`); msg[3].strip.should.equal(`Actual:[[1], [2, 2], [3, 3, 3]]`); }); }); static foreach(Type; NumericTypes) { describe("using an array of " ~ Type.stringof, { Type[] aList; Type[] anotherList; Type[] aListInOtherOrder; before({ static if(is(ifloat == Type) || is(idouble == Type) || is(ireal == Type)) { aList = [ cast(Type) 1i, cast(Type) 2i, cast(Type) 3i ]; aListInOtherOrder = [ cast(Type) 3i, cast(Type) 2i, cast(Type) 1i ]; anotherList = [ cast(Type) 2i, cast(Type) 3i ]; } else { aList = [ cast(Type) 1, cast(Type) 2, cast(Type) 3 ]; aListInOtherOrder = [ cast(Type) 3, cast(Type) 2, cast(Type) 1 ]; anotherList = [ cast(Type) 2, cast(Type) 3 ]; } }); it("should compare two exact arrays", { expect(aList).to.equal(aList); }); it("should be able to compare that two arrays are not equal", { expect(aList).to.not.equal(aListInOtherOrder); expect(aList).to.not.equal(anotherList); expect(anotherList).to.not.equal(aList); }); it("should throw a detailed error message when the two arrays are not equal", { auto msg = ({ expect(aList).to.equal(anotherList); }).should.throwException!TestException.msg.split("\n"); msg[0].strip.should.equal(aList.to!string ~ " should equal " ~ anotherList.to!string ~ "."); msg[1].strip.should.equal("Diff:"); static if(is(ifloat == Type) || is(idouble == Type) || is(ireal == Type)) { msg[2].strip.should.equal("[[+1i, ]2i, 3i]"); } else static if(is(cfloat == Type) || is(cdouble == Type) || is(creal == Type)) { msg[2].strip.should.equal("[[+1+0i, ]2+0i, 3+0i]"); } else { msg[2].strip.should.equal("[[+1, ]2, 3]"); } msg[4].strip.should.equal("Expected:" ~ anotherList.to!string); msg[5].strip.should.equal("Actual:" ~ aList.to!string); }); it("should throw an error when the arrays have the same values in a different order", { auto msg = ({ expect(aList).to.equal(aListInOtherOrder); }).should.throwException!TestException.msg.split("\n"); msg[0].strip.should.equal(aList.to!string ~ " should equal " ~ aListInOtherOrder.to!string ~ "."); msg[1].strip.should.equal("Diff:"); static if(is(ifloat == Type) || is(idouble == Type) || is(ireal == Type)) { msg[2].strip.should.equal("[[-3][+1]i, 2i, [-1][+3]i]"); } else static if(is(cfloat == Type) || is(cdouble == Type) || is(creal == Type)) { msg[2].strip.should.equal("[[-3][+1]+0i, 2+0i, [-1][+3]+0i]"); } else { msg[2].strip.should.equal("[[-3][+1], 2, [-1][+3]]"); } msg[4].strip.should.equal("Expected:" ~ aListInOtherOrder.to!string); msg[5].strip.should.equal("Actual:" ~ aList.to!string); }); it("should throw an error when the arrays should not be equal", { auto msg = ({ expect(aList).not.to.equal(aList); }).should.throwException!TestException.msg.split("\n"); msg[0].strip.should.startWith(aList.to!string ~ " should not equal " ~ aList.to!string ~ "."); static if(is(ifloat == Type) || is(idouble == Type) || is(ireal == Type)) { msg[2].strip.should.equal("Expected:not [1i, 2i, 3i]"); msg[3].strip.should.equal("Actual:[1i, 2i, 3i]"); } else static if(is(cfloat == Type) || is(cdouble == Type) || is(creal == Type)) { msg[2].strip.should.equal("Expected:not [1+0i, 2+0i, 3+0i]"); msg[3].strip.should.equal("Actual:[1+0i, 2+0i, 3+0i]"); } else { msg[2].strip.should.equal("Expected:not [1, 2, 3]"); msg[3].strip.should.equal("Actual:[1, 2, 3]"); } }); }); } describe("using an array of objects with opEquals", { Thing[] aList; Thing[] anotherList; Thing[] aListInOtherOrder; string strAList; string strAnotherList; string strAListInOtherOrder; before({ aList = [ new Thing(1), new Thing(2), new Thing(3) ]; aListInOtherOrder = [ new Thing(3), new Thing(2), new Thing(1) ]; anotherList = [ new Thing(2), new Thing(3) ]; strAList = SerializerRegistry.instance.niceValue(aList); strAnotherList = SerializerRegistry.instance.niceValue(anotherList); strAListInOtherOrder = SerializerRegistry.instance.niceValue(aListInOtherOrder); }); it("should compare two exact arrays", { expect(aList).to.equal(aList); }); it("should be able to compare that two arrays are not equal", { expect(aList).to.not.equal(aListInOtherOrder); expect(aList).to.not.equal(anotherList); expect(anotherList).to.not.equal(aList); }); it("should throw a detailed error message when the two arrays are not equal", { auto msg = ({ expect(aList).to.equal(anotherList); }).should.throwException!TestException.msg.split("\n"); msg[0].strip.should.equal(strAList.to!string ~ " should equal " ~ strAnotherList.to!string ~ "."); msg[1].strip.should.equal("Diff:"); msg[4].strip.should.equal("Expected:" ~ strAnotherList.to!string); msg[5].strip.should.equal("Actual:" ~ strAList.to!string); }); it("should throw an error when the arrays have the same values in a different order", { auto msg = ({ expect(aList).to.equal(aListInOtherOrder); }).should.throwException!TestException.msg.split("\n"); msg[0].strip.should.equal(strAList.to!string ~ " should equal " ~ strAListInOtherOrder ~ "."); msg[1].strip.should.equal("Diff:"); msg[4].strip.should.equal("Expected:" ~ strAListInOtherOrder); msg[5].strip.should.equal("Actual:" ~ strAList.to!string); }); it("should throw an error when the arrays should not be equal", { auto msg = ({ expect(aList).not.to.equal(aList); }).should.throwException!TestException.msg.split("\n"); msg[0].strip.should.startWith(strAList.to!string ~ " should not equal " ~ strAList.to!string ~ "."); msg[2].strip.should.equal("Expected:not " ~ strAList); msg[3].strip.should.equal("Actual:" ~ strAList); }); }); }); version(unittest) : class Thing { int x; this(int x) { this.x = x; } override bool opEquals(Object o) { if(typeid(this) != typeid(o)) return false; alias a = this; auto b = cast(typeof(this)) o; return a.x == b.x; } override string toString() { return x.to!string; } }