Skip to content

Commit

Permalink
string_view convertible printto (#562)
Browse files Browse the repository at this point in the history
* string_view convertible printto

* up

* test

* fix

* fix

* fix

* fix

* fix

* fix

* fix test

* fix

* fix
  • Loading branch information
srz-zumix committed Feb 14, 2021
1 parent 615c15f commit eb4992f
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 16 deletions.
15 changes: 15 additions & 0 deletions include/internal/iutest_type_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ template<typename T>struct is_member_function_pointer;

using ::std::true_type;
using ::std::false_type;
using ::std::conditional;
using ::std::remove_const;
using ::std::remove_volatile;
using ::std::remove_reference;
Expand Down Expand Up @@ -211,6 +212,20 @@ template<bool B>const bool bool_constant<B>::value;
typedef bool_constant<true> true_type;
typedef bool_constant<false> false_type;

/**
* @brief conditional
*/
template<bool B, typename T, typename U>
class conditional
{
template<bool X, typename TMP>
struct impl { typedef T type; };
template<typename TMP>
struct impl<false, TMP> { typedef U type; };
public:
typedef typename impl<B, void>::type type;
};

#if !defined(IUTEST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)

/**
Expand Down
64 changes: 49 additions & 15 deletions include/iutest_printers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@
namespace iutest
{

// PrintTo (User defined)
// PrintTo (iutest)
// DefaultPrintTo
// container
// pointer
// ostream operator <<
// BiggestInt
// bytes

//======================================================================
// declare
template<typename T>
Expand All @@ -34,6 +43,9 @@ std::string PrintToString(const T& v);
namespace detail
{

template<typename T>
void UniversalPrint(const T& value, iu_ostream* os);

inline void PrintBytesInObjectTo(const unsigned char* buf, size_t size, iu_ostream* os)
{
IUTEST_PRAGMA_CONSTEXPR_CALLED_AT_RUNTIME_WARN_DISABLE_BEGIN()
Expand Down Expand Up @@ -66,9 +78,7 @@ namespace printer_internal
namespace formatter
{

/** @private */
template<bool convertible>
struct Printer
struct RawBytesPrinter
{
template<typename T>
static void Print(const T& value, iu_ostream* os)
Expand All @@ -80,21 +90,40 @@ struct Printer
}
};

template<>
struct Printer<true>
struct StringViewPrinter
{
template<typename T>
static void Print(const T& value, iu_ostream* os)
static void Print(iu_string_view value, iu_ostream* os)
{
UniversalPrint(value, os);
}
};

struct BiggestIntPrinter
{
static void Print(BiggestInt value, iu_ostream* os)
{
#if IUTEST_HAS_BIGGESTINT_OSTREAM
const BiggestInt v = value;
*os << value;
#else
const Int32 v = value;
#endif
*os << v;
#endif
}
};

/** @private */
template<typename T>
struct PrinterTypeSelecter
{
typedef typename iutest_type_traits::conditional<iutest_type_traits::is_convertible<T, BiggestInt>::value
, BiggestIntPrinter
, typename iutest_type_traits::conditional<iutest_type_traits::is_convertible<T, iu_string_view>::value
, StringViewPrinter
, RawBytesPrinter
>::type
>::type type;
};

} // end of namespace formatter

/** @private */
Expand All @@ -104,8 +133,8 @@ class TypeWithoutFormatter
template<typename T>
static void PrintValue(const T& value, iu_ostream* os)
{
formatter::Printer<
iutest_type_traits::is_convertible<const T&, BiggestInt>::value>::Print(value, os);
typedef typename formatter::PrinterTypeSelecter<const T&>::type Printer;
Printer::Print(value, os);
}
};

Expand Down Expand Up @@ -149,8 +178,6 @@ void DefaultPrintNonContainerTo(const T& value, iu_ostream* os)

//======================================================================
// declare
template<typename T>
void UniversalPrint(const T& value, iu_ostream* os);

