Skip to content

Commit

Permalink
checking if state name is an outcome
Browse files Browse the repository at this point in the history
  • Loading branch information
mgonzs13 committed Nov 8, 2024
1 parent 305a294 commit 53ffd21
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
3 changes: 2 additions & 1 deletion yasmin/include/yasmin/state_machine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ class StateMachine : public State {
* state.
* @param transitions A map of transitions where the key is the outcome
* and the value is the target state name.
* @throws std::logic_error If the state is already registered.
* @throws std::logic_error If the state is already registered or is an
* outcome.
* @throws std::invalid_argument If any transition has empty source or target,
* or references unregistered outcomes.
*/
Expand Down
6 changes: 6 additions & 0 deletions yasmin/src/yasmin/state_machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ void StateMachine::add_state(std::string name, std::shared_ptr<State> state,
"' already registered in the state machine");
}

if (std::find(this->outcomes.begin(), this->outcomes.end(), name) !=
this->outcomes.end()) {
throw std::logic_error("State name '" + name +
"' is already registered as an outcome");
}

for (auto it = transitions.begin(); it != transitions.end(); ++it) {
const std::string &key = it->first;
const std::string &value = it->second;
Expand Down
14 changes: 14 additions & 0 deletions yasmin/tests/python/test_state_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,20 @@ def test_add_repeated_state(self):
"\"State 'FOO' already registered in the state machine\"",
)

def test_add_outcome_state(self):
with self.assertRaises(KeyError) as context:
self.sm.add_state(
"outcome4",
FooState(),
transitions={
"outcome1": "BAR",
},
)
self.assertEqual(
str(context.exception),
"\"State name 'outcome4' is already registered as an outcome\"",
)

def test_add_state_with_wrong_outcome(self):
with self.assertRaises(KeyError) as context:
self.sm.add_state(
Expand Down
6 changes: 5 additions & 1 deletion yasmin/yasmin/state_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def add_state(
transitions (Dict[str, str], optional): A dictionary mapping source outcomes to target states. Defaults to None.
Raises:
KeyError: If the state name is already registered.
KeyError: If the state name is already registered or is an outcome.
ValueError: If transitions contain empty keys or values.
KeyError: If transitions reference unregistered outcomes.
"""
Expand All @@ -102,6 +102,10 @@ def add_state(
if name in self._states:
raise KeyError(f"State '{name}' already registered in the state machine")

# Check if state name is an outcome of the state machine
if name in self._outcomes:
raise KeyError(f"State name '{name}' is already registered as an outcome")

# Check the transitions
for key in transitions:
if not key:
Expand Down

0 comments on commit 53ffd21

Please sign in to comment.