from transitions.core import *
from unittest import TestCase

class Stuff(object):

    def __init__(self):

        self.state = None

        states = ['A', 'B', 'C', 'D', 'E']
        self.machine = Machine(self, states=states, initial='A')

    def this_passes(self):
        return True

    def this_fails(self):
        return False


class TestClass(TestCase):

    def setUp(self):
        self.stuff = Stuff()

    def tearDown(self):
        pass

    def test_basic(self):
        s = self.stuff
        s.machine.add_transition('advance', 'A', 'B', conditions='this_passes')
        s.machine.add_transition('advance', 'B', 'C')
        s.machine.add_transition('advance', 'C', 'task')
        s.advance()
        self.assertEquals(s.state, 'B')
        self.assertFalse(s.is_A())
        self.assertTrue(s.is_B())
        s.advance()
        self.assertEquals(s.state, 'C')

    def test_multiple_add_transitions_from_state(self):
        s = self.stuff
        s.machine.add_transition('advance', 'A', 'B', conditions=['this_fails'])
        s.machine.add_transition('advance', 'A', 'C')
        s.advance()
        self.assertEquals(s.state, 'C')

    def test_use_machine_as_model(self):
        states = ['A', 'B', 'C', 'D']
        m = Machine(states=states, initial='A')
        m.add_transition('move', 'A', 'B')
        m.add_transition('move_to_C', 'B', 'C')
        m.move()
        self.assertEquals(m.state, 'B')



@@ -334,16 +334,149 @@
  False%0A%0A
+    def goodbye(self):%0A        self.message = %22So long, suckers!%22%0A%0A    def hello_world(self):%0A        self.message = %22Hello World!%22%0A%0A
 %0Aclass T
@@ -1566,9 +1566,495 @@
 , 'B')%0A%0A
+    def test_state_change_listeners(self):%0A        s = self.stuff%0A        s.machine.add_transition('advance', 'A', 'B')%0A        s.machine.add_transition('reverse', 'B', 'A')%0A        s.machine.on_enter_B('hello_world')%0A        s.machine.on_exit_B('goodbye')%0A        s.advance()%0A        self.assertEquals(s.state, 'B')%0A        self.assertEquals(s.message, 'Hello World!')%0A        s.reverse()%0A        self.assertEquals(s.state, 'A')%0A        self.assertTrue(s.message.startswith('So long'))
 %0A

test for state change listeners