Skip to content

Commit 3358036

Browse files
committed
Fixed problems with fixtures, where the setup and tear down was not matching across .cpp unit tests for issue #21.
1 parent 882e508 commit 3358036

File tree

6 files changed

+201
-61
lines changed

6 files changed

+201
-61
lines changed

test/TestClass.hpp

Lines changed: 33 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ limitations under the License.
2929
#include "TestStdioFileFunctor.hpp"
3030
#include "TestMemoryLeakCheck.hpp"
3131
#include "TestMemoryCounter.hpp"
32-
#include "TestSetupTeardownFunctor.hpp"
32+
#include "TestSetupTeardown.hpp"
3333

3434
#include <cstring>
3535
#include <climits>
@@ -73,6 +73,8 @@ class TestClass
7373
, m_test_number(0)
7474
, m_logged(false)
7575
{
76+
GetSharedData().SetClassName(class_name);
77+
7678
// Ensure that the next constructor can complete
7779
// the snapshot memory leak checking.
7880
TestClass*& prev_test = GetPreviousTest();
@@ -189,58 +191,50 @@ class TestClass
189191
return str;
190192
}
191193

192-
private:
193-
// Setup and tear down functionality, called via Execute function.
194-
template<bool const is_setup>
195-
void SetupTeardown()
196-
{
197-
TestSetupTeardownFunctor*& setup_or_teardown = GetSetupOrTeardown<is_setup>();
198-
199-
if (setup_or_teardown != NULL)
200-
{
201-
setup_or_teardown->Execute();
202-
setup_or_teardown = NULL;
203-
}
204-
}
205-
206-
// Setting the setup and tear down functions, which get called later.
207-
// See TEST_SETUP and TEST_TEARDOWN macros for usage.
208-
template<bool const is_setup>
209-
static void SetSetupTeardown(TestSetupTeardownFunctor& setup_teardown_functor)
210-
{
211-
TestSetupTeardownFunctor*& setup_or_teardown = GetSetupOrTeardown<is_setup>();
212-
213-
TestString class_name;
214-
if (setup_or_teardown != NULL)
215-
class_name.Assign(setup_or_teardown->GetClassName());
216-
217-
// If setup_or_teardown is null or class name has changed then set the functor.
218-
if (class_name != setup_teardown_functor.GetClassName())
219-
setup_or_teardown = &setup_teardown_functor;
220-
}
221-
222194
// Call the user defined setup and tear down functions via TestClass::Execute.
223195
public:
224196
void Setup()
225197
{
226-
SetupTeardown<true>();
198+
TestSetupTeardown& setup_teardown = privateSetupTeardown();
199+
if (setup_teardown.IsSetupSet())
200+
setup_teardown.Execute<true>();
227201
}
228202

229203
void Teardown()
230204
{
231-
SetupTeardown<false>();
205+
TestSetupTeardown& setup_teardown = privateSetupTeardown();
206+
if (setup_teardown.IsTeardownSet() && setup_teardown.IsSetupRun())
207+
setup_teardown.Execute<false>();
232208
}
233209

234210
// Set the appropriate class objects via using the TEST_SETUP and TEST_TEARDOWN macros.
235211
public:
236212
static void SetSetup(TestSetupTeardownFunctor& setup_functor)
237213
{
238-
SetSetupTeardown<true>(setup_functor);
214+
TestSetupTeardown& setup_teardown = privateSetupTeardown();
215+
216+
if (setup_teardown.IsSetupRun() &&
217+
(setup_teardown.GetSetupClassName() != setup_functor.GetClassName()))
218+
{
219+
// If tear down execute is not run before setting new setup functor,
220+
// Then the class has changed and we need to complete the previous
221+
// tear down before starting a new class fixture.
222+
setup_teardown.Execute<false>();
223+
}
224+
225+
setup_teardown.SetSetupFunctor(setup_functor);
239226
}
240227

241228
static void SetTeardown(TestSetupTeardownFunctor& teardown_functor)
242229
{
243-
SetSetupTeardown<false>(teardown_functor);
230+
TestSetupTeardown& setup_teardown = privateSetupTeardown();
231+
setup_teardown.SetTeardownFunctor(teardown_functor);
232+
}
233+
234+
static void ClearSetupTeardown()
235+
{
236+
TestSetupTeardown& setup_teardown = privateSetupTeardown();
237+
setup_teardown.Clear();
244238
}
245239

246240
// General helper functions.
@@ -1009,24 +1003,12 @@ class TestClass
10091003
return prev_test;
10101004
}
10111005

