1 /*
2  * Copyright 2009-2020 The VOTCA Development Team (http://www.votca.org)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 #include <boost/test/tools/old/interface.hpp>
19 #include <stdexcept>
20 #define BOOST_TEST_MAIN
21 
22 #define BOOST_TEST_MODULE name_test
23 
24 // Standard includes
25 #include <exception>
26 #include <string>
27 
28 // Third party includes
29 #include <boost/test/unit_test.hpp>
30 
31 // Local VOTCA includes
32 #include "votca/tools/objectfactory.h"
33 
34 using namespace std;
35 using namespace votca::tools;
36 
37 BOOST_AUTO_TEST_SUITE(objectfactory_test)
38 
39 class base {
40 
41  public:
42   base() = default;
43   virtual ~base() = default;
44 
45   virtual std::string identify() = 0;
46 };
47 
48 class A : public base {
49  public:
identify()50   std::string identify() override { return "A"; }
51 };
52 
53 class B : public base {
54  public:
identify()55   std::string identify() override { return "B"; }
56 };
57 
BOOST_AUTO_TEST_CASE(construction_test)58 BOOST_AUTO_TEST_CASE(construction_test) {
59 
60   ObjectFactory<std::string, base> factory;
61 
62   factory.Register<A>("A");
63   factory.Register<B>("B");
64 
65   std::unique_ptr<base> A = factory.Create("A");
66   BOOST_REQUIRE_EQUAL(A->identify(), "A");
67 
68   BOOST_REQUIRE(factory.IsRegistered("A"));
69 
70   std::unique_ptr<base> B = factory.Create("B");
71   BOOST_REQUIRE_EQUAL(B->identify(), "B");
72   std::vector<std::string> keys = factory.getKeys();
73   bool A_present = (std::find(keys.begin(), keys.end(), "A") != keys.end());
74   bool B_present = (std::find(keys.begin(), keys.end(), "B") != keys.end());
75 
76   BOOST_REQUIRE(A_present);
77   BOOST_REQUIRE(B_present);
78   BOOST_REQUIRE_THROW(factory.Create("C"), std::runtime_error);
79   BOOST_REQUIRE_EQUAL(factory.IsRegistered("D"), false);
80 }
81 
82 BOOST_AUTO_TEST_SUITE_END()
83