本文整理匯總了Python中statemachine.StateMachine.add_transition方法的典型用法代碼示例。如果您正苦於以下問題:Python StateMachine.add_transition方法的具體用法?Python StateMachine.add_transition怎麽用?Python StateMachine.add_transition使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類statemachine.StateMachine
的用法示例。
在下文中一共展示了StateMachine.add_transition方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: TestStateMachine
# 需要導入模塊: from statemachine import StateMachine [as 別名]
# 或者: from statemachine.StateMachine import add_transition [as 別名]
class TestStateMachine(object):
def setUp(self):
self.sm = StateMachine()
def test_can_transition_to_initial_state(self):
on_state_enter = call_counter()
self.sm.add_state("state1", on_enter=on_state_enter)
self.sm.tick()
assert on_state_enter.count == 1
def test_can_transition_to_second_state(self):
on_state2_enter = call_counter()
state1 = self.sm.add_state("state1")
state2 = self.sm.add_state("state2", on_enter=on_state2_enter)
self.sm.add_transition(state1, state2, "gone foobar")
self.sm.tick()
assert on_state2_enter.count == 0
self.sm.send_event("gone foobar")
self.sm.tick()
assert on_state2_enter.count == 1
def test_can_transition_to_itself(self):
on_state2_enter = call_counter()
state1 = self.sm.add_state("state1", on_enter=on_state2_enter)
self.sm.add_transition(state1, state1, "gone foobar")
self.sm.tick()
assert on_state2_enter.count == 1
self.sm.send_event("gone foobar")
self.sm.tick()
assert on_state2_enter.count == 2
def test_no_transition_until_state_done(self):
def on_state_enter():
return False
state1 = self.sm.add_state("state1", on_enter=on_state_enter)
state2 = self.sm.add_state("state2")
self.sm.add_transition(state1, state2, "gone foobar")
self.sm.tick()
self.sm.send_event("gone foobar")
self.sm.tick()
assert self.sm.current_state() == state1
self.sm.set_state_done()
self.sm.tick()
assert self.sm.current_state() == state2
def test_transition_triggers_callback(self):
on_transit = call_counter()
state1 = self.sm.add_state("state1")
state2 = self.sm.add_state("state2")
self.sm.add_transition(state1, state2, "gone foobar", on_transit=on_transit)
self.sm.tick()
assert on_transit.count == 0
self.sm.send_event("gone foobar")
self.sm.tick()
assert on_transit.count == 1