Coverage for manyworlds/scenario.py: 100%
43 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-08-11 11:24 +0200
« prev ^ index » next coverage.py v7.2.7, created at 2023-08-11 11:24 +0200
1"""Defines the Scenario Class"""
2import re
3import igraph as ig
5from .step import Step, Prerequisite, Action, Assertion
7class Scenario:
8 """A BDD Scenario"""
10 def __init__(self, name, vertex):
11 """Constructor method
13 Parameters
14 ----------
15 name : str
16 The name of the scenario
18 vertex : igraph.Vertex
19 The vertex representing the scenario's position in a scenario tree
20 """
22 self.name = name.strip()
23 self.vertex = vertex
24 self.graph = vertex.graph
25 self.steps = []
26 self._validated = False
28 @property
29 def validated(self):
30 """The validated property"""
31 return self._validated
33 @validated.setter
34 def validated(self, value):
35 """The validated property setter"""
36 self._validated = value
38 def prerequisites(self):
39 """Return all steps of type Prerequisite
41 Returns
42 ----------
43 list
44 list[Prerequisite]. List of steps of type Prerequisite
45 """
47 return self.steps_of_type(Prerequisite)
49 def actions(self):
50 """Return all steps of type Action
52 Returns
53 ----------
54 list
55 list[Action]. List of steps of type Action
56 """
58 return self.steps_of_type(Action)
60 def assertions(self):
61 """Return all steps of type Assertion
63 Returns
64 ----------
65 list
66 list[Assertion]. List of steps of type Assertion
67 """
69 return self.steps_of_type(Assertion)
71 def steps_of_type(self, step_type):
72 """Return all steps of the supplied type
74 Parameters
75 ----------
76 step_class : {Prerequisite, Action, Assertion}
77 A step subclass
79 Returns
80 ----------
81 list
82 list[Step]. List of steps of type Assertion
83 """
85 return [st for st in self.steps if type(st) is step_type]
87 def __str__(self):
88 """Return a string representation of the Scenario instance for terminal output
90 Returns
91 ----------
92 str
93 String representation of the Scenario instance
94 """
96 return "<Scenario: {} ({} prerequisites, {} actions, {} assertions)>".format(self.name, len(self.prerequisites()), len(self.actions()), len(self.assertions()))
98 def __repr__(self):
99 """Return a string representation of the Scenario instance for terminal output
101 Returns
102 ----------
103 str
104 String representation of the Scenario instance
105 """
107 return self.__str__()
109 def ancestors(self):
110 """Return the scenario's ancestors, starting with the root scenario
112 Returns
113 ----------
114 list
115 list[Scenario]. List of scenarios
116 """
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)]
122 def path_scenarios(self):
123 """Return the complete scenario path from the root scenario to self
125 Returns
126 ----------
127 list
128 list[Scenario]. List of scenarios
129 """
131 return self.ancestors() + [self]
133 def organizational_only_ancestors(self):
134 """Return the scenario's ancestors that are organizational only
136 Returns
137 ----------
138 list
139 list[Scenario]. List of scenarios
140 """
142 return [sc for sc in self.ancestors() if sc.organizational_only()]
144 def level(self):
145 """Return the scenario's level in the scenario tree.
147 Level 1 = root scenario
149 Returns
150 ----------
151 int
152 The scenario's level
153 """
155 return self.graph.neighborhood_size(self.vertex, mode="IN", order=1000)
157 def organizational_only(self):
158 """Return whether the scenario is an 'organizational' scenario used for grouping only
160 Returns
161 ----------
162 bool
163 """
165 return len(self.assertions()) == 0
167 def name_with_breadcrumbs(self):
168 """Return a name of the Scenario prepended with 'breadcrumbs'
170 Breadcrumbs are the scenario's organizational ancestor names joined by ' > '
172 Returns
173 ----------
174 str
175 String representation of the Scenario instance
176 """
178 breadcrumbs = ''.join([sc.name + ' > ' for sc in self.organizational_only_ancestors()])
179 return breadcrumbs + self.name