C++Spec 1.1.0
BDD testing for C++
Loading...
Searching...
No Matches
CppSpec::Pretty Class Reference

A helper base class that assists in pretty-printing various objects. More...

#include <pretty_matchers.hpp>

Inheritance diagram for CppSpec::Pretty:

Public Member Functions

std::string name (const std::string &name) const
std::string name_to_sentence (const std::string &name) const
template<typename O>
std::string inspect_object (const O &o)
 Generate a string of the class and data of an object.

Static Public Member Functions

static std::string split_words (const std::string &sym)
static std::string underscore (const std::string &camel_cased_word)
static std::string last (const std::string &s, char delim)
static std::string improve_hash_formatting (const std::string &inspect_string)
template<typename T>
static std::string to_word (const T &item)
 Formats an object as a string when operator<< is available.
template<typename T>
static std::string to_word (const T &item)
template<typename T>
static std::string to_word (const T &item)
template<typename T>
static std::string to_word (const T &item)
template<typename T>
static std::string to_word (const T &item)
template<typename T>
static std::string to_word (const T &item)
template<typename T>
static std::string to_word (const std::optional< T > &item)
 Formats an optional as its value, or std::nullopt when it holds none.
template<typename T>
static std::string to_word_type (const T &item)
template<typename A, typename E>
static std::string to_word (const Matchers::MatcherBase< A, E > &matcher)
template<class T>
static std::string to_sentence (const T &item)
 Take a single object and format it as a sentance.
template<class T>
static std::string to_sentence (const std::vector< T > &words)
template<typename T>
static std::string inspect_object (const T &object)
static std::string address_of (const void *address)
 Formats an address as 0x and lowercase hex digits, the same on every platform.
template<>
std::string to_word (const bool &item)
template<>
std::string to_word (const unsigned char &item)
template<>
std::string to_word (const signed char &item)

Public Attributes

std::string _name

Detailed Description

A helper base class that assists in pretty-printing various objects.

This class provides helper string manipulation functions that are used to generate human-readable information and output.

Member Function Documentation

◆ address_of()

std::string CppSpec::Pretty::address_of ( const void * address)
inlinestatic

Formats an address as 0x and lowercase hex digits, the same on every platform.

std::ostream prints a pointer in the platform's p form, which on MSVC has no 0x and uppercase digits.

Parameters
addressthe address to be processed
Returns
the string representation, e.g. 0x7ffd5c3e1a2c

◆ inspect_object()

template<typename O>
std::string CppSpec::Pretty::inspect_object ( const O & o)
inline

Generate a string of the class and data of an object.

Parameters
othe object to inspect
Returns
the generated string

◆ to_sentence() [1/2]

template<typename T>
std::string CppSpec::Pretty::to_sentence ( const std::vector< T > & objects)
inlinestatic

Takes a vector of objects and formats them as an English style list (i.e. "a, b, and c")

Parameters
wordsthe collection of objects to be processed
Returns
a human-readable comma-delimited list

◆ to_sentence() [2/2]

template<typename T>
std::string CppSpec::Pretty::to_sentence ( const T & item)
inlinestatic

Take a single object and format it as a sentance.

Parameters
itemthe object to be processed
Returns
a human-readable representation of the object (as a list)

◆ to_word() [1/3]

template<typename A, typename E>
std::string CppSpec::Pretty::to_word ( const Matchers::MatcherBase< A, E > & matcher)
inlinestatic

Formats a Matcher as a string

Unlike the generic to_word(), this is specialized for Matchers, which have a custom description associated with them for better information.

Parameters
matcherthe matcher to process
Returns
a string representation of the Matcher

◆ to_word() [2/3]

template<typename T>
std::string CppSpec::Pretty::to_word ( const std::optional< T > & item)
inlinestatic

Formats an optional as its value, or std::nullopt when it holds none.

An optional that holds another optional wraps the value it holds, so it reads apart from an empty one: std::optional(std::nullopt) against std::nullopt.

Parameters
itemthe optional to be processed
Returns
the string representation

◆ to_word() [3/3]

template<typename T>
std::string CppSpec::Pretty::to_word ( const T & item)
inlinestatic

Formats an object as a string when operator<< is available.

Formats an enum that has no operator<< as its type and underlying value as a number, e.g. Colour(2).

Formats a container, pair or tuple whose elements Pretty prints differently from operator<<, element by element through to_word().

Formats a pointer to signed char, unsigned char or a wide character as an address, or nullptr.

Formats a wide character (wchar_t, char8_t, char16_t or char32_t) as the number of its code unit.

Formats an object as a string when there is no operator<<.

In reality, this uses the << operator, simply providing a way to convert the object to a string without having to deal with stringstreams.

Parameters
itemthe object to be processed
Returns
the string representation
Parameters
itemthe object to be processed
Returns
the string representation

std::ostream has no operator<< for these, so they are printed as numbers, like byte-sized integers.

Parameters
itemthe character to be processed
Returns
the string representation, e.g. 97 for u'a'

Only a char pointer is read as text. These point at bytes or code units, which std::ostream would otherwise read up to a NUL (signed and unsigned char) or not print at all (wide characters).

Parameters
itemthe pointer to be processed
Returns
the string representation

The container keeps cxx-prettyprint's delimiters ([1, 2], {1, 2} for a set, (1, 2) for a pair or tuple), so only the elements read differently: std::vector<std::uint8_t>{65, 66} prints [65, 66], not [A, B].

Parameters
itemthe container, pair or tuple to be processed
Returns
the string representation

The value is widened to the largest integer of its signedness first, so an enum based on a character type prints a number too, not a character.

Parameters
itemthe enumerator to be processed
Returns
the string representation

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