New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proper Notification object comparison #41
base: master
Are you sure you want to change the base?
Conversation
|
||
def ==(other) | ||
self.to_bytes == other.to_bytes | ||
rescue |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of rescue
ing would something like this work?
other.respond_to?(:to_bytes) &&
self.to_bytes == other.to_bytes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
No because to_bytes can throw an exception if the payload is too large. That's why I did it this way. But I see what you mean. Perhaps we should actually do both. |
Oh yeah good call. That's got me thinking now. What if you want to check equality of 2 invalid notifications? Comparing instance variables might be better than the result of |
|
||
def ==(other) | ||
other.respond_to?(:to_bytes) && self.to_bytes == other.to_bytes | ||
rescue |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we limit this to only rescuing Grocer::PayloadTooLargeError
?
Added type comparison. Would love to see this merged so I can remove my local monkey patch. |
def ==(other) | ||
other.respond_to?(:to_bytes) && | ||
self.to_bytes == other.to_bytes && | ||
self.class == other.class |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if the Class
is the same, they should both respond_to?(:to_bytes)
, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If they're different, they might still both respond_to?(:to_bytes)
, which is why I make the check
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I'm saying is, you could simplify this to just be
def ==(other)
self.class === other &&
self.to_bytes == other.to_bytes
end
Agreed. Since to_bytes can raise when invalid, rendering any two invalid notifications NOT equal, even if they are... maybe we should be comparing the important state? For example, comparing their |
Yeah I agree that serializing the data is not important for equality. |
Oh wait... there is more to the state than just the payload... like the |
Comparing two objects that evaluate to the same byte representation should be 'true'. Particularly useful in tests/specs.