tests/factory.cpp
Go to the documentation of this file.
1 // Copyright 2010 Thomas Moulard.
2 //
3 
4 #include <dynamic-graph/entity.h>
7 
8 #include <sstream>
9 
10 #define BOOST_TEST_MODULE factory
11 
12 #if BOOST_VERSION >= 105900
13 #include <boost/test/tools/output_test_stream.hpp>
14 #else
15 #include <boost/test/output_test_stream.hpp>
16 #endif
17 #include <boost/test/unit_test.hpp>
18 
19 using boost::test_tools::output_test_stream;
20 
21 namespace dynamicgraph {
22 class CustomEntity : public Entity {
23  public:
24  static const std::string CLASS_NAME;
25  virtual const std::string &getClassName() const { return CLASS_NAME; }
26  explicit CustomEntity(const std::string &n) : Entity(n) {}
27 };
28 const std::string CustomEntity::CLASS_NAME = "CustomEntity";
29 } // namespace dynamicgraph
30 
31 dynamicgraph::Entity *makeEntity(const std::string &objectName) {
32  return new dynamicgraph::CustomEntity(objectName);
33 }
34 
35 BOOST_AUTO_TEST_CASE(constructor) {
37 }
38 
39 BOOST_AUTO_TEST_CASE(registerEntity) {
41  &makeEntity);
42 
43  try {
45  &makeEntity);
46  BOOST_ERROR("Should never happen.");
47  } catch (const dynamicgraph::ExceptionFactory &exception) {
48  BOOST_CHECK_EQUAL(exception.getCode(),
50  }
51 
52  try {
54  BOOST_ERROR("Should never happen.");
55  } catch (const dynamicgraph::ExceptionFactory &exception) {
56  BOOST_CHECK_EQUAL(exception.getCode(),
58  }
59 }
60 
61 BOOST_AUTO_TEST_CASE(unregisterEntity) {
63 
64  try {
66  BOOST_ERROR("Should never happen.");
67  } catch (const dynamicgraph::ExceptionFactory &exception) {
68  BOOST_CHECK_EQUAL(exception.getCode(),
70  }
71 
72  try {
74  "I do not exist.");
75  BOOST_ERROR("Should never happen.");
76  } catch (const dynamicgraph::ExceptionFactory &exception) {
77  BOOST_CHECK_EQUAL(exception.getCode(),
79  }
80 }
81 
84  &makeEntity);
85 
86  {
87  boost::shared_ptr<dynamicgraph::Entity> entity(
88  dynamicgraph::FactoryStorage::getInstance()->newEntity("myEntity",
89  "foo"));
90 
91  boost::shared_ptr<dynamicgraph::Entity> entity2(
92  dynamicgraph::FactoryStorage::getInstance()->newEntity("myEntity",
93  "foo2"));
94 
95  boost::shared_ptr<dynamicgraph::Entity> entity3(
96  dynamicgraph::FactoryStorage::getInstance()->newEntity("myEntity", ""));
97  }
98 
99  try {
101  "");
102  BOOST_ERROR("Should never happen.");
103  } catch (const dynamicgraph::ExceptionFactory &exception) {
104  BOOST_CHECK_EQUAL(exception.getCode(),
106  }
107 
108  try {
111  // BOOST_ERROR ("Should never happen.");
112  } catch (const dynamicgraph::ExceptionFactory &exception) {
113  BOOST_CHECK_EQUAL(exception.getCode(),
115  }
116 }
117 
118 BOOST_AUTO_TEST_CASE(existEntity) {
119  // BOOST_CHECK (dynamicgraph::FactoryStorage::getInstance()->existEntity
120  // ("myEntity"));
121  BOOST_CHECK(
122  !dynamicgraph::FactoryStorage::getInstance()->existEntity("myEntity2"));
123  BOOST_CHECK(!dynamicgraph::FactoryStorage::getInstance()->existEntity(""));
124 }
void registerEntity(const std::string &entname, EntityConstructor_ptr ent)
Add a new entity to the factory.
This class represents an entity, i.e. a generic computational unit that provides input and output sig...
bool existEntity(const std::string &name) const
Check if an Entity associated with a particular name has already been registered. ...
void deregisterEntity(const std::string &entname)
Delete an entity from the factory.
static const std::string CLASS_NAME
BOOST_AUTO_TEST_CASE(constructor)
virtual const std::string & getClassName() const
int getCode() const
Access to the error code.
CustomEntity(const std::string &n)
Entity(const std::string &name)
Entity * newEntity(const std::string &classname, const std::string &objname) const
Instantiate (and allocate) an entity.
static FactoryStorage * getInstance()
Get pointer to unique object of the class.
static void destroy()
Destroy the unique instance of the class.
dynamicgraph::Entity * makeEntity(const std::string &objectName)


dynamic-graph
Author(s): Nicolas Mansard, Olivier Stasse
autogenerated on Sun Jun 25 2023 02:06:03