Repository navigation
ENH: Add GoogleTests covering ITK exception classes #6956
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
hjmjohnson
merged 1 commit into
InsightSoftwareConsortium:main
from
hjmjohnson:enh-exception-class-gtests
Oct 23, 2026
+452
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)); | ||
| 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"); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
Modules/IO/ImageBase/test/itkImageIOExceptionsGTest.cxx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,5 +19,6 @@ itk_module( | |
| ITKVoronoi | ||
| TEST_DEPENDS | ||
| ITKTestKernel | ||
| ITKGoogleTest | ||
| DESCRIPTION "${DOCUMENTATION}" | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:But for now,
EXPECT_TRUE(Contains(...is fine to me. 👍There was a problem hiding this comment.
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.shimports onlygoogletest/,BUILD_GMOCKis forced OFF, and the drivers link onlyGTest::gtest. So I am keepingContainshere 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.