CppEphem
test_CEException.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * test_CEException.cpp: CppEphem *
3  * ----------------------------------------------------------------------- *
4  * Copyright © 2018 JCardenzana *
5  * ----------------------------------------------------------------------- *
6  * *
7  * This program is free software: you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation, either version 3 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19  * *
20  ***************************************************************************/
21 
22 #include "test_CEException.h"
23 #include "CENamespace.h"
24 
25 
26 /**********************************************************************/
30  CETestSuite()
31 {}
32 
33 
34 /**********************************************************************/
38 {}
39 
40 
41 /**********************************************************************/
47 {
48  std::cout << "\nTesting test_CEException:\n";
49 
50  // Run each of the tests
56 
57  return pass();
58 }
59 
60 
61 /**********************************************************************/
66 {
67  // Create a message
68  std::string msg = "Testing throwing 'CEException::invalid_value'";
69 
70  // Try throwing
71  try {
72  throw CEException::invalid_value("test_CEException::test_invalid_value()",
73  msg);
74  // If we get here there's a problem
75  log_failure("CEException::invalid_value failed to throw", __func__, __LINE__);
76  update_pass(false);
77  } catch (CEException::invalid_value & e) {
78  // This is where we should be
79  log_success("CEException::invalid_value thrown successfully", __func__, __LINE__);
80  update_pass(true);
81 
82  // Test that we can access the message
83  std::string test_msg(e.message());
84  test_string(test_msg, msg, __func__, __LINE__);
85 
86  // Test that the backtrace is in fact filled
87  std::string test_trace(e.trace());
88  test_greaterthan(test_trace.size(), 1, __func__, __LINE__);
89 
90  // Make sure the combined output exceeds both the message and trace
91  std::string test_what(e.what());
92  test_greaterthan(test_what.size(), test_msg.size()+test_trace.size(), __func__, __LINE__);
93 
94  // See if we can append to the exception
95  std::string msg2(" ... now with more text!");
96  e.message_append(msg2);
97  test_msg = std::string(e.message());
98  test_string(test_msg, msg+msg2, __func__, __LINE__);
99  }
100 
101  return pass();
102 }
103 
104 
105 /**********************************************************************/
110 {
111  // Try throwing
112  try {
113  throw CEException::invalid_value("test_CEException::test_invalid_value()",
114  "Testing throwing 'CEException::invalid_value'");
115  // If we get here there's a problem
116  log_failure("CEException::invalid_value failed to throw", __func__, __LINE__);
117  update_pass(false);
118  } catch (CEException::invalid_value & e) {
119  // This is where we should be
120  log_success("CEException::invalid_value thrown successfully", __func__, __LINE__);
121  update_pass(true);
122  }
123 
124  return pass();
125 }
126 
127 
128 /**********************************************************************/
133 {
134  // Try throwing
135  try {
136  // TODO: find a sofa method that can be configured to return a bad error code
137  throw CEException::sofa_error("test_CEException::test_sofa_error()",
138  "No method currently checked",
139  1, "Testing throwing 'CEException::sofa_error");
140  // If we get here there's a problem
141  log_failure("CEException::sofa_error failed to throw", __func__, __LINE__);
142  update_pass(false);
143  } catch (CEException::sofa_error & e) {
144  // This is where we should be
145  log_success("CEException::sofa_error thrown successfully", __func__, __LINE__);
146  update_pass(true);
147  }
148 
149  return pass();
150 }
151 
152 
153 /**********************************************************************/
158 {
159  // Try throwing
160  try {
161  // TODO: find a sofa method that can be configured to return a bad error code
162  throw CEException::sofa_exception("test_CEException::test_sofa_exception()",
163  "No method currently checked",
164  "Testing throwing 'CEException::sofa_exception'");
165  // If we get here there's a problem
166  log_failure("CEException::sofa_exception failed to throw", __func__, __LINE__);
167  update_pass(false);
168  } catch (CEException::sofa_exception & e) {
169  // This is where we should be
170  log_success("CEException::sofa_exception thrown successfully", __func__, __LINE__);
171  update_pass(true);
172  }
173 
174  return pass();
175 }
176 
177 
178 /**********************************************************************/
183 {
184  // Try throwing
185  try {
186  // TODO: find a sofa method that can be configured to return a bad error code
187  throw CEException::corr_file_load_error("test_CEException::test_corr_file_except()",
188  "Testing throwing 'CEException::corr_file_load_error'");
189  // If we get here there's a problem
190  log_failure("CEException::corr_file_load_error failed to throw", __func__, __LINE__);
191  update_pass(false);
192  } catch (CEException::corr_file_load_error & e) {
193  // This is where we should be
194  log_success("CEException::corr_file_load_error thrown and caught successfully", __func__, __LINE__);
195  update_pass(true);
196  }
197 
198  return pass();
199 }
200 
201 
202 /**********************************************************************/
205 int main(int argc, char** argv)
206 {
207  test_CEException tester;
208  return (!tester.runtests());
209 }
test_CEException::test_CEException
test_CEException()
Default constructor.
Definition: test_CEException.cpp:29
main
int main(int argc, char **argv)
Main method that actually runs the tests.
Definition: test_CEException.cpp:205
CENamespace.h
CEExceptionHandler::message
virtual void message(const std::string &msg)
Overwrite the error message.
Definition: CEException.cpp:73
test_CEException.h
test_CEException::test_sofa_exception
virtual bool test_sofa_exception(void)
Try throwing 'CEException::sofa_exception'.
Definition: test_CEException.cpp:157
CEException::sofa_error
Definition: CEException.h:102
CEExceptionHandler::trace
virtual const char * trace(void) const noexcept
Return the backtrace string.
Definition: CEException.cpp:91
CEExceptionHandler::message_append
virtual void message_append(const std::string &msg)
Append a string to the end of the message.
Definition: CEException.cpp:63
test_CEException::~test_CEException
virtual ~test_CEException()
Destructor.
Definition: test_CEException.cpp:37
test_CEException::test_construct
virtual bool test_construct(void)
Try constructing and accesing information about a CEException.
Definition: test_CEException.cpp:65
test_CEException::test_invalid_value
virtual bool test_invalid_value(void)
Try throwing 'CEException::invalid_value'.
Definition: test_CEException.cpp:109
test_CEException::runtests
virtual bool runtests()
Run tests.
Definition: test_CEException.cpp:46
test_CEException
Definition: test_CEException.h:27
CEException::invalid_value
Definition: CEException.h:73
test_CEException::test_sofa_error
virtual bool test_sofa_error(void)
Try throwing 'CEException::sofa_errcode'.
Definition: test_CEException.cpp:132
CEException::sofa_exception
Definition: CEException.h:110
CEExceptionHandler::what
virtual const char * what() const noexcept
Generates and returns the error message.
Definition: CEException.cpp:48
test_CEException::test_corr_file_except
virtual bool test_corr_file_except(void)
Try throwing 'CEException::sofa_exception'.
Definition: test_CEException.cpp:182
CEException::corr_file_load_error
Definition: CEException.h:89