Skip to content

Commit b058ecf

Browse files
committed
feat: Implement opEquals for Thing class and add unit tests for equality checks
1 parent 86550f2 commit b058ecf

File tree

1 file changed

+38
-0
lines changed
  • source/fluentasserts/operations/equality

1 file changed

+38
-0
lines changed

source/fluentasserts/operations/equality/equal.d

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,3 +969,41 @@ class EqualThing {
969969
return this.x == b.x;
970970
}
971971
}
972+
973+
class Thing {
974+
int x;
975+
this(int x) {
976+
this.x = x;
977+
}
978+
979+
override bool opEquals(Object o) {
980+
if (typeid(this) != typeid(o)) {
981+
return false;
982+
}
983+
auto b = cast(typeof(this)) o;
984+
return this.x == b.x;
985+
}
986+
}
987+
988+
@("opEquals honored for class objects with same field value")
989+
unittest {
990+
auto a1 = new Thing(1);
991+
auto b1 = new Thing(1);
992+
993+
assert(a1 == b1, "D's == operator should use opEquals");
994+
995+
auto evaluation = ({
996+
a1.should.equal(b1);
997+
}).recordEvaluation;
998+
999+
assert(evaluation.result.expected.length == 0, "opEquals should return true for objects with same x value, but got expected: " ~ evaluation.result.expected[]);
1000+
}
1001+
1002+
@("opEquals honored for class objects with different field values")
1003+
unittest {
1004+
auto a1 = new Thing(1);
1005+
auto a2 = new Thing(2);
1006+
1007+
assert(a1 != a2, "D's != operator should use opEquals");
1008+
a1.should.not.equal(a2);
1009+
}

0 commit comments

Comments
 (0)