1 #include "core/wrapsystem.h"
  2 
  3 #include <gtest/gtest.h>
  4 
  5 //#define TEST_OLD_MACROS
  6 
  7 namespace {
  8 
  9 class TestImplementation;
 10 
 11 class TestInterface : public WrapableInterface<TestImplementation, TestInterface> {
 12 public:
 13     TestInterface();
 14     ~TestInterface();
 15 
 16     virtual void testMethodReturningVoid() /* const */ = 0;
 17     virtual int testMethodReturningInt(int i) /* const */ = 0;
 18 
 19     static int testMethodReturningVoidCalls;
 20     static int testMethodReturningIntCalls;
 21 
 22 private:
 23     TestInterface(TestInterface const&);
 24     TestInterface& operator=(TestInterface const&);
 25 };
 26 
 27 // Needs a magic number for the count of "wrappable" functions
 28 class TestImplementation : public WrapableHandler<TestInterface, 2> {
 29 public:
 30 
 31     // 1. need for magic numbers
 32     // 2. why can't we just pass &TestInterface::testMethod (and deduce return etc.
 33     // 3. relies on __VA_ARGS__ when an extra set of parentheses would be enough
 34     WRAPABLE_HND (0, TestInterface, void, testMethodReturningVoid)
 35 
 36     WRAPABLE_HND (1, TestInterface, int, testMethodReturningInt, int)
 37 
 38     static int testMethodReturningVoidCalls;
 39     static int testMethodReturningIntCalls;
 40 };
 41 
 42 class TestWrapper : public TestInterface {
 43     TestImplementation& impl;
 44 public:
 45 
 46     TestWrapper(TestImplementation& impl)
 47         : impl(impl)
 48     { setHandler(&impl, true); }  // The need to remember this is a PITA
 49 
 50     ~TestWrapper()
 51     { setHandler(&impl, false); }  // The need to remember this is a PITA
 52 
 53     virtual void testMethodReturningVoid();
 54     virtual int testMethodReturningInt(int i);
 55 
 56     static int testMethodReturningVoidCalls;
 57     static int testMethodReturningIntCalls;
 58 
 59     void disableTestMethodReturningVoid() {
 60         impl.testMethodReturningVoidSetEnabled (this, false);
 61     }
 62 };
 63 } // (abstract) namespace
 64 
 65 
 66 int TestWrapper::testMethodReturningVoidCalls = 0;
 67 int TestInterface::testMethodReturningVoidCalls = 0;
 68 int TestImplementation::testMethodReturningVoidCalls = 0;
 69 
 70 int TestWrapper::testMethodReturningIntCalls = 0;
 71 int TestInterface::testMethodReturningIntCalls = 0;
 72 int TestImplementation::testMethodReturningIntCalls = 0;
 73 
 74 // A pain these need definition after TestImplementation definition
 75 TestInterface::TestInterface() {}
 76 TestInterface::~TestInterface() {}
 77 
 78 void TestInterface::testMethodReturningVoid() /* const */ {
 79     WRAPABLE_DEF (testMethodReturningVoid);
CID 12597 - UNREACHABLE
This code cannot be reached: "::TestInterface::t...".
 80     testMethodReturningVoidCalls++;
 81 }
 82 
 83 int TestInterface::testMethodReturningInt(int i) {
 84     WRAPABLE_DEF (testMethodReturningInt, i);
 85     testMethodReturningIntCalls++;
 86     return i;
 87 }
 88 
 89 void TestImplementation::testMethodReturningVoid() {
 90 #ifdef TEST_OLD_MACROS
 91     WRAPABLE_HND_FUNC(0, testMethodReturningVoid) // Magic number needs to match class definition
 92 #else
 93     WRAPABLE_HND_FUNCTN(testMethodReturningVoid)
 94 #endif
 95     testMethodReturningVoidCalls++;
 96 }
 97 
 98 int TestImplementation::testMethodReturningInt(int i) {
 99 #ifdef TEST_OLD_MACROS
100     WRAPABLE_HND_FUNC_RETURN(1, int, testMethodReturningInt, i) // Magic number needs to match class definition
101 #else
102     WRAPABLE_HND_FUNCTN_RETURN(int, testMethodReturningInt, i)
103 #endif
104     testMethodReturningIntCalls++;
105     return i;
106 }
107 
108 void TestWrapper::testMethodReturningVoid() {
109     impl.testMethodReturningVoid();
110     testMethodReturningVoidCalls++;
111 }
112 
113 int TestWrapper::testMethodReturningInt(int i) {
114     testMethodReturningIntCalls++;
115     return impl.testMethodReturningInt(i);
116 }
117 
118 
119 TEST(WrapSystem, an_interface_never_gets_functions_called)
120 {
121     TestInterface::testMethodReturningIntCalls = 0;
122 
123     TestImplementation imp;
124 
125     imp.testMethodReturningInt(1);
126     ASSERT_EQ(0, TestInterface::testMethodReturningIntCalls);
127 
128     {
129         TestWrapper wrap(imp);
130 
131         imp.testMethodReturningInt(1);
132         ASSERT_EQ(0, TestInterface::testMethodReturningIntCalls);
133     }
134 
135     imp.testMethodReturningInt(1);
136     ASSERT_EQ(0, TestInterface::testMethodReturningIntCalls);
137 }
138 
139 TEST(WrapSystem, an_interface_never_gets_void_functions_called)
140 {
141     TestInterface::testMethodReturningVoidCalls = 0;
142 
143     TestImplementation imp;
144 
145     imp.testMethodReturningVoid();
146     ASSERT_EQ(0, TestInterface::testMethodReturningVoidCalls);
147 
148     {
149         TestWrapper wrap(imp);
150 
151         imp.testMethodReturningVoid();
152         ASSERT_EQ(0, TestInterface::testMethodReturningVoidCalls);
153     }
154 
155     imp.testMethodReturningVoid();
156     ASSERT_EQ(0, TestInterface::testMethodReturningVoidCalls);
157 }
158 
159 TEST(WrapSystem, an_implementation_gets_functions_called)
160 {
161     TestImplementation::testMethodReturningVoidCalls = 0;
162 
163     TestImplementation imp;
164     {
165         TestWrapper wrap(imp);
166 
167         imp.testMethodReturningVoid();
168 
169         ASSERT_EQ(1, TestImplementation::testMethodReturningVoidCalls);
170     }
171 
172     imp.testMethodReturningVoid();
173 
174     ASSERT_EQ(2, TestImplementation::testMethodReturningVoidCalls);
175 }
176 
177 TEST(WrapSystem, a_wrapper_gets_its_functions_called)
178 {
179     TestWrapper::testMethodReturningVoidCalls = 0;
180 
181     TestImplementation imp;
182     {
183         TestWrapper wrap(imp);
184 
185         imp.testMethodReturningVoid();
186 
187         ASSERT_EQ(1, TestWrapper::testMethodReturningVoidCalls);
188     }
189 
190     imp.testMethodReturningVoid();
191 
192     ASSERT_EQ(1, TestWrapper::testMethodReturningVoidCalls);
193 }
194 
195 TEST(WrapSystem, a_wrapper_doesnt_get_disabled_functions_called)
196 {
197     TestWrapper::testMethodReturningVoidCalls = 0;
198 
199     TestImplementation imp;
200     {
201         TestWrapper wrap(imp);
202 
203         wrap.disableTestMethodReturningVoid();
204 
205         imp.testMethodReturningVoid();
206 
207         ASSERT_EQ(0, TestWrapper::testMethodReturningVoidCalls);
208     }
209 }
210 
211 TEST(WrapSystem, two_wrappers_get_their_functions_called)
212 {
213     TestWrapper::testMethodReturningVoidCalls = 0;
214 
215     TestImplementation imp;
216     {
217         TestWrapper wrap1(imp);
218         TestWrapper wrap2(imp);
219 
220         imp.testMethodReturningVoid();
221 
222         ASSERT_EQ(2, TestWrapper::testMethodReturningVoidCalls);
223     }
224 
225     imp.testMethodReturningVoid();
226 
227     ASSERT_EQ(2, TestWrapper::testMethodReturningVoidCalls);
228 }