DryChem 1.0.0
A generic, compile-time C++ toolbox with no dependencies for the modern computational chemistry project.
Loading...
Searching...
No Matches
testExceptionHandling.hpp
Go to the documentation of this file.
1// Copyright (c) 2020-2025 Cody R. Drisko. All rights reserved.
2// Licensed under the MIT License. See the LICENSE file in the project root for more information.
3//
4// Name: testExceptionHandling.hpp
5// Author: crdrisko
6// Date: 08/27/2020-12:07:48
7// Description: Provides ~100% unit test coverage over all exception handing functions
8
9#ifndef DRYCHEM_COMMON_UTILITIES_LIBS_ERRORS_TESTS_TESTEXCEPTIONS_TESTEXCEPTIONHANDLING_HPP
10#define DRYCHEM_COMMON_UTILITIES_LIBS_ERRORS_TESTS_TESTEXCEPTIONS_TESTEXCEPTIONHANDLING_HPP
11
12#include <exception>
13#include <iostream>
14#include <sstream>
15#include <string>
16
18#include <gtest/gtest.h>
19
20GTEST_TEST(testExceptionHandling, thisIsHowWeWouldCatchAndHandleAStdException)
21{
22 std::stringstream deathRegex;
23
24 deathRegex << "YourProgramName Fatal Error:\n An exception was thrown from " << std::exception().what();
25
26 ASSERT_DEATH(
27 {
28 try
29 {
30 try
31 {
32 throw std::exception();
33 }
34 catch (const std::exception& except)
35 {
36 // Toss the exception back up for program termination and a more verbose message
37 DryChem::ErrorMessage error1;
38 error1.programName = "YourProgramName";
39 error1.message = "An exception was thrown from " + std::string {except.what()};
40
41 throw DryChem::FatalException(error1);
42 }
43 }
44 catch (const DryChem::FatalException& except)
45 {
46 except.handleErrorWithMessage();
47 }
48 },
49 deathRegex.str());
50}
51
52GTEST_TEST(testExceptionHandling, thisIsHowWeWouldCatchAndHandleAFatalException)
53{
54 std::stringstream deathRegex;
55
56 deathRegex << "YourProgramName Fatal Error:\n An exception was thrown from ";
57
58#if GTEST_USES_POSIX_RE
59 deathRegex << "[(]testExceptionHandling.hpp: *[0-9]*[)]";
60#elif GTEST_USES_SIMPLE_RE
61 deathRegex << "\\(testExceptionHandling.hpp: \\d*\\)";
62#endif
63
64 deathRegex << "\n\tLocation message.\n";
65
66 ASSERT_DEATH(
67 {
68 try
69 {
70 try
71 {
72 throw DryChem::FatalException(
73 DryChem::ErrorMessage {"Common-Utilities", "Location message.", __FILE__, __LINE__});
74 }
75 catch (const std::exception& except)
76 {
77 // Toss the exception back up for program termination and a more verbose message
78 DryChem::ErrorMessage error1;
79 error1.programName = "YourProgramName";
80 error1.message = "An exception was thrown from " + std::string {except.what()};
81
82 throw DryChem::FatalException(error1);
83 }
84 }
85 catch (const DryChem::FatalException& except)
86 {
87 except.handleErrorWithMessage();
88 }
89 },
90 deathRegex.str());
91}
92
93GTEST_TEST(testExceptionHandling, fatalErrorsAreHandledByTerminating)
94{
95 std::stringstream deathRegex;
96
97 deathRegex << "Common-Utilities Fatal Error: ";
98
99#if GTEST_USES_POSIX_RE
100 deathRegex << "[(]testExceptionHandling.hpp: *[0-9]*[)]";
101#elif GTEST_USES_SIMPLE_RE
102 deathRegex << "\\(testExceptionHandling.hpp: \\d*\\)";
103#endif
104
105 deathRegex << "\n\tThis would be the error message.\n";
106
107 ASSERT_DEATH(
108 {
109 DryChem::ErrorMessage error;
110 error.programName = "Common-Utilities";
111 error.message = "This would be the error message.";
112 error.fileName = __FILE__;
113 error.lineNumber = __LINE__;
114
115 DryChem::FatalException exceptFatal {error};
116 exceptFatal.handleErrorWithMessage();
117 },
118 deathRegex.str());
119}
120
121GTEST_TEST(testExceptionHandling, derivedExceptionClassIsCaughtByParentClass)
122{
123 testing::internal::CaptureStderr();
124
125 try
126 {
127 DryChem::ErrorMessage error {};
128 error.programName = "Common-Utilities";
129 error.message = "Let's throw a non-fatal warning.";
130 error.fileName = __FILE__;
131 error.lineNumber = __LINE__;
132
133 throw DryChem::FatalException(error);
134 }
135 catch (const std::exception& except)
136 {
137 std::cerr << except.what() << std::endl;
138 }
139
140 std::string actualOutput = testing::internal::GetCapturedStderr();
141 ASSERT_EQ(actualOutput, "(testExceptionHandling.hpp: 131)\n\tLet's throw a non-fatal warning.\n");
142}
143
144#endif
GTEST_TEST(testExceptionHandling, thisIsHowWeWouldCatchAndHandleAStdException)
Definition testExceptionHandling.hpp:20