BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bsls_asserttest.h
Go to the documentation of this file.
1/// @file bsls_asserttest.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bsls_asserttest.h -*-C++-*-
8#ifndef INCLUDED_BSLS_ASSERTTEST
9#define INCLUDED_BSLS_ASSERTTEST
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bsls_asserttest bsls_asserttest
15/// @brief Provide a test facility for assertion macros.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bsls
19/// @{
20/// @addtogroup bsls_asserttest
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bsls_asserttest-purpose"> Purpose</a>
25/// * <a href="#bsls_asserttest-classes"> Classes </a>
26/// * <a href="#bsls_asserttest-macros"> Macros </a>
27/// * <a href="#bsls_asserttest-description"> Description </a>
28/// * <a href="#bsls_asserttest-negative-testing"> Negative Testing </a>
29/// * <a href="#bsls_asserttest-a-note-on-exceptions"> A Note On Exceptions </a>
30/// * <a href="#bsls_asserttest-the-test-facility"> The Test Facility </a>
31/// * <a href="#bsls_asserttest-installing-the-assert-failure-handler"> Installing the Assert-Failure Handler </a>
32/// * <a href="#bsls_asserttest-basic-test-macros"> Basic Test Macros </a>
33/// * <a href="#bsls_asserttest-raw-test-macros"> Raw Test Macros </a>
34/// * <a href="#bsls_asserttest-enabling-negative-testing"> Enabling Negative Testing </a>
35/// * <a href="#bsls_asserttest-validating-disabled-macro-expressions"> Validating Disabled Macro Expressions </a>
36/// * <a href="#bsls_asserttest-validating-macro-testing-levels"> Validating Macro Testing Levels </a>
37/// * <a href="#bsls_asserttest-addtional-test-pass-macros"> Addtional Test Pass Macros </a>
38/// * <a href="#bsls_asserttest-usage"> Usage </a>
39/// * <a href="#bsls_asserttest-example-1-testing-assertions-in-a-simple-vector-implementation"> Example 1: Testing Assertions In A Simple Vector Implementation </a>
40/// * <a href="#bsls_asserttest-example-2-using-pass-macros-to-help-with-formatting"> Example 2: Using PASS macros to help with formatting </a>
41///
42/// # Purpose {#bsls_asserttest-purpose}
43/// Provide a test facility for assertion macros.
44///
45/// # Classes {#bsls_asserttest-classes}
46///
47/// - bsls::AssertTest: namespace for "assert" validating functions
48/// - bsls::AssertTestHandlerGuard: guard for the negative testing assert-handler
49///
50/// # Macros {#bsls_asserttest-macros}
51///
52/// - BSLS_ASSERTTEST_ASSERT_FAIL(EXPRESSION): macro failure expected
53/// - BSLS_ASSERTTEST_ASSERT_FAIL_RAW(EXPRESSION): no origination check
54/// - BSLS_ASSERTTEST_ASSERT_PASS(EXPRESSION): macro success expected
55/// - BSLS_ASSERTTEST_ASSERT_PASS_RAW(EXPRESSION): macro success expected
56/// - BSLS_ASSERTTEST_ASSERT_OPT_FAIL(EXPRESSION): "opt" macro failure expected
57/// - BSLS_ASSERTTEST_ASSERT_OPT_FAIL_RAW(EXPRESSION): no origination check
58/// - BSLS_ASSERTTEST_ASSERT_OPT_PASS(EXPRESSION): macro success expected
59/// - BSLS_ASSERTTEST_ASSERT_OPT_PASS_RAW(EXPRESSION): macro success expected
60/// - BSLS_ASSERTTEST_ASSERT_SAFE_FAIL(EXPRESSION): "safe" macro failure expected
61/// - BSLS_ASSERTTEST_ASSERT_SAFE_FAIL_RAW(EXPRESSION): no origination check
62/// - BSLS_ASSERTTEST_ASSERT_SAFE_PASS(EXPRESSION): macro success expected
63/// - BSLS_ASSERTTEST_ASSERT_SAFE_PASS_RAW(EXPRESSION): macro success expected
64/// - BSLS_ASSERTTEST_ASSERT_INVOKE_FAIL(EXPRESSION): "invoke" macro expected
65/// - BSLS_ASSERTTEST_ASSERT_INVOKE_FAIL_RAW(EXPRESSION): no origination check
66/// - BSLS_ASSERTTEST_ASSERT_INVOKE_PASS(EXPRESSION): macro success expected
67/// - BSLS_ASSERTTEST_ASSERT_INVOKE_PASS_RAW(EXPRESSION): macro success expected
68///
69/// @see bsls_assert, bsls_asserttestexception
70///
71/// # Description {#bsls_asserttest-description}
72/// This component provides a facility to test that `BSLS_ASSERT_*`
73/// macros are used as intended, in the appropriate build modes, and have the
74/// expected effects. The class `bsls::AssertTest` provides a small set of
75/// static methods that can be used to support detailed test cases, especially
76/// in table-driven test scenarios. Additionally, a set of macros automate use
77/// of these methods to support simple testing of single expressions.
78///
79/// A testing-specific handler guard, `bsls::AssertTestHandlerGuard`, is also
80/// provided to be used wherever the `BSLS_ASSERTTEST_*` macros are used.
81///
82/// ## Negative Testing {#bsls_asserttest-negative-testing}
83///
84///
85/// "Negative testing" is the principle of testing for a negative result, which
86/// implies the function under test must fail in some way. Testable failures
87/// typically occur when a function is called with values outside the defined
88/// contract: a well-implemented function will validate function arguments, in
89/// appropriate build modes, using the various `BSLS_ASSERT` macros (see
90/// @ref bsls_assert ). When a function fails as a result of an assertion, the
91/// default behavior is to terminate the program. However, the @ref bsls_assert
92/// facility allows a user-supplied assertion-failure handler function to be
93/// installed, which can be used to build a test facility for expected
94/// assertions.
95///
96/// One important issue to be aware of with negative testing is that you are
97/// testing undefined behavior within a program. For the purpose of the test
98/// driver, the behavior of calling a function outside its contract is well-
99/// defined if it is guarded by assertions that are active in the current build
100/// mode. However, it is important that those tests are not run if the assert
101/// macros are not active, otherwise truly undefined behavior will result, with
102/// potentially disastrous consequences.
103///
104/// ### A Note On Exceptions {#bsls_asserttest-a-note-on-exceptions}
105///
106///
107/// It is important to note that this facility relies on throwing and catching
108/// an exception in order to identify that an assertion has been violated,
109/// cleanup any objects created on the way to that assertion, and avoid
110/// executing any of the code after that assertion with deliberately bad input.
111/// This means that this component cannot be used to test assertions in
112/// functions that are `noexcept`, particular destructors that are implicitly
113/// `noexcept` in C++11 and beyond.
114///
115/// For most functions, if you have a narrow contract you should not be
116/// `noexcept`, as this is guaranteeing part of your behavior when your contract
117/// is violated (and actively preventing you from doing negative testing in this
118/// manner). For functions such as a destructor that are implicitly `noexcept`
119/// and greatly benefit from being so, it is advisable to move the checks into a
120/// separate `validate` method, and test destruction out of contract by just
121/// testing that the validate method asserts instead.
122///
123/// ## The Test Facility {#bsls_asserttest-the-test-facility}
124///
125///
126///
127/// ### Installing the Assert-Failure Handler {#bsls_asserttest-installing-the-assert-failure-handler}
128///
129///
130/// The function `bsls::AssertTest::failTestDriver` (and the parallel function
131/// `bsls::AssertTest::failTestDriverByReview`) is provided as the basis for a
132/// negative testing facility. It can act as an assertion-failure handler
133/// function that throws an exception, of type `bsls::AssertTestException`,
134/// containing the text of the failed assertion, the name of the file where it
135/// triggered, and the relevant line number within that file. The filename can
136/// be tested to ensure that the assertion was raised by the component under
137/// test, rather than by some deeper implementation detail as a consequence of
138/// the expected assertion not being present in the function under test.
139///
140/// Once the function `bsls::AssertTest::failTestDriver` has been registered as
141/// the active assertion-failure handler, a set of testing macros automate much
142/// of the boilerplate code involved in writing a negative test, so that a test
143/// can be effectively written as a single line. This is an important quality
144/// for reading tests, to clearly see the test logic in action without being
145/// distracted by the surrounding machinery.
146///
147/// ### Basic Test Macros {#bsls_asserttest-basic-test-macros}
148///
149///
150/// The five basic test macros are
151/// * `BSLS_ASSERTTEST_ASSERT_PASS`
152/// * `BSLS_ASSERTTEST_ASSERT_SAFE_FAIL`
153/// * `BSLS_ASSERTTEST_ASSERT_FAIL`
154/// * `BSLS_ASSERTTEST_ASSERT_OPT_FAIL`
155/// * `BSLS_ASSERTTEST_ASSERT_INVOKE_FAIL`
156/// Each of these macros takes a single expression as an argument, tests whether
157/// an assertion is raised while evaluating that expression, and, if an
158/// assertion is both raised and expected, whether that assertion was raised by
159/// the component under test.
160///
161/// A test failure is indicated by invoking `ASSERT(EXPRESSION)`, where `ASSERT`
162/// is either a macro or function that must be defined by the test driver, and
163/// `EXPRESSION` is an expression that evaluates to `true` or `false` according
164/// to whether the `ASSERTTEST_ASSERT` macro was expected to `_PASS` or `_FAIL`.
165///
166/// For example, if we have `std::vector<int> v` and `v` is empty, then the
167/// macro test `BSLS_ASSERTTEST_ASSERT_SAFE_FAIL((v.back()))` will fail when the
168/// effective assertion-level is `BSLS_ASSERT_LEVEL_ASSERT_SAFE` unless an
169/// assertion is raised. However, if the assertion-level is not
170/// `BSLS_ASSERT_LEVEL_ASSERT_SAFE`, then the test will not be run.
171///
172/// ### Raw Test Macros {#bsls_asserttest-raw-test-macros}
173///
174///
175/// The four "raw" test macros are
176/// * `BSLS_ASSERTTEST_ASSERT_SAFE_FAIL_RAW`
177/// * `BSLS_ASSERTTEST_ASSERT_FAIL_RAW`
178/// * `BSLS_ASSERTTEST_ASSERT_OPT_FAIL_RAW`
179/// * `BSLS_ASSERTTEST_ASSERT_INVOKE_FAIL_RAW`
180/// These testing macros perform the same test as the corresponding basic
181/// testing macros, except that there is no check to confirm that the assertion
182/// originated in the component under test.
183///
184/// ### Enabling Negative Testing {#bsls_asserttest-enabling-negative-testing}
185///
186///
187/// In order to enable the negative testing facility, you must:
188/// * `#include` this component header, `bsls_asserttest.h`.
189/// * Supply an implementation of an `ASSERT` macro in your test driver.
190/// * Register `bsls::AssertTest::failTestDriver` as the active
191/// assertion-failure handler (preferably with an instance of
192/// `AssertTestHandlerGuard`).
193///
194/// ### Validating Disabled Macro Expressions {#bsls_asserttest-validating-disabled-macro-expressions}
195///
196///
197/// An additional external macro, `BSLS_ASSERTTEST_VALIDATE_DISABLED_MACROS`,
198/// can be defined to control the compile time behavior of @ref bsls_asserttest .
199/// Enabling this macro configures all *disabled* asserttest macros to still
200/// instantiate their expressions (in a non-evaluated context) to be sure that
201/// the expression is still syntactically valid. This can be used to ensure
202/// tests that are rarely enabled have valid expressions.
203///
204/// ### Validating Macro Testing Levels {#bsls_asserttest-validating-macro-testing-levels}
205///
206///
207/// Another external macro, `BSLS_ASSERTTEST_CHECK_LEVEL`, can be used to add an
208/// additional check that the assertion that fails is of the same level or
209/// narrower than the macro testing the assertion. This will ensure that in all
210/// build modes where the assertion is enabled the test for that assertion will
211/// also be enabled.
212///
213/// Note that some variations of language contracts might not support the
214/// checking of levels when testing assertions, and in those cases the macro
215/// `BSLS_ASSERTTEST_CAN_CHECK_LEVELS` will not be defined.
216///
217/// ### Addtional Test Pass Macros {#bsls_asserttest-addtional-test-pass-macros}
218///
219///
220/// Seven additional `PASS` macros exist to parallel the remaining `FAIL`
221/// macros.
222/// * `BSLS_ASSERTTEST_ASSERT_SAFE_PASS`
223/// * `BSLS_ASSERTTEST_ASSERT_SAFE_PASS_RAW`
224/// * `BSLS_ASSERTTEST_ASSERT_PASS_RAW`
225/// * `BSLS_ASSERTTEST_ASSERT_OPT_PASS`
226/// * `BSLS_ASSERTTEST_ASSERT_OPT_PASS_RAW`
227/// * `BSLS_ASSERTTEST_ASSERT_INVOKE_PASS`
228/// * `BSLS_ASSERTTEST_ASSERT_INVOKE_PASS_RAW`
229/// These macros are all functionally identical to
230/// `BSLS_ASSERTTEST_ASSERT_PASS`. They exist so that `PASS` checks format
231/// consistently with the corresponding negative tests they are associated with
232/// in a test driver. See {Example 2}, below, for how this can help formatting
233/// assertion testing code.
234///
235/// ## Usage {#bsls_asserttest-usage}
236///
237///
238///
239/// ### Example 1: Testing Assertions In A Simple Vector Implementation {#bsls_asserttest-example-1-testing-assertions-in-a-simple-vector-implementation}
240///
241///
242/// First we will demonstrate how "negative testing" might be used to verify
243/// that the correct assertions are in place on `std::vector::operator[]`. We
244/// start by supplying a primitive vector-like class that offers the minimal set
245/// of operations necessary to demonstrate the test case.
246/// @code
247/// template <class T>
248/// class AssertTestVector {
249/// // This class simulates a 'std::vector' with a fixed capacity of 10
250/// // elements.
251///
252/// private:
253/// // DATA
254/// T d_data[10];
255/// int d_size;
256///
257/// public:
258/// // CREATORS
259/// AssertTestVector();
260/// // Create an empty 'AssertTestVector' object.
261///
262/// // MANIPULATORS
263/// void push_back(const T& value);
264/// // Append the specified 'value' to the back of this object. The
265/// // behavior is undefined unless this method has been called fewer
266/// // than 10 times on this object.
267///
268/// // ACCESSORS
269/// const T& operator[](int index) const;
270/// // Return a reference with non-modifiable access to the object at
271/// // the specified 'index' in this object.
272/// };
273/// @endcode
274/// Next we implement the support functions.
275/// @code
276/// template <class T>
277/// AssertTestVector<T>::AssertTestVector()
278/// : d_data()
279/// , d_size()
280/// {
281/// }
282///
283/// template<class T>
284/// void AssertTestVector<T>::push_back(const T& value)
285/// {
286/// BSLS_ASSERT_SAFE(d_size < 10);
287///
288/// d_data[d_size] = value;
289/// ++d_size;
290/// }
291/// @endcode
292/// We conclude the definition of this support type with the implementation of
293/// the `operator[]` overload. Note the use of `BSLS_ASSERT_SAFE`, which is
294/// typical for function template definitions and inline function definitions.
295/// It is most appropriate in this case as the cost of evaluating each test is
296/// significant (> ~20%) compared to simply returning a reference to the result.
297/// @code
298/// template <class T>
299/// const T& AssertTestVector<T>::operator[](int index) const
300/// {
301/// BSLS_ASSERT_SAFE(0 <= index);
302/// BSLS_ASSERT_SAFE( index < d_size);
303///
304/// return d_data[index];
305/// }
306/// @endcode
307/// Finally, we can write the function to test that the `BSLS_ASSERT_SAFE`
308/// macros placed in `operator[]` work as expected. We want to validate that
309/// the assertions trigger when the function preconditions are violated; we
310/// further want to validate that the assertion macros are enabled in the build
311/// modes that we expect. We start by defining some macro aliases that will
312/// make the test driver more readable. These macro aliases are a common
313/// feature of test drivers.
314/// @code
315/// #define ASSERT_PASS(EXPR) BSLS_ASSERTTEST_ASSERT_PASS(EXPR)
316/// #define ASSERT_SAFE_FAIL(EXPR) BSLS_ASSERTTEST_ASSERT_SAFE_FAIL(EXPR)
317/// #define ASSERT_FAIL(EXPR) BSLS_ASSERTTEST_ASSERT_FAIL(EXPR)
318/// #define ASSERT_OPT_FAIL(EXPR) BSLS_ASSERTTEST_ASSERT_OPT_FAIL(EXPR)
319/// @endcode
320/// Then we implement the test function itself. Note that we check that
321/// exceptions are available in the current build mode, as the test macros rely
322/// on the exception facility in order to return their diagnostic results. If
323/// exceptions are not available, there is nothing for a "negative test" to do.
324/// @code
325/// void testVectorArrayAccess()
326/// {
327/// #ifdef BDE_BUILD_TARGET_EXC
328/// bsls::AssertTestHandlerGuard g;
329///
330/// AssertTestVector<void *> mA; const AssertTestVector<void *> &A = mA;
331///
332/// ASSERT_SAFE_FAIL(mA[-1]);
333/// ASSERT_SAFE_FAIL(mA[ 0]);
334/// ASSERT_SAFE_FAIL(mA[ 1]);
335///
336/// ASSERT_SAFE_FAIL( A[-1]);
337/// ASSERT_SAFE_FAIL( A[ 0]);
338/// ASSERT_SAFE_FAIL( A[ 1]);
339///
340/// mA.push_back(0); // increase the length to one
341///
342/// ASSERT_SAFE_FAIL(mA[-1]);
343/// ASSERT_PASS (mA[ 0]);
344/// ASSERT_SAFE_FAIL(mA[ 1]);
345///
346/// ASSERT_SAFE_FAIL( A[-1]);
347/// ASSERT_PASS ( A[ 0]);
348/// ASSERT_SAFE_FAIL( A[ 1]);
349/// #else // defined(BDE_BUILD_TARGET_EXC)
350/// @endcode
351/// If exceptions are not available, then we write a diagnostic message to the
352/// console alerting the user that this part of the test has not run, without
353/// failing the test.
354/// @code
355/// if (globalVerbose) printf(
356/// "\tDISABLED in this (non-exception) build mode.\n");
357///
358/// #endif // !defined(BDE_BUILD_TARGET_EXC)
359/// }
360/// @endcode
361///
362/// ### Example 2: Using PASS macros to help with formatting {#bsls_asserttest-example-2-using-pass-macros-to-help-with-formatting}
363///
364///
365/// When testing the various inputs to a function to be sure that some trigger
366/// an assertion and some are in contract, it often helps to align the testing
367/// macros so that the various arguments are easily readable in relation to one
368/// another. We start by defining additional macro aliases to match the
369/// existing aliases already defined:
370/// @code
371/// #define ASSERT_SAFE_PASS(EXPR) BSLS_ASSERTTEST_ASSERT_SAFE_PASS(EXPR)
372/// #define ASSERT_OPT_PASS(EXPR) BSLS_ASSERTTEST_ASSERT_OPT_PASS(EXPR)
373/// @endcode
374/// Considering the function `testVectorArrayAccess` from {Example 1}, we could
375/// instead implement it without padded white space by using `ASSERT_SAFE_PASS`
376/// to replace `ASSERT_PASS`, matching the existing `ASSERT_SAFE_FAIL` tests,
377/// like this:
378/// @code
379/// void testVectorArrayAccess2()
380/// {
381/// #ifdef BDE_BUILD_TARGET_EXC
382/// bsls::AssertTestHandlerGuard g;
383///
384/// AssertTestVector<void *> mA; const AssertTestVector<void *> &A = mA;
385///
386/// ASSERT_SAFE_FAIL(mA[-1]);
387/// ASSERT_SAFE_FAIL(mA[ 0]);
388/// ASSERT_SAFE_FAIL(mA[ 1]);
389///
390/// ASSERT_SAFE_FAIL( A[-1]);
391/// ASSERT_SAFE_FAIL( A[ 0]);
392/// ASSERT_SAFE_FAIL( A[ 1]);
393///
394/// mA.push_back(0); // increase the length to one
395///
396/// ASSERT_SAFE_FAIL(mA[-1]);
397/// ASSERT_SAFE_PASS(mA[ 0]);
398/// ASSERT_SAFE_FAIL(mA[ 1]);
399///
400/// ASSERT_SAFE_FAIL( A[-1]);
401/// ASSERT_SAFE_PASS( A[ 0]);
402/// ASSERT_SAFE_FAIL( A[ 1]);
403/// #endif // defined(BDE_BUILD_TARGET_EXC)
404/// }
405/// @endcode
406/// @}
407/** @} */
408/** @} */
409
410/** @addtogroup bsl
411 * @{
412 */
413/** @addtogroup bsls
414 * @{
415 */
416/** @addtogroup bsls_asserttest
417 * @{
418 */
419
420#include <bsls_annotation.h>
421#include <bsls_assert.h>
423#include <bsls_buildtarget.h>
425#include <bsls_platform.h>
426#include <bsls_review.h>
427
428 // =============================
429 // Checks for Pre-Defined macros
430 // =============================
431
432#if defined (ASSERTTEST_ASSERT_ACTIVE_FLAG)
433#error ASSERTTEST_ASSERT_ACTIVE_FLAG is alread defined!
434#endif
435
436#if defined (ASSERTTEST_ASSERT_OPT_ACTIVE_FLAG)
437#error ASSERTTEST_ASSERT_OPT_ACTIVE_FLAG is alread defined!
438#endif
439
440#if defined (ASSERTTEST_ASSERT_SAFE_ACTIVE_FLAG)
441#error ASSERTTEST_ASSERT_SAFE_ACTIVE_FLAG is alread defined!
442#endif
443
444#if defined (ASSERTTEST_SAFE_2_BUILD_FLAG)
445#error ASSERTTEST_SAFE_2_BUILD_FLAG is alread defined!
446#endif
447
448#if defined (ASSERTTEST_IS_ACTIVE)
449#error ASSERTTEST_IS_ACTIVE is alread defined!
450#endif
451
452#if defined (ASSERTTEST_ASSERT_FAIL)
453#error ASSERTTEST_ASSERT_FAIL is alread defined!
454#endif
455
456#if defined (ASSERTTEST_ASSERT_FAIL_RAW)
457#error ASSERTTEST_ASSERT_FAIL_RAW is alread defined!
458#endif
459
460#if defined (ASSERTTEST_ASSERT_INVOKE_FAIL)
461#error ASSERTTEST_ASSERT_INVOKE_FAIL is alread defined!
462#endif
463
464#if defined (ASSERTTEST_ASSERT_INVOKE_FAIL_RAW)
465#error ASSERTTEST_ASSERT_INVOKE_FAIL_RAW is alread defined!
466#endif
467
468#if defined (ASSERTTEST_ASSERT_OPT_FAIL)
469#error ASSERTTEST_ASSERT_OPT_FAIL is alread defined!
470#endif
471
472#if defined (ASSERTTEST_ASSERT_OPT_FAIL_RAW)
473#error ASSERTTEST_ASSERT_OPT_FAIL_RAW is alread defined!
474#endif
475
476#if defined (ASSERTTEST_ASSERT_SAFE_FAIL)
477#error ASSERTTEST_ASSERT_SAFE_FAIL is alread defined!
478#endif
479
480#if defined (ASSERTTEST_ASSERT_SAFE_FAIL_RAW)
481#error ASSERTTEST_ASSERT_SAFE_FAIL_RAW is alread defined!
482#endif
483
484#if defined (ASSERTTEST_ASSERT_INVOKE_PASS)
485#error ASSERTTEST_ASSERT_INVOKE_PASS is alread defined!
486#endif
487
488#if defined (ASSERTTEST_ASSERT_INVOKE_PASS_RAW)
489#error ASSERTTEST_ASSERT_INVOKE_PASS_RAW is alread defined!
490#endif
491
492#if defined (ASSERTTEST_ASSERT_OPT_PASS)
493#error ASSERTTEST_ASSERT_OPT_PASS is alread defined!
494#endif
495
496#if defined (ASSERTTEST_ASSERT_OPT_PASS_RAW)
497#error ASSERTTEST_ASSERT_OPT_PASS_RAW is alread defined!
498#endif
499
500#if defined (ASSERTTEST_ASSERT_PASS)
501#error ASSERTTEST_ASSERT_PASS is alread defined!
502#endif
503
504#if defined (ASSERTTEST_ASSERT_PASS_RAW)
505#error ASSERTTEST_ASSERT_PASS_RAW is alread defined!
506#endif
507
508#if defined (ASSERTTEST_ASSERT_SAFE_PASS)
509#error ASSERTTEST_ASSERT_SAFE_PASS is alread defined!
510#endif
511
512#if defined (ASSERTTEST_ASSERT_SAFE_PASS_RAW)
513#error ASSERTTEST_ASSERT_SAFE_PASS_RAW is alread defined!
514#endif
515
516#if defined (ASSERTTEST_BRUTE_FORCE_IMP)
517#error ASSERTTEST_BRUTE_FORCE_IMP is alread defined!
518#endif
519
520#if defined (ASSERTTEST_BRUTE_FORCE_IMP_RAW)
521#error ASSERTTEST_BRUTE_FORCE_IMP_RAW is alread defined!
522#endif
523
524#if defined (ASSERTTEST_CAN_CHECK_LEVELS)
525#error ASSERTTEST_CAN_CHECK_LEVELS is alread defined!
526#endif
527
528#if defined (ASSERTTEST_CHECK_LEVEL_ARG)
529#error ASSERTTEST_CHECK_LEVEL_ARG is alread defined!
530#endif
531
532#if defined (ASSERTTEST_DISABLED_IMP)
533#error ASSERTTEST_DISABLED_IMP is alread defined!
534#endif
535
536 // ==============================
537 // ASSERTTEST_*_BUILD_FLAG macros
538 // ==============================
539
540#ifdef BDE_BUILD_TARGET_SAFE_2
541 #define BSLS_ASSERTTEST_SAFE_2_BUILD_FLAG true
542#else
543 #define BSLS_ASSERTTEST_SAFE_2_BUILD_FLAG false
544#endif
545
546#ifdef BSLS_ASSERT_SAFE_IS_ACTIVE
547 #define BSLS_ASSERTTEST_ASSERT_SAFE_ACTIVE_FLAG true
548#else
549 #define BSLS_ASSERTTEST_ASSERT_SAFE_ACTIVE_FLAG false
550#endif
551
552#ifdef BSLS_ASSERT_IS_ACTIVE
553 #define BSLS_ASSERTTEST_ASSERT_ACTIVE_FLAG true
554#else
555 #define BSLS_ASSERTTEST_ASSERT_ACTIVE_FLAG false
556#endif
557
558#ifdef BSLS_ASSERT_OPT_IS_ACTIVE
559 #define BSLS_ASSERTTEST_ASSERT_OPT_ACTIVE_FLAG true
560#else
561 #define BSLS_ASSERTTEST_ASSERT_OPT_ACTIVE_FLAG false
562#endif
563
564#define BSLS_ASSERTTEST_IS_ACTIVE(TYPE) ( \
565 ( '\0' == TYPE[1] \
566 || BSLS_ASSERTTEST_SAFE_2_BUILD_FLAG \
567 ) \
568 && \
569 ( ('S' == TYPE[0] && BSLS_ASSERTTEST_ASSERT_SAFE_ACTIVE_FLAG) \
570 || ('A' == TYPE[0] && BSLS_ASSERTTEST_ASSERT_ACTIVE_FLAG) \
571 || ('O' == TYPE[0] && BSLS_ASSERTTEST_ASSERT_OPT_ACTIVE_FLAG) \
572 ) \
573)
574
575#if !defined(BSLS_ASSERT_USE_CONTRACTS)
576 #define BSLS_ASSERTTEST_CAN_CHECK_LEVELS
577#else
578 // Determine if the current use of language contracts can support level
579 // checking. As of now, none can.
580#endif
581
582#ifdef BSLS_ASSERTTEST_CHECK_LEVEL
583 #define BSLS_ASSERTTEST_CHECK_LEVEL_ARG true
584#else
585 #define BSLS_ASSERTTEST_CHECK_LEVEL_ARG false
586#endif
587
588#define BSLS_ASSERTTEST_BRUTE_FORCE_IMP(RESULT, LVL, EXPRESSION_UNDER_TEST) { \
589 try { \
590 EXPRESSION_UNDER_TEST; \
591 \
592 ASSERT(BloombergLP::bsls::AssertTest::tryProbe(RESULT, LVL)); \
593 } \
594 catch (const BloombergLP::bsls::AssertTestException& e) { \
595 ASSERT(BloombergLP::bsls::AssertTest::catchProbe( \
596 RESULT, \
597 BSLS_ASSERTTEST_CHECK_LEVEL_ARG, \
598 LVL, \
599 e, \
600 __FILE__)); \
601 } \
602}
603
604#define BSLS_ASSERTTEST_VALIDATE_DISABLED_MACROS
605#ifdef BSLS_ASSERTTEST_VALIDATE_DISABLED_MACROS
606 #define BSLS_ASSERTTEST_DISABLED_IMP(EXPRESSION_UNDER_TEST) \
607 if (false) { EXPRESSION_UNDER_TEST ; } else {}
608#else
609 #define BSLS_ASSERTTEST_DISABLED_IMP(EXPERSSION_UNDER_TEST)
610#endif
611
612#if !defined(BDE_BUILD_TARGET_EXC)
613// In non-exception enabled builds there is no way to safely use the
614// ASSERT_FAIL macros as they require installing an assert-handler that throws
615// a specific exception. ASSERT_FAIL negative tests require calling a method
616// under test with out-of-contract values; running those tests without a
617// functioning assert-handler would trigger undefined behavior with no
618// protection so we choose to simple not execute the test calls that are
619// designed to fail by expanding the test macros to an empty statement, '{ }'.
620// All of the ASSERT_PASS macros are expanded however, as such tests call
621// methods with in-contract values, and they may still be needed to guarantee
622// stateful side-effects required by the test-driver.
623
624#define BSLS_ASSERTTEST_ASSERT_SAFE_PASS(EXPRESSION_UNDER_TEST) \
625 { EXPRESSION_UNDER_TEST; }
626
627#define BSLS_ASSERTTEST_ASSERT_PASS(EXPRESSION_UNDER_TEST) \
628 { EXPRESSION_UNDER_TEST; }
629
630#define BSLS_ASSERTTEST_ASSERT_OPT_PASS(EXPRESSION_UNDER_TEST) \
631 { EXPRESSION_UNDER_TEST; }
632
633#define BSLS_ASSERTTEST_ASSERT_INVOKE_PASS(EXPRESSION_UNDER_TEST) \
634 { EXPRESSION_UNDER_TEST; }
635
636#define BSLS_ASSERTTEST_ASSERT_SAFE_PASS_RAW(EXPRESSION_UNDER_TEST) \
637 { EXPRESSION_UNDER_TEST; }
638
639#define BSLS_ASSERTTEST_ASSERT_PASS_RAW(EXPRESSION_UNDER_TEST) \
640 { EXPRESSION_UNDER_TEST; }
641
642#define BSLS_ASSERTTEST_ASSERT_OPT_PASS_RAW(EXPRESSION_UNDER_TEST) \
643 { EXPRESSION_UNDER_TEST; }
644
645#define BSLS_ASSERTTEST_ASSERT_INVOKE_PASS_RAW(EXPRESSION_UNDER_TEST) \
646 { EXPRESSION_UNDER_TEST; }
647
648#define BSLS_ASSERTTEST_ASSERT_SAFE_FAIL(EXPRESSION_UNDER_TEST) \
649 BSLS_ASSERTTEST_DISABLED_IMP(EXPRESSION_UNDER_TEST)
650
651#define BSLS_ASSERTTEST_ASSERT_FAIL(EXPRESSION_UNDER_TEST) \
652 BSLS_ASSERTTEST_DISABLED_IMP(EXPRESSION_UNDER_TEST)
653
654#define BSLS_ASSERTTEST_ASSERT_OPT_FAIL(EXPRESSION_UNDER_TEST) \
655 BSLS_ASSERTTEST_DISABLED_IMP(EXPRESSION_UNDER_TEST)
656
657#define BSLS_ASSERTTEST_ASSERT_SAFE_FAIL_RAW(EXPRESSION_UNDER_TEST) \
658 BSLS_ASSERTTEST_DISABLED_IMP(EXPRESSION_UNDER_TEST)
659
660#define BSLS_ASSERTTEST_ASSERT_FAIL_RAW(EXPRESSION_UNDER_TEST) \
661 BSLS_ASSERTTEST_DISABLED_IMP(EXPRESSION_UNDER_TEST)
662
663#define BSLS_ASSERTTEST_ASSERT_OPT_FAIL_RAW(EXPRESSION_UNDER_TEST) \
664 BSLS_ASSERTTEST_DISABLED_IMP(EXPRESSION_UNDER_TEST)
665
666#define BSLS_ASSERTTEST_ASSERT_INVOKE_FAIL(EXPRESSION_UNDER_TEST) \
667 BSLS_ASSERTTEST_DISABLED_IMP(EXPRESSION_UNDER_TEST)
668
669#define BSLS_ASSERTTEST_ASSERT_INVOKE_FAIL_RAW(EXPRESSION_UNDER_TEST) \
670 BSLS_ASSERTTEST_DISABLED_IMP(EXPRESSION_UNDER_TEST)
671
672#else // defined BDE_BUILD_TARGET_EXC
673
674// The following macros are not expanded on the Microsoft compiler to avoid
675// internal compiler errors in optimized builds, which are the result of
676// attempts to optimize many try/catch blocks in large switch statements. Note
677// that the resulting test driver is just as thorough, but will report failure
678// of a buggy library by simply crashing, rather than capturing and reporting
679// the specific error detected.
680#if (defined(BSLS_PLATFORM_CMP_MSVC) && defined(BDE_BUILD_TARGET_OPT))
681#define BSLS_ASSERTTEST_ASSERT_SAFE_PASS(EXPRESSION_UNDER_TEST) \
682 { EXPRESSION_UNDER_TEST; }
683
684#define BSLS_ASSERTTEST_ASSERT_PASS(EXPRESSION_UNDER_TEST) \
685 { EXPRESSION_UNDER_TEST; }
686
687#define BSLS_ASSERTTEST_ASSERT_OPT_PASS(EXPRESSION_UNDER_TEST) \
688 { EXPRESSION_UNDER_TEST; }
689
690#define BSLS_ASSERTTEST_ASSERT_INVOKE_PASS(EXPRESSION_UNDER_TEST) \
691 { EXPRESSION_UNDER_TEST; }
692#else // optimized msvc
693#define BSLS_ASSERTTEST_ASSERT_SAFE_PASS(EXPRESSION_UNDER_TEST) \
694 BSLS_ASSERTTEST_BRUTE_FORCE_IMP('P', 'S', EXPRESSION_UNDER_TEST)
695
696#define BSLS_ASSERTTEST_ASSERT_PASS(EXPRESSION_UNDER_TEST) \
697 BSLS_ASSERTTEST_BRUTE_FORCE_IMP('P', 'A', EXPRESSION_UNDER_TEST)
698
699#define BSLS_ASSERTTEST_ASSERT_OPT_PASS(EXPRESSION_UNDER_TEST) \
700 BSLS_ASSERTTEST_BRUTE_FORCE_IMP('P', 'O', EXPRESSION_UNDER_TEST)
701
702#define BSLS_ASSERTTEST_ASSERT_INVOKE_PASS(EXPRESSION_UNDER_TEST) \
703 BSLS_ASSERTTEST_BRUTE_FORCE_IMP('P', 'I', EXPRESSION_UNDER_TEST)
704#endif // not (msvc and optimized)
705
706#if defined(BSLS_ASSERT_SAFE_IS_ACTIVE)
707# define BSLS_ASSERTTEST_ASSERT_SAFE_FAIL(EXPRESSION_UNDER_TEST) \
708 BSLS_ASSERTTEST_BRUTE_FORCE_IMP('F', 'S', EXPRESSION_UNDER_TEST)
709#else
710# define BSLS_ASSERTTEST_ASSERT_SAFE_FAIL(EXPRESSION_UNDER_TEST) \
711 BSLS_ASSERTTEST_DISABLED_IMP(EXPRESSION_UNDER_TEST)
712#endif
713
714#if defined(BSLS_ASSERT_IS_ACTIVE)
715 #define BSLS_ASSERTTEST_ASSERT_FAIL(EXPRESSION_UNDER_TEST) \
716 BSLS_ASSERTTEST_BRUTE_FORCE_IMP('F', 'A', EXPRESSION_UNDER_TEST)
717#else
718 #define BSLS_ASSERTTEST_ASSERT_FAIL(EXPRESSION_UNDER_TEST) \
719 BSLS_ASSERTTEST_DISABLED_IMP(EXPRESSION_UNDER_TEST)
720#endif
721
722#if defined(BSLS_ASSERT_OPT_IS_ACTIVE)
723 #define BSLS_ASSERTTEST_ASSERT_OPT_FAIL(EXPRESSION_UNDER_TEST) \
724 BSLS_ASSERTTEST_BRUTE_FORCE_IMP('F', 'O', EXPRESSION_UNDER_TEST)
725#else
726 #define BSLS_ASSERTTEST_ASSERT_OPT_FAIL(EXPRESSION_UNDER_TEST) \
727 BSLS_ASSERTTEST_DISABLED_IMP(EXPRESSION_UNDER_TEST)
728#endif
729
730#define BSLS_ASSERTTEST_ASSERT_INVOKE_FAIL(EXPRESSION_UNDER_TEST) \
731 BSLS_ASSERTTEST_BRUTE_FORCE_IMP('F', 'I', EXPRESSION_UNDER_TEST)
732
733#define BSLS_ASSERTTEST_BRUTE_FORCE_IMP_RAW(RESULT, \
734 LVL, \
735 EXPRESSION_UNDER_TEST) { \
736 try { \
737 EXPRESSION_UNDER_TEST; \
738 \
739 ASSERT(BloombergLP::bsls::AssertTest::tryProbeRaw(RESULT, LVL)); \
740 } \
741 catch (const BloombergLP::bsls::AssertTestException& e) { \
742 ASSERT(BloombergLP::bsls::AssertTest::catchProbeRaw( \
743 RESULT, \
744 BSLS_ASSERTTEST_CHECK_LEVEL_ARG, \
745 LVL, \
746 e)); \
747 } \
748}
749
750#if defined(BSLS_PLATFORM_CMP_MSVC) && defined(BDE_BUILD_TARGET_OPT)
751// The following MSVC-specific work-around avoids compilation issues with MSVC
752// optimized builds.
753
754#define BSLS_ASSERTTEST_ASSERT_SAFE_PASS_RAW(EXPRESSION_UNDER_TEST) \
755 { EXPRESSION_UNDER_TEST; }
756
757#define BSLS_ASSERTTEST_ASSERT_PASS_RAW(EXPRESSION_UNDER_TEST) \
758 { EXPRESSION_UNDER_TEST; }
759
760#define BSLS_ASSERTTEST_ASSERT_OPT_PASS_RAW(EXPRESSION_UNDER_TEST) \
761 { EXPRESSION_UNDER_TEST; }
762
763#define BSLS_ASSERTTEST_ASSERT_INVOKE_PASS_RAW(EXPRESSION_UNDER_TEST) \
764 { EXPRESSION_UNDER_TEST; }
765#else
766#define BSLS_ASSERTTEST_ASSERT_SAFE_PASS_RAW(EXPRESSION_UNDER_TEST) \
767 BSLS_ASSERTTEST_BRUTE_FORCE_IMP_RAW('P', 'S', EXPRESSION_UNDER_TEST)
768
769#define BSLS_ASSERTTEST_ASSERT_PASS_RAW(EXPRESSION_UNDER_TEST) \
770 BSLS_ASSERTTEST_BRUTE_FORCE_IMP_RAW('P', 'A', EXPRESSION_UNDER_TEST)
771
772#define BSLS_ASSERTTEST_ASSERT_OPT_PASS_RAW(EXPRESSION_UNDER_TEST) \
773 BSLS_ASSERTTEST_BRUTE_FORCE_IMP_RAW('P', 'O', EXPRESSION_UNDER_TEST)
774
775#define BSLS_ASSERTTEST_ASSERT_INVOKE_PASS_RAW(EXPRESSION_UNDER_TEST) \
776 BSLS_ASSERTTEST_BRUTE_FORCE_IMP_RAW('P', 'I', EXPRESSION_UNDER_TEST)
777#endif
778
779#if defined(BSLS_ASSERT_SAFE_IS_ACTIVE)
780 #define BSLS_ASSERTTEST_ASSERT_SAFE_FAIL_RAW(EXPRESSION_UNDER_TEST) \
781 BSLS_ASSERTTEST_BRUTE_FORCE_IMP_RAW('F', 'S', EXPRESSION_UNDER_TEST)
782#else
783 #define BSLS_ASSERTTEST_ASSERT_SAFE_FAIL_RAW(EXPRESSION_UNDER_TEST) \
784 BSLS_ASSERTTEST_DISABLED_IMP(EXPRESSION_UNDER_TEST)
785#endif
786
787#if defined(BSLS_ASSERT_IS_ACTIVE)
788 #define BSLS_ASSERTTEST_ASSERT_FAIL_RAW(EXPRESSION_UNDER_TEST) \
789 BSLS_ASSERTTEST_BRUTE_FORCE_IMP_RAW('F', 'A', EXPRESSION_UNDER_TEST)
790#else
791 #define BSLS_ASSERTTEST_ASSERT_FAIL_RAW(EXPRESSION_UNDER_TEST) \
792 BSLS_ASSERTTEST_DISABLED_IMP(EXPRESSION_UNDER_TEST)
793#endif
794
795#if defined(BSLS_ASSERT_OPT_IS_ACTIVE)
796 #define BSLS_ASSERTTEST_ASSERT_OPT_FAIL_RAW(EXPRESSION_UNDER_TEST) \
797 BSLS_ASSERTTEST_BRUTE_FORCE_IMP_RAW('F', 'O', EXPRESSION_UNDER_TEST)
798#else
799 #define BSLS_ASSERTTEST_ASSERT_OPT_FAIL_RAW(EXPRESSION_UNDER_TEST) \
800 BSLS_ASSERTTEST_DISABLED_IMP(EXPRESSION_UNDER_TEST)
801#endif
802
803#define BSLS_ASSERTTEST_ASSERT_INVOKE_FAIL_RAW(EXPRESSION_UNDER_TEST) \
804 BSLS_ASSERTTEST_BRUTE_FORCE_IMP_RAW('F', 'I', EXPRESSION_UNDER_TEST)
805
806#endif // BDE_BUILD_TARGET_EXC
807
808// Provide a deeper "include-guard" to protect against repeated inclusion by
809// the test driver for this component. This is necessary because, in order to
810// test the macros and pre-processor logic that constitute key parts of this
811// component, this header must be included multiple times by the test driver,
812// which will deliberately '#undef' the main include-guard to achieve this
813// effect. The deeper include-guard protects the non-macro parts of this
814// header that cannot be defined more than once.
815#ifndef BSLS_ASSERTTEST_RECURSIVELY_INCLUDED_TESTDRIVER_GUARD
816#define BSLS_ASSERTTEST_RECURSIVELY_INCLUDED_TESTDRIVER_GUARD
817
818
819namespace bsls {
820
821 // ================
822 // class AssertTest
823 // ================
824
825/// This utility `struct` provides a suite of methods designed for use in
826/// conjunction with preprocessor macros during the negative testing of
827/// defensive checks using the facilities provided by the @ref bsls_assert
828/// component. Unlike usual BDE functionality methods in this `struct`
829/// provide wide contracts because they need to function without assertion
830/// failures during testing, under unforeseen circumstances.
832
833 // CLASS METHODS
834
835 // Test Specification Validation
836
837 /// Return `true` if the specified `specString` represents a valid
838 /// textual description of an assertion build type, and `false`
839 /// otherwise. `specString` is valid if it refers to a null-terminated
840 /// character string of length either 1 or 2, where the first character
841 /// is an (UPPERCASE) `S`, `A`, `O`, or `I`, and the second character,
842 /// if present, is the numeral `2`. Note that `S`, `A`, `O`, and `I`
843 /// are intended to represent the respective assertion types
844 /// `BSLS_ASSERT_SAFE`, `BSLS_ASSERT`, `BSLS_ASSERT_OPT`, and
845 /// `BSLS_ASSERT_INVOKE`, and the optional `2` is intended to indicate
846 /// that the component (and program as a whole) was built with
847 /// `BDE_BUILD_TARGET_SAFE_2` defined. The behavior is undefined unless
848 /// `specString` points to a null terminated string (C string).
849 static bool isValidAssertBuild(const char *specString);
850
851 /// Return `true` if the specified `specChar` represents a valid
852 /// description of the expected result, and `false` otherwise.
853 /// `specChar` is valid if it is either an (UPPERCASE) `P` (for Pass) or
854 /// `F` (for Fail). Note that `P` is intended to represent the
855 /// expectation that the function under test will "pass" by satisfying
856 /// all preconditions, including the specific one guarded by the
857 /// assertion under test, while an `F` is intended to indicate the
858 /// expectation that the specific assertion under test will "fail"
859 /// (thereby triggering a call to the currently-installed
860 /// assertion-failure handler).
861 static bool isValidExpected(char specChar);
862
863 /// Return `true` if the specified `specChar` represents a valid
864 /// description of the expected assert level, and `false` otherwise.
865 /// `specChar` is valid if it is either a `O` (for `OPT`), `S` (for
866 /// `SAFE`), `A` (for `ASSERT`), or `I` (for `INVOKE`).
867 static bool isValidExpectedLevel(char specChar);
868
869 // Testing Apparatus
870
871 /// Return `true` if the specified `expectedResult` is `P` (for Pass)
872 /// and the specified `expectedLevel` is a valid level; otherwise,
873 /// return `false`. If `expectedResult` is anything other than `P` or
874 /// `F` (for Fail), this function reports the invalid `expectedResult`
875 /// value to `stdout` before returning `false`. If `expectedlevel` is
876 /// anything other than `S`, `A`, `O`, or `I`, this function reports the
877 /// invalid `expectedLevel` value to `stdout` before returning `false`.
878 static bool tryProbe(char expectedResult, char expectedLevel);
879
880 /// Return `true` if the specified `expectedResult` is `F` (for Fail),
881 /// the specified `checkLevel` flag is `false` or the `expectedLevel` is
882 /// as wide or wider than the actual assertion failure level, the
883 /// specified `caughtException` contains valid fields, and the specified
884 /// `testDriverFileName` is either null or refers to the same (valid)
885 /// component name as the filename in `caughtException`; otherwise,
886 /// return `false`. If `expectedResult`, `testDriverFileName`, or any
887 /// field of `caughtException` is invalid (i.e., an invalid filename,
888 /// null or empty expression text, or a non-positive line number), this
889 /// function reports the invalid value(s) to `stdout` before returning
890 /// `false`. If `testDriverFileName` is not null, but does not reflect
891 /// the same component name as the otherwise valid filename in
892 /// `caughtException`, this function prints a message delineating the
893 /// mismatching deduced component names to `stdout` before returning
894 /// `false`.
895 static bool catchProbe(char expectedResult,
896 bool checkLevel,
897 char expectedLevel,
898 const AssertTestException& caughtException,
899 const char *testDriverFileName);
900
901 /// Return `true` if the specified `expectedResult` is `P` (for Pass)
902 /// and the specified `expectedLevel` is a valid level; otherwise,
903 /// return `false`. If `expectedResult` is anything other than `P` or
904 /// `F` (for Fail), this function reports the invalid `expectedResult`
905 /// value to `stdout` before returning `false`. If `expectedlevel` is
906 /// anything other than `S`, `A`, `O`, or `I`, this function reports the
907 /// invalid `expectedLevel` value to `stdout` before returning `false`.
908 static bool tryProbeRaw(char expectedResult, char expectedLevel);
909
910 /// Return `true` if the specified `expectedResult` is `F` (for Fail),
911 /// the specified `checkLevel` flag is `false` or the `expectedLevel` is
912 /// as wide or wider than the actual assertion failure level, and the
913 /// specified `caughtException` contains valid fields; otherwise, return
914 /// `false`. If `expectedResult`, `expectedLevel`, or any field of
915 /// `caughtException` is invalid (i.e., an invalid filename, null or
916 /// empty expression text, or a non-positive line number), this function
917 /// reports the invalid value(s) to `stdout` before returning `false`.
918 static bool catchProbeRaw(char expectedResult,
919 bool checkLevel,
920 char expectedLevel,
921 const AssertTestException& caughtException);
922
923 // Testing Failure Handlers
924
925 /// Throw an `AssertTestException` having the pointer values `text` and
926 /// `file` and the integer `line` from the specified `violation` as its
927 /// salient attributes, provided that `BDE_BUILD_TARGET_EXC` is defined;
928 /// otherwise, log an appropriate message and abort the program (similar
929 /// to `Assert::failAbort`). Note that this function is intended to
930 /// have a signature compatible with a registered assertion-failure
931 /// handler function in @ref bsls_assert .
933 static void failTestDriver(const AssertViolation &violation);
934
935 /// Throw an `AssertTestException` having the `comment`, `fileName`, and
936 /// `lineNumber` taken from the specified `violation` as its salient
937 /// attributes, provided that `BDE_BUILD_TARGET_EXC` is defined;
938 /// otherwise, log an appropriate message and abort the program (similar
939 /// to `Assert::failAbort`). Note that this function is intended to
940 /// have a signature compatible with a registered assertion-failure
941 /// handler function in @ref bsls_assert .
942 static void failTestDriverByReview(const ReviewViolation &violation);
943};
944
945 // ============================
946 // class AssertTestHandlerGuard
947 // ============================
948
949/// This class provides a guard that will install and uninstall the negative
950/// testing assertion handler, `AssertTest::failTestDriver`, within the
951/// protected scope, as well as a corresponding review handler,
952/// `AssertTest::failTestDriverByReview`.
953///
954/// See @ref bsls_asserttest
956
957 // DATA
958 AssertFailureHandlerGuard d_assertGuard;
959 ReviewFailureHandlerGuard d_reviewGuard;
960
961 public:
962 // CREATORS
963
964 /// Create a `AssertTestHandlerGuard` object, installing the
965 /// `AssertTest::failTestDriver` assertion handler.
967
969 // Destroy this object and uninstall 'AssertTest::failTestDriver' as
970 // the current assertion handler.
971};
972
973
974// ============================================================================
975// INLINE FUNCTION DEFINITIONS
976// ============================================================================
977
978
979 // ------------------------
980 // class AssertHandlerGuard
981 // ------------------------
982
983inline
985: d_assertGuard(&AssertTest::failTestDriver)
986, d_reviewGuard(&AssertTest::failTestDriverByReview)
987{
988}
989
990} // close package namespace
991
992#ifndef BDE_OPENSOURCE_PUBLICATION // BACKWARD_COMPATIBILITY
993// ============================================================================
994// BACKWARD COMPATIBILITY
995// ============================================================================
996
997/// This alias is defined for backward compatibility.
999
1000/// This alias is defined for backward compatibility.
1002#endif // BDE_OPENSOURCE_PUBLICATION -- BACKWARD_COMPATIBILITY
1003
1004
1005
1006#endif // test driver internal include guard
1007
1008#endif
1009
1010// ----------------------------------------------------------------------------
1011// Copyright 2018 Bloomberg Finance L.P.
1012//
1013// Licensed under the Apache License, Version 2.0 (the "License");
1014// you may not use this file except in compliance with the License.
1015// You may obtain a copy of the License at
1016//
1017// http://www.apache.org/licenses/LICENSE-2.0
1018//
1019// Unless required by applicable law or agreed to in writing, software
1020// distributed under the License is distributed on an "AS IS" BASIS,
1021// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1022// See the License for the specific language governing permissions and
1023// limitations under the License.
1024// ----------------------------- END-OF-FILE ----------------------------------
1025
1026/** @} */
1027/** @} */
1028/** @} */
Definition bsls_assert.h:2225
Definition bsls_asserttestexception.h:114
Definition bsls_asserttest.h:955
AssertTestHandlerGuard()
Definition bsls_asserttest.h:984
Definition bsls_assert.h:1929
Definition bsls_review.h:1203
Definition bsls_review.h:1001
#define BSLS_ANNOTATION_NORETURN
Definition bsls_annotation.h:383
bsls::AssertTest bsls_AssertTest
This alias is defined for backward compatibility.
Definition bsls_asserttest.h:1001
bsls::AssertTestHandlerGuard bsls_AssertTestHandlerGuard
This alias is defined for backward compatibility.
Definition bsls_asserttest.h:998
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlt_iso8601util.h:691
Definition bsls_asserttest.h:831
static bool catchProbe(char expectedResult, bool checkLevel, char expectedLevel, const AssertTestException &caughtException, const char *testDriverFileName)
static bool isValidAssertBuild(const char *specString)
static bool tryProbe(char expectedResult, char expectedLevel)
static bool isValidExpected(char specChar)
static bool catchProbeRaw(char expectedResult, bool checkLevel, char expectedLevel, const AssertTestException &caughtException)
static void failTestDriverByReview(const ReviewViolation &violation)
static bool tryProbeRaw(char expectedResult, char expectedLevel)
static bool isValidExpectedLevel(char specChar)
static BSLS_ANNOTATION_NORETURN void failTestDriver(const AssertViolation &violation)