Skip to content

Commit 2226db9

Browse files
authored
Merge pull request #5890 from hjmjohnson/convert-itkcommandobserverobjecttest-to-gtest
ENH: Convert itkCommandObserverObjectTest to GTest
2 parents 44aba42 + 27d0058 commit 2226db9

2 files changed

Lines changed: 38 additions & 73 deletions

File tree

Modules/Core/Common/test/CMakeLists.txt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ itk_module_test()
22
set(
33
ITKCommon1Tests
44
itkImageRegionExplicitTest.cxx
5-
itkCommandObserverObjectTest.cxx
65
itkCovariantVectorGeometryTest.cxx
76
itkExtractImage3Dto2DTest.cxx
87
itkExtractImageTest.cxx
@@ -203,12 +202,6 @@ if(ITK_BUILD_SHARED_LIBS)
203202
add_dependencies(ITKCommon2TestDriver FactoryTestLib)
204203
endif()
205204

206-
itk_add_test(
207-
NAME itkCommandObserverObjectTest
208-
COMMAND
209-
ITKCommon1TestDriver
210-
itkCommandObserverObjectTest
211-
)
212205
itk_add_test(
213206
NAME itkThreadedIteratorRangePartitionerTest
214207
COMMAND
@@ -1571,6 +1564,7 @@ set(
15711564
itkBooleanStdVectorGTest.cxx
15721565
itkBSplineKernelFunctionGTest.cxx
15731566
itkBuildInformationGTest.cxx
1567+
itkCommandObserverObjectGTest.cxx
15741568
itkConnectedImageNeighborhoodShapeGTest.cxx
15751569
itkConstantBoundaryImageNeighborhoodPixelAccessPolicyGTest.cxx
15761570
itkCopyGTest.cxx

Modules/Core/Common/test/itkCommandObserverObjectTest.cxx renamed to Modules/Core/Common/test/itkCommandObserverObjectGTest.cxx

Lines changed: 37 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,17 @@
1616
*
1717
*=========================================================================*/
1818

19-
#include <iostream>
20-
#include "itkObject.h"
21-
22-
#include "itkCommand.h"
23-
#include "itkTestingMacros.h"
24-
25-
2619
//
2720
// This test is for testing the Command Observer functionality of the
2821
// itk::Object class.
2922
//
3023

24+
// First include the header file to be tested:
25+
#include "itkCommand.h"
26+
#include "itkObject.h"
27+
#include "itkGTest.h"
28+
#include <iostream>
29+
3130

3231
namespace
3332
{
@@ -71,8 +70,10 @@ onUserRemove(itk::Object * o, const itk::EventObject &, void * data)
7170
o->RemoveObserver(idToRemove);
7271
}
7372

74-
int
75-
testDeleteObserverDuringEvent()
73+
} // namespace
74+
75+
76+
TEST(CommandObserverObject, DeleteObserverDuringEvent)
7677
{
7778
const itk::Object::Pointer o = itk::Object::New();
7879

@@ -92,16 +93,16 @@ testDeleteObserverDuringEvent()
9293

9394
onAnyCount = 0;
9495
o->InvokeEvent(itk::AnyEvent());
95-
ITK_TEST_EXPECT_TRUE(onAnyCount == 2);
96+
EXPECT_EQ(onAnyCount, 2u);
9697

9798
onAnyCount = 0;
9899
o->InvokeEvent(itk::UserEvent());
99-
ITK_TEST_EXPECT_TRUE(onAnyCount == 1);
100+
EXPECT_EQ(onAnyCount, 1u);
100101

101102

102103
onAnyCount = 0;
103104
o->InvokeEvent(itk::AnyEvent());
104-
ITK_TEST_EXPECT_TRUE(onAnyCount == 1);
105+
EXPECT_EQ(onAnyCount, 1u);
105106

106107
o->RemoveAllObservers();
107108

@@ -113,16 +114,16 @@ testDeleteObserverDuringEvent()
113114

114115
onAnyCount = 0;
115116
o->InvokeEvent(itk::AnyEvent());
116-
ITK_TEST_EXPECT_TRUE(onAnyCount == 2);
117+
EXPECT_EQ(onAnyCount, 2u);
117118

118119
onAnyCount = 0;
119120
o->InvokeEvent(itk::UserEvent());
120-
ITK_TEST_EXPECT_TRUE(onAnyCount == 2);
121+
EXPECT_EQ(onAnyCount, 2u);
121122

122123

123124
onAnyCount = 0;
124125
o->InvokeEvent(itk::AnyEvent());
125-
ITK_TEST_EXPECT_TRUE(onAnyCount == 1);
126+
EXPECT_EQ(onAnyCount, 1u);
126127

127128
o->RemoveAllObservers();
128129

@@ -134,16 +135,16 @@ testDeleteObserverDuringEvent()
134135

135136
onAnyCount = 0;
136137
o->InvokeEvent(itk::AnyEvent());
137-
ITK_TEST_EXPECT_TRUE(onAnyCount == 2);
138+
EXPECT_EQ(onAnyCount, 2u);
138139

139140
onAnyCount = 0;
140141
o->InvokeEvent(itk::UserEvent());
141-
ITK_TEST_EXPECT_TRUE(onAnyCount == 2);
142+
EXPECT_EQ(onAnyCount, 2u);
142143

143144

144145
onAnyCount = 0;
145146
o->InvokeEvent(itk::AnyEvent());
146-
ITK_TEST_EXPECT_TRUE(onAnyCount == 1);
147+
EXPECT_EQ(onAnyCount, 1u);
147148

148149
o->RemoveAllObservers();
149150

@@ -155,26 +156,22 @@ testDeleteObserverDuringEvent()
155156

156157
onAnyCount = 0;
157158
o->InvokeEvent(itk::AnyEvent());
158-
ITK_TEST_EXPECT_TRUE(onAnyCount == 2);
159+
EXPECT_EQ(onAnyCount, 2u);
159160

160161
onAnyCount = 0;
161162
o->InvokeEvent(itk::UserEvent());
162-
ITK_TEST_EXPECT_TRUE(onAnyCount == 1);
163+
EXPECT_EQ(onAnyCount, 1u);
163164

164165

165166
onAnyCount = 0;
166167
o->InvokeEvent(itk::AnyEvent());
167-
ITK_TEST_EXPECT_TRUE(onAnyCount == 1);
168+
EXPECT_EQ(onAnyCount, 1u);
168169

169170
o->RemoveAllObservers();
170-
171-
172-
return EXIT_SUCCESS;
173171
}
174172

175173

176-
int
177-
testCommandConstObject()
174+
TEST(CommandObserverObject, CommandConstObject)
178175
{
179176

180177
const itk::Object::Pointer o = itk::Object::New();
@@ -189,24 +186,21 @@ testCommandConstObject()
189186
removeCmd->SetObjectName("Remove Command");
190187

191188
co->AddObserver(itk::AnyEvent(), cmd);
192-
ITK_TEST_EXPECT_TRUE(co->HasObserver(itk::AnyEvent()));
189+
EXPECT_TRUE(co->HasObserver(itk::AnyEvent()));
193190

194191
// the constant command doesn't get executed from the non-const
195192
// invocation
196193
onAnyCount = 0;
197194
o->InvokeEvent(itk::AnyEvent());
198-
ITK_TEST_EXPECT_TRUE(onAnyCount == 0);
195+
EXPECT_EQ(onAnyCount, 0u);
199196

200197
onAnyCount = 0;
201198
co->InvokeEvent(itk::AnyEvent());
202-
ITK_TEST_EXPECT_TRUE(onAnyCount == 1);
203-
204-
return EXIT_SUCCESS;
199+
EXPECT_EQ(onAnyCount, 1u);
205200
}
206201

207202

208-
int
209-
testCommandRecursiveObject()
203+
TEST(CommandObserverObject, CommandRecursiveObject)
210204
{
211205
// this test has an command invoking another event, while removing a
212206
// a Command.
@@ -235,7 +229,7 @@ testCommandRecursiveObject()
235229

236230
onAnyCount = 0;
237231
o->InvokeEvent(itk::AnyEvent());
238-
ITK_TEST_EXPECT_TRUE(onAnyCount == 2);
232+
EXPECT_EQ(onAnyCount, 2u);
239233

240234
o->RemoveAllObservers();
241235

@@ -247,7 +241,7 @@ testCommandRecursiveObject()
247241

248242
onAnyCount = 0;
249243
o->InvokeEvent(itk::AnyEvent());
250-
ITK_TEST_EXPECT_TRUE(onAnyCount == 2);
244+
EXPECT_EQ(onAnyCount, 2u);
251245

252246
o->RemoveAllObservers();
253247

@@ -259,14 +253,11 @@ testCommandRecursiveObject()
259253

260254
onAnyCount = 0;
261255
o->InvokeEvent(itk::AnyEvent());
262-
ITK_TEST_EXPECT_TRUE(onAnyCount == 0);
263-
264-
return EXIT_SUCCESS;
256+
EXPECT_EQ(onAnyCount, 0u);
265257
}
266258

267259

268-
bool
269-
testDeleteEventThrow()
260+
TEST(CommandObserverObject, DeleteEventThrow)
270261
{
271262
// check the case where an exception in thrown in the DeleteEvent
272263
const itk::Object::Pointer o = itk::Object::New();
@@ -275,11 +266,10 @@ testDeleteEventThrow()
275266
cmd->SetCallback(onAnyThrow);
276267

277268
o->AddObserver(itk::DeleteEvent(), cmd);
278-
return EXIT_SUCCESS;
279269
}
280270

281-
int
282-
testLambdaCommand()
271+
272+
TEST(CommandObserverObject, LambdaCommand)
283273
{
284274
// spell-check-disable
285275
// NOTE: cnt needs to be defined BEFORE "o" because it MUST exist when the "DeleteEvent()" is causes the
@@ -305,29 +295,10 @@ testLambdaCommand()
305295
});
306296

307297
o->InvokeEvent(itk::AnyEvent());
308-
ITK_TEST_EXPECT_EQUAL(1, cnt);
309-
ITK_TEST_EXPECT_EQUAL(1, name_of_class_cnt);
298+
EXPECT_EQ(1, cnt);
299+
EXPECT_EQ(1, name_of_class_cnt);
310300

311301
} // A DeleteEvent is called here! as object "o" is deleted
312-
ITK_TEST_EXPECT_EQUAL(2, cnt); // Verify that cnt really was incremented during DeleteEvent!
313-
ITK_TEST_EXPECT_EQUAL(2, name_of_class_cnt);
314-
return EXIT_SUCCESS;
315-
}
316-
317-
318-
} // end namespace
319-
320-
321-
int
322-
itkCommandObserverObjectTest(int, char *[])
323-
{
324-
bool ret = true;
325-
326-
ret &= (testDeleteObserverDuringEvent() == EXIT_SUCCESS);
327-
ret &= (testCommandConstObject() == EXIT_SUCCESS);
328-
ret &= (testCommandRecursiveObject() == EXIT_SUCCESS);
329-
ret &= (testDeleteEventThrow() == EXIT_SUCCESS);
330-
ret &= (testLambdaCommand() == EXIT_SUCCESS);
331-
332-
return ret ? EXIT_SUCCESS : EXIT_FAILURE;
302+
EXPECT_EQ(2, cnt); // Verify that cnt really was incremented during DeleteEvent!
303+
EXPECT_EQ(2, name_of_class_cnt);
333304
}

0 commit comments

Comments
 (0)