From 6fc6337f2ecec03777a72a028a293ca7b3df73ec Mon Sep 17 00:00:00 2001
From: jvallve <jvallve@iri.upc.edu>
Date: Tue, 29 Mar 2022 10:56:36 +0200
Subject: [PATCH] added gtest for generic factory

---
 test/CMakeLists.txt                 |   5 ++
 test/dummy/dummy_object.h           | 115 ++++++++++++++++++++++++++++
 test/dummy/dummy_object_derived.cpp |  38 +++++++++
 test/dummy/dummy_object_derived.h   |  49 ++++++++++++
 test/dummy/factory_dummy_object.h   |  48 ++++++++++++
 test/gtest_factory.cpp              |  53 +++++++++++++
 6 files changed, 308 insertions(+)
 create mode 100644 test/dummy/dummy_object.h
 create mode 100644 test/dummy/dummy_object_derived.cpp
 create mode 100644 test/dummy/dummy_object_derived.h
 create mode 100644 test/dummy/factory_dummy_object.h
 create mode 100644 test/gtest_factory.cpp

diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 9479b9015..dd7732c47 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -34,6 +34,7 @@ target_link_libraries(gtest_example ${PLUGIN_NAME})      #
 set(SRC_DUMMY
   dummy/processor_tracker_feature_dummy.cpp
   dummy/processor_tracker_landmark_dummy.cpp
+  dummy/dummy_object_derived.cpp
   )
 add_library(dummy SHARED ${SRC_DUMMY})
 target_link_libraries(dummy ${PLUGIN_NAME})
@@ -64,6 +65,10 @@ target_link_libraries(gtest_factor_base ${PLUGIN_NAME})
 wolf_add_gtest(gtest_factor_autodiff gtest_factor_autodiff.cpp)
 target_link_libraries(gtest_factor_autodiff ${PLUGIN_NAME})
 
+# Factory test
+wolf_add_gtest(gtest_factory gtest_factory.cpp)
+target_link_libraries(gtest_factory ${PLUGIN_NAME} dummy)
+
 # FactoryStateBlock factory test
 wolf_add_gtest(gtest_factory_state_block gtest_factory_state_block.cpp)
 target_link_libraries(gtest_factory_state_block ${PLUGIN_NAME})
