Unity Scopes API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
TypedScopeFixture.h
1 /*
2  * Copyright (C) 2013 Canonical Ltd
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the Lesser GNU General Public License version 3 as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Authored by: Thomas Voß <thomas.voss@canonical.com>
17  */
18 
19 #ifndef UNITY_SCOPES_TESTING_TYPED_SCOPE_FIXTURE_H
20 #define UNITY_SCOPES_TESTING_TYPED_SCOPE_FIXTURE_H
21 
22 #include <unity/scopes/Version.h>
23 
24 #include <unity/scopes/testing/MockRegistry.h>
25 
26 #include <gtest/gtest.h>
27 
28 #include <memory>
29 
30 namespace unity
31 {
32 
33 namespace scopes
34 {
35 
36 class ScopeBase;
37 
38 namespace testing
39 {
40 
42 
43 template<typename Scope>
44 struct ScopeTraits
45 {
46  inline static const char* name()
47  {
48  return "unknown";
49  }
50 
51  inline static std::shared_ptr<Scope> construct()
52  {
53  return std::make_shared<Scope>();
54  }
55 };
56 
57 struct TypedScopeFixtureHelper
58 {
59 private:
60  static void set_registry(std::shared_ptr<ScopeBase> const& scope, RegistryProxy const& r);
61 
62  static void set_scope_directory(std::shared_ptr<ScopeBase> const& scope, std::string const& path);
63 
64  template<typename Scope>
65  friend class TypedScopeFixture;
66 };
67 
68 template<typename Scope>
69 class TypedScopeFixture : public ::testing::Test
70 {
71 public:
72  TypedScopeFixture()
73  : registry_proxy(&registry, [](unity::scopes::Registry*) {})
74  , scope(ScopeTraits<Scope>::construct())
75  {
76  TypedScopeFixtureHelper::set_registry(scope, registry_proxy);
77  }
78 
79  void SetUp()
80  {
81  ASSERT_NO_THROW(scope->start(ScopeTraits<Scope>::name()));
82  ASSERT_NO_THROW(scope->run());
83  }
84 
85  void set_scope_directory(std::string const& path)
86  {
87  TypedScopeFixtureHelper::set_scope_directory(scope, path);
88  }
89 
90  void TearDown()
91  {
92  EXPECT_NO_THROW(scope->stop());
93  }
94 
95 protected:
96  unity::scopes::testing::MockRegistry registry;
97  unity::scopes::RegistryProxy registry_proxy;
98  std::shared_ptr<Scope> scope;
99 };
100 
102 
103 } // namespace testing
104 
105 } // namespace scopes
106 
107 } // namespace unity
108 #endif
Definition: OnlineAccountClient.h:40
Top-level namespace for all things Unity-related.
Definition: Version.h:50
std::shared_ptr< Registry > RegistryProxy
Convenience type definition.
Definition: RegistryProxyFwd.h:34