Skip to content

Commit 7b0e409

Browse files
authored
Introduced Test Procedure (with "unittest" module) and Introduced Logging mechanism (with "logging" module) (#8)
* Introduced Test Procedure (with "unittest" module) Introduced Logging mechanism (with "logging" module) * Simple Update README
1 parent 870056f commit 7b0e409

File tree

8 files changed

+119
-110
lines changed

8 files changed

+119
-110
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Work in Progess
1313
## Example
1414
Work in Progess
1515

16+
## Test Suite
17+
Work in Progress
18+
1619
## How to contribute [![GitHub contributors](https://img.shields.io/github/contributors/ZigRazor/PyStateMachine.svg)](https://GitHub.com/ZigRazor/PyStateMachine/graphs/contributors/)
1720
Read the [CONTRIBUTING GUIDE](https://github.com/ZigRazor/PyStateMachine/blob/main/CONTRIBUTING.md)
1821
## Contact

sample/sample2.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
<ToState>Exit</ToState>
88
<PreConditions>
99
<Condition>
10-
<Expression>test2.everFalse</Expression>
10+
<Expression>testBaseStateMachine.everFalse</Expression>
1111
<Result>False</Result>
1212
</Condition>
1313
<Condition>
14-
<Expression>test2.test</Expression>
14+
<Expression>testBaseStateMachine.test</Expression>
1515
<Result>2</Result>
1616
</Condition>
1717
</PreConditions>
@@ -24,7 +24,7 @@
2424
<ToState>Null</ToState>
2525
<PreConditions>
2626
<Condition>
27-
<Expression>test2.everFalse</Expression>
27+
<Expression>testBaseStateMachine.everFalse</Expression>
2828
<Result>True</Result>
2929
</Condition>
3030
</PreConditions>

sample/sample3.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77
<ToState>Exit</ToState>
88
<PreConditions>
99
<Condition>
10-
<Expression>test3.everFalse</Expression>
10+
<Expression>testBaseStateMachine.everFalse</Expression>
1111
<Result>False</Result>
1212
</Condition>
1313
<Condition>
14-
<Expression>test3.test</Expression>
14+
<Expression>testBaseStateMachine.test</Expression>
1515
<Result>2</Result>
1616
</Condition>
1717
</PreConditions>
1818
<PreActions>
1919
<Action>
20-
<Expression>test3.testPrint</Expression>
20+
<Expression>testBaseStateMachine.testPrint</Expression>
2121
</Action>
2222
</PreActions>
2323
</Event>
@@ -29,7 +29,7 @@
2929
<ToState>Null</ToState>
3030
<PreConditions>
3131
<Condition>
32-
<Expression>test3.everFalse</Expression>
32+
<Expression>testBaseStateMachine.everFalse</Expression>
3333
<Result>True</Result>
3434
</Condition>
3535
</PreConditions>

src/StateMachine.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
from ReadStateMachine import ReadStateMachineFile
22
from State import State
33
from Event import Event
4+
import logging
45

56
class StateMachine:
67
def __init__(self, xml_file : str):
78
self.xml_file = xml_file
89
self.states = None
910
self.current_state = ""
1011
self.context = {}
12+
logging.basicConfig(filename=xml_file.split(sep=".xml")[0] + '.log',format='%(asctime)s - %(levelname)s - %(message)s', level=logging.DEBUG)
1113

1214
def __CheckConditions(self,conditions):
1315
all_conditions_satisfied = True
@@ -25,10 +27,10 @@ def __CheckConditions(self,conditions):
2527
all_conditions_satisfied = False
2628
break
2729
else:
28-
print("No Found Condition Expression ", condition.expression ," in Context")
30+
logging.error("No Found Condition Expression %s in Context", condition.expression)
2931
all_conditions_satisfied = False
3032
else:
31-
print("No Condition")
33+
logging.info("No Condition")
3234
return all_conditions_satisfied
3335

3436
def __ExecActions(self,actions):
@@ -44,21 +46,22 @@ def __ExecActions(self,actions):
4446
else:
4547
func
4648
else:
47-
print("No Found Action Expression ", action.expression ," in Context")
49+
logging.error("No Found Action Expression %s in Context", action.expression)
4850
all_action_executed = False;
4951
else:
50-
print("No Action")
52+
logging.info("No Action")
5153
return all_action_executed
5254

5355
def get_current_state(self):
5456
return self.current_state
5557

5658
def LoadStateMachine(self):
5759
if (self.states != None):
58-
print("State Machine already loaded")
60+
logging.error("State Machine already loaded")
5961
else:
6062
self.xml_file
6163
self.states , self.current_state = ReadStateMachineFile(self.xml_file)
64+
logging.info('State Machine Loaded')
6265

6366
def addVariableToContext(self, module : str, variable : str):
6467
mod = __import__(module)
@@ -77,14 +80,14 @@ def InjectEvent(self, event : str):
7780
all_pre_actions_executed = self.__ExecActions(handled_event.pre_actions)
7881
if(all_pre_actions_executed):
7982
## Transition
80-
print("Transition ", self.current_state, " ------> ", handled_event.to_state)
83+
logging.debug("Transition %s ------> %s", self.current_state, handled_event.to_state)
8184
self.current_state = handled_event.to_state
8285
## Postactions
8386
## Postconditions
8487
else:
85-
print("Not all PreConditions satisfied")
88+
logging.error("Not all PreConditions satisfied")
8689
else:
87-
print("Not a possible event")
90+
logging.error("Not a possible event")
8891

8992

9093

tests/test1.py

Lines changed: 0 additions & 25 deletions
This file was deleted.

tests/test2.py

Lines changed: 0 additions & 33 deletions
This file was deleted.

tests/test3.py

Lines changed: 0 additions & 37 deletions
This file was deleted.

tests/testBaseStateMachine.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import sys
2+
import unittest
3+
sys.path.append('../src')
4+
5+
from StateMachine import StateMachine
6+
7+
test = 2
8+
9+
def everFalse():
10+
return False
11+
12+
def testPrint():
13+
print("Test")
14+
15+
16+
class TestBaseStateMachine(unittest.TestCase):
17+
18+
def test1(self):
19+
20+
sm = StateMachine("../sample/sample1.xml")
21+
22+
sm.LoadStateMachine()
23+
self.assertEqual(sm.get_current_state(),"Enter", "Should be Enter")
24+
# OK Event
25+
sm.InjectEvent("ToExit")
26+
self.assertEqual(sm.get_current_state(),"Exit", "Should be Exit")
27+
# Not Possible Event
28+
sm.InjectEvent("ToExit")
29+
self.assertEqual(sm.get_current_state(),"Exit", "Should be Exit")
30+
# OK Event
31+
sm.InjectEvent("ToNull")
32+
self.assertEqual(sm.get_current_state(),"Null", "Should be Null")
33+
# Not Possible Event
34+
sm.InjectEvent("ToExit")
35+
self.assertEqual(sm.get_current_state(),"Null", "Should be Null")
36+
# OK Event (same state)
37+
sm.InjectEvent("ToNull")
38+
self.assertEqual(sm.get_current_state(),"Null", "Should be Null")
39+
40+
41+
def test2(self):
42+
43+
sm = StateMachine("../sample/sample2.xml")
44+
45+
sm.LoadStateMachine()
46+
47+
sm.addVariableToContext("testBaseStateMachine", "everFalse")
48+
sm.addVariableToContext("testBaseStateMachine", "test")
49+
50+
self.assertEqual(sm.get_current_state(),"Enter", "Should be Enter")
51+
# Precondition Verified
52+
sm.InjectEvent("ToExit")
53+
self.assertEqual(sm.get_current_state(),"Exit", "Should be Exit")
54+
# Not Valid Event
55+
sm.InjectEvent("ToExit")
56+
self.assertEqual(sm.get_current_state(),"Exit", "Should be Exit")
57+
58+
# Precondition Not Verified
59+
sm.InjectEvent("ToNull")
60+
self.assertEqual(sm.get_current_state(),"Exit", "Should be Exit")
61+
# Not Valid Event
62+
sm.InjectEvent("ToExit")
63+
self.assertEqual(sm.get_current_state(),"Exit", "Should be Exit")
64+
# Precondition Not Verified
65+
sm.InjectEvent("ToNull")
66+
self.assertEqual(sm.get_current_state(),"Exit", "Should be Exit")
67+
68+
def test3(self):
69+
sm = StateMachine("../sample/sample3.xml")
70+
71+
sm.LoadStateMachine()
72+
73+
sm.addVariableToContext("testBaseStateMachine", "everFalse")
74+
sm.addVariableToContext("testBaseStateMachine", "test")
75+
sm.addVariableToContext("testBaseStateMachine", "testPrint")
76+
77+
self.assertEqual(sm.get_current_state(),"Enter", "Should be Enter")
78+
# Precondition Verified / Execute PreAction
79+
sm.InjectEvent("ToExit")
80+
self.assertEqual(sm.get_current_state(),"Exit", "Should be Exit")
81+
# Not Valid Event
82+
sm.InjectEvent("ToExit")
83+
self.assertEqual(sm.get_current_state(),"Exit", "Should be Exit")
84+
85+
# Precondition Not Verified
86+
sm.InjectEvent("ToNull")
87+
self.assertEqual(sm.get_current_state(),"Exit", "Should be Exit")
88+
# Not Valid Event
89+
sm.InjectEvent("ToExit")
90+
self.assertEqual(sm.get_current_state(),"Exit", "Should be Exit")
91+
# Precondition Not Verified
92+
sm.InjectEvent("ToNull")
93+
self.assertEqual(sm.get_current_state(),"Exit", "Should be Exit")
94+
95+
96+
97+
if __name__ == '__main__':
98+
unittest.main()

0 commit comments

Comments
 (0)