Stefan | e8ba771 | 2022-04-28 02:47:24 +0200 | [diff] [blame] | 1 | #ifdef _MSC_VER |
| 2 | __pragma(warning(push)) |
| 3 | __pragma(warning(disable : 4643)) |
| 4 | namespace std { |
| 5 | template <typename> struct char_traits; |
| 6 | template <typename, typename> class basic_ostream; |
| 7 | typedef basic_ostream<char, char_traits<char>> ostream; |
| 8 | template<class TRAITS> |
| 9 | basic_ostream<char, TRAITS>& operator<<(basic_ostream<char, TRAITS>&, const char*); |
| 10 | } |
| 11 | __pragma(warning(pop)) |
| 12 | #else |
| 13 | #include <iostream> |
| 14 | #endif |
| 15 | |
| 16 | namespace N { |
| 17 | struct A { }; |
| 18 | struct B { |
| 19 | friend std::ostream& operator<<(std::ostream& os, const B&) { return os << "B"; } |
| 20 | }; |
| 21 | struct C { }; |
| 22 | static std::ostream& operator<<(std::ostream& os, const C&) { return os << "C"; } |
| 23 | } |
| 24 | |
| 25 | static std::ostream& operator<<(std::ostream& os, const N::A&) { return os << "A"; } |
| 26 | |
ncihnegn | c5458f2 | 2019-01-28 05:08:18 -0800 | [diff] [blame] | 27 | #include <doctest/doctest.h> |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 28 | |
Stefan | e8ba771 | 2022-04-28 02:47:24 +0200 | [diff] [blame] | 29 | #include <utility> |
| 30 | |
| 31 | TEST_CASE("operator<<") { |
| 32 | MESSAGE(N::A{ }); |
| 33 | MESSAGE(N::B{ }); |
| 34 | MESSAGE(N::C{ }); |
| 35 | } |
| 36 | |
onqtam | 7cc0e96 | 2017-04-17 23:30:36 +0300 | [diff] [blame] | 37 | #include "header.h" |
| 38 | |
Stefan | e8ba771 | 2022-04-28 02:47:24 +0200 | [diff] [blame] | 39 | // std::move is broken with VS <= 15 |
| 40 | #if defined(_MSC_VER) && _MSC_VER <= 1900 |
| 41 | #define MOVE(...) __VA_ARGS__ |
| 42 | #else |
| 43 | #define MOVE std::move |
| 44 | #endif |
| 45 | |
| 46 | TEST_CASE("no headers") { |
| 47 | char chs[] = { '1', 'a', 's' }; |
| 48 | MESSAGE(chs); CHECK(chs == nullptr); |
| 49 | MESSAGE("1as"); CHECK("1as" == nullptr); |
| 50 | |
| 51 | int ints[] = { 0, 1, 1, 2, 3, 5, 8, 13 }; |
| 52 | MESSAGE(ints); CHECK(ints == nullptr); |
| 53 | MESSAGE(MOVE(ints)); |
| 54 | |
| 55 | char* cptr = reinterpret_cast<char*>(ints + 4); |
| 56 | const char* ccptr = const_cast<const char*>(cptr); |
| 57 | void* vptr = reinterpret_cast<void*>(cptr); |
| 58 | CHECK(doctest::toString(cptr) == doctest::toString(ccptr)); |
| 59 | CHECK(doctest::toString(ccptr) == doctest::toString(vptr)); |
| 60 | |
| 61 | char* cnptr = nullptr; |
| 62 | MESSAGE(cnptr); CHECK(cnptr != nullptr); |
| 63 | |
| 64 | enum Test { |
| 65 | A = 0, B, C = 100, |
| 66 | }; |
| 67 | MESSAGE(A); CHECK(A == C); |
| 68 | |
| 69 | MESSAGE(doctest::toString<int>()); |
| 70 | } |
| 71 | |
onqtam | abf39d2 | 2017-10-28 21:30:45 +0300 | [diff] [blame] | 72 | DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 73 | #include <string> |
| 74 | #include <vector> |
| 75 | #include <list> |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 76 | #include <sstream> |
Stefan | 58f3ec8 | 2022-01-13 21:03:37 +0100 | [diff] [blame] | 77 | #include <limits> |
onqtam | abf39d2 | 2017-10-28 21:30:45 +0300 | [diff] [blame] | 78 | DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 79 | |
onqtam | 190ba25 | 2019-03-23 09:58:34 +0200 | [diff] [blame] | 80 | DOCTEST_MSVC_SUPPRESS_WARNING(5045) // Spectre mitigation diagnostics |
| 81 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 82 | // the standard forbids writing in the std namespace but it works on all compilers |
| 83 | namespace std |
| 84 | { |
| 85 | template <typename T> |
| 86 | ostream& operator<<(ostream& stream, const vector<T>& in) { |
| 87 | stream << "["; |
Stefan | e8ba771 | 2022-04-28 02:47:24 +0200 | [diff] [blame] | 88 | for (size_t i = 0; i < in.size(); ++i) { |
| 89 | if (i != 0) { stream << ", "; } |
| 90 | stream << in[i]; |
| 91 | } |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 92 | stream << "]"; |
| 93 | return stream; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // as an alternative you may write a specialization of doctest::StringMaker |
| 98 | namespace doctest |
| 99 | { |
| 100 | template <typename T> |
Stefan | e8ba771 | 2022-04-28 02:47:24 +0200 | [diff] [blame] | 101 | struct StringMaker<std::list<T>> |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 102 | { |
| 103 | static String convert(const std::list<T>& in) { |
| 104 | std::ostringstream oss; |
| 105 | |
| 106 | oss << "["; |
Stefan | e8ba771 | 2022-04-28 02:47:24 +0200 | [diff] [blame] | 107 | for (typename std::list<T>::const_iterator it = in.begin(); it != in.end();) { |
| 108 | oss << *it; |
| 109 | if (++it != in.end()) { oss << ", "; } |
| 110 | } |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 111 | oss << "]"; |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 112 | return oss.str().c_str(); |
| 113 | } |
| 114 | }; |
| 115 | } |
| 116 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 117 | template <typename T, typename K> |
| 118 | struct MyType |
| 119 | { |
| 120 | T one; |
| 121 | K two; |
| 122 | }; |
| 123 | |
| 124 | template <typename T> |
onqtam | 02b7eb7 | 2017-10-28 23:59:49 +0300 | [diff] [blame] | 125 | struct MyTypeInherited : MyType<T, unsigned> |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 126 | {}; |
| 127 | |
| 128 | template <typename T, typename K> |
| 129 | bool operator==(const MyType<T, K>& lhs, const MyType<T, K>& rhs) { |
| 130 | return lhs.one == rhs.one && lhs.two == rhs.two; |
| 131 | } |
| 132 | |
| 133 | template <typename T, typename K> |
| 134 | std::ostream& operator<<(std::ostream& stream, const MyType<T, K>& in) { |
| 135 | stream << "[" << in.one << ", " << in.two << "]"; |
| 136 | return stream; |
| 137 | } |
| 138 | |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 139 | namespace Bar |
| 140 | { |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 141 | struct Foo |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 142 | { |
| 143 | friend bool operator==(const Foo&, const Foo&) { return false; } |
| 144 | }; |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 145 | |
onqtam | b18680d | 2016-11-15 14:08:56 +0200 | [diff] [blame] | 146 | // as a third option you may provide an overload of toString() |
onqtam | 7cc0e96 | 2017-04-17 23:30:36 +0300 | [diff] [blame] | 147 | inline doctest::String toString(const Foo&) { return "Foo{}"; } |
Viktor Kirilov | 17d984c | 2020-12-04 15:13:33 +0200 | [diff] [blame] | 148 | |
Viktor Kirilov | 81fe2e8 | 2020-12-04 18:59:26 +0200 | [diff] [blame] | 149 | struct MyOtherType |
Viktor Kirilov | 17d984c | 2020-12-04 15:13:33 +0200 | [diff] [blame] | 150 | { |
Viktor Kirilov | 81fe2e8 | 2020-12-04 18:59:26 +0200 | [diff] [blame] | 151 | int data; |
| 152 | friend bool operator==(const MyOtherType& l, const MyOtherType& r) { return l.data == r.data; } |
Viktor Kirilov | 17d984c | 2020-12-04 15:13:33 +0200 | [diff] [blame] | 153 | }; |
| 154 | |
Viktor Kirilov | 81fe2e8 | 2020-12-04 18:59:26 +0200 | [diff] [blame] | 155 | // you also can use a template operator<< if your code does not use std::ostream |
Viktor Kirilov | 17d984c | 2020-12-04 15:13:33 +0200 | [diff] [blame] | 156 | template <class OStream> |
Viktor Kirilov | 81fe2e8 | 2020-12-04 18:59:26 +0200 | [diff] [blame] | 157 | OStream& operator<<(OStream& stream, const MyOtherType& in) { |
| 158 | stream << "MyOtherType: " << in.data; |
Viktor Kirilov | 17d984c | 2020-12-04 15:13:33 +0200 | [diff] [blame] | 159 | return stream; |
| 160 | } |
| 161 | |
onqtam | b18680d | 2016-11-15 14:08:56 +0200 | [diff] [blame] | 162 | } // namespace Bar |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 163 | |
onqtam | 05bcc37 | 2017-03-17 02:10:38 +0200 | [diff] [blame] | 164 | // set an exception translator for MyTypeInherited<int> |
| 165 | REGISTER_EXCEPTION_TRANSLATOR(MyTypeInherited<int>& ex) { |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 166 | return doctest::String("MyTypeInherited<int>(") + doctest::toString(ex.one) + ", " + |
| 167 | doctest::toString(ex.two) + ")"; |
onqtam | 05bcc37 | 2017-03-17 02:10:38 +0200 | [diff] [blame] | 168 | } |
| 169 | |
Stefan | e8ba771 | 2022-04-28 02:47:24 +0200 | [diff] [blame] | 170 | #define CHECK_NOT_DEFAULT_STR(var) CHECK(toString(var) != "{?}") |
| 171 | |
onqtam | 7cc0e96 | 2017-04-17 23:30:36 +0300 | [diff] [blame] | 172 | TEST_CASE("all asserts should fail and show how the objects get stringified") { |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 173 | MyTypeInherited<int> bla1; |
| 174 | bla1.one = 5; |
onqtam | 02b7eb7 | 2017-10-28 23:59:49 +0300 | [diff] [blame] | 175 | bla1.two = 4u; |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 176 | |
onqtam | b18680d | 2016-11-15 14:08:56 +0200 | [diff] [blame] | 177 | Bar::Foo f1; |
Stefan | e8ba771 | 2022-04-28 02:47:24 +0200 | [diff] [blame] | 178 | MESSAGE(f1); |
onqtam | b18680d | 2016-11-15 14:08:56 +0200 | [diff] [blame] | 179 | Bar::Foo f2; |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 180 | CHECK(f1 == f2); |
| 181 | |
| 182 | // std::string already has an operator<< working with std::ostream |
onqtam | 367098b | 2021-12-15 15:15:06 +0200 | [diff] [blame] | 183 | std::string dummy = "omg"; |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 184 | |
Stefan | e8ba771 | 2022-04-28 02:47:24 +0200 | [diff] [blame] | 185 | MESSAGE(dummy); |
| 186 | |
onqtam | 367098b | 2021-12-15 15:15:06 +0200 | [diff] [blame] | 187 | CHECK(dummy == "tralala"); // should fail |
| 188 | CHECK("tralala" == dummy); // should fail |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 189 | |
| 190 | std::vector<int> vec1; |
| 191 | vec1.push_back(1); |
| 192 | vec1.push_back(2); |
| 193 | vec1.push_back(3); |
| 194 | |
Stefan | e8ba771 | 2022-04-28 02:47:24 +0200 | [diff] [blame] | 195 | MESSAGE(vec1); |
| 196 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 197 | std::vector<int> vec2; |
| 198 | vec2.push_back(1); |
| 199 | vec2.push_back(2); |
| 200 | vec2.push_back(4); |
| 201 | |
| 202 | CHECK(vec1 == vec2); |
| 203 | |
| 204 | std::list<int> lst_1; |
| 205 | lst_1.push_back(1); |
| 206 | lst_1.push_back(42); |
| 207 | lst_1.push_back(3); |
| 208 | |
Stefan | e8ba771 | 2022-04-28 02:47:24 +0200 | [diff] [blame] | 209 | MESSAGE(lst_1); |
| 210 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 211 | std::list<int> lst_2; |
| 212 | lst_2.push_back(1); |
| 213 | lst_2.push_back(2); |
| 214 | lst_2.push_back(666); |
| 215 | |
| 216 | CHECK(lst_1 == lst_2); |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 217 | |
Viktor Kirilov | 17d984c | 2020-12-04 15:13:33 +0200 | [diff] [blame] | 218 | { |
Viktor Kirilov | 81fe2e8 | 2020-12-04 18:59:26 +0200 | [diff] [blame] | 219 | Bar::MyOtherType s1 {42}; |
| 220 | Bar::MyOtherType s2 {666}; |
Viktor Kirilov | 17d984c | 2020-12-04 15:13:33 +0200 | [diff] [blame] | 221 | INFO("s1=", s1, " s2=", s2); |
Viktor Kirilov | 17d984c | 2020-12-04 15:13:33 +0200 | [diff] [blame] | 222 | CHECK(s1 == s2); |
| 223 | CHECK_MESSAGE(s1 == s2, s1, " is not really ", s2); |
| 224 | } |
| 225 | |
Stefan | e8ba771 | 2022-04-28 02:47:24 +0200 | [diff] [blame] | 226 | CHECK_NOT_DEFAULT_STR(doctest::IsNaN<double>(0.5)); |
| 227 | CHECK_NOT_DEFAULT_STR(!doctest::IsNaN<float>(std::numeric_limits<float>::infinity())); |
| 228 | CHECK_NOT_DEFAULT_STR(doctest::IsNaN<double long>(std::numeric_limits<double long>::quiet_NaN())); |
Stefan | 58f3ec8 | 2022-01-13 21:03:37 +0100 | [diff] [blame] | 229 | |
Menno Fraters | 8de4cf7 | 2022-03-05 23:14:35 +0000 | [diff] [blame] | 230 | CHECK("a" == doctest::Contains("aaa")); |
| 231 | |
onqtam | 05bcc37 | 2017-03-17 02:10:38 +0200 | [diff] [blame] | 232 | // lets see if this exception gets translated |
onqtam | 7cc0e96 | 2017-04-17 23:30:36 +0300 | [diff] [blame] | 233 | throw_if(true, bla1); |
onqtam | 05bcc37 | 2017-03-17 02:10:38 +0200 | [diff] [blame] | 234 | } |
| 235 | |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 236 | static doctest::String intTranslator(int ex) { |
| 237 | return doctest::String("int: ") + doctest::toString(ex); |
| 238 | } |
onqtam | 246e817 | 2017-03-17 02:48:12 +0200 | [diff] [blame] | 239 | |
onqtam | 05bcc37 | 2017-03-17 02:10:38 +0200 | [diff] [blame] | 240 | TEST_CASE("a test case that registers an exception translator for int and then throws one") { |
| 241 | // set an exception translator for int - note that this shouldn't be done in a test case but |
onqtam | 7cc0e96 | 2017-04-17 23:30:36 +0300 | [diff] [blame] | 242 | // in main() or somewhere before executing the tests - but here I'm just lazy... |
onqtam | 246e817 | 2017-03-17 02:48:12 +0200 | [diff] [blame] | 243 | doctest::registerExceptionTranslator(intTranslator); |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 244 | |
onqtam | 7cc0e96 | 2017-04-17 23:30:36 +0300 | [diff] [blame] | 245 | throw_if(true, 5); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 246 | } |