Coverage for manyworlds/step.py: 100%
36 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-08-10 17:41 +0200
« 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
4class Step:
5 """A BDD scenario step"""
7 step_pattern = r'(?P<conjunction>Given|When|Then|And|But) (?P<name>[^#]+)(# (?P<comment>.+))?'
8 table_pattern = r'\| ([^|]* +\|)+'
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
17 step_class = {
18 'Given': Prerequisite,
19 'When': Action,
20 'Then': Assertion
21 }[conjunction]
22 return step_class(match['name'], comment=match['comment'])
24 def __init__(self, name, data=None, comment=None):
25 """Constructor method
27 Parameters
28 ----------
29 name : str
30 The name of the step
32 data : list, optional
33 list[dict]. List of dict representing a data table
35 comment : str, optional
36 A comment
38 Returns
39 ----------
40 None
41 """
43 self.name = name.strip()
44 self.type = type
45 self.data = data
46 self.comment = comment
48 def format(self, first_of_type=True):
49 """Return a string representation of the Scenario instance for feature file output
51 Returns
52 ----------
53 str
54 String representation of the Scenario instance
55 """
57 conjunction = (self.conjunction if first_of_type else ' And')
58 return conjunction + ' ' + self.name
60 def __str__(self):
61 """Return a string representation of the Scenario instance for terminal output
63 Returns
64 ----------
65 str
66 String representation of the Scenario instance
67 """
69 return "<{}: {}>".format(self.__class__.__name__, (self.name[0].upper() + self.name[1:]))
71 def __repr__(self):
72 """Return a string representation of the Scenario instance for terminal output
74 Returns
75 ----------
76 str
77 String representation of the Scenario instance
78 """
80 return self.__str__()
82class Prerequisite(Step):
83 """A BDD scenario prerequisite ("Given") step"""
85 def __init__(self, name, data=None, comment=None):
86 self.conjunction = 'Given'
87 super().__init__(name, data=data, comment=comment)
89class Action(Step):
90 """A BDD scenario action ("When") step"""
92 def __init__(self, name, data=None, comment=None):
93 self.conjunction = 'When'
94 super().__init__(name, data=data, comment=comment)
96class Assertion(Step):
97 """A BDD scenario assertion ("Then") step"""
99 def __init__(self, name, data=None, comment=None):
100 self.conjunction = 'Then'
101 super().__init__(name, data=data, comment=comment)