Skip to content

Commit

Permalink
STYLE: Prefer using ITK_TRY_EXPECT_NO_EXCEPTION macro in tests
Browse files Browse the repository at this point in the history
Use the `ITK_TRY_EXPECT_NO_EXCEPTION` macro in tests in lieu of
`try/catch` blocks for the sake of readability and compactness, and to
save typing/avoid boilerplate code.
  • Loading branch information
jhlegarreta authored and dzenanz committed Apr 16, 2022
1 parent 4b0a501 commit 5ff58ba
Show file tree
Hide file tree
Showing 18 changed files with 97 additions and 284 deletions.
11 changes: 2 additions & 9 deletions Modules/Core/Mesh/test/itkWarpMeshFilterTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,8 @@ itkWarpMeshFilterTest(int, char *[])

warpFilter->SetDisplacementField(deformationField);

try
{
warpFilter->Update();
}
catch (const itk::ExceptionObject & excp)
{
std::cerr << "Exception: " << excp << std::endl;
return EXIT_FAILURE;
}
ITK_TRY_EXPECT_NO_EXCEPTION(warpFilter->Update());


MeshType::Pointer outputMesh = warpFilter->GetOutput();
MeshType::ConstPointer inputMesh = warpFilter->GetInput();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,33 +45,19 @@ itkVTKPolyDataIOQuadEdgeMeshTest(int argc, char * argv[])

std::cout << "polyDataReader:" << std::endl;
std::cout << polyDataReader << std::endl;
try
{
polyDataReader->Update();
}
catch (const itk::ExceptionObject & excp)
{
std::cerr << "Error during Update() " << std::endl;
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}

ITK_TRY_EXPECT_NO_EXCEPTION(polyDataReader->Update());


MeshType::Pointer mesh = polyDataReader->GetOutput();

polyDataWriter->SetInput(mesh);

std::cout << "polyDataWriter:" << std::endl;
std::cout << polyDataWriter << std::endl;
try
{
polyDataWriter->Update();
}
catch (const itk::ExceptionObject & excp)
{
std::cerr << "Error during Update() " << std::endl;
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}

ITK_TRY_EXPECT_NO_EXCEPTION(polyDataWriter->Update());


// Should make a diff

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "itkBinaryThresholdImageFilter.h"
#include "itkImageRegionConstIterator.h"
#include "itkTestingMacros.h"


int
Expand Down Expand Up @@ -123,16 +124,8 @@ itkFastMarchingImageFilterRealWithNumberOfElementsTest(int, char *[])

marcher->SetInput(speedImage);

try
{
// update the marcher
marcher->Update();
}
catch (const itk::ExceptionObject & excep)
{
std::cerr << excep << std::endl;
return EXIT_FAILURE;
}
ITK_TRY_EXPECT_NO_EXCEPTION(marcher->Update());


using OutputPixelType = char;

Expand All @@ -148,16 +141,8 @@ itkFastMarchingImageFilterRealWithNumberOfElementsTest(int, char *[])
thresholder->SetInsideValue(1);
thresholder->SetInput(marcher->GetOutput());

try
{
thresholder->Update();
}
catch (const itk::ExceptionObject & excep)
{
std::cerr << "Exception caught !" << std::endl;
std::cerr << excep << std::endl;
return EXIT_FAILURE;
}
ITK_TRY_EXPECT_NO_EXCEPTION(thresholder->Update());


OutputImageType::Pointer output = thresholder->GetOutput();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "itkRegularSphereMeshSource.h"
#include "itkFastMarchingThresholdStoppingCriterion.h"
#include "itkMeshFileWriter.h"
#include "itkTestingMacros.h"

int
itkFastMarchingQuadEdgeMeshFilterBaseTest(int, char *[])
Expand Down Expand Up @@ -84,16 +85,8 @@ itkFastMarchingQuadEdgeMeshFilterBaseTest(int, char *[])
fmm_filter->SetTrialPoints(trial);
fmm_filter->SetStoppingCriterion(criterion);

try
{
fmm_filter->Update();
}
catch (const itk::ExceptionObject & excep)
{
std::cerr << "Exception caught !" << std::endl;
std::cerr << excep << std::endl;
return EXIT_FAILURE;
}
ITK_TRY_EXPECT_NO_EXCEPTION(fmm_filter->Update());


using WriterType = itk::MeshFileWriter<MeshType>;
auto writer = WriterType::New();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "itkQuadEdgeMeshExtendedTraits.h"
#include "itkRegularSphereMeshSource.h"
#include "itkFastMarchingNumberOfElementsStoppingCriterion.h"
#include "itkTestingMacros.h"

int
itkFastMarchingQuadEdgeMeshFilterWithNumberOfElementsTest(int, char *[])
Expand Down Expand Up @@ -83,16 +84,8 @@ itkFastMarchingQuadEdgeMeshFilterWithNumberOfElementsTest(int, char *[])
fmm_filter->SetTrialPoints(trial);
fmm_filter->SetStoppingCriterion(criterion);

try
{
fmm_filter->Update();
}
catch (const itk::ExceptionObject & excep)
{
std::cerr << "Exception caught !" << std::endl;
std::cerr << excep << std::endl;
return EXIT_FAILURE;
}
ITK_TRY_EXPECT_NO_EXCEPTION(fmm_filter->Update());


MeshType::Pointer output = fmm_filter->GetOutput();

Expand Down
16 changes: 5 additions & 11 deletions Modules/Filtering/ImageGrid/test/itkTileImageFilterTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,11 @@ itkTileImageFilterTest(int argc, char * argv[])

