Coverage for manyworlds/step.py: 95%

38 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-08-06 10:28 -0400

1"""Defines the ScenarioForest Class""" 

2import re 

3from igraph import Graph 

4import pdb 

5 

6class Step: 

7 

8 step_pattern = r'(?P<conjunction>Given|When|Then|And|But) (?P<name>[^#]+)(# (?P<comment>.+))?' 

9 table_pattern = r'| ([^|]* +|)+' 

10 

11 @classmethod 

12 def parse(cls, string, previous_step=None): 

13 match = re.compile(Step.step_pattern).match(string) 

14 conjunction = match['conjunction'] 

15 if conjunction in ['And', 'But']: 

16 conjunction = previous_step.conjunction 

17 

18 step_class = { 

19 'Given': Prerequisite, 

20 'When': Action, 

21 'Then': Assertion 

22 }[conjunction] 

23 return step_class(match['name'], comment=match['comment']) 

24 

25 def __init__(self, name, data=None, comment=None): 

26 """Constructor method 

27 """ 

28 self.name = name.strip() 

29 self.type = type 

30 self.data = data 

31 self.comment = comment 

32 

33 def format(self, first_of_type=True): 

34 conjunction = (self.conjunction if first_of_type else 'And') 

35 return conjunction + ' ' + self.name 

36 

37 def __str__(self): 

38 return "<{}: {}>".format(self.__class__.__name__, (self.name[0].upper() + self.name[1:])) 

39 

40 def __repr__(self): 

41 return self.__str__() 

42 

43class Prerequisite(Step): 

44 def __init__(self, name, data=None, comment=None): 

45 self.conjunction = 'Given' 

46 super().__init__(name, data=data, comment=comment) 

47 

48class Action(Step): 

49 def __init__(self, name, data=None, comment=None): 

50 self.conjunction = 'When' 

51 super().__init__(name, data=data, comment=comment) 

52 

53class Assertion(Step): 

54 def __init__(self, name, data=None, comment=None): 

55 self.conjunction = 'Then' 

56 super().__init__(name, data=data, comment=comment)