Bläddra i källkod

Add "ls_std_logic" header

This header file replaces the generic header file to remove unnecessary dependencies from "logic" module tests.
Patrick-Christopher Mattulat 2 år sedan
förälder
incheckning
1423f82800

+ 26 - 7
CMakeLists.txt

@@ -176,17 +176,14 @@ if (${LS_STD_BUILD_WITH_TESTS})
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/logic/serialization/SerializableJsonStateTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/logic/StateConnectionTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/logic/StateMachineTest.cpp
-            ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/logic/StateTest.cpp)
+            ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/logic/StateTest.cpp
+            ${CMAKE_CURRENT_SOURCE_DIR}/test/classes/logic/TestDataFactory.cpp)
 
     set(TEST_FILES_SERIALIZATION
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/serialization/JsonTest.cpp)
 
     set(TEST_FILES_TIME
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/time/DateTest.cpp)
-
-    set(TEST_FILES
-            ${CMAKE_CURRENT_SOURCE_DIR}/test/TestDataFactory.hpp
-            ${CMAKE_CURRENT_SOURCE_DIR}/test/TestDataFactory.cpp)
 endif ()
 
 ##########################################################
@@ -225,6 +222,15 @@ if (${LS_STD_BUILD_WITH_TESTS})
     add_executable(${MODULE_NAME_IO}_test ${TEST_FILES_IO})
 endif ()
 
+##########################################################
+# Build Tests (logic)
+##########################################################
+
+if (${LS_STD_BUILD_WITH_TESTS})
+    message("${MODULE_NAME_LOGIC}: Building tests...")
+    add_executable(${MODULE_NAME_LOGIC}_test ${TEST_FILES_LOGIC})
+endif ()
+
 ##########################################################
 # Build Tests
 ##########################################################
@@ -238,8 +244,7 @@ if (${LS_STD_BUILD_WITH_TESTS})
             ${TEST_FILES_IO}
             ${TEST_FILES_LOGIC}
             ${TEST_FILES_SERIALIZATION}
-            ${TEST_FILES_TIME}
-            ${TEST_FILES})
+            ${TEST_FILES_TIME})
 endif ()
 
 ##########################################################
@@ -373,6 +378,20 @@ if (${LS_STD_BUILD_WITH_TESTS})
             "${MODULE_NAME_CORE}")
 endif ()
 
+##########################################################
+# Linking (logic)
+##########################################################
+
+if (${LS_STD_BUILD_WITH_TESTS})
+    message("${MODULE_NAME_LOGIC}: Linking libraries for test application...")
+    target_link_libraries(${MODULE_NAME_LOGIC}_test
+            gtest
+            gmock
+            gtest_main
+            "${MODULE_NAME_LOGIC}"
+            "${MODULE_NAME_CORE}")
+endif ()
+
 ##########################################################
 # Linking
 ##########################################################

+ 20 - 0
include/ls_std/ls_std_logic.hpp

@@ -0,0 +1,20 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2022-05-15
+ * Changed:         2022-05-15
+ *
+ * */
+
+#ifndef LS_STD_LS_STD_LOGIC_HPP
+#define LS_STD_LS_STD_LOGIC_HPP
+
+#include <ls_std/logic/serialization/SerializableJsonState.hpp>
+#include <ls_std/logic/serialization/SerializableJsonStateConnection.hpp>
+#include <ls_std/logic/serialization/SerializableJsonStateMachine.hpp>
+#include <ls_std/logic/State.hpp>
+#include <ls_std/logic/StateConnection.hpp>
+#include <ls_std/logic/StateMachine.hpp>
+
+#endif

+ 0 - 72
test/TestDataFactory.cpp

