BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdlt_formattestutil.h
Go to the documentation of this file.
1/// @file bdlt_formattestutil.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlt_formattestutil.h -*-C++-*-
8#ifndef INCLUDED_BDLT_FORMATTESTUTIL
9#define INCLUDED_BDLT_FORMATTESTUTIL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlt_formattestutil bdlt_formattestutil
15/// @brief Provide macros and utilities for testing of `format` in bdlt.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlt
19/// @{
20/// @addtogroup bdlt_formattestutil
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlt_formattestutil-purpose"> Purpose</a>
25/// * <a href="#bdlt_formattestutil-classes"> Classes </a>
26/// * <a href="#bdlt_formattestutil-description"> Description </a>
27///
28/// # Purpose {#bdlt_formattestutil-purpose}
29/// Provide macros and utilities for testing of `format` in bdlt.
30///
31/// # Classes {#bdlt_formattestutil-classes}
32///
33/// - bdlt::FormatTestUtil: namespace class for static functions
34///
35/// @see bslfmt::FormatterTestUtil
36///
37/// # Description {#bdlt_formattestutil-description}
38/// This component provides a pair of functions that support
39/// BDE-style testing of the formatting functionality for `bdlt` value types and
40/// functions facilitating conversions between `bsl::string` and `bsl::wstring`.
41/// @}
42/** @} */
43/** @} */
44
45/** @addtogroup bdl
46 * @{
47 */
48/** @addtogroup bdlt
49 * @{
50 */
51/** @addtogroup bdlt_formattestutil
52 * @{
53 */
54
55#include <bdlscm_version.h>
56
58
59#include <bslma_default.h>
61#include <bslma_testallocator.h>
62
63#include <bsls_assert.h>
64#include <bsls_asserttest.h>
65#include <bsls_review.h>
66
67#include <bsl_iostream.h>
68#include <bsl_string.h>
69
70
71namespace bdlt {
72
73 // ====================
74 // class FormatTestUtil
75 // ====================
76
77/// This `struct` serves as a namespace for static functions to facilitate
78/// testing of formatting of `bdlt` value types -- used in the implementation
79/// of the above macros, and for conversion between `bsl::string` and
80/// `bsl::wstring`.
81///
82/// See @ref bdlt_formattestutil
84 // CLASS METHODS
85
86 /// Convert the string referred to by the specified `viewIn` from `wchar_t`
87 /// to `char` and return the resulting `bsl::string` by value.
88 ///
89 /// \pre The behavior is undefined unless all of the `wchar_t` values in `viewIn`
90 /// are ascii.
91 static bsl::string narrow(const bsl::wstring_view& viewIn);
92
93 /// Compare the specified `exp` to the string result of calling
94 /// `bsl::format` with the specified `format` and `value`, and return
95 /// `true` if they match and `false` otherwise. If they do not match, load
96 /// into the specified `message` a value describing the inputs and result
97 /// of the failed test. Perform these tests for both `char`-based and
98 /// `wchar_t`-based strings.
99 template <class t_VALUE_TYPE>
100 static bool testFormat(bsl::string *message,
101 const bsl::string_view& exp,
102 const bsl::string_view& format,
103 const t_VALUE_TYPE& value);
104
105 /// Compare the specified `exp` to the string result of calling
106 /// `bsl::format` with the specified `format`, `value`, and `arg`, and
107 /// return `true` if they match and `false` otherwise. If they do not
108 /// match, load into the specified `message` a value describing the inputs
109 /// and result of the failed test. Perform these tests for both
110 /// `char`-based and `wchar_t`-based strings.
111 template <class t_VALUE_TYPE, class t_ARG_TYPE>
112 static bool testFormatArg(bsl::string *message,
113 const bsl::string_view& exp,
114 const bsl::string_view& format,
115 const t_VALUE_TYPE& value,
116 const t_ARG_TYPE& arg);
117
118 /// Load the `wstring` version of the specified `char`-based string view `viewIn` into the specified `result`.
119 ///
120 /// \pre The behavior is undefined unless
121 /// all of the `char` values in `viewIn` are ascii.
122 static void widen(bsl::wstring *result, const bsl::string_view& viewIn);
123};
124
125 // --------------------
126 // class FormatTestUtil
127 // --------------------
128
129template <class t_VALUE_TYPE>
131 const bsl::string_view& exp,
132 const bsl::string_view& format,
133 const t_VALUE_TYPE& value)
134{
135 message->clear();
137 message, exp, true, format, value);
138 if (!rc) {
139 message->insert(0, "<char> ");
140 }
141 else {
142 message->clear();
143 bsl::wstring wFormat, expw;
144 widen(&wFormat, format);
145 widen(&expw, exp);
147 message, expw, true, wFormat, value);
148 if (!rc) {
149 message->insert(0, "<wchar_t> ");
150 }
151 }
152
153 return rc;
154}
155
156template <class t_VALUE_TYPE, class t_ARG_TYPE>
158 const bsl::string_view& exp,
159 const bsl::string_view& format,
160 const t_VALUE_TYPE& value,
161 const t_ARG_TYPE& arg)
162{
163 message->clear();
165 message, exp, true, format, value, arg);
166 if (!rc) {
167 message->insert(0, "<char> ");
168 }
169 else {
170 message->clear();
171 bsl::wstring wFormat, expw;
172 widen(&wFormat, format);
173 widen(&expw, exp);
175 message, expw, true, wFormat, value, arg);
176 if (!rc) {
177 message->insert(0, "<wchar_t> ");
178 }
179 }
180
181 return rc;
182}
183
184inline
186 const bsl::string_view& viewIn)
187{
188 result->clear();
189 result->reserve(viewIn.length());
191 for (It it = viewIn.begin(), end = viewIn.end(); it < end; ++it) {
192 const char c = *it;
193 BSLS_ASSERT_SAFE(!(0x80 & c));
194
195 *result += wchar_t(c);
196 }
197}
198
199} // close package namespace
200
201
202#endif
203
204// ----------------------------------------------------------------------------
205// Copyright 2026 Bloomberg Finance L.P.
206//
207// Licensed under the Apache License, Version 2.0 (the "License");
208// you may not use this file except in compliance with the License.
209// You may obtain a copy of the License at
210//
211// http://www.apache.org/licenses/LICENSE-2.0
212//
213// Unless required by applicable law or agreed to in writing, software
214// distributed under the License is distributed on an "AS IS" BASIS,
215// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
216// See the License for the specific language governing permissions and
217// limitations under the License.
218// ----------------------------- END-OF-FILE ----------------------------------
219
220/** @} */
221/** @} */
222/** @} */
Definition bslstl_stringview.h:471
BSLS_KEYWORD_CONSTEXPR const_iterator end() const BSLS_KEYWORD_NOEXCEPT
Return the past-the-end iterator for this view.
Definition bslstl_stringview.h:1848
const_iterator iterator
Definition bslstl_stringview.h:482
BSLS_KEYWORD_CONSTEXPR size_type length() const BSLS_KEYWORD_NOEXCEPT
Return the length of this view.
Definition bslstl_stringview.h:1913
BSLS_KEYWORD_CONSTEXPR const_iterator begin() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_stringview.h:1830
Definition bslstl_string.h:1252
void reserve(size_type newCapacity)
Definition bslstl_string.h:6020
void clear() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_string.h:6043
#define BSLS_ASSERT_SAFE(X)
Definition bsls_assert.h:1917
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bbldc_basicisma30360.h:112
Definition bdlt_formattestutil.h:83
static bsl::string narrow(const bsl::wstring_view &viewIn)
static bool testFormatArg(bsl::string *message, const bsl::string_view &exp, const bsl::string_view &format, const t_VALUE_TYPE &value, const t_ARG_TYPE &arg)
Definition bdlt_formattestutil.h:157
static bool testFormat(bsl::string *message, const bsl::string_view &exp, const bsl::string_view &format, const t_VALUE_TYPE &value)
Definition bdlt_formattestutil.h:130
static void widen(bsl::wstring *result, const bsl::string_view &viewIn)
Definition bdlt_formattestutil.h:185
Definition bslfmt_formattertestutil.h:481