Coverage for manyworlds/step.py: 100%

36 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-08-10 17:41 +0200

1"""Defines the Step Class and subclasses""" 

2import re 

3 

4class Step: 

5 """A BDD scenario step""" 

6 

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

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

9 

10 @classmethod 

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

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

13 conjunction = match['conjunction'] 

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

15 conjunction = previous_step.conjunction 

16 

17 step_class = { 

18 'Given': Prerequisite, 

19 'When': Action, 

20 'Then': Assertion 

21 }[conjunction] 

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

23 

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

25 """Constructor method 

26 

27 Parameters 

28 ---------- 

29 name : str 

30 The name of the step 

31 

32 data : list, optional 

33 list[dict]. List of dict representing a data table 

34 

35 comment : str, optional 

36 A comment 

37 

38 Returns 

39 ---------- 

40 None 

41 """ 

42 

43 self.name = name.strip() 

44 self.type = type 

45 self.data = data 

46 self.comment = comment 

47 

48 def format(self, first_of_type=True): 

49 """Return a string representation of the Scenario instance for feature file output 

50 

51 Returns 

52 ---------- 

53 str 

54 String representation of the Scenario instance 

55 """ 

56 

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

58 return conjunction + ' ' + self.name 

59 

60 def __str__(self): 

61 """Return a string representation of the Scenario instance for terminal output 

62 

63 Returns 

64 ---------- 

65 str 

66 String representation of the Scenario instance 

67 """ 

68 

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

70 

71 def __repr__(self): 

72 """Return a string representation of the Scenario instance for terminal output 

73 

74 Returns 

75 ---------- 

76 str 

77 String representation of the Scenario instance 

78 """ 

79 

80 return self.__str__() 

81 

82class Prerequisite(Step): 

83 """A BDD scenario prerequisite ("Given") step""" 

84 

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

86 self.conjunction = 'Given' 

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

88 

89class Action(Step): 

90 """A BDD scenario action ("When") step""" 

91 

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

93 self.conjunction = 'When' 

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

95 

96class Assertion(Step): 

97 """A BDD scenario assertion ("Then") step""" 

98 

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

100 self.conjunction = 'Then' 

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