Coverage for manyworlds/scenario.py: 100%

1"""Defines the Scenario Class"""

2 import re

3 import igraph as ig

4

5 from .step import Step, Prerequisite, Action, Assertion

6

7 class Scenario:

8 """A BDD Scenario"""

9

10 def __init__(self, name, vertex):

11 """Constructor method

12

13 Parameters

14 ----------

15 name : str

16 The name of the scenario

17

18 vertex : igraph.Vertex

19 The vertex representing the scenario's position in a scenario tree

20 """

21

22 self.name = name.strip()

23 self.vertex = vertex

24 self.graph = vertex.graph

25 self.steps = []

26 self._validated = False

27

28 @property

29 def validated(self):

30 """The validated property"""

31 return self._validated

32

33 @validated.setter

34 def validated(self, value):

35 """The validated property setter"""

36 self._validated = value

37

38 def prerequisites(self):

39 """Return all steps of type Prerequisite

40

41 Returns

42 ----------

43 list

44 list[Prerequisite]. List of steps of type Prerequisite

45 """

46

47 return self.steps_of_type(Prerequisite)

48

49 def actions(self):

50 """Return all steps of type Action

51

52 Returns

53 ----------

54 list

55 list[Action]. List of steps of type Action

56 """

57

58 return self.steps_of_type(Action)

59

60 def assertions(self):

61 """Return all steps of type Assertion

62

63 Returns

64 ----------

65 list

66 list[Assertion]. List of steps of type Assertion

67 """

68

69 return self.steps_of_type(Assertion)

70

71 def steps_of_type(self, step_type):

72 """Return all steps of the supplied type

73

74 Parameters

75 ----------

76 step_class : {Prerequisite, Action, Assertion}

77 A step subclass

78

79 Returns

80 ----------

81 list

82 list[Step]. List of steps of type Assertion

83 """

84

85 return [st for st in self.steps if type(st) is step_type]

86

87 def __str__(self):

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

89

90 Returns

91 ----------

92 str

93 String representation of the Scenario instance

94 """

95

96 return "<Scenario: {} ({} prerequisites, {} actions, {} assertions)>".format(self.name, len(self.prerequisites()), len(self.actions()), len(self.assertions()))

97

98 def __repr__(self):

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

100

101 Returns

102 ----------

103 str

104 String representation of the Scenario instance

105 """

106

107 return self.__str__()

108

109 def ancestors(self):

110 """Return the scenario's ancestors, starting with the root scenario

111

112 Returns

113 ----------

114 list

115 list[Scenario]. List of scenarios

116 """

117

118 ancestors = self.graph.neighborhood(self.vertex, mode='IN', order=1000, mindist=1)

119 ancestors.reverse()

120 return [vx['scenario'] for vx in self.graph.vs(ancestors)]

121

122 def path_scenarios(self):

123 """Return the complete scenario path from the root scenario to self

124

125 Returns

126 ----------

127 list

128 list[Scenario]. List of scenarios

129 """

130

131 return self.ancestors() + [self]

132

133 def organizational_only_ancestors(self):

134 """Return the scenario's ancestors that are organizational only

135

136 Returns

137 ----------

138 list

139 list[Scenario]. List of scenarios

140 """

141

142 return [sc for sc in self.ancestors() if sc.organizational_only()]

143

144 def level(self):

145 """Return the scenario's level in the scenario tree.

146

147 Level 1 = root scenario

148

149 Returns

150 ----------

151 int

152 The scenario's level

153 """

154

155 return self.graph.neighborhood_size(self.vertex, mode="IN", order=1000)

156

157 def organizational_only(self):

158 """Return whether the scenario is an 'organizational' scenario used for grouping only

159

160 Returns

161 ----------

162 bool

163 """

164

165 return len(self.assertions()) == 0

166

167 def name_with_breadcrumbs(self):

168 """Return a name of the Scenario prepended with 'breadcrumbs'

169

170 Breadcrumbs are the scenario's organizational ancestor names joined by ' > '

171

172 Returns

173 ----------

174 str

175 String representation of the Scenario instance

176 """

177

178 breadcrumbs = ''.join([sc.name + ' > ' for sc in self.organizational_only_ancestors()])

179 return breadcrumbs + self.name