1012-
static TestSetupTeardownFunctor*& GetSetup()
1006+
static TestSetupTeardown& privateSetupTeardown()
10131007
{
1014-
static TestSetupTeardownFunctor* setup_functor;
1015-
return setup_functor;
1008+
static TestSetupTeardown setup_teardown;
1009+
return setup_teardown;
10161010
}
1017-
1018-
static TestSetupTeardownFunctor*& GetTeardown()
1019-
{
1020-
static TestSetupTeardownFunctor* teardown_functor;
1021-
return teardown_functor;
1022-
}
1023-
1024-
template<bool const is_setup>
1025-
static TestSetupTeardownFunctor*& GetSetupOrTeardown()
1026-
{
1027-
return is_setup ? GetSetup() : GetTeardown();
1028-
}
1029-
1011+
10301012
// Data for this test.
10311013
private:
10321014
TestMemoryLeakCheck m_leak_check;

test/TestClassSharedData.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ limitations under the License.
1818
#define OCL_GUARD_TEST_TESTCLASSSHAREDDATA_HPP
1919

2020
#include "StdioTestLog.hpp"
21+
#include "TestString.hpp"
2122
#include <cstddef>
2223

2324
namespace ocl
@@ -224,6 +225,21 @@ class TestClassSharedData
224225
m_total_tests = total_tests;
225226
}
226227

228+
char const* GetClassName() const throw()
229+
{
230+
return m_class_name;
231+
}
232+
233+
void SetClassName(char const* class_name) throw()
234+
{
235+
m_class_name = class_name;
236+
}
237+
238+
bool IsClassNameSame(char const* class_name) const
239+
{
240+
return TestString(m_class_name) == class_name;
241+
}
242+
227243
void IncTotalTests()
228244
{
229245
++m_total_tests;
@@ -276,6 +292,9 @@ class TestClassSharedData
276292
SizeType m_total_timed_functions;
277293
SizeType m_total_tests;
278294

295+
// Last class name set for TestClass.
296+
char const* m_class_name;
297+
279298
// message for start of each test.
280299
const char* m_success_message;
281300
const char* m_failed_message;

test/TestMacros.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ TEST_MEMBER_FUNCTION(MyString, SetSize, size_t)
3939
#include "TestSetupTeardownFunctor.hpp"
4040

4141
#define TEST_SETUP_TEARDOWN(Type, is_setup, class_name) \
42-
class TestSetupTeardownFunctor_##Type_##class_name : public ocl::TestSetupTeardownFunctor \
42+
class TestSetupTeardownFunctor_##Type##_##class_name : public ocl::TestSetupTeardownFunctor \
4343
{ \
4444
public: \
45-
TestSetupTeardownFunctor_##Type_##class_name(char const* name = #class_name) : \
45+
TestSetupTeardownFunctor_##Type##_##class_name(char const* name = #class_name) : \
4646
ocl::TestSetupTeardownFunctor(name, is_setup) \
4747
{ \
4848
if (IsSetup()) \
@@ -51,12 +51,12 @@ TEST_MEMBER_FUNCTION(MyString, SetSize, size_t)
5151
ocl::TestClass::SetTeardown(*this); \
5252
} \
5353
void Execute(); \
54-
} TestSetupTeardownFunctor_##Type_##class_name##_instance; \
55-
void TestSetupTeardownFunctor_##Type_##class_name::Execute()
54+
} TestSetupTeardownFunctor_##Type##_##class_name##_instance; \
55+
void TestSetupTeardownFunctor_##Type##_##class_name::Execute()
5656

57-
#define TEST_SETUP(class_name) TEST_SETUP_TEARDOWN(class_name, true, Setup)
57+
#define TEST_SETUP(class_name) TEST_SETUP_TEARDOWN(Setup, true, class_name)
5858

59-
#define TEST_TEARDOWN(class_name) TEST_SETUP_TEARDOWN(class_name, false, Teardown)
59+
#define TEST_TEARDOWN(class_name) TEST_SETUP_TEARDOWN(Teardown, false, class_name)
6060

6161
#ifndef TEST
6262
#define TEST(name) \