writer->SetSeriesFormat(argv[argc - 1]);

try
{
writer->SetInput(tiler->GetOutput());
writer->Update();
}
catch (const itk::ExceptionObject & excp)
{
std::cerr << "Error while writing the series with SeriesFileNames generator" << std::endl;
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
ITK_TRY_EXPECT_NO_EXCEPTION(writer->SetInput(tiler->GetOutput()));

ITK_TRY_EXPECT_NO_EXCEPTION(writer->Update());


std::cout << "Test passed." << std::endl;
return EXIT_SUCCESS;
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,8 @@ itkCleanQuadEdgeMeshFilterTest(int argc, char * argv[])
auto reader = ReaderType::New();
reader->SetFileName(argv[1]);

try
{
reader->Update();
}
catch (const itk::ExceptionObject & excp)
{
std::cerr << "Exception thrown while reading the input file " << std::endl;
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
ITK_TRY_EXPECT_NO_EXCEPTION(reader->Update());


MeshType::Pointer mesh = reader->GetOutput();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,8 @@ itkLaplacianDeformationQuadEdgeMeshFilterWithHardConstraintsTest(int argc, char
++it;
}

try
{
filter->Update();
}
catch (const itk::ExceptionObject & except)
{
std::cerr << "Failure: " << except.what();
return EXIT_FAILURE;
}
ITK_TRY_EXPECT_NO_EXCEPTION(filter->Update());


using WriterType = itk::MeshFileWriter<MeshType>;
auto writer = WriterType::New();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,8 @@ itkRegularSphereQuadEdgeMeshSourceTest(int argc, char * argv[])

mySphereMeshSource->Modified();

try
{
mySphereMeshSource->Update();
}
catch (const itk::ExceptionObject & excp)
{
std::cerr << "Error during Update() " << std::endl;
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
ITK_TRY_EXPECT_NO_EXCEPTION(mySphereMeshSource->Update());


std::cout << "mySphereMeshSource: " << mySphereMeshSource;

Expand Down
25 changes: 3 additions & 22 deletions Modules/IO/BioRad/test/itkBioRadImageIOTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -48,35 +48,16 @@ itkBioRadImageIOTest(int argc, char * argv[])
auto bioradImageIO = ImageIOType::New();
reader->SetImageIO(bioradImageIO);

try
{
reader->Update();
}
catch (const itk::ExceptionObject & e)
{
std::cerr << "exception in file reader " << std::endl;
std::cerr << e.GetDescription() << std::endl;
std::cerr << e.GetLocation() << std::endl;
return EXIT_FAILURE;
}
ITK_TRY_EXPECT_NO_EXCEPTION(reader->Update());


using WriterType = itk::ImageFileWriter<InputImageType>;
auto writer = WriterType::New();
writer->SetFileName(outfilename);
writer->SetInput(reader->GetOutput());
writer->SetImageIO(bioradImageIO);

try
{
writer->Update();
}
catch (const itk::ExceptionObject & e)
{
std::cerr << "exception in file writer " << std::endl;
std::cerr << e.GetDescription() << std::endl;
std::cerr << e.GetLocation() << std::endl;
return EXIT_FAILURE;
}
ITK_TRY_EXPECT_NO_EXCEPTION(writer->Update());

bioradImageIO->Print(std::cout);

Expand Down
24 changes: 6 additions & 18 deletions Modules/IO/MeshVTK/test/itkMeshFileWriteReadTensorTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,9 @@ itkMeshFileWriteReadTensorTest(int argc, char * argv[])

mesh2dWriter->SetInput(mesh2d);

try
{
mesh2dWriter->Update();
}
catch (const itk::ExceptionObject & excp)
{
std::cerr << "Failed the VTKPolyDataMeshIO 2d test" << excp;
return EXIT_FAILURE;
}
ITK_TRY_EXPECT_NO_EXCEPTION(mesh2dWriter->Update());


std::cout << "End of VTKPolyDataMeshIO 2D test. Completed successfully!" << std::endl << std::endl;


Expand Down Expand Up @@ -118,15 +112,9 @@ itkMeshFileWriteReadTensorTest(int argc, char * argv[])

mesh3dWriter->SetInput(mesh3d);

try
{
mesh3dWriter->Update();
}
catch (const itk::ExceptionObject & excp)
{
std::cerr << "Failed the VTKPolyDataMeshIO 3D test" << excp;
return EXIT_FAILURE;
}
ITK_TRY_EXPECT_NO_EXCEPTION(mesh3dWriter->Update());


std::cout << "End of VTKPolyDataMeshIO 3D test. Completed successfully!" << std::endl << std::endl;

return EXIT_SUCCESS;
Expand Down
13 changes: 3 additions & 10 deletions Modules/IO/Stimulate/test/itkStimulateImageIOTest2.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,9 @@ itkStimulateImageIOTest2(int argc, char * argv[])
std::cout << "Filename: " << argv[1] << std::endl;
reader->SetFileName(argv[1]);
reader->SetImageIO(io);
try
{
reader->Update();
}
catch (const itk::ExceptionObject & e)
{
std::cout << "Exception in file reader " << std::endl;
std::cout << e << std::endl;
return EXIT_FAILURE;
}

ITK_TRY_EXPECT_NO_EXCEPTION(reader->Update());


myImage::Pointer image = reader->GetOutput();
image->Print(std::cout);
Expand Down
Loading

0 comments on commit 5ff58ba

Please sign in to comment.