2 * Copyright (C) 2015-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
9 #include "utils/ComponentContainer.h"
13 #include <gtest/gtest.h>
18 virtual ~BaseTestType() = default;
21 struct DerivedType1
: public BaseTestType
26 struct DerivedType2
: public BaseTestType
31 struct DerivedType3
: public BaseTestType
36 class TestContainer
: public CComponentContainer
<BaseTestType
>
38 FRIEND_TEST(TestComponentContainer
, Generic
);
41 TEST(TestComponentContainer
, Generic
)
43 TestContainer container
;
45 // check that we can register types
46 container
.RegisterComponent(std::make_shared
<DerivedType1
>());
47 EXPECT_EQ(container
.size(), 1u);
48 container
.RegisterComponent(std::make_shared
<DerivedType2
>());
49 EXPECT_EQ(container
.size(), 2u);
51 // check that trying to register a component twice does nothing
52 container
.RegisterComponent(std::make_shared
<DerivedType2
>());
53 EXPECT_EQ(container
.size(), 2u);
55 // check that first component is valid
56 const auto t1
= container
.GetComponent
<DerivedType1
>();
57 EXPECT_TRUE(t1
!= nullptr);
60 // check that second component is valid
61 const auto t2
= container
.GetComponent
<DerivedType2
>();
62 EXPECT_TRUE(t2
!= nullptr);
65 // check that third component is not there
66 EXPECT_THROW(container
.GetComponent
<DerivedType3
>(), std::logic_error
);
68 // check that component instance is constant
69 const auto t4
= container
.GetComponent
<DerivedType1
>();
70 EXPECT_EQ(t1
.get(), t4
.get());
72 // check we can call the const overload for GetComponent
73 // and that the returned type is const
74 const auto t5
= const_cast<const TestContainer
&>(container
).GetComponent
<DerivedType1
>();
75 EXPECT_TRUE(std::is_const_v
<typename
decltype(t5
)::element_type
>);