Coverage for manyworlds/step.py: 100%
31 statements
« prev ^ index » next coverage.py v7.3.0, created at 2023-08-25 16:44 +0200
« prev ^ index » next coverage.py v7.3.0, created at 2023-08-25 16:44 +0200
1"""Defines the Step Class and subclasses"""
3import re
4from typing import Optional, Literal
6from .data_table import DataTable
9class Step:
10 """A BDD scenario step"""
12 STEP_PATTERN: re.Pattern = re.compile(
13 "(?P<conjunction>Given|When|Then|And|But) (?P<name>[^#]+)(# (?P<comment>.+))?"
14 )
16 name: str
17 conjunction: Literal["Given", "When", "Then"]
18 data: Optional[DataTable]
19 comment: Optional[str]
21 """
22 re.Pattern
24 A conjunction ("Given", "When", ...), followed by an arbitrary string,
25 followed by an optional comment
26 """
28 def __init__(
29 self, name: str, data: Optional[DataTable] = None, comment: Optional[str] = None
30 ) -> None:
31 """Constructor method
33 Parameters
34 ----------
35 name : str
36 The name of the step
38 data : DataTable, optional
39 A data table
41 comment : str, optional
42 A comment
43 """
45 self.name = name.strip()
46 self.data = data
47 self.comment = comment
49 def format(self, first_of_type: bool = True) -> str:
50 """Returns a string representation of the Step instance
51 for feature file output.
53 Uses "And" as a conjunction if the step is not the first
54 of its type.
56 Parameters
57 ----------
58 first_of_type : bool
59 Whether or not the step is the first of it"s type
61 Returns
62 -------
63 str
64 String representation of the Step instance
65 """
67 return "{conjunction} {name}".format(
68 conjunction=self.conjunction if first_of_type else " And", name=self.name
69 )
71 def __str__(self) -> str:
72 """Return. a string representation of the Step instance
73 for terminal output.
75 Returns
76 -------
77 str
78 String representation of the Step instance
79 """
81 return "<{type}: {name}>".format(
82 type=self.__class__.__name__, name=(self.name[0].upper() + self.name[1:])
83 )
85 def __repr__(self) -> str:
86 """Return a string representation of the Step instance
87 for terminal output.
89 Returns
90 -------
91 str
92 String representation of the Step instance
93 """
95 return self.__str__()
98class Prerequisite(Step):
99 """A BDD scenario prerequisite ("Given") step"""
101 def __init__(
102 self, name: str, data: Optional[DataTable] = None, comment: Optional[str] = None
103 ) -> None:
104 self.conjunction = "Given"
105 super().__init__(name, data=data, comment=comment)
108class Action(Step):
109 """A BDD scenario action ("When") step"""
111 def __init__(
112 self, name: str, data: Optional[DataTable] = None, comment: Optional[str] = None
113 ) -> None:
114 self.conjunction = "When"
115 super().__init__(name, data=data, comment=comment)
118class Assertion(Step):
119 """A BDD scenario assertion ("Then") step"""
121 def __init__(
122 self, name: str, data: Optional[DataTable] = None, comment: Optional[str] = None
123 ) -> None:
124 self.conjunction = "Then"
125 super().__init__(name, data=data, comment=comment)