test/TestSetupTeardown.hpp

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
Copyright 2016 Colin Girling
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
#ifndef OCL_GUARD_TEST_TESTSETUPTEARDOWN_HPP
18+
#define OCL_GUARD_TEST_TESTSETUPTEARDOWN_HPP
19+
20+
#include "TestSetupTeardownFunctor.hpp"
21+
#include "TestString.hpp"
22+
#include <cstddef>
23+
24+
namespace ocl
25+
{
26+
27+
class TestSetupTeardown
28+
{
29+
public:
30+
TestSetupTeardown()
31+
: m_setup_functor(NULL)
32+
, m_teardown_functor(NULL)
33+
, m_setup_run(false)
34+
{
35+
}
36+
37+
bool IsSetupSet() const throw()
38+
{
39+
return m_setup_functor != NULL;
40+
}
41+
42+
bool IsTeardownSet() const throw()
43+
{
44+
return m_teardown_functor != NULL;
45+
}
46+
47+
bool IsSetupTeardownSet() const throw()
48+
{
49+
return (m_setup_functor != NULL) && (m_teardown_functor != NULL);
50+
}
51+
52+
bool IsSetupRun() const throw()
53+
{
54+
return m_setup_run;
55+
}
56+
57+
TestString GetSetupClassName() const
58+
{
59+
TestString class_name(m_setup_functor != NULL ?
60+
m_setup_functor->GetClassName() : "");
61+
return class_name;
62+
}
63+
64+
TestString GetTeardownClassName() const
65+
{
66+
TestString class_name(m_teardown_functor != NULL ?
67+
m_teardown_functor->GetClassName() : "");
68+
return class_name;
69+
}
70+
71+
template<bool const is_setup>
72+
char const* GetClassName() const throw()
73+
{
74+
return is_setup ? GetSetupClassName() : GetTeardownClassName();
75+
}
76+
77+
TestSetupTeardownFunctor* GetSetupFunctor()
78+
{
79+
return m_setup_functor;
80+
}
81+
82+
void SetSetupFunctor(TestSetupTeardownFunctor& setup_functor)
83+
{
84+
m_setup_functor = &setup_functor;
85+
}
86+
87+
TestSetupTeardownFunctor* GetTeardownFunctor()
88+
{
89+
return m_teardown_functor;
90+
}
91+
92+
void SetTeardownFunctor(TestSetupTeardownFunctor& teardown_functor)
93+
{
94+
m_teardown_functor = &teardown_functor;
95+
}
96+
97+
template<bool const is_setup>
98+
TestSetupTeardownFunctor* GetSetupTeardownFunctor()
99+
{
100+
return is_setup ? GetSetupFunctor() : GetTeardownFunctor();
101+
}
102+
103+
template<bool const is_setup>
104+
TestSetupTeardownFunctor*& SetupOrTeardownFunctor()
105+
{
106+
return is_setup ? m_setup_functor : m_teardown_functor;
107+
}
108+
109+
template<bool const is_setup>
110+
void Execute()
111+
{
112+
TestSetupTeardownFunctor* functor = SetupOrTeardownFunctor<is_setup>();
113+
if (functor != NULL)
114+
{
115+
m_setup_run = is_setup;
116+
functor->Execute();
117+
}
118+
}
119+
120+
void Clear()
121+
{
122+
m_setup_functor = NULL;
123+
m_teardown_functor = NULL;
124+
}
125+
126+
private:
127+
TestSetupTeardownFunctor* m_setup_functor;
128+
TestSetupTeardownFunctor* m_teardown_functor;
129+
bool m_setup_run;
130+
};
131+
132+
}
133+
134+
#endif // OCL_GUARD_TEST_TESTSETUPTEARDOWN_HPP

test/TestString.hpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class TestString
4444
: m_length(size_type_default)
4545
, m_string(NULL)
4646
{
47-
if (str != NULL)
47+
if ((str != NULL) && (*str != '\0'))
4848
TestStringUtility::UnsafeAllocateCopy(m_string, m_length, str);
4949
}
5050

@@ -132,14 +132,18 @@ class TestString
132132
{
133133
if (m_string == NULL)
134134
return (str == NULL) || (*str == '\0');
135-
return ::strcmp(m_string, str) == 0;
135+
if (str != NULL)
136+
return ::strcmp(m_string, str) == 0;
137+
return false;
136138
}
137139

138140
bool operator ==(TestString const& str) const
139141
{
140142
if (m_string == NULL)
141-
return str.m_string == NULL;
142-
return ::strcmp(m_string, str.Ptr()) == 0;
143+
return (str.m_string == NULL) || (*str.m_string == '\0');
144+
if (str.m_string != NULL)
145+
return ::strcmp(m_string, str.Ptr()) == 0;
146+
return false;
143147
}
144148

145149
bool operator !=(char const* str) const

test/unit_tests/ide/VS2015/test_unit_tests/test_unit_tests.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
2525
..\..\..\..\TestMemoryUtility.hpp = ..\..\..\..\TestMemoryUtility.hpp
2626
..\..\..\..\TestMinMax.hpp = ..\..\..\..\TestMinMax.hpp
2727
..\..\..\..\TestNumericUtility.hpp = ..\..\..\..\TestNumericUtility.hpp
28+
..\..\..\..\TestSetupTeardown.hpp = ..\..\..\..\TestSetupTeardown.hpp
2829
..\..\..\..\TestSetupTeardownFunctor.hpp = ..\..\..\..\TestSetupTeardownFunctor.hpp
2930
..\..\..\..\TestStdioFileFunctor.hpp = ..\..\..\..\TestStdioFileFunctor.hpp
3031
..\..\..\..\TestString.hpp = ..\..\..\..\TestString.hpp

0 commit comments

Comments
 (0)