summaryrefslogtreecommitdiffstats
path: root/xbmc/utils/test/TestComponentContainer.cpp
blob: d7246be7c5125970270350a620b34fbb90177ccf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
 *  Copyright (C) 2015-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "utils/ComponentContainer.h"

#include <utility>

#include <gtest/gtest.h>

class BaseTestType
{
public:
  virtual ~BaseTestType() = default;
};

struct DerivedType1 : public BaseTestType
{
  int a = 1;
};

struct DerivedType2 : public BaseTestType
{
  int a = 2;
};

struct DerivedType3 : public BaseTestType
{
  int a = 3;
};

class TestContainer : public CComponentContainer<BaseTestType>
{
  FRIEND_TEST(TestComponentContainer, Generic);
};

TEST(TestComponentContainer, Generic)
{
  TestContainer container;

  // check that we can register types
  container.RegisterComponent(std::make_shared<DerivedType1>());
  EXPECT_EQ(container.size(), 1u);
  container.RegisterComponent(std::make_shared<DerivedType2>());
  EXPECT_EQ(container.size(), 2u);

  // check that trying to register a component twice does nothing
  container.RegisterComponent(std::make_shared<DerivedType2>());
  EXPECT_EQ(container.size(), 2u);

  // check that first component is valid
  const auto t1 = container.GetComponent<DerivedType1>();
  EXPECT_TRUE(t1 != nullptr);
  EXPECT_EQ(t1->a, 1);

  // check that second component is valid
  const auto t2 = container.GetComponent<DerivedType2>();
  EXPECT_TRUE(t2 != nullptr);
  EXPECT_EQ(t2->a, 2);

  // check that third component is not there
  EXPECT_THROW(container.GetComponent<DerivedType3>(), std::logic_error);

  // check that component instance is constant
  const auto t4 = container.GetComponent<DerivedType1>();
  EXPECT_EQ(t1.get(), t4.get());

  // check we can call the const overload for GetComponent
  // and that the returned type is const
  const auto t5 = const_cast<const TestContainer&>(container).GetComponent<DerivedType1>();
  EXPECT_TRUE(std::is_const_v<typename decltype(t5)::element_type>);
}