BDE 4.14.0 Production release
|
Macros | |
#define | BSLS_PROTOCOLTEST_ASSERT(test, methodCall) |
#define | BSLS_PROTOCOLTEST_RV_ASSERT(test, methodCall, returnValue) |
#define | bsls_ProtocolTest bsls::ProtocolTest |
This alias is defined for backward compatibility. | |
#define | bsls_ProtocolTestImp bsls::ProtocolTestImp |
This alias is defined for backward compatibility. | |
Provide classes and macros for testing abstract protocols.
This component provides classes and macros that simplify the creation of test drivers for protocol (i.e., pure abstract interface) classes.
The purpose of a test driver for a protocol class is to verify concerns for that protocol's definition. Although each protocol is different and requires its own test driver, there is a common set of concerns that apply to all protocol classes. This component allows us to verify those concerns in a generic manner.
Each protocol class has to satisfy the following set of requirements (concerns):
There are two main exceptions to the above requirements:
bsl::memory_resource::allocate
calls private virtual do_allocate
. In this case test drivers using this component should reference the non-virtual function as a proxy for the virtual function.This protocol test component is intended to verify conformance to these requirements; however, it is not possible to verify all protocol requirements fully within the framework of the C++ language. The following aspects of the above requirements are not verified by this component:
Additionally some coding guidelines related to protocols are also not verified:
This section illustrates intended use of this component.
This example demonstrates how to test a protocol class, ProtocolClass
, using this protocol test component. Our ProtocolClass
provides two of pure virtual methods (foo
and bar
), along with a virtual destructor:
First, we define a test class derived from this protocol, and implement its virtual methods. Rather than deriving the test class from ProtocolClass
directly, the test class is derived from bsls::ProtocolTestImp<ProtocolClass>
(which, in turn, is derived automatically from ProtocolClass
). This special base class implements boilerplate code and provides useful functionality for testing of protocols.
Notice that in ProtocolClassTestImp
we must provide an implementation for every protocol method except for the destructor. The implementation of each method calls the (protected) markDone
which is provided by the base class for the purpose of verifying that the method from which it's called is declared as virtual in the protocol class.
Then, in our protocol test case we describe the concerns we have for the protocol class and the plan to test those concerns:
Next we print the banner for this test case:
Then, we create an object of type bsls::ProtocolTest<ProtocolClassTestImp>
, testObj
:
Now we use the testObj
to test some general concerns about the protocol class.
Finally we use the testObj
to test concerns for each individual method of the protocol class. To test a protocol method we need to call it from inside the BSLS_PROTOCOLTEST_ASSERT
macro, and also pass the testObj
:
These steps conclude the protocol testing. If there are any failures, they will be reported via standard test driver assertions (i.e., the standard ASSERT
macro).
Suppose we have a protocol that represent a sequence of integers. Such a protocol will have an overloaded at()
method of both the const
and the "mutable" variation. In verification of such methods we need to ensure that we verify both overloads of the virtual
function.
First let's define the interesting parts of our imaginary sequence, the overloaded at()
methods, and a virtual destructor to avoid warnings:
Next, we define the test implementation as usual:
Note the use of a dummy variable to return a reference. We also use that variable, by giving it different values in the two overloads, to demonstrate that we have called the overload we have intended.
Then, we test the non-const
overload as usual:
Now, we can verify that we have indeed tested the non-const
overload:
Finally, we test at(size_t) const
and also verify that we indeed called the intended overload. Notice that we "force" the const
variant of the method to be picked by specifying a const Implementation
type argument to bsls::ProtocolTest
:
Note that the assertion that verifies that the intended overload was called is not strictly necessary, it is included for demonstration purposes.
This component has a number of private meta-functions on some platforms, e.g., ProtocolTest_EnableIf
, ProtocolTest_IsClass
, and ProtocolTest_IsAbstract
. These mimic, to a limited extent, standard library meta-functions in the namespace std
that are not available on all platforms. For general use, see the {bslmf
} package and the {bsl
} namespace for portable implementations of some of these meta-functions.
#define bsls_ProtocolTest bsls::ProtocolTest |
#define BSLS_PROTOCOLTEST_ASSERT | ( | test, | |
methodCall | |||
) |
#define BSLS_PROTOCOLTEST_RV_ASSERT | ( | test, | |
methodCall, | |||
returnValue | |||
) |
#define bsls_ProtocolTestImp bsls::ProtocolTestImp |