BDE 4.14.0 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
101
102namespace bsls {
103
104 // =========================
105 // class AssertTestException
106 // =========================
107
108/// This class is an implementation detail of the `bsls` testing framework
109/// and should not be used directly in user code. It implements an
110/// immutable mechanism to communicate information about the context of an
111/// assertion that fails to a test-case handler.
112///
113/// See @ref bsls_asserttestexception
115
116 // DATA
117 const char *d_expression; // expression that failed to assert as 'true'
118 const char *d_filename; // name of file where the assert failed
119 const int d_lineNumber; // line number in file where the assert failed
120 const char *d_level; // level of failed assertion or review
121
122 private:
123 // NOT IMPLEMENTED
124 AssertTestException& operator=(
126
127 public:
128 // CREATORS
129
130 /// Create a `AssertTestException` object with the specified
131 /// `expression`, `filename`, `lineNumber`, and `level`. The behavior
132 /// is undefined unless `0 < line` and all of `expression`, `filename`,
133 /// and `level` point to valid null-terminated character strings that
134 /// will remain unmodified for the lifetime of this object (e.g., string
135 /// literals).
138 const char *filename,
139 int lineNumber,
140 const char *level = "UNKNOWN");
141
142#ifdef BSLS_COMPILERFEATURES_SUPPORT_DEFAULTED_FUNCTIONS
143 // To avoid warnings about future incompatibility due to the deleted copy
144 // assignment operator we declare the copy constructor as implicitly
145 // generated. For consistency the destructor was also placed here and
146 // declared to be explicitly generated.
147
148 /// Create a `AssertTestException` object that is a copy of the
149 /// specified `original`, having the same value for the `expression`,
150 /// `filename`, and `lineNumber` attributes. Note that this trivial
151 /// constructor's definition is compiler generated.
152 AssertTestException(const AssertTestException& original) = default;
153
154 /// Destroy this object. Note that this trivial destructor's definition
155 /// is compiler generated.
156 ~AssertTestException() = default;
157#endif
158
159 // ACCESSORS
160
161 /// Return a string containing the program source of the assertion that
162 /// has failed.
163 const char *expression() const;
164
165 /// Return a string containing the filename of the source file
166 /// containing the assertion that has failed.
167 const char *filename() const;
168
169 /// Return a string containing a representation of the level of
170 /// assertion or review macro that failed.
171 const char *level() const;
172
173 /// Return the number of the line within the file `filename` containing
174 /// the assertion that failed.
175 int lineNumber() const;
176};
177
178// ============================================================================
179// INLINE FUNCTION DEFINITIONS
180// ============================================================================
181
182 // =========================
183 // class AssertTestException
184 // =========================
185
186// CREATORS
188inline
190 const char *filename,
191 int lineNumber,
192 const char *level)
193: d_expression(expression)
194, d_filename(filename)
195, d_lineNumber(lineNumber)
196, d_level(level)
197{
198}
199
200// ACCESSORS
201inline
203{
204 return d_expression;
205}
206
207inline
209{
210 return d_filename;
211}
212
213inline
214const char *AssertTestException::level() const
215{
216 return d_level;
217}
218
219inline
221{
222 return d_lineNumber;
223}
224
225} // close package namespace
226
227#ifndef BDE_OPENSOURCE_PUBLICATION // BACKWARD_COMPATIBILITY
228// ============================================================================
229// BACKWARD COMPATIBILITY
230// ============================================================================
231
232/// This alias is defined for backward compatibility.
234#endif // BDE_OPENSOURCE_PUBLICATION -- BACKWARD_COMPATIBILITY
235
236
237
238#endif
239
240// ----------------------------------------------------------------------------
241// Copyright 2018 Bloomberg Finance L.P.
242//
243// Licensed under the Apache License, Version 2.0 (the "License");
244// you may not use this file except in compliance with the License.
245// You may obtain a copy of the License at
246//
247// http://www.apache.org/licenses/LICENSE-2.0
248//
249// Unless required by applicable law or agreed to in writing, software
250// distributed under the License is distributed on an "AS IS" BASIS,
251// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
252// See the License for the specific language governing permissions and
253// limitations under the License.
254// ----------------------------- END-OF-FILE ----------------------------------
255
256/** @} */
257/** @} */
258/** @} */
Definition bsls_asserttestexception.h:114
BSLS_KEYWORD_CONSTEXPR AssertTestException(const char *expression, const char *filename, int lineNumber, const char *level="UNKNOWN")
Definition bsls_asserttestexception.h:189
const char * level() const
Definition bsls_asserttestexception.h:214
const char * expression() const
Definition bsls_asserttestexception.h:202
const char * filename() const
Definition bsls_asserttestexception.h:208
int lineNumber() const
Definition bsls_asserttestexception.h:220
bsls::AssertTestException bsls_AssertTestException
This alias is defined for backward compatibility.
Definition bsls_asserttestexception.h:233
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
#define BSLS_KEYWORD_CONSTEXPR
Definition bsls_keyword.h:588
#define BSLS_KEYWORD_DELETED
Definition bsls_keyword.h:609
Definition bdlt_iso8601util.h:691