@@ -1,72 +0,0 @@
-/*
- * Author:          Patrick-Christopher Mattulat
- * Company:         Lynar Studios
- * E-Mail:          webmaster@lynarstudios.com
- * Created:         2020-09-16
- * Changed:         2022-05-11
- *
- * */
-
-#include "TestDataFactory.hpp"
-
-ls::std::logic::StateMachine ls_std_test::TestDataFactory::createStateMachine()
-{
-  ls::std::logic::StateMachine stateMachine{"test_machine"};
-
-  std::shared_ptr<ls::std::logic::State> stateA = std::make_shared<ls::std::logic::State>("A");
-  std::shared_ptr<ls::std::logic::State> stateB = std::make_shared<ls::std::logic::State>("B");
-  std::shared_ptr<ls::std::logic::State> stateC = std::make_shared<ls::std::logic::State>("C");
-  std::shared_ptr<ls::std::logic::State> stateD = std::make_shared<ls::std::logic::State>("D");
-  std::shared_ptr<ls::std::logic::State> stateE = std::make_shared<ls::std::logic::State>("E");
-
-  // add states
-
-  stateMachine.addState(stateA);
-  stateMachine.addState(stateB);
-  stateMachine.addState(stateC);
-  stateMachine.addState(stateD);
-  stateMachine.addState(stateE);
-
-  // add connections / see state_machine_test.png
-
-  stateA->addStateConnection("AB", stateB);
-  stateB->addStateConnection("BC", stateC);
-  stateB->addStateConnection("BD", stateD);
-  stateC->addStateConnection("CB", stateB);
-  stateC->addStateConnection("CE", stateE);
-  stateD->addStateConnection("DE", stateE);
-
-  return stateMachine;
-}
-
-std::shared_ptr<ls::std::io::XmlNode> ls_std_test::TestDataFactory::createXmlContent()
-{
-  std::shared_ptr<ls::std::io::XmlNode> root = std::make_shared<ls::std::io::XmlNode>("dialog");
-  std::shared_ptr<ls::std::io::XmlAttribute> attribute{};
-  std::shared_ptr<ls::std::io::XmlNode> child{};
-  std::shared_ptr<ls::std::io::XmlNode> text{};
-
-  attribute = std::make_shared<ls::std::io::XmlAttribute>("name");
-  attribute->setValue("dungeon_001");
-  root->addAttributeToEnd(attribute);
-
-  child = std::make_shared<ls::std::io::XmlNode>("dialogUnit");
-  attribute = std::make_shared<ls::std::io::XmlAttribute>("id");
-  attribute->setValue("001");
-  child->addAttributeToEnd(attribute);
-  text = std::make_shared<ls::std::io::XmlNode>("text");
-  text->setValue("Hello!");
-  child->addChildToEnd(text);
-  root->addChildToEnd(child);
-
-  child = std::make_shared<ls::std::io::XmlNode>("dialogUnit");
-  attribute = std::make_shared<ls::std::io::XmlAttribute>("id");
-  attribute->setValue("002");
-  child->addAttributeToEnd(attribute);
-  text = std::make_shared<ls::std::io::XmlNode>("text");
-  text->setValue("Hello again!");
-  child->addChildToEnd(text);
-  root->addChildToEnd(child);
-
-  return root;
-}

+ 51 - 3
test/TestHelper.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-15
- * Changed:         2022-05-15
+ * Changed:         2022-05-16
  *
  * */
 
@@ -17,6 +17,7 @@
 #include <sstream>
 #include <climits>
 #include <unistd.h>
