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 }
dynamicgraph::FactoryStorage::destroy
static void destroy()
Destroy the unique instance of the class.
Definition: src/dgraph/factory.cpp:22
entity.h
dynamicgraph::CustomEntity::getClassName
virtual const std::string & getClassName() const
Definition: tests/factory.cpp:25
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(constructor)
Definition: tests/factory.cpp:35
dynamicgraph
dynamicgraph::Entity
This class represents an entity, i.e. a generic computational unit that provides input and output sig...
Definition: include/dynamic-graph/entity.h:52
dynamicgraph::ExceptionFactory::OBJECT_CONFLICT
@ OBJECT_CONFLICT
Definition: exception-factory.h:28
dynamicgraph::CustomEntity
Definition: command-test.cpp:36
dynamicgraph::CustomEntity::CustomEntity
CustomEntity(const std::string &n)
Definition: tests/factory.cpp:26
CustomEntity
Definition: custom-entity.cpp:22
dynamicgraph::FactoryStorage::registerEntity
void registerEntity(const std::string &entname, EntityConstructor_ptr ent)
Add a new entity to the factory.
Definition: src/dgraph/factory.cpp:34
dynamicgraph::ExceptionAbstract::getCode
int getCode() const
Access to the error code.
Definition: exception-abstract.cpp:24
dynamicgraph::ExceptionFactory
Generic error class.
Definition: exception-factory.h:18
dynamicgraph::Entity::Entity
Entity(const std::string &name)
Definition: src/dgraph/entity.cpp:33
exception-factory.h
dynamicgraph::FactoryStorage::deregisterEntity
void deregisterEntity(const std::string &entname)
Delete an entity from the factory.
Definition: src/dgraph/factory.cpp:61
dynamicgraph::ExceptionFactory::UNREFERED_OBJECT
@ UNREFERED_OBJECT
Definition: exception-factory.h:22
factory.h
dynamicgraph::CustomEntity::CLASS_NAME
static const std::string CLASS_NAME
Definition: command-test.cpp:38
dynamicgraph::FactoryStorage::existEntity
bool existEntity(const std::string &name) const
Check if an Entity associated with a particular name has already been registered.
Definition: src/dgraph/factory.cpp:95
makeEntity
dynamicgraph::Entity * makeEntity(const std::string &objectName)
Definition: tests/factory.cpp:31
dynamicgraph::FactoryStorage::newEntity
Entity * newEntity(const std::string &classname, const std::string &objname) const
Instantiate (and allocate) an entity.
Definition: src/dgraph/factory.cpp:79
dynamicgraph::FactoryStorage::getInstance
static FactoryStorage * getInstance()
Get pointer to unique object of the class.
Definition: src/dgraph/factory.cpp:15


dynamic-graph
Author(s): Nicolas Mansard, Olivier Stasse
autogenerated on Fri Aug 2 2024 08:36:38