ToStringWhich.tests.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // Copyright Catch2 Authors
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // https://www.boost.org/LICENSE_1_0.txt)
  5. // SPDX-License-Identifier: BSL-1.0
  6. #include <catch2/catch_test_macros.hpp>
  7. #if defined(__GNUC__)
  8. // This has to be left enabled until end of the TU, because the GCC
  9. // frontend reports operator<<(std::ostream& os, const has_maker_and_operator&)
  10. // as unused anyway
  11. # pragma GCC diagnostic ignored "-Wunused-function"
  12. #endif
  13. namespace {
  14. struct has_operator { };
  15. struct has_maker {};
  16. struct has_maker_and_operator {};
  17. struct has_neither {};
  18. struct has_template_operator {};
  19. std::ostream& operator<<(std::ostream& os, const has_operator&) {
  20. os << "operator<<( has_operator )";
  21. return os;
  22. }
  23. std::ostream& operator<<(std::ostream& os, const has_maker_and_operator&) {
  24. os << "operator<<( has_maker_and_operator )";
  25. return os;
  26. }
  27. template <typename StreamT>
  28. StreamT& operator<<(StreamT& os, const has_template_operator&) {
  29. os << "operator<<( has_template_operator )";
  30. return os;
  31. }
  32. } // end anonymous namespace
  33. namespace Catch {
  34. template<>
  35. struct StringMaker<has_maker> {
  36. static std::string convert( const has_maker& ) {
  37. return "StringMaker<has_maker>";
  38. }
  39. };
  40. template<>
  41. struct StringMaker<has_maker_and_operator> {
  42. static std::string convert( const has_maker_and_operator& ) {
  43. return "StringMaker<has_maker_and_operator>";
  44. }
  45. };
  46. }
  47. // Call the operator
  48. TEST_CASE( "stringify( has_operator )", "[toString]" ) {
  49. has_operator item;
  50. REQUIRE( ::Catch::Detail::stringify( item ) == "operator<<( has_operator )" );
  51. }
  52. // Call the stringmaker
  53. TEST_CASE( "stringify( has_maker )", "[toString]" ) {
  54. has_maker item;
  55. REQUIRE( ::Catch::Detail::stringify( item ) == "StringMaker<has_maker>" );
  56. }
  57. // Call the stringmaker
  58. TEST_CASE( "stringify( has_maker_and_operator )", "[toString]" ) {
  59. has_maker_and_operator item;
  60. REQUIRE( ::Catch::Detail::stringify( item ) == "StringMaker<has_maker_and_operator>" );
  61. }
  62. TEST_CASE("stringify( has_neither )", "[toString]") {
  63. has_neither item;
  64. REQUIRE( ::Catch::Detail::stringify(item) == "{?}" );
  65. }
  66. // Call the templated operator
  67. TEST_CASE( "stringify( has_template_operator )", "[toString]" ) {
  68. has_template_operator item;
  69. REQUIRE( ::Catch::Detail::stringify( item ) == "operator<<( has_template_operator )" );
  70. }
  71. // Vectors...
  72. TEST_CASE( "stringify( vectors<has_operator> )", "[toString]" ) {
  73. std::vector<has_operator> v(1);
  74. REQUIRE( ::Catch::Detail::stringify( v ) == "{ operator<<( has_operator ) }" );
  75. }
  76. TEST_CASE( "stringify( vectors<has_maker> )", "[toString]" ) {
  77. std::vector<has_maker> v(1);
  78. REQUIRE( ::Catch::Detail::stringify( v ) == "{ StringMaker<has_maker> }" );
  79. }
  80. TEST_CASE( "stringify( vectors<has_maker_and_operator> )", "[toString]" ) {
  81. std::vector<has_maker_and_operator> v(1);
  82. REQUIRE( ::Catch::Detail::stringify( v ) == "{ StringMaker<has_maker_and_operator> }" );
  83. }
  84. namespace {
  85. // Range-based conversion should only be used if other possibilities fail
  86. struct int_iterator {
  87. using iterator_category = std::input_iterator_tag;
  88. using difference_type = std::ptrdiff_t;
  89. using value_type = int;
  90. using reference = int&;
  91. using pointer = int*;
  92. int_iterator() = default;
  93. int_iterator(int i) :val(i) {}
  94. value_type operator*() const { return val; }
  95. bool operator==(int_iterator rhs) const { return val == rhs.val; }
  96. bool operator!=(int_iterator rhs) const { return val != rhs.val; }
  97. int_iterator operator++() { ++val; return *this; }
  98. int_iterator operator++(int) {
  99. auto temp(*this);
  100. ++val;
  101. return temp;
  102. }
  103. private:
  104. int val = 5;
  105. };
  106. struct streamable_range {
  107. int_iterator begin() const { return int_iterator{ 1 }; }
  108. int_iterator end() const { return {}; }
  109. };
  110. std::ostream& operator<<(std::ostream& os, const streamable_range&) {
  111. os << "op<<(streamable_range)";
  112. return os;
  113. }
  114. struct stringmaker_range {
  115. int_iterator begin() const { return int_iterator{ 1 }; }
  116. int_iterator end() const { return {}; }
  117. };
  118. } // end anonymous namespace
  119. namespace Catch {
  120. template <>
  121. struct StringMaker<stringmaker_range> {
  122. static std::string convert(stringmaker_range const&) {
  123. return "stringmaker(streamable_range)";
  124. }
  125. };
  126. }
  127. namespace {
  128. struct just_range {
  129. int_iterator begin() const { return int_iterator{ 1 }; }
  130. int_iterator end() const { return {}; }
  131. };
  132. struct disabled_range {
  133. int_iterator begin() const { return int_iterator{ 1 }; }
  134. int_iterator end() const { return {}; }
  135. };
  136. } // end anonymous namespace
  137. namespace Catch {
  138. template <>
  139. struct is_range<disabled_range> {
  140. static const bool value = false;
  141. };
  142. }
  143. TEST_CASE("stringify ranges", "[toString]") {
  144. REQUIRE(::Catch::Detail::stringify(streamable_range{}) == "op<<(streamable_range)");
  145. REQUIRE(::Catch::Detail::stringify(stringmaker_range{}) == "stringmaker(streamable_range)");
  146. REQUIRE(::Catch::Detail::stringify(just_range{}) == "{ 1, 2, 3, 4 }");
  147. REQUIRE(::Catch::Detail::stringify(disabled_range{}) == "{?}");
  148. }