Java has two forms of equality, corresponding to eql?
and equals?
in Ruby. Ruby additionally has two more forms of equality, ==
and ===
. As far as I can tell the follow contracts for each are expected:
a.eql? b
then it is assumed that a.hash == b.hash
. 1 == 1.0
). The probable contract is that the relationship is reflexive, symmetric, and transitive and that if a.eql? b
then a == b
.when
clause of a case
statement. This relationship can be much weaker — I haven't found an implied contract besides reflexivity. In particular a === b
does not mean that b === a
.The recipe noted in Junit Recipes applies pretty closely to a test of a modified eql?
method. I have yet to find tool equivalent to EqualsTester
— I may write one myself.
This recipe is virtually identical to the one in JUnit Recipes (namely test the side effects). The example code from the book can be written in Ruby as follows:
Again the general points made in JUnit Recipes apply to Ruby as well. The book show tqo examples (outside of the discussion), one where the constructor is checked using properties, and one where the constructor is checked using a validation method. See the section Testing-only attributes