Skip to main content

anonymous{reporter-tap.cpp} Namespace

Local helpers of the TAP reporter. More...

Definition​

namespace micro_os_plus::micro_test_plus::detail::anonymous{reporter-tap.cpp} { ... }

Classes Index​

classtap_name

A name prepared for a single TAP line, without a copy when it needs no change. More...

Functions Index​

std::stringtap_text (std::string_view text, bool escape)

Prepares a text for a single TAP line. More...

std::stringyaml_double_quoted (std::string_view text)

Formats a text as a YAML double-quoted scalar. More...

std::stringyaml_scalar (std::string_view text)

Formats a text as a YAML scalar, double-quoted only when needed. More...

Description​

Local helpers of the TAP reporter.

The unnamed namespace gives the helpers internal linkage, so that they are visible only in this translation unit, and cannot conflict with the names of the application or of the other source files of the framework. They are not declared in any header.

It contains the functions that make the texts safe to write in the TAP output: tap_text(), for the test point descriptions, the reasons, and the comments, and yaml_double_quoted(), for the values of the YAML diagnostic blocks.

Functions​

tap_text()​

std::string micro_os_plus::micro_test_plus::detail::anonymous{reporter-tap.cpp}::tap_text (std::string_view text, bool escape)

Prepares a text for a single TAP line.

Each line break (\\n, \\r, or the \\r\\n pair) is replaced by a space, so that the text does not end the line. When escape is true, each \\ is written as \\\\ and each # as \\#, as the TAP 14 specification recommends for the test point descriptions and the reasons, so that a # in the text does not start a directive (for example # TODO, which would hide a failure). The # Subtest: comments are not escaped, since the specification defines no escapes in comments, and the node-tap parser uses their text as it is.

Parameters
text

The text to prepare.

escape

Whether to escape the \\ and # characters.

Returns

The text, without line breaks, and escaped if requested.

Definition at line 104 of file reporter-tap.cpp.

104 tap_text (std::string_view text, bool escape)
105 {
106 std::string result;
107 result.reserve (text.size ());
108
109 for (size_t i = 0; i < text.size (); ++i)
110 {
111 const char c = text[i];
112 if (c == '\n' || c == '\r')
113 {
114 // The `\r\n` pair is a single line break.
115 if (c == '\r' && i + 1 < text.size () && text[i + 1] == '\n')
116 {
117 ++i;
118 }
119 result.push_back (' ');
120 }
121 else if (escape && (c == '\\' || c == '#'))
122 {
123 result.push_back ('\\');
124 result.push_back (c);
125 }
126 else
127 {
128 result.push_back (c);
129 }
130 }
131
132 return result;
133 }

Reference tap_text.

Referenced by micro_os_plus::micro_test_plus::detail::anonymous{reporter-tap.cpp}::tap_name::tap_name and tap_text.

yaml_double_quoted()​

std::string micro_os_plus::micro_test_plus::detail::anonymous{reporter-tap.cpp}::yaml_double_quoted (std::string_view text)

Formats a text as a YAML double-quoted scalar.

The text is enclosed in ", with each \\ written as \\\\, each " as \\", and each control character (including the line breaks, the tab, and DEL) as a YAML escape sequence (\\0, \\t, \\n, \\r, \\e, and so on, or \\xHH for the others), so that the value is a single line, and is valid YAML whatever it contains. As plain scalars, the values starting with {, [, or ', or containing : or #, would be rejected or truncated by the YAML parsers used by the TAP consumers. The bytes above 0x7F are written as they are, since they are expected to be parts of UTF-8 sequences.

Parameters
text

The text to format.

Returns

The double-quoted scalar.

Definition at line 294 of file reporter-tap.cpp.

294 yaml_double_quoted (std::string_view text)
295 {
296 constexpr std::string_view digits{ "0123456789ABCDEF" };
297
298 std::string result;
299 result.reserve (text.size () + 2);
300
301 result.push_back ('"');
302 for (const char c : text)
303 {
304 const auto u = static_cast<unsigned char> (c);
305 switch (c)
306 {
307 case '\\':
308 result.append ("\\\\");
309 break;
310 case '"':
311 result.append ("\\\"");
312 break;
313 case '\0':
314 result.append ("\\0");
315 break;
316 case '\a':
317 result.append ("\\a");
318 break;
319 case '\b':
320 result.append ("\\b");
321 break;
322 case '\t':
323 result.append ("\\t");
324 break;
325 case '\n':
326 result.append ("\\n");
327 break;
328 case '\v':
329 result.append ("\\v");
330 break;
331 case '\f':
332 result.append ("\\f");
333 break;
334 case '\r':
335 result.append ("\\r");
336 break;
337 case '\x1B':
338 result.append ("\\e");
339 break;
340 default:
341 if (u < 0x20 || u == 0x7F)
342 {
343 result.append ("\\x");
344 result.push_back (digits[static_cast<std::size_t> (u >> 4)]);
345 result.push_back (
346 digits[static_cast<std::size_t> (u & 0x0F)]);
347 }
348 else
349 {
350 result.push_back (c);
351 }
352 break;
353 }
354 }
355 result.push_back ('"');
356
357 return result;
358 }

Reference yaml_double_quoted.

Referenced by yaml_double_quoted and yaml_scalar.

yaml_scalar()​

std::string micro_os_plus::micro_test_plus::detail::anonymous{reporter-tap.cpp}::yaml_scalar (std::string_view text)

Formats a text as a YAML scalar, double-quoted only when needed.

A text made only of letters, digits, and the characters _, ., +, /, and -, and not starting with -, is a valid plain scalar, whatever its meaning, and is returned unchanged, so that the usual values are written as before. Any other text, for example a file name with a space, a #, or a :, which a YAML parser would truncate, split, or reject as a plain scalar, is returned double-quoted, as yaml_double_quoted() does.

Parameters
text

The text to format.

Returns

The scalar.

Definition at line 377 of file reporter-tap.cpp.

377 yaml_scalar (std::string_view text)
378 {
379 bool is_plain = !text.empty () && text.front () != '-';
380 for (const char c : text)
381 {
382 const bool is_safe = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
383 || (c >= '0' && c <= '9') || c == '_'
384 || c == '.' || c == '+' || c == '/' || c == '-';
385 is_plain = is_plain && is_safe;
386 }
387
388 return is_plain ? std::string{ text } : yaml_double_quoted (text);
389 }

References yaml_double_quoted and yaml_scalar.

Referenced by yaml_scalar.


The documentation for this namespace was generated from the following file:


Generated via doxygen2docusaurus 2.2.8 by Doxygen 1.18.0.