Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Modules/Core/Common/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1427,6 +1427,7 @@ set(
itkByteSwapGTest.cxx
itkBridgeCholeskySolveGTest.cxx
itkCommandObserverObjectGTest.cxx
itkCommonExceptionsGTest.cxx
itkCommonTypeTraitsGTest.cxx
itkCompensatedSummationGTest.cxx
itkConicShellInteriorExteriorSpatialFunctionGTest.cxx
Expand Down
166 changes: 166 additions & 0 deletions Modules/Core/Common/test/itkCommonExceptionsGTest.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/

// First include the header files to be tested:
#include "itkDataObject.h"
#include "itkDeref.h"
#include "itkExceptionObject.h"

#include "itkImage.h"
#include <gtest/gtest.h>
#include <sstream>
#include <string>
#include <type_traits>
#include <utility>


namespace
{
template <typename TException>
std::string
PrintToString(const TException & exception)
{
std::ostringstream stream;
stream << exception;
return stream.str();
}


bool
Contains(const std::string & text, const std::string & part)
{
return text.find(part) != std::string::npos;
}


// Checks an exception type that inherits the constructors of ExceptionObject.
template <typename TException>
void
CheckExceptionWithInheritedConstructors(const std::string & expectedName)
{
static_assert(std::is_base_of_v<itk::ExceptionObject, TException>);
static_assert(std::is_nothrow_default_constructible_v<TException>);
static_assert(std::is_nothrow_copy_constructible_v<TException>);
static_assert(std::is_nothrow_copy_assignable_v<TException>);

const TException defaultConstructed{};
EXPECT_EQ(defaultConstructed.GetNameOfClass(), expectedName);
EXPECT_STREQ(defaultConstructed.GetDescription(), "");
EXPECT_TRUE(Contains(PrintToString(defaultConstructed), "itk::" + expectedName));

const std::string testDescription = "test description";
const std::string testLocation = "test location";
const TException exception(__FILE__, __LINE__, testDescription, testLocation);
EXPECT_EQ(exception.GetNameOfClass(), expectedName);
EXPECT_EQ(exception.GetDescription(), testDescription);
EXPECT_EQ(exception.GetLocation(), testLocation);
EXPECT_TRUE(Contains(exception.what(), testDescription));

const std::string printed = PrintToString(exception);
EXPECT_TRUE(Contains(printed, "itk::" + expectedName));
EXPECT_TRUE(Contains(printed, "Description: " + testDescription));

const TException copy(exception);
EXPECT_EQ(copy, exception);

TException assigned;
assigned = exception;
EXPECT_EQ(assigned, exception);

const itk::ExceptionObject & base = exception;
EXPECT_EQ(base.GetNameOfClass(), expectedName);
}


// Checks a DataObjectError type, both without and with a data object attached.
template <typename TException>
void
CheckDataObjectError(const std::string & expectedName)
{
static_assert(std::is_base_of_v<itk::DataObjectError, TException>);

TException defaultConstructed;
EXPECT_EQ(defaultConstructed.GetNameOfClass(), expectedName);
EXPECT_EQ(defaultConstructed.GetDataObject(), nullptr);
EXPECT_EQ(std::as_const(defaultConstructed).GetDataObject(), nullptr);

const std::string printedWithoutDataObject = PrintToString(defaultConstructed);
EXPECT_TRUE(Contains(printedWithoutDataObject, "itk::" + expectedName));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a future improvement, I think it would be nice if we could use GoogleTest Matchers like HasSubstr, instead of using our own local Contains helper function:

EXPECT_THAT(printedWithoutDataObject, HasSubstr("itk::" + expectedName));

But for now, EXPECT_TRUE(Contains(... is fine to me. 👍

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed that matchers would be nicer. They need googlemock, though, and ITK neither vendors it nor builds it: UpdateFromUpstream.sh imports only googletest/, BUILD_GMOCK is forced OFF, and the drivers link only GTest::gtest. So I am keeping Contains here and treating matchers as out of scope until a larger investigation of using gMock in ITK is done: its benefit to the tests, the cost of vendoring it, and the system-GoogleTest builds. I will bring that to Discourse before proposing a change.

EXPECT_TRUE(Contains(printedWithoutDataObject, "Data object: (None)"));

const auto image = itk::Image<float, 2>::New();
TException exception(__FILE__, __LINE__);
exception.SetDataObject(image);
EXPECT_EQ(exception.GetDataObject(), image.get());
EXPECT_EQ(std::as_const(exception).GetDataObject(), image.get());

const std::string printedWithDataObject = PrintToString(exception);
EXPECT_TRUE(Contains(printedWithDataObject, "itk::" + expectedName));
EXPECT_TRUE(Contains(printedWithDataObject, "Data object:"));
EXPECT_FALSE(Contains(printedWithDataObject, "(None)"));
EXPECT_TRUE(Contains(printedWithDataObject, "LargestPossibleRegion"));

const TException copy(exception);
EXPECT_EQ(copy.GetDataObject(), image.get());

TException assigned;
assigned = exception;
EXPECT_EQ(assigned.GetDataObject(), image.get());

const itk::DataObjectError & base = exception;
EXPECT_EQ(base.GetNameOfClass(), expectedName);
EXPECT_EQ(base.GetDataObject(), image.get());
}
} // namespace


TEST(CommonExceptions, InheritedConstructors)
{
CheckExceptionWithInheritedConstructors<itk::MemoryAllocationError>("MemoryAllocationError");
CheckExceptionWithInheritedConstructors<itk::RangeError>("RangeError");
CheckExceptionWithInheritedConstructors<itk::InvalidArgumentError>("InvalidArgumentError");
CheckExceptionWithInheritedConstructors<itk::IncompatibleOperandsError>("IncompatibleOperandsError");
CheckExceptionWithInheritedConstructors<itk::DerefError>("DerefError");
}


TEST(CommonExceptions, ProcessAborted)
{
constexpr const char * expectedDescription = "Filter execution was aborted by an external request";

const itk::ProcessAborted defaultConstructed;
EXPECT_STREQ(defaultConstructed.GetNameOfClass(), "ProcessAborted");
EXPECT_STREQ(defaultConstructed.GetDescription(), expectedDescription);

const unsigned int line = __LINE__;
const itk::ProcessAborted exception(__FILE__, line);
EXPECT_STREQ(exception.GetDescription(), expectedDescription);
EXPECT_STREQ(exception.GetFile(), __FILE__);
EXPECT_EQ(exception.GetLine(), line);

const std::string printed = PrintToString(exception);
EXPECT_TRUE(Contains(printed, "itk::ProcessAborted"));
EXPECT_TRUE(Contains(printed, expectedDescription));
}


TEST(CommonExceptions, DataObjectErrors)
{
CheckDataObjectError<itk::DataObjectError>("DataObjectError");
CheckDataObjectError<itk::InvalidRequestedRegionError>("InvalidRequestedRegionError");
}
1 change: 1 addition & 0 deletions Modules/IO/ImageBase/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,7 @@ set(
itkImageFileReaderGTest1.cxx
itkImageIOBaseGTest.cxx
itkImageIOFileNameExtensionsGTests.cxx
itkImageIOExceptionsGTest.cxx
itkImageSeriesReaderOriginOverrideGTest.cxx
itkNumericSeriesFileNamesGTest.cxx
itkWriteImageFunctionGTest.cxx
Expand Down
104 changes: 104 additions & 0 deletions Modules/IO/ImageBase/test/itkImageIOExceptionsGTest.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/

// First include the header files to be tested:
#include "itkImageFileReaderException.h"
#include "itkImageFileWriter.h"
#include "itkImageSeriesWriter.h"

#include <gtest/gtest.h>
#include <sstream>
#include <string>
#include <type_traits>
#include <utility>


namespace
{
const std::string testMessage = "test message";
const std::string testLocation = "test location";


bool
Contains(const std::string & text, const std::string & part)
{
return text.find(part) != std::string::npos;
}


// Checks the behavior shared by the IO exceptions, which are constructed from a file, a line, and a message.
template <typename TException>
void
CheckIOException(const std::string & expectedName)
{
static_assert(std::is_base_of_v<itk::ExceptionObject, TException>);
static_assert(std::is_nothrow_destructible_v<TException>);

const unsigned int line = __LINE__;
const TException withDefaultMessage(__FILE__, line);
EXPECT_EQ(withDefaultMessage.GetNameOfClass(), expectedName);
EXPECT_STREQ(withDefaultMessage.GetDescription(), "Error in IO");
EXPECT_STREQ(withDefaultMessage.GetFile(), __FILE__);
EXPECT_EQ(withDefaultMessage.GetLine(), line);

const TException withMessage(__FILE__, __LINE__, testMessage);
EXPECT_EQ(withMessage.GetDescription(), testMessage);

std::ostringstream stream;
stream << withMessage;
EXPECT_TRUE(Contains(stream.str(), "itk::" + expectedName));
EXPECT_TRUE(Contains(stream.str(), "Description: " + testMessage));

TException copy(withMessage);
EXPECT_EQ(copy, withMessage);

const TException moved(std::move(copy));
EXPECT_EQ(moved.GetDescription(), testMessage);

TException assigned(__FILE__, __LINE__);
assigned = withMessage;
EXPECT_EQ(assigned, withMessage);

const itk::ExceptionObject & base = withMessage;
EXPECT_EQ(base.GetNameOfClass(), expectedName);
}


// Checks the IO exceptions that also accept a location.
template <typename TException>
void
CheckIOExceptionWithLocation(const std::string & expectedName)
{
CheckIOException<TException>(expectedName);

const TException exception(__FILE__, __LINE__, testMessage, testLocation);
EXPECT_EQ(exception.GetLocation(), testLocation);

TException moveAssigned(__FILE__, __LINE__);
moveAssigned = TException(exception);
EXPECT_EQ(moveAssigned.GetLocation(), testLocation);
}
} // namespace


TEST(ImageIOExceptions, ReaderAndWriterExceptions)
{
CheckIOExceptionWithLocation<itk::ImageFileReaderException>("ImageFileReaderException");
CheckIOExceptionWithLocation<itk::ImageFileWriterException>("ImageFileWriterException");
CheckIOException<itk::ImageSeriesWriterException>("ImageSeriesWriterException");
}
1 change: 1 addition & 0 deletions Modules/IO/MeshBase/itk-module.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ itk_module(
ITKVoronoi
TEST_DEPENDS
ITKTestKernel
ITKGoogleTest
DESCRIPTION "${DOCUMENTATION}"
)
3 changes: 3 additions & 0 deletions Modules/IO/MeshBase/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ itk_add_test(
ITK_REMOVE_TEMPORARY_TEST_FILES
${ITK_TEST_OUTPUT_DIR}/itkMeshFileReaderWriterTest.vtk
)

set(ITKIOMeshBaseGTests itkMeshIOExceptionsGTest.cxx)
creategoogletestdriver(ITKIOMeshBase "${ITKIOMeshBase-Test_LIBRARIES}" "${ITKIOMeshBaseGTests}")
90 changes: 90 additions & 0 deletions Modules/IO/MeshBase/test/itkMeshIOExceptionsGTest.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/

// First include the header files to be tested:
#include "itkMeshFileReaderException.h"
#include "itkMeshFileWriterException.h"

#include <gtest/gtest.h>
#include <sstream>
#include <string>
#include <type_traits>
#include <utility>


namespace
{
bool
Contains(const std::string & text, const std::string & part)
{
return text.find(part) != std::string::npos;
}


template <typename TException>
void
CheckMeshIOException(const std::string & expectedName)
{
static_assert(std::is_base_of_v<itk::ExceptionObject, TException>);
static_assert(std::is_nothrow_destructible_v<TException>);

const unsigned int line = __LINE__;
const TException withDefaults(__FILE__, line);
EXPECT_EQ(withDefaults.GetNameOfClass(), expectedName);
EXPECT_STREQ(withDefaults.GetDescription(), "Error in IO");
EXPECT_STREQ(withDefaults.GetFile(), __FILE__);
EXPECT_EQ(withDefaults.GetLine(), line);
EXPECT_STREQ(withDefaults.GetLocation(), "");

const std::string testMessage = "test message";
const std::string testLocation = "test location";
const TException exception(__FILE__, __LINE__, testMessage, testLocation);
EXPECT_EQ(exception.GetDescription(), testMessage);
EXPECT_EQ(exception.GetLocation(), testLocation);

std::ostringstream stream;
stream << exception;
EXPECT_TRUE(Contains(stream.str(), "itk::" + expectedName));
EXPECT_TRUE(Contains(stream.str(), "Location: \"" + testLocation + '"'));
EXPECT_TRUE(Contains(stream.str(), "Description: " + testMessage));

TException copy(exception);
EXPECT_EQ(copy, exception);

const TException moved(std::move(copy));
EXPECT_EQ(moved.GetDescription(), testMessage);

TException assigned(__FILE__, __LINE__);
assigned = exception;
EXPECT_EQ(assigned, exception);

TException moveAssigned(__FILE__, __LINE__);
moveAssigned = TException(exception);
EXPECT_EQ(moveAssigned.GetLocation(), testLocation);

const itk::ExceptionObject & base = exception;
EXPECT_EQ(base.GetNameOfClass(), expectedName);
}
} // namespace


TEST(MeshIOExceptions, ReaderAndWriterExceptions)
{
CheckMeshIOException<itk::MeshFileReaderException>("MeshFileReaderException");
CheckMeshIOException<itk::MeshFileWriterException>("MeshFileWriterException");
}
Loading
Loading