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