John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 1 | #include "aos/util/global_factory.h" |
Parker Schuh | 3641669 | 2017-02-18 17:34:15 -0800 | [diff] [blame] | 2 | #include "gtest/gtest.h" |
| 3 | |
| 4 | namespace aos { |
| 5 | |
| 6 | namespace test_a { |
| 7 | class BaseType { |
| 8 | public: |
| 9 | virtual ~BaseType() {} |
| 10 | |
| 11 | virtual std::pair<int, int> Get() = 0; |
| 12 | }; |
| 13 | |
| 14 | SETUP_FACTORY(BaseType, int, int); |
| 15 | |
| 16 | class BaseTypeNoArgs { |
| 17 | public: |
| 18 | virtual ~BaseTypeNoArgs() {} |
| 19 | |
| 20 | virtual int Get() = 0; |
| 21 | }; |
| 22 | |
| 23 | SETUP_FACTORY(BaseTypeNoArgs); |
| 24 | |
| 25 | } // namespace test_a |
| 26 | |
| 27 | namespace test_b { |
| 28 | |
| 29 | class SubType : public test_a::BaseType { |
| 30 | public: |
| 31 | SubType(int t1, int t2) : value_(t1, t2) {} |
| 32 | std::pair<int, int> Get() override { return value_; } |
| 33 | |
| 34 | private: |
| 35 | std::pair<int, int> value_; |
| 36 | }; |
| 37 | |
| 38 | REGISTER_SUBCLASS(test_a::BaseType, SubType); |
| 39 | |
| 40 | } // namespace test_b |
| 41 | |
| 42 | namespace { |
| 43 | |
| 44 | class SubType1 : public test_a::BaseTypeNoArgs { |
| 45 | public: |
| 46 | int Get() override { return 1; } |
| 47 | }; |
| 48 | |
| 49 | class SubType2 : public test_a::BaseTypeNoArgs { |
| 50 | public: |
| 51 | int Get() override { return 2; } |
| 52 | }; |
| 53 | REGISTER_SUBCLASS(test_a::BaseTypeNoArgs, SubType1); |
| 54 | REGISTER_SUBCLASS(test_a::BaseTypeNoArgs, SubType2); |
| 55 | |
| 56 | TEST(GlobalFactoryTest, CheckFactory) { |
| 57 | auto val = test_a::BaseTypeGlobalFactory::Get("SubType")(2, 7)->Get(); |
| 58 | EXPECT_EQ(val.first, 2); |
| 59 | EXPECT_EQ(val.second, 7); |
| 60 | } |
| 61 | TEST(GlobalFactoryTest, CheckFactoryNoArgs) { |
| 62 | EXPECT_EQ(1, test_a::BaseTypeNoArgsGlobalFactory::Get("SubType1")()->Get()); |
| 63 | EXPECT_EQ(2, test_a::BaseTypeNoArgsGlobalFactory::Get("SubType2")()->Get()); |
| 64 | } |
| 65 | |
| 66 | } // namespace |
| 67 | } // namespace aos |