BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslim_gtestutil.h
Go to the documentation of this file.
1/// @file bslim_gtestutil.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslim_gtestutil.h -*-C++-*-
8#ifndef INCLUDED_BSLIM_GTESTUTIL
9#define INCLUDED_BSLIM_GTESTUTIL
10
11#include <bsls_ident.h>
12
13/// @defgroup bslim_gtestutil bslim_gtestutil
14/// @brief Provide facilities for debugging BDE with gtest.
15/// @addtogroup bsl
16/// @{
17/// @addtogroup bslim
18/// @{
19/// @addtogroup bslim_gtestutil
20/// @{
21///
22/// <h1> Outline </h1>
23/// * <a href="#bslim_gtestutil-purpose"> Purpose</a>
24/// * <a href="#bslim_gtestutil-description"> Description </a>
25/// * <a href="#bslim_gtestutil-usage"> Usage </a>
26///
27/// # Purpose {#bslim_gtestutil-purpose}
28/// Provide facilities for debugging BDE with gtest.
29///
30/// # Description {#bslim_gtestutil-description}
31/// The @ref bslim_gtestutil component provides utitlities to
32/// facilitate testing with Google Test (GTest).
33///
34/// ## Usage {#bslim_gtestutil-usage}
35///
36///
37/// Suppose we have a string `str` that we want to output:
38/// @code
39/// bsl::string str =
40/// "No matter where you go, There you are! -- Buckaroo Banzai";
41/// @endcode
42/// Call `PrintTo`, passing the string and a pointer to a `bsl::ostream`:
43/// @code
44/// PrintTo(str, &cout);
45/// cout << endl;
46/// @endcode
47/// Which results in the string being streamed to standard output, surrounded by
48/// double quotes:
49/// @code
50/// "No matter where you go, There you are! -- Buckaroo Banzai"
51/// @endcode
52/// @}
53/** @} */
54/** @} */
55
56/** @addtogroup bsl
57 * @{
58 */
59/** @addtogroup bslim
60 * @{
61 */
62/** @addtogroup bslim_gtestutil
63 * @{
64 */
65
66#include <bslscm_version.h>
67
68#include <bsl_optional.h>
69
70#include <bsl_ostream.h>
71#include <bsl_string.h>
72
73namespace testing {
74namespace internal {
75
76 // ===================================
77 // bslim_Gtestutil_TestingStreamHolder
78 // ===================================
79
80/// This `class` serves as a type in the `testing` namespace to be passed to
81/// an unqualified call to `PrintTo`. By supplying
82/// `bslim_Gtestutil_TestingStreamHolder(&stream)`, which is implicitly
83/// convertible to `bsl::ostream *`, we supply an argument in the `testing`
84/// namespace in the `UniversalPrint` call within the
85/// `bsl::PrintTo(const optional<TYPE>&, ...)` call below, which will affect
86/// ADL to draw in `UniversalPrint` declarations from the `testing`
87/// namespace into consideration. For detailed discussion, see
88/// `IMPLEMENTATION NOTE` in the implementation file.
89///
90/// See @ref bslim_gtestutil
92
93 // DATA
94 bsl::ostream *d_stream_p;
95
96 public:
97 // CREATORS
98
99 /// Create an object bound to the specified `stream`.
100 explicit
101 bslim_Gtestutil_TestingStreamHolder(bsl::ostream *stream);
102
103 // bslim_Gtestutil_TestingStreamHolder(
104 // const bslim_Gtestutil_TestingStreamHolder&) = default;
105
106 // MANIPULATORS
107 // bslim_Gtestutil_TestingStreamHolder& operator=(
108 // const bslim_Gtestutil_TestingStreamHolder&) = default;
109
110 // ACCESSORS
111
112 /// Implicitly return a pointer to the stream this object is bound to.
113 operator bsl::ostream *() const;
114};
115
116} // close namespace internal
117} // close namespace testing
118
119namespace bsl {
120
121// FREE FUNCTIONS
122
123/// Write the specified `value` to the specified `*stream`, surrounded by
124/// double quotes.
125void PrintTo(const string& value, ostream *stream);
126
127/// Write the specified `value` to the specified `*stream`, surrounded by
128/// double quotes, writing non-printable characters with '\x...' escapes.
129void PrintTo(const wstring& value, ostream *stream);
130
131/// Write the specified `value` to the specified `*stream`, surrounded by
132/// double quotes.
133void PrintTo(const BloombergLP::bslstl::StringRef& value, ostream *stream);
134
135/// Write the specified `value` to the specified `*stream`, surrounded by
136/// double quotes.
137template <class TYPE>
138void PrintTo(const optional<TYPE>& value, ostream *stream);
139
140// ============================================================================
141// INLINE DEFINITIONS
142// ============================================================================
143
144} // close namespace bsl
145
146namespace testing {
147namespace internal {
148
149 // -----------------------------------
150 // bslim_Gtestutil_TestingStreamHolder
151 // -----------------------------------
152
153// CREATOR
154inline
156 bsl::ostream *stream)
157: d_stream_p(stream)
158{}
159
160// ACCESSORS
161inline
162bslim_Gtestutil_TestingStreamHolder::operator bsl::ostream *() const
163{
164 return d_stream_p;
165}
166
167} // close namespace internal
168} // close namespace testing
169
170// FREE FUNCTIONS
171template <class TYPE>
172inline
173void bsl::PrintTo(const bsl::optional<TYPE>& value, bsl::ostream *stream)
174{
175 *stream << '(';
176 if (!value.has_value()) {
177 *stream << "nullopt";
178 }
179 else {
180 // 'UniversalPrint' does not need to be forward declared above,
181 // provided that it is eventually declared before this template
182 // function is called.
183
184 UniversalPrint(*value, testing::internal::
185 bslim_Gtestutil_TestingStreamHolder(stream));
186 }
187 *stream << ')';
188}
189
190#endif
191
192// ----------------------------------------------------------------------------
193// Copyright 2018 Bloomberg Finance L.P.
194//
195// Licensed under the Apache License, Version 2.0 (the "License");
196// you may not use this file except in compliance with the License.
197// You may obtain a copy of the License at
198//
199// http://www.apache.org/licenses/LICENSE-2.0
200//
201// Unless required by applicable law or agreed to in writing, software
202// distributed under the License is distributed on an "AS IS" BASIS,
203// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
204// See the License for the specific language governing permissions and
205// limitations under the License.
206// ----------------------------- END-OF-FILE ----------------------------------
207
208/** @} */
209/** @} */
210/** @} */
Definition bslstl_string.h:1281
Definition bslstl_optional.h:1861
bslim_Gtestutil_TestingStreamHolder(bsl::ostream *stream)
Create an object bound to the specified stream.
Definition bslim_gtestutil.h:155
Definition bdlb_printmethods.h:283
void PrintTo(const string &value, ostream *stream)
Definition bslim_gtestutil.h:74
Definition bslim_gtestutil.h:73