diff --git a/test/dummy/dummy_object.h b/test/dummy/dummy_object.h
new file mode 100644
index 000000000..ab865a144
--- /dev/null
+++ b/test/dummy/dummy_object.h
@@ -0,0 +1,115 @@
+//--------LICENSE_START--------
+//
+// Copyright (C) 2020,2021,2022 Institut de Robòtica i Informàtica Industrial, CSIC-UPC.
+// Authors: Joan Solà Ortega (jsola@iri.upc.edu)
+// All rights reserved.
+//
+// This file is part of WOLF
+// WOLF is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with this program.  If not, see <http://www.gnu.org/licenses/>.
+//
+//--------LICENSE_END--------
+#ifndef WOLF_DUMMY_OBJECT_H_
+#define WOLF_DUMMY_OBJECT_H_
+
+/**************************
+ *      WOLF includes     *
+ **************************/
+#include "core/common/wolf.h"
+#include <core/utils/params_server.h>
+#include "factory_dummy_object.h"
+
+namespace wolf {
+WOLF_PTR_TYPEDEFS(DummyObject);
+
+/*
+ * Macro for defining Autoconf DummyObject creator for WOLF's high level API.
+ *
+ * Place a call to this macro inside your class declaration (in the DummyObject_class.h file),
+ * preferably just after the constructors.
+ *
+ * In order to use this macro, the derived DummyObject class, DummyObjectClass,
+ * must have a constructor available with the API:
+ *
+ *   DummyObjectClass(const std::string& _unique_name,
+ *                   const ParamsServer& _server);
+ */
+#define WOLF_DUMMY_OBJECT_CREATE(DummyObjectClass)                                                 \
+static DummyObjectPtr create(const std::string& _unique_name,                                    \
+                            const ParamsServer& _server)                                               \
+{                                                                                               \
+    DummyObjectPtr sub = std::make_shared<DummyObjectClass>(_unique_name, _server);  \
+    return sub;                                                                                 \
+}                                                                                               \
+
+class DummyObject
+{
+    protected:
+        //wolf
+        std::string prefix_;
+        std::string name_;
+        std::string topic_;
+
+    public:
+        DummyObject(const std::string& _unique_name,
+                    const ParamsServer& _server) :
+            prefix_("ROS DummyObject/" + _unique_name),
+            name_(_unique_name)
+        {
+            //topic_  = _server.getParam<std::string>(prefix_ + "/topic");
+        }
+
+        virtual ~DummyObject(){};
+
+        //std::string getTopic() const;
+
+        std::string getName() const;
+
+        virtual void print() const = 0;
+
+    protected:
+
+        template<typename T>
+        T getParamWithDefault(const ParamsServer &_server,
+                              const std::string &_param_name,
+                              const T _default_value) const;
+};
+
+// inline std::string DummyObject::getTopic() const
+// {
+//     return topic_;
+// }
+
+inline std::string DummyObject::getName() const
+{
+    return name_;
+}
+
+// template<typename T>
+// inline T DummyObject::getParamWithDefault(const ParamsServer &_server,
+//                                           const std::string &_param_name,
+//                                           const T _default_value) const
+// {
+//     try
+//     {
+//         return _server.getParam<T>(_param_name);
+//     }
+//     catch (...)
+//     {
+//         WOLF_INFO("DummyObject: Parameter ", _param_name, " is missing. Taking default value: ", _default_value);
+//         return _default_value;
+//     }
+// }
+
+}
+#endif
diff --git a/test/dummy/dummy_object_derived.cpp b/test/dummy/dummy_object_derived.cpp
new file mode 100644
index 000000000..4bd2b7087
--- /dev/null
+++ b/test/dummy/dummy_object_derived.cpp
@@ -0,0 +1,38 @@
+//--------LICENSE_START--------
+//
+// Copyright (C) 2020,2021,2022 Institut de Robòtica i Informàtica Industrial, CSIC-UPC.
+// Authors: Joan Solà Ortega (jsola@iri.upc.edu)
+// All rights reserved.
+//
+// This file is part of WOLF
+// WOLF is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with this program.  If not, see <http://www.gnu.org/licenses/>.
+//
+//--------LICENSE_END--------
+#include "dummy_object_derived.h"
+
+namespace wolf
+{
+
+DummyObjectDerived::DummyObjectDerived(const std::string& _unique_name,
+                                       const ParamsServer& _server) ://,
+    DummyObject(_unique_name, _server)
+{
+}
+
+void DummyObjectDerived::print() const
+{
+    WOLF_INFO("DummyObjectDerived::print()");
+}
+
+}
\ No newline at end of file
diff --git a/test/dummy/dummy_object_derived.h b/test/dummy/dummy_object_derived.h
new file mode 100644
index 000000000..78bf5079f
--- /dev/null
+++ b/test/dummy/dummy_object_derived.h
@@ -0,0 +1,49 @@
+//--------LICENSE_START--------
+//
+// Copyright (C) 2020,2021,2022 Institut de Robòtica i Informàtica Industrial, CSIC-UPC.
+// Authors: Joan Solà Ortega (jsola@iri.upc.edu)
+// All rights reserved.
+//
+// This file is part of WOLF
+// WOLF is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with this program.  If not, see <http://www.gnu.org/licenses/>.
+//
+//--------LICENSE_END--------
+#ifndef DUMMY_OBJECT_DERIVED_H_
+#define DUMMY_OBJECT_DERIVED_H_
+
+/**************************
+ *      WOLF includes     *
+ **************************/
+#include "core/common/wolf.h"
+
+#include "dummy_object.h"
+
+namespace wolf {
+
+class DummyObjectDerived : public DummyObject
+{
+    public:
+        DummyObjectDerived(const std::string& _unique_name,
+                           const ParamsServer& _server);
+        WOLF_DUMMY_OBJECT_CREATE(DummyObjectDerived);
+        
+        void print() const override;
+
+        virtual ~DummyObjectDerived(){};
+};
+
+WOLF_REGISTER_DUMMY_OBJECT(DummyObjectDerived)
+}
+
+#endif
\ No newline at end of file
diff --git a/test/dummy/factory_dummy_object.h b/test/dummy/factory_dummy_object.h
new file mode 100644
index 000000000..34a215963
--- /dev/null
+++ b/test/dummy/factory_dummy_object.h
@@ -0,0 +1,48 @@
+//--------LICENSE_START--------
+//
+// Copyright (C) 2020,2021,2022 Institut de Robòtica i Informàtica Industrial, CSIC-UPC.
+// Authors: Joan Solà Ortega (jsola@iri.upc.edu)
+// All rights reserved.
+//
+// This file is part of WOLF
+// WOLF is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with this program.  If not, see <http://www.gnu.org/licenses/>.
+//
+//--------LICENSE_END--------
+#ifndef FACTORY_DUMMY_OBJECT_H_
+#define FACTORY_DUMMY_OBJECT_H_
+
+// wolf
+#include <core/common/factory.h>
+#include <core/utils/params_server.h>
+
+namespace wolf
+{
+
+class DummyObject;
+typedef Factory<DummyObject,
+                const std::string&,
+                const ParamsServer&> FactoryDummyObject;
+template<>
+inline std::string FactoryDummyObject::getClass() const
+{
+  return "FactoryDummyObject";
+}
+
+
+#define WOLF_REGISTER_DUMMY_OBJECT(DummyObjectType)                        \
+    namespace{ const bool WOLF_UNUSED DummyObjectType##Registered =      \
+            wolf::FactoryDummyObject::registerCreator(#DummyObjectType, DummyObjectType::create); } \
+
+} /* namespace wolf */
+#endif /* FACTORY_DUMMY_OBJECT_H_ */
diff --git a/test/gtest_factory.cpp b/test/gtest_factory.cpp
new file mode 100644
index 000000000..f46ee7506
--- /dev/null
+++ b/test/gtest_factory.cpp
@@ -0,0 +1,53 @@
+//--------LICENSE_START--------
+//
+// Copyright (C) 2020,2021,2022 Institut de Robòtica i Informàtica Industrial, CSIC-UPC.
+// Authors: Joan Solà Ortega (jsola@iri.upc.edu)
+// All rights reserved.
+//
+// This file is part of WOLF
+// WOLF is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with this program.  If not, see <http://www.gnu.org/licenses/>.
+//
+//--------LICENSE_END--------
+#include "core/utils/utils_gtest.h"
+#include "dummy/factory_dummy_object.h"
+#include "dummy/dummy_object.h"
+#include "dummy/dummy_object_derived.h"
+#include "core/yaml/parser_yaml.h"
+#include "core/utils/params_server.h"
+
+using namespace wolf;
+
+std::string wolf_root = _WOLF_ROOT_DIR;
+
+TEST(TestFactory, DummyObjectFactory)
+{
+  ParserYaml parser   = ParserYaml("test/yaml/params_basic.yaml", wolf_root);
+  ParamsServer server = ParamsServer(parser.getParams());
+
+  auto object = FactoryDummyObject::create("DummyObjectDerived","ACoolDummyObject", server);
+
+  object->getName();
+  object->print();
+
+  // FORCE LOADING
+  DummyObjectDerived obj_derived = DummyObjectDerived("AnotherCoolDummyObject", server);
+}
+
+int main(int argc, char **argv)
+{
+  testing::InitGoogleTest(&argc, argv);
+  //::testing::GTEST_FLAG(filter) = "TestTest.DummyTestExample"; // Test only this one
+  //::testing::GTEST_FLAG(filter) = "TestTest.*"; // Test only the tests in this group
+  return RUN_ALL_TESTS();
+}
-- 
GitLab