//======================================================================
// function
Expand Down Expand Up @@ -192,7 +219,7 @@ inline void DefaultPrintNonContainerTo(const T& value, iu_ostream* os)
#if !defined(IUTEST_NO_ARGUMENT_DEPENDENT_LOOKUP)
printer_internal2::DefaultPrintNonContainerTo(value, os);
#else
printer_internal::formatter::Printer<false>::Print(value, os);
printer_internal::formatter::RawBytesPrinter::Print(value, os);
#endif
}
/** @overload */
Expand Down Expand Up @@ -386,7 +413,14 @@ inline void PrintTo(const unsigned char value, iu_ostream* os)
{
*os << static_cast<unsigned int>(value);
}
#if IUTEST_HAS_CXX_HDR_STRING_VIEW
#if IUTEST_USE_OWN_STRING_VIEW
template<typename CharT, typename Traits>
inline void PrintTo(const iu_basic_string_view<CharT, Traits>& value, iu_ostream* os)
{
const ::std::basic_string<CharT, Traits> str = value.data();
UniversalTersePrint(str.c_str(), os);
}
#else
template<typename CharT, typename Traits>
inline void PrintTo(const ::std::basic_string_view<CharT, Traits>& value, iu_ostream* os)
{
Expand Down
77 changes: 76 additions & 1 deletion test/unit_string_view_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* @author t.shirayanagi
* @par copyright
* Copyright (C) 2019, Takazumi Shirayanagi\n
* Copyright (C) 2019-2021, Takazumi Shirayanagi\n
* The new BSD License is applied to this software.
* see LICENSE
*/
Expand All @@ -16,6 +16,47 @@
//======================================================================
// include
#include "iutest.hpp"
#include "logger_tests.hpp"

#if !defined(IUTEST_USE_GTEST)

#define IUTEST_PRINTTOSTRING_EQ(expect, val) \
IUTEST_EXPECT_STREQ(static_cast<const char*>(expect), ::iutest::PrintToString(val))

#if !defined(IUTEST_NO_ARGUMENT_DEPENDENT_LOOKUP)

#define IUTEST_PRINTTOSTRING_CHECK IUTEST_PRINTTOSTRING_EQ

#else

#define IUTEST_PRINTTOSTRING_CHECK(expect, val) \
IUTEST_EXPECT_STRIN(static_cast<const char*>(expect), ::iutest::PrintToString(val))

#endif

#else

#define IUTEST_PRINTTOSTRING_EQ(expect, val) \
(void)(expect); \
(void)(val)

#define IUTEST_PRINTTOSTRING_CHECK(expect, val) \
(void)(expect); \
(void)(val)

#endif

#if !defined(IUTEST_USE_GTEST) && !defined(IUTEST_NO_ARGUMENT_DEPENDENT_LOOKUP)

#define IUTEST_STREAMOUT_CHECK(val) \
IUTEST_SUCCEED() << val

#else

#define IUTEST_STREAMOUT_CHECK(val) \
IUTEST_SUCCEED() << ::iutest::PrintToString(val)

#endif

IUTEST(UnitTest, StringView)
{
Expand Down Expand Up @@ -43,3 +84,37 @@ IUTEST(UnitTest, StringView)

//IUTEST_EXPECT_STRNE("Abc", sv1);
}

IUTEST(UnitTest, PrintStringView)
{
LogChecker ck("XYZ");
::iutest::detail::iu_string_view view = "XYZ";
IUTEST_PRINTTOSTRING_EQ(ck, view);
IUTEST_STREAMOUT_CHECK(view);
}

struct StringViewConvertible
{
::std::string name;
StringViewConvertible()
: name("ABC")
{
}

operator ::iutest::detail::iu_string_view () const
{
return name;
}
};

IUTEST(UnitTest, PrintStringViewConvertible)
{
#if !defined(IUTEST_NO_ARGUMENT_DEPENDENT_LOOKUP)
LogChecker ck("ABC");
#else
LogChecker ck("-Byte object");
#endif
StringViewConvertible v;
IUTEST_PRINTTOSTRING_CHECK(ck, v);
IUTEST_STREAMOUT_CHECK(v);
}

0 comments on commit eb4992f

Please sign in to comment.