dwhall / farc

Framework for state machines with run-to-completion concurrency using asyncio. Python 3.4 or later
MIT License
28 stars 6 forks source link

Support comparing Event objects #8

Closed maxpeng closed 3 years ago

maxpeng commented 3 years ago

When I perform unit testing for the python code using farc, I may need to compare two different Event objects for assertion. So I propose to add __eq__ function to Event class for supporting that.

class Event(object):
    ...
    def __eq__(self, other):
        return self.signal == other.signal and self.value == other.value
maxpeng commented 3 years ago

From python - Compare object instances for equality by their attributes - Stack Overflow, this should be better:

class Event(object):
    ...
    def __eq__(self, other):
        if isinstance(other, Event):
            return self.signal == other.signal and self.value == other.value

        return False
dwhall commented 3 years ago

On one hand, I like this implementation because it is concise. On the other hand, I don't feel it is necessary; comparing signals should be sufficient. Can you tell me a situation or use case where comparing the values is important?

maxpeng commented 3 years ago

Assume the state machine forwards the received event to a handler. When we perform the unit testing, we can use a mock as the handler and assert the mock was called with given event. This requires the Event clas implement the __eq__ method.