summaryrefslogtreecommitdiff
path: root/cesar/maximus/python/test/test_fsm.py
blob: 598768eb28f83999f49bdeae92bc884ae4a80214 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python

print "\n*** " + __file__ + " ***\n"

import startup

from maximus.fsm import *


# FSM TEST

def create (fsm):
    fsm.something.append ("station")
    print "Create station"
def send (fsm):
    print "Send a message"
def receive (fsm):
    print "Receive the response"
def remove (fsm):
    fsm.something.pop ()
    print "Remove station"
def error (fsm):
    print "Error"
    print str(fsm.input_symbol)

def trace(fsm):
	print '=>',fsm.current_state,fsm.something

f = FSM.FSM('INIT',[])
f.set_default_transition (error, 'IDLE')
f.add_transition_any  ('INIT', None, 'END')
f.add_transition_any  ('IDLE', None, 'END')
f.add_transition_any  ('BUSY', None, 'END')
f.add_transition      ('create_station',  	'INIT',	create,     'IDLE')
f.add_transition      ('send_message',    	'IDLE',	send,   	'BUSY')
f.add_transition      ('receive_response',	'BUSY',	receive,	'IDLE')
f.add_transition      ('remove_station',	'IDLE',	remove,		'INIT')

trace(f)
f.process("create_station")
trace(f)
f.process("send_message")
trace(f)
f.process("receive_response")
trace(f)
f.process("remove_station")
trace(f)
f.process("I want to quit!")
trace(f)

while f.current_state!='END':
	inputs = raw_input ('>')
	previous_state = f.current_state
	f.process (inputs)
	print previous_state,'=>',f.current_state
	print f.something


# DOC TEST

import doctest
doctest.testmod(FSM)


# UNIT TEST

import unittest
suite = unittest.TestSuite()
try:
	suite.addTest(doctest.DocTestSuite(FSM))
except ValueError:
	print "has no tests"

if __name__ == '__main__':
    testResult = unittest.TextTestRunner(verbosity=2).run(suite)