cppx-core
boost-test-framework-with-wide-string-fix.hpp
Go to the documentation of this file.
1 #pragma once // Source encoding: UTF-8 with BOM (π is a lowercase Greek "pi").
2 //
3 // In one translation unit, define BOOST_TEST_MODULE before including this.
4 // See the Boost Test documentation.
5 
6 #include <cppx-core/testing/boost-test-framework.hpp> // boost::*, CPPX_xxx
7 
8 #include <cppx-core/language/syntax/macro-use.hpp> // CPPX_USE_STD
9 #include <cppx-core/language/syntax/type-assemblers.hpp> // cppx::(P_, Raw_array_)
10 
11 #include <string_view> // std::(wstring, u16string, u32string)
12 #include <string_view> // std::(wstring_view, u16string_view, u32string_view)
13 #include <ostream> // std::ostream
14 #include <iomanip> // std::(setw, setfill)
15 
16 //namespace boost::test_tools::tt_detail // Depends on Boost version.. :(
17 
18 namespace cppx::boost_fix
19 {
20  CPPX_USE_STD( ostream, basic_string_view );
21 
23  namespace impl
24  {
25  template< class Char >
26  struct Char_type_id_;
27 
28  template<> struct Char_type_id_<char>{ enum{ value = 0 }; };
29  template<> struct Char_type_id_<wchar_t>{ enum{ value = 1 }; };
30  template<> struct Char_type_id_<char16_t>{ enum{ value = 2 }; };
31  template<> struct Char_type_id_<char32_t>{ enum{ value = 3 }; };
32 
33  template< class Char >
34  constexpr int char_type_id_ = Char_type_id_<Char>::value;
35  } // namespace impl
37 
38  template< class Char >
39  inline auto print_on( ostream& os, const P_<const char> type_spec, const basic_string_view<Char>& ws )
40  -> ostream&
41  {
42  static const Raw_array_<P_<const char>> prefixes = { "", "L", "u", "U" };
43 
44  os
45  << type_spec
46  << (*type_spec? "( " : "")
47  << prefixes[impl::char_type_id_<Char>]
48  << "\"";
49  for( const Char wch : ws )
50  {
51  if( ' ' < wch and wch < 127 )
52  {
53  os << char( wch );
54  }
55  else
56  {
57  os
58  << "\\u"
59  << std::hex << std::setfill( '0' )
60  << std::setw( 2*sizeof( wchar_t ) ) << unsigned( wch )
61  << std::setfill( ' ' ) << std::dec
62  << "\" " << prefixes[impl::char_type_id_<Char>] << "\"";
63  }
64  }
65  os << "\"" << (*type_spec? " )" : "");
66  return os;
67  }
68 } // namespace cppx::boost_fix
69 
70 // See <url: https://www.boost.org/doc/libs/1_64_0/libs/test/doc/html/boost_test/test_output/test_tools_support_for_logging/testing_tool_output_disable.html#ref_log_output_custom_customization_point>
71 namespace std // For ADL lookup. Not formally permitted, but needed.
72 {
73  inline auto boost_test_print_type( ostream& os, const wstring_view& ws )
74  -> ostream&
75  { return cppx::boost_fix::print_on( os, "wstring_view", ws ); }
76 
77  inline auto boost_test_print_type( ostream& os, const u16string_view& ws )
78  -> ostream&
79  { return cppx::boost_fix::print_on( os, "u16string_view", ws ); }
80 
81  inline auto boost_test_print_type( ostream& os, const u32string_view& ws )
82  -> ostream&
83  { return cppx::boost_fix::print_on( os, "u32string_view", ws ); }
84 
85  inline auto boost_test_print_type( ostream& os, const wstring& ws )
86  -> ostream&
87  { return cppx::boost_fix::print_on<wchar_t>( os, "wstring", ws ); }
88 
89  inline auto boost_test_print_type( ostream& os, const u16string& ws )
90  -> ostream&
91  { return cppx::boost_fix::print_on<char16_t>( os, "u16string", ws ); }
92 
93  inline auto boost_test_print_type( ostream& os, const u32string& ws )
94  -> ostream&
95  { return cppx::boost_fix::print_on<char32_t>( os, "u32string", ws ); }
96 
97  inline auto boost_test_print_type( ostream& os, const cppx::P_<const wchar_t> ws )
98  -> ostream&
99  { return cppx::boost_fix::print_on<wchar_t>( os, "", ws ); }
100 
101  inline auto boost_test_print_type( ostream& os, const cppx::P_<const char16_t> ws )
102  -> ostream&
103  { return cppx::boost_fix::print_on<char16_t>( os, "", ws ); }
104 
105  inline auto boost_test_print_type( ostream& os, const cppx::P_<char32_t> ws )
106  -> ostream&
107  { return cppx::boost_fix::print_on<char32_t>( os, "", ws ); }
108 } // namespace std
Some_type * P_
Creates a raw pointer type.
auto print_on(ostream &os, const P_< const char > type_spec, const basic_string_view< Char > &ws) -> ostream &
auto boost_test_print_type(ostream &os, const cppx::P_< char32_t > ws) -> ostream &
CPPX_USE_STD(ostream, basic_string_view)
Simple type builders Type_, P_, R_, Raw_array_ and Raw_array_of_.
Item[] Raw_array_
Ceates a raw array type of unknown size.
Macros for generating more concise and clear using statements, primarily $use_cppx and $use_std,...