+#include <fstream>
 
 namespace ls_std_test
 {
@@ -27,12 +28,12 @@ namespace ls_std_test
       TestHelper() = default;
       ~TestHelper() = default;
 
-      static std::string getResourcesFolderLocation()
+      static ::std::string getResourcesFolderLocation()
       {
         return TestHelper::getTestFolderLocation() + "resources" + ls_std_test::TestHelper::_getFilePathSeparator();
       }
 
-      static std::string getTestFolderLocation()
+      static ::std::string getTestFolderLocation()
       {
         ::std::string buildDirectory = ls_std_test::TestHelper::_getWorkingDirectory();
         buildDirectory = ls_std_test::TestHelper::_normalizePath(buildDirectory);
@@ -40,8 +41,39 @@ namespace ls_std_test
         return ls_std_test::TestHelper::_getParent(buildDirectory) + "test" + ls_std_test::TestHelper::_getFilePathSeparator();
       }
 
+      static ::std::string normalize(const ::std::string &_path)
+      {
+        return ls_std_test::TestHelper::_normalizePath(_path);
+      }
+
+      static ::std::string readFile(const ::std::string &_absoluteFilePath)
+      {
+        char *data;
+        ::std::ifstream inputStream{_absoluteFilePath, ::std::ifstream::binary};
+        int length = (int) ls_std_test::TestHelper::_getFileSize(_absoluteFilePath);
+        data = new ls::std::core::type::byte[length];
+        inputStream.read(data, length);
+
+        if (inputStream.fail())
+        {
+          throw ::std::runtime_error("invalid file operation!");
+        }
+
+        inputStream.close();
+        ls::std::core::type::byte_field readData = ls::std::core::type::byte_field{data, (size_t) ls_std_test::TestHelper::_getFileSize(_absoluteFilePath)};
+        delete[] data;
+
+        return readData;
+      }
+
     private:
 
+      static bool _fileExists(const ::std::string &_path)
+      {
+        struct stat _stat{};
+        return (stat(_path.c_str(), &_stat) == 0);
+      }
+
       static char _getFilePathSeparator()
       {
         char separator;
@@ -56,6 +88,22 @@ namespace ls_std_test
         return separator;
       }
 
+      static long _getFileSize(const ::std::string &_absoluteFilePath)
+      {
+        ::std::streampos fileSize{};
+
+        if (ls_std_test::TestHelper::_fileExists(_absoluteFilePath))
+        {
+          ::std::ifstream fileHandler{_absoluteFilePath, ::std::ios::in};
+          fileSize = fileHandler.tellg();
+          fileHandler.seekg(0, ::std::ios::end);
+          fileSize = fileHandler.tellg() - fileSize;
+          fileHandler.close();
+        }
+
+        return (long) fileSize;
+      }
+
       static std::string _getParent(const ::std::string &_path)
       {
         ::std::string parent{};

+ 3 - 2
test/cases/logic/StateConnectionTest.cpp

@@ -3,12 +3,13 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-09-10
- * Changed:         2022-05-11
+ * Changed:         2022-05-15
  *
  * */
 
 #include <gtest/gtest.h>
-#include <ls_std/ls_std.hpp>
+#include <ls_std/ls_std_core.hpp>
+#include <ls_std/ls_std_logic.hpp>
 
 namespace
 {

+ 6 - 5
test/cases/logic/StateMachineTest.cpp

@@ -3,13 +3,14 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-09-09
- * Changed:         2022-05-12
+ * Changed:         2022-05-15
  *
  * */
 
 #include <gtest/gtest.h>
-#include <ls_std/ls_std.hpp>
-#include <TestDataFactory.hpp>
+#include <ls_std/ls_std_core.hpp>
+#include <ls_std/ls_std_logic.hpp>
+#include <ls_std_logic_test.hpp>
 
 namespace
 {
@@ -103,7 +104,7 @@ namespace
 
   TEST_F(StateMachineTest, hasState)
   {
-    ls::std::logic::StateMachine stateMachine = ls_std_test::TestDataFactory::createStateMachine();
+    ls::std::logic::StateMachine stateMachine = ls_std_logic_test::TestDataFactory::createStateMachine();
 
     ASSERT_TRUE(stateMachine.hasState("A"));
     ASSERT_TRUE(stateMachine.hasState("B"));
@@ -120,7 +121,7 @@ namespace
 
   TEST_F(StateMachineTest, proceed)
   {
-    ls::std::logic::StateMachine stateMachine = ls_std_test::TestDataFactory::createStateMachine();
+    ls::std::logic::StateMachine stateMachine = ls_std_logic_test::TestDataFactory::createStateMachine();
     ASSERT_STREQ("test_machine", stateMachine.getName().c_str());
     ASSERT_TRUE(stateMachine.setStartState("A"));
 

+ 3 - 2
test/cases/logic/StateTest.cpp

@@ -3,13 +3,14 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-09-05
- * Changed:         2022-05-11
+ * Changed:         2022-05-15
  *
  * */
 
 #include <gtest/gtest.h>
 #include <memory>
-#include <ls_std/ls_std.hpp>
+#include <ls_std/ls_std_core.hpp>
+#include <ls_std/ls_std_logic.hpp>
 
 namespace
 {

+ 9 - 7
test/cases/logic/serialization/SerializableJsonStateConnectionTest.cpp

@@ -3,12 +3,13 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-09-14
- * Changed:         2022-05-11
+ * Changed:         2022-05-15
  *
  * */
 
 #include <gtest/gtest.h>
-#include <ls_std/ls_std.hpp>
+#include <ls_std/ls_std_core.hpp>
+#include <ls_std/ls_std_logic.hpp>
 
 namespace
 {
@@ -46,9 +47,9 @@ namespace
   {
     ls::std::logic::StateConnection x{"AB", "B"};
     ls::std::logic::SerializableJsonStateConnection serializable{std::make_shared<ls::std::logic::StateConnection>(x)};
-    ls::std::boxing::String jsonString{serializable.marshal()};
+    ::std::string jsonString{serializable.marshal()};
 
-    ASSERT_STREQ(R"({"condition":false,"connectionId":"AB","stateId":"B"})", jsonString.toString().c_str());
+    ASSERT_STREQ(R"({"condition":false,"connectionId":"AB","stateId":"B"})", jsonString.c_str());
   }
 
   TEST_F(SerializableJsonStateConnectionTest, unmarshal)
@@ -77,9 +78,9 @@ namespace
   {
     ls::std::logic::StateConnection x{"AB", "B"};
     ls::std::logic::SerializableJsonStateConnection serializable{std::make_shared<ls::std::logic::StateConnection>(x)};
-    ls::std::boxing::String jsonString{serializable.marshal()};
+    ::std::string jsonString{serializable.marshal()};
 
-    ASSERT_STREQ(R"({"condition":false,"connectionId":"AB","stateId":"B"})", jsonString.toString().c_str());
+    ASSERT_STREQ(R"({"condition":false,"connectionId":"AB","stateId":"B"})", jsonString.c_str());
 
     // set value should now reset json
 
@@ -87,7 +88,8 @@ namespace
     serializable.setValue(std::make_shared<ls::std::logic::StateConnection>(y));
     jsonString = serializable.marshal();
 
-    ASSERT_STREQ(R"({"condition":false,"connectionId":"BC","stateId":"C"})", jsonString.toString().c_str());
+    ASSERT_STREQ(R"({"condition":false,"connectionId":"BC","stateId":"C"})", jsonString.c_str());
+    //I love YOU
   }
 
   TEST_F(SerializableJsonStateConnectionTest, setValue_no_parameter_set)

+ 13 - 11
test/cases/logic/serialization/SerializableJsonStateMachineTest.cpp

@@ -3,13 +3,14 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-09-17
- * Changed:         2022-05-15
+ * Changed:         2022-05-16
  *
  * */
 
 #include <gtest/gtest.h>
-#include <ls_std/ls_std.hpp>
-#include <TestDataFactory.hpp>
+#include <ls_std/ls_std_core.hpp>
+#include <ls_std/ls_std_logic.hpp>
+#include <ls_std_logic_test.hpp>
 #include "TestHelper.hpp"
 
 namespace
@@ -46,7 +47,7 @@ namespace
 
   TEST_F(SerializableJsonStateMachineTest, marshal)
   {
-    ls::std::logic::StateMachine stateMachine = ls_std_test::TestDataFactory::createStateMachine();
+    ls::std::logic::StateMachine stateMachine = ls_std_logic_test::TestDataFactory::createStateMachine();
     stateMachine.setStartState("A");
     stateMachine.setMemory({"A", "B", "C"});
     ls::std::logic::SerializableJsonStateMachine serializable{std::make_shared<ls::std::logic::StateMachine>(stateMachine)};
@@ -54,21 +55,22 @@ namespace
     std::string jsonString = serializable.marshal();
     ASSERT_TRUE(!jsonString.empty());
 
-    ls::std::io::File file{ls_std_test::TestHelper::getResourcesFolderLocation() + "/state_machine_test.json"};
-    ls::std::io::FileReader reader{file};
-    ls::std::boxing::String data{reader.read()};
+    ::std::string path = ls_std_test::TestHelper::getResourcesFolderLocation() + "/state_machine_test.json";
+    path = ls_std_test::TestHelper::normalize(path);
+    ::std::string content = ls_std_test::TestHelper::readFile(path);
 
-    ASSERT_TRUE(data.contains(jsonString));
+    ASSERT_TRUE(content.find(jsonString) != ::std::string::npos);
   }
 
   TEST_F(SerializableJsonStateMachineTest, unmarshal)
   {
-    ls::std::io::File file{ls_std_test::TestHelper::getResourcesFolderLocation() + "/state_machine_test.json"};
-    ls::std::io::FileReader reader{file};
+    ::std::string path = ls_std_test::TestHelper::getResourcesFolderLocation() + "/state_machine_test.json";
+    path = ls_std_test::TestHelper::normalize(path);
+
     std::shared_ptr<ls::std::logic::StateMachine> machine = std::make_shared<ls::std::logic::StateMachine>("bla");
     ls::std::logic::SerializableJsonStateMachine serializable{machine};
 
-    serializable.unmarshal(reader.read());
+    serializable.unmarshal(ls_std_test::TestHelper::readFile(path));
 
     ASSERT_STREQ("test_machine", machine->getName().c_str());
 

+ 3 - 2
test/cases/logic/serialization/SerializableJsonStateTest.cpp

@@ -3,12 +3,13 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-09-15
- * Changed:         2022-05-12
+ * Changed:         2022-05-15
  *
  * */
 
 #include <gtest/gtest.h>
-#include <ls_std/ls_std.hpp>
+#include <ls_std/ls_std_core.hpp>
+#include <ls_std/ls_std_logic.hpp>
 
 namespace
 {

+ 40 - 0
test/classes/logic/TestDataFactory.cpp

@@ -0,0 +1,40 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-09-16
+ * Changed:         2022-05-15
+ *
+ * */
+
+#include "TestDataFactory.hpp"
+
+ls::std::logic::StateMachine ls_std_logic_test::TestDataFactory::createStateMachine()
+{
+  ls::std::logic::StateMachine stateMachine{"test_machine"};
+
+  std::shared_ptr<ls::std::logic::State> stateA = std::make_shared<ls::std::logic::State>("A");
+  std::shared_ptr<ls::std::logic::State> stateB = std::make_shared<ls::std::logic::State>("B");
+  std::shared_ptr<ls::std::logic::State> stateC = std::make_shared<ls::std::logic::State>("C");
+  std::shared_ptr<ls::std::logic::State> stateD = std::make_shared<ls::std::logic::State>("D");
+  std::shared_ptr<ls::std::logic::State> stateE = std::make_shared<ls::std::logic::State>("E");
+
+  // add states
+
+  stateMachine.addState(stateA);
+  stateMachine.addState(stateB);
+  stateMachine.addState(stateC);
+  stateMachine.addState(stateD);
+  stateMachine.addState(stateE);
+
+  // add connections / see state_machine_test.png
+
+  stateA->addStateConnection("AB", stateB);
+  stateB->addStateConnection("BC", stateC);
+  stateB->addStateConnection("BD", stateD);
+  stateC->addStateConnection("CB", stateB);
+  stateC->addStateConnection("CE", stateE);
+  stateD->addStateConnection("DE", stateE);
+
+  return stateMachine;
+}

+ 3 - 4
test/TestDataFactory.hpp → test/classes/logic/TestDataFactory.hpp

@@ -10,11 +10,11 @@
 #ifndef LS_STD_TEST_DATA_FACTORY_HPP
 #define LS_STD_TEST_DATA_FACTORY_HPP
 
-#include <ls_std/ls_std.hpp>
+#include <ls_std/ls_std_logic.hpp>
 
-namespace ls_std_test
+namespace ls_std_logic_test
 {
-  class TestDataFactory // TODO: remove class, after migrated to submodules
+  class TestDataFactory
   {
     public:
 
@@ -22,7 +22,6 @@ namespace ls_std_test
       ~TestDataFactory() = default;
 
       static ls::std::logic::StateMachine createStateMachine();
-      static std::shared_ptr<ls::std::io::XmlNode> createXmlContent();
   };
 }
 

+ 15 - 0
test/ls_std_logic_test.hpp

@@ -0,0 +1,15 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2022-05-15
+ * Changed:         2022-05-15
+ *
+ * */
+
+#ifndef LS_STD_LS_STD_LOGIC_TEST_HPP
+#define LS_STD_LS_STD_LOGIC_TEST_HPP
+
+#include <classes/logic/TestDataFactory.hpp>
+
+#endif

+ 1 - 1
test/ls_std_test.hpp

@@ -9,7 +9,7 @@
 
 #ifndef LS_STD_LS_STD_TEST_HPP
 #define LS_STD_LS_STD_TEST_HPP
-
+// TODO: file can be removed
 #include "classes/base/ClassWrapper.hpp"
 
 #include "classes/event/NewsAgency.hpp"