Skip to content
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

Channel Manager State Machine with Tests #59

Closed
nullcount opened this issue Dec 31, 2022 · 0 comments
Closed

Channel Manager State Machine with Tests #59

nullcount opened this issue Dec 31, 2022 · 0 comments

Comments

@nullcount
Copy link
Owner

example:

import unittest

class StateMachine:
    def __init__(self, initial_state):
        self.state = initial_state
    
    def transition(self, event):
        self.state = self.state.transition(event)
        
class State:
    def transition(self, event):
        raise NotImplementedError
        
class InitialState(State):
    def transition(self, event):
        if event == "start":
            return StartedState()
        return self
        
class StartedState(State):
    def transition(self, event):
        if event == "stop":
            return StoppedState()
        return self
        
class StoppedState(State):
    def transition(self, event):
        if event == "start":
            return StartedState()
        return self

class TestStateMachine(unittest.TestCase):
    def test_state_machine(self):
        machine = StateMachine(InitialState())
        
        # Test initial state
        self.assertEqual(machine.state.__class__, InitialState)
        
        # Test transition to StartedState
        machine.transition("start")
        self.assertEqual(machine.state.__class__, StartedState)
        
        # Test transition to StoppedState
        machine.transition("stop")
        self.assertEqual(machine.state.__class__, StoppedState)
        
        # Test transition back to StartedState
        machine.transition("start")
        self.assertEqual(machine.state.__class__, StartedState)

if __name__ == "__main__":
    unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant