Browse Source

Extended StateMachine class

- added "hasState" method
- extended tests for StateMachine class
Patrick-Laptop 4 years ago
parent
commit
cb4121a993

+ 10 - 5
source/logic/StateMachine.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-09-05
- * Changed:         2020-09-11
+ * Changed:         2020-09-16
  *
  * */
 
@@ -15,11 +15,11 @@ name(std::move(_name))
 {}
 
 bool ls_std::StateMachine::addState(const std::shared_ptr<State>& _state) {
-  bool condition = !this->_stateExists(_state->getId());
+  bool condition = !this->_hasState(_state->getId());
 
   if(condition) {
     this->states.insert({_state->getId(), _state});
-    condition = this->_stateExists(_state->getId());
+    condition = this->_hasState(_state->getId());
   }
 
   return condition;
@@ -37,6 +37,11 @@ std::string ls_std::StateMachine::getName() {
   return this->name;
 }
 
+bool ls_std::StateMachine::hasState(const StateId &_id)
+{
+  return this->_hasState(_id);
+}
+
 bool ls_std::StateMachine::proceed() {
   std::vector<ls_std::StateId> nextValidStates = this->_getNextValidStates();
   bool condition = nextValidStates.size() == 1;
@@ -50,7 +55,7 @@ bool ls_std::StateMachine::proceed() {
 }
 
 bool ls_std::StateMachine::setStartState(const ls_std::StateId&_id) {
-  bool exists = this->_stateExists(_id);
+  bool exists = this->_hasState(_id);
 
   if(exists) {
     this->currentState = this->states[_id];
@@ -76,6 +81,6 @@ void ls_std::StateMachine::_remember(const ls_std::StateId &_id) {
   this->memory.push_back(_id);
 }
 
-bool ls_std::StateMachine::_stateExists(const ls_std::StateId &_id) {
+bool ls_std::StateMachine::_hasState(const ls_std::StateId &_id) {
   return this->states.find(_id) != this->states.end();
 }

+ 3 - 2
source/logic/StateMachine.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-09-05
- * Changed:         2020-09-11
+ * Changed:         2020-09-16
  *
  * */
 
@@ -29,6 +29,7 @@ namespace ls_std {
       std::shared_ptr<State> getCurrentState();
       std::vector<ls_std::StateId> getMemory();
       std::string getName();
+      bool hasState(const StateId& _id);
       bool proceed();
       bool setStartState(const StateId& _id);
 
@@ -41,7 +42,7 @@ namespace ls_std {
 
       std::vector<StateId> _getNextValidStates();
       void _remember(const StateId& _id);
-      bool _stateExists(const StateId& _id);
+      bool _hasState(const StateId& _id);
   };
 }
 

+ 18 - 1
test/cases/logic/StateMachineTest.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-09-09
- * Changed:         2020-09-11
+ * Changed:         2020-09-16
  *
  * */
 
@@ -113,6 +113,23 @@ namespace {
     ASSERT_STREQ("test_machine", stateMachine.getName().c_str());
   }
 
+  TEST_F(StateMachineTest, hasState)
+  {
+    ls_std::StateMachine stateMachine = _createStateMachine();
+
+    ASSERT_TRUE(stateMachine.hasState("A"));
+    ASSERT_TRUE(stateMachine.hasState("B"));
+    ASSERT_TRUE(stateMachine.hasState("C"));
+    ASSERT_TRUE(stateMachine.hasState("D"));
+    ASSERT_TRUE(stateMachine.hasState("E"));
+  }
+
+  TEST_F(StateMachineTest, hasStateNegative)
+  {
+    ls_std::StateMachine stateMachine = _createStateMachine();
+    ASSERT_FALSE(stateMachine.hasState("F"));
+  }
+
   TEST_F(StateMachineTest, proceed)
   {
     ls_std::StateMachine stateMachine = _createStateMachine();