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