Coverage for manyworlds/step.py: 100%

31 statements  

« prev     ^ index     » next       coverage.py v7.3.0, created at 2023-08-25 16:44 +0200

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

2 

3import re 

4from typing import Optional, Literal 

5 

6from .data_table import DataTable 

7 

8 

9class Step: 

10 """A BDD scenario step""" 

11 

12 STEP_PATTERN: re.Pattern = re.compile( 

13 "(?P<conjunction>Given|When|Then|And|But) (?P<name>[^#]+)(# (?P<comment>.+))?" 

14 ) 

15 

16 name: str 

17 conjunction: Literal["Given", "When", "Then"] 

18 data: Optional[DataTable] 

19 comment: Optional[str] 

20 

21 """ 

22 re.Pattern 

23 

24 A conjunction ("Given", "When", ...), followed by an arbitrary string, 

25 followed by an optional comment 

26 """ 

27 

28 def __init__( 

29 self, name: str, data: Optional[DataTable] = None, comment: Optional[str] = None 

30 ) -> None: 

31 """Constructor method 

32 

33 Parameters 

34 ---------- 

35 name : str 

36 The name of the step 

37 

38 data : DataTable, optional 

39 A data table 

40 

41 comment : str, optional 

42 A comment 

43 """ 

44 

45 self.name = name.strip() 

46 self.data = data 

47 self.comment = comment 

48 

49 def format(self, first_of_type: bool = True) -> str: 

50 """Returns a string representation of the Step instance 

51 for feature file output. 

52 

53 Uses "And" as a conjunction if the step is not the first 

54 of its type. 

55 

56 Parameters 

57 ---------- 

58 first_of_type : bool 

59 Whether or not the step is the first of it"s type 

60 

61 Returns 

62 ------- 

63 str 

64 String representation of the Step instance 

65 """ 

66 

67 return "{conjunction} {name}".format( 

68 conjunction=self.conjunction if first_of_type else " And", name=self.name 

69 ) 

70 

71 def __str__(self) -> str: 

72 """Return. a string representation of the Step instance 

73 for terminal output. 

74 

75 Returns 

76 ------- 

77 str 

78 String representation of the Step instance 

79 """ 

80 

81 return "<{type}: {name}>".format( 

82 type=self.__class__.__name__, name=(self.name[0].upper() + self.name[1:]) 

83 ) 

84 

85 def __repr__(self) -> str: 

86 """Return a string representation of the Step instance 

87 for terminal output. 

88 

89 Returns 

90 ------- 

91 str 

92 String representation of the Step instance 

93 """ 

94 

95 return self.__str__() 

96 

97 

98class Prerequisite(Step): 

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

100 

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) 

106 

107 

108class Action(Step): 

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

110 

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) 

116 

117 

118class Assertion(Step): 

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

120 

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)