Unity Scopes API
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 Voss <thomas.voss@canonical.com>
17  */
18 
19 #pragma once
20 
21 #include <unity/scopes/Version.h>
22 
23 #include <unity/scopes/testing/MockRegistry.h>
24 
25 #include <gtest/gtest.h>
26 
27 #include <memory>
28 
29 namespace unity
30 {
31 
32 namespace scopes
33 {
34 
35 class ScopeBase;
36 
37 namespace testing
38 {
39 
41 
42 template<typename Scope>
43 struct ScopeTraits
44 {
45  inline static const char* name()
46  {
47  return "unknown";
48  }
49 
50  inline static std::shared_ptr<Scope> construct()
51  {
52  return std::make_shared<Scope>();
53  }
54 };
55 
56 class TypedScopeFixtureHelper
57 {
58  static void set_scope_directory(std::shared_ptr<ScopeBase> const& scope, std::string const& path);
59  static void set_cache_directory(std::shared_ptr<ScopeBase> const& scope, std::string const& path);
60  static void set_app_directory(std::shared_ptr<ScopeBase> const& scope, std::string const& path);
61  static void set_tmp_directory(std::shared_ptr<ScopeBase> const& scope, std::string const& path);
62  static void set_registry(std::shared_ptr<ScopeBase> const& scope, RegistryProxy const& r);
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 set_cache_directory(std::string const& path)
91  {
92  TypedScopeFixtureHelper::set_cache_directory(scope, path);
93  }
94 
95  void set_app_directory(std::string const& path)
96  {
97  TypedScopeFixtureHelper::set_app_directory(scope, path);
98  }
99 
100  void set_tmp_directory(std::string const& path)
101  {
102  TypedScopeFixtureHelper::set_tmp_directory(scope, path);
103  }
104 
105  static void set_registry(std::shared_ptr<ScopeBase> const& scope, RegistryProxy const& r)
106  {
107  TypedScopeFixtureHelper::set_registry(scope, r);
108  }
109 
110  void TearDown()
111  {
112  EXPECT_NO_THROW(scope->stop());
113  }
114 
115 protected:
116  unity::scopes::testing::MockRegistry registry;
117  unity::scopes::RegistryProxy registry_proxy;
118  std::shared_ptr<Scope> scope;
119 };
120 
122 
123 } // namespace testing
124 
125 } // namespace scopes
126 
127 } // namespace unity
Definition: OnlineAccountClient.h:39
Top-level namespace for all things Unity-related.
Definition: Version.h:49
std::shared_ptr< Registry > RegistryProxy
Convenience type definition.
Definition: RegistryProxyFwd.h:33