BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bsls_asserttestexception.h
Go to the documentation of this file.
1/// @file bsls_asserttestexception.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bsls_asserttestexception.h -*-C++-*-
8#ifndef INCLUDED_BSLS_ASSERTTESTEXCEPTION
9#define INCLUDED_BSLS_ASSERTTESTEXCEPTION
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bsls_asserttestexception bsls_asserttestexception
15/// @brief Provide an exception type to support testing for failed assertions.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bsls
19/// @{
20/// @addtogroup bsls_asserttestexception
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bsls_asserttestexception-purpose"> Purpose</a>
25/// * <a href="#bsls_asserttestexception-classes"> Classes </a>
26/// * <a href="#bsls_asserttestexception-description"> Description </a>
27/// * <a href="#bsls_asserttestexception-usage"> Usage </a>
28///
29/// # Purpose {#bsls_asserttestexception-purpose}
30/// Provide an exception type to support testing for failed assertions.
31///
32/// # Classes {#bsls_asserttestexception-classes}
33///
34/// - bsls::AssertTestException: type describing the context of failed assertion
35///
36/// @see bsls_assert, bsls_asserttest
37///
38/// # Description {#bsls_asserttestexception-description}
39/// This component implements an exception class,
40/// `bsls::AssertTestException`, that provides a mechanism to convey context
41/// information from a failing assertion to a test handler. The context that is
42/// captured consists of the program source of the failing expression, the name
43/// of the file containing the assertion, the line number within that file where
44/// the asserted expression may be found, and the level of the assertion that
45/// has failed.
46///
47/// Note that this class is intended as an implementation detail of the `bsls`
48/// testing framework (see @ref bsls_asserttest ), though it may be used in other
49/// contexts.
50///
51/// ## Usage {#bsls_asserttestexception-usage}
52///
53///
54/// First we write a macro to act as an `assert` facility that will throw an
55/// exception of type `bsls::AssertTestException` if the asserted expression
56/// fails. The thrown exception will capture the source code, filename, and
57/// line number of the failing expression:
58/// @code
59/// #define TEST_ASSERT(EXPRESSION) \$
60/// if (!(EXPRESSION)) { \$
61/// throw bsls::AssertTestException(#EXPRESSION, __FILE__, __LINE__, \$
62/// "LEVEL"); \$
63/// }
64/// @endcode
65/// Next we use the macro inside a `try`-block, so that we can catch the
66/// exception thrown if the tested expression fails:
67/// @code
68/// try {
69/// void *p = NULL;
70/// TEST_ASSERT(0 != p);
71/// }
72/// @endcode
73/// If the assertion fails, catch the exception and confirm that it correctly
74/// recorded the context of where the assertion failed:
75/// @code
76/// catch (const bsls::AssertTestException& exception) {
77/// assert(0 == strcmp("0 != p", exception.expression()));
78/// assert(0 == strcmp(__FILE__, exception.filename()));
79/// assert(9 == __LINE__ - exception.lineNumber());
80/// assert(0 == strcmp("LEVEL", exception.level());
81/// }
82/// @endcode
83/// @}
84/** @} */
85/** @} */
86
87/** @addtogroup bsl
88 * @{
89 */
90/** @addtogroup bsls
91 * @{
92 */
93/** @addtogroup bsls_asserttestexception
94 * @{
95 */
96
97#include <bsls_compilerfeatures.h>
98#include <bsls_keyword.h>
99
100#include <cstring>
101#include <cstdlib>
102
103
104
105namespace bsls {
106
107 // ================================
108 // class AssertTestException_String
109 // ================================
110
111/// This non-assignable class owns a null-terminated string, provides no
112/// accessors to manipulate that string, and allocates a new copy of that
113/// string when copied.
114///
115/// See @ref bsls_asserttestexception
117 const char *d_data; // the string
118
119 private:
120 // NOT IMPLEMENTED
123
124 public:
125 // Create an `AssertTestException_String` that owns a copy of the string
126 // denoted by the specified `data` if it is not `NULL`.
128
129 // Create an `AssertionTestException_String` that owns a string whose value
130 // is the same as that owned by `original` if any.
132
133#ifdef BSLS_COMPILERFEATURES_SUPPORT_RVALUE_REFERENCES
134 // Take ownership of the string owned by the specified `original`, if any,
135 // and set `original` to own no string.
137#endif
138
139 // Free the string owned by this object, if any.
141
142 // Return a pointer to the owned string.
143 const char *data() const;
144};
145
146 // =========================
147 // class AssertTestException
148 // =========================
149
150/// This class is an implementation detail of the `bsls` testing framework
151/// and should not be used directly in user code. It implements an
152/// immutable mechanism to communicate information about the context of an
153/// assertion that fails to a test-case handler.
154///
155/// See @ref bsls_asserttestexception
157
158 // DATA
159 AssertTestException_String d_expression;
160 // expression that failed to assert as 'true'
162 // name of file where the assert failed
163 const int d_lineNumber;
164 // line number in file where the assert failed
166 // level of failed assertion or review
167
168 private:
169 // NOT IMPLEMENTED
170 AssertTestException& operator=(
172
173 public:
174 // CREATORS
175
176 /// Create a `AssertTestException` object with the specified
177 /// `expression`, `filename`, `lineNumber`, and `level`.
178 ///
179 /// \pre The behavior is undefined unless `0 < line` and all of `expression`, `filename`,
180 /// and `level` are `NULL` or point to valid null-terminated character
181 /// strings.
183 const char *filename,
184 int lineNumber,
185 const char *level = "UNKNOWN");
186
187#ifdef BSLS_COMPILERFEATURES_SUPPORT_DEFAULTED_FUNCTIONS
188 // To avoid warnings about future incompatibility due to the deleted copy
189 // assignment operator we declare the copy constructor as implicitly
190 // generated. For consistency the destructor was also placed here and
191 // declared to be explicitly generated.
192
193 /// Create a `AssertTestException` object that is a copy of the
194 /// specified `original`, having the same value for the `expression`, `filename`, and `lineNumber` attributes.
195 ///
196 /// \note Note that this
197 /// constructor's definition is compiler generated.
198 AssertTestException(const AssertTestException& original) = default;
199
200#ifdef BSLS_COMPILERFEATURES_SUPPORT_RVALUE_REFERENCES
201 /// Create a `AssertTestException` object whose value is that of
202 /// specified `original`, having the same value for the `expression`, `filename`, and `lineNumber` attributes.
203 ///
204 /// \note Note that this
205 /// constructor's definition is compiler generated.
206 AssertTestException(AssertTestException&& original) = default;
207#endif
208
209 /// Destroy this object.
210 /// \note Note that this destructor's definition is
211 /// compiler generated.
212 ~AssertTestException() = default;
213#endif
214
215 // ACCESSORS
216
217 /// Return a string containing the program source of the assertion that
218 /// has failed.
219 const char *expression() const;
220
221 /// Return a string containing the filename of the source file
222 /// containing the assertion that has failed.
223 const char *filename() const;
224
225 /// Return a string containing a representation of the level of
226 /// assertion or review macro that failed.
227 const char *level() const;
228
229 /// Return the number of the line within the file `filename` containing
230 /// the assertion that failed.
231 int lineNumber() const;
232};
233
234// ============================================================================
235// INLINE FUNCTION DEFINITIONS
236// ============================================================================
237
238 // =========================
239 // class AssertTestException
240 // =========================
241
242// CREATORS
243inline
245 const char *filename,
246 int lineNumber,
247 const char *level)
248: d_expression(expression)
249, d_filename(filename)
250, d_lineNumber(lineNumber)
251, d_level(level)
252{
253}
254
255// ACCESSORS
256inline
258{
259 return d_expression.data();
260}
261
262inline
264{
265 return d_filename.data();
266}
267
268inline
269const char *AssertTestException::level() const
270{
271 return d_level.data();
272}
273
274inline
276{
277 return d_lineNumber;
278}
279
280} // close package namespace
281
282#ifndef BDE_OPENSOURCE_PUBLICATION // BACKWARD_COMPATIBILITY
283// ============================================================================
284// BACKWARD COMPATIBILITY
285// ============================================================================
286
287/// This alias is defined for backward compatibility.
289#endif // BDE_OPENSOURCE_PUBLICATION -- BACKWARD_COMPATIBILITY
290
291
292
293#endif
294
295// ----------------------------------------------------------------------------
296// Copyright 2018 Bloomberg Finance L.P.
297//
298// Licensed under the Apache License, Version 2.0 (the "License");
299// you may not use this file except in compliance with the License.
300// You may obtain a copy of the License at
301//
302// http://www.apache.org/licenses/LICENSE-2.0
303//
304// Unless required by applicable law or agreed to in writing, software
305// distributed under the License is distributed on an "AS IS" BASIS,
306// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
307// See the License for the specific language governing permissions and
308// limitations under the License.
309// ----------------------------- END-OF-FILE ----------------------------------
310
311/** @} */
312/** @} */
313/** @} */
Definition bsls_asserttestexception.h:116
AssertTestException_String(const char *data)
AssertTestException_String(const AssertTestException_String &original)
Definition bsls_asserttestexception.h:156
const char * level() const
Definition bsls_asserttestexception.h:269
const char * expression() const
Definition bsls_asserttestexception.h:257
const char * filename() const
Definition bsls_asserttestexception.h:263
int lineNumber() const
Definition bsls_asserttestexception.h:275
AssertTestException(const char *expression, const char *filename, int lineNumber, const char *level="UNKNOWN")
Definition bsls_asserttestexception.h:244
bsls::AssertTestException bsls_AssertTestException
This alias is defined for backward compatibility.
Definition bsls_asserttestexception.h:288
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
#define BSLS_KEYWORD_DELETED
Definition bsls_keyword.h:651
Definition bdlt_iso8601util.h:707