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; |
Salvage | 7c2737c | 2022-05-29 16:10:28 +0200 | [diff] [blame] | 7 | typedef basic_ostream<char, char_traits<char>> ostream; // NOLINT(modernize-use-using) |
Stefan | e8ba771 | 2022-04-28 02:47:24 +0200 | [diff] [blame] | 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") { |
Salvage | 1c94137 | 2022-05-28 13:40:20 +0200 | [diff] [blame] | 47 | char chs[] = { '1', 'a', 's' }; // NOLINT(*-avoid-c-arrays) |
Stefan | e8ba771 | 2022-04-28 02:47:24 +0200 | [diff] [blame] | 48 | MESSAGE(chs); CHECK(chs == nullptr); |
| 49 | MESSAGE("1as"); CHECK("1as" == nullptr); |
| 50 | |
Salvage | 1c94137 | 2022-05-28 13:40:20 +0200 | [diff] [blame] | 51 | int ints[] = { 0, 1, 1, 2, 3, 5, 8, 13 }; // NOLINT(*-avoid-c-arrays) |
Stefan | e8ba771 | 2022-04-28 02:47:24 +0200 | [diff] [blame] | 52 | MESSAGE(ints); CHECK(ints == nullptr); |
Salvage | 1c94137 | 2022-05-28 13:40:20 +0200 | [diff] [blame] | 53 | MESSAGE(MOVE(ints)); // NOLINT(*-move-const-arg) |
Stefan | e8ba771 | 2022-04-28 02:47:24 +0200 | [diff] [blame] | 54 | |
Salvage | 1c94137 | 2022-05-28 13:40:20 +0200 | [diff] [blame] | 55 | char* cptr = reinterpret_cast<char*>(ints + 4); // NOLINT |
| 56 | const char* ccptr = cptr; |
Stefan | e8ba771 | 2022-04-28 02:47:24 +0200 | [diff] [blame] | 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 |
Salvage | 1c94137 | 2022-05-28 13:40:20 +0200 | [diff] [blame] | 83 | namespace std // NOLINT(cert-dcl58-cpp) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 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 << "["; |
Salvage | 1c94137 | 2022-05-28 13:40:20 +0200 | [diff] [blame] | 107 | // NOLINTNEXTLINE(*-use-auto) |
Stefan | e8ba771 | 2022-04-28 02:47:24 +0200 | [diff] [blame] | 108 | for (typename std::list<T>::const_iterator it = in.begin(); it != in.end();) { |
| 109 | oss << *it; |
| 110 | if (++it != in.end()) { oss << ", "; } |
| 111 | } |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 112 | oss << "]"; |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 113 | return oss.str().c_str(); |
| 114 | } |
| 115 | }; |
| 116 | } |
| 117 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 118 | template <typename T, typename K> |
| 119 | struct MyType |
| 120 | { |
| 121 | T one; |
| 122 | K two; |
| 123 | }; |
| 124 | |
| 125 | template <typename T> |
onqtam | 02b7eb7 | 2017-10-28 23:59:49 +0300 | [diff] [blame] | 126 | struct MyTypeInherited : MyType<T, unsigned> |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 127 | {}; |
| 128 | |
| 129 | template <typename T, typename K> |
| 130 | bool operator==(const MyType<T, K>& lhs, const MyType<T, K>& rhs) { |
| 131 | return lhs.one == rhs.one && lhs.two == rhs.two; |
| 132 | } |
| 133 | |
| 134 | template <typename T, typename K> |
| 135 | std::ostream& operator<<(std::ostream& stream, const MyType<T, K>& in) { |
| 136 | stream << "[" << in.one << ", " << in.two << "]"; |
| 137 | return stream; |
| 138 | } |
| 139 | |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 140 | namespace Bar |
| 141 | { |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 142 | struct Foo |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 143 | { |
| 144 | friend bool operator==(const Foo&, const Foo&) { return false; } |
| 145 | }; |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 146 | |
onqtam | b18680d | 2016-11-15 14:08:56 +0200 | [diff] [blame] | 147 | // as a third option you may provide an overload of toString() |
onqtam | 7cc0e96 | 2017-04-17 23:30:36 +0300 | [diff] [blame] | 148 | inline doctest::String toString(const Foo&) { return "Foo{}"; } |
Viktor Kirilov | 17d984c | 2020-12-04 15:13:33 +0200 | [diff] [blame] | 149 | |
Viktor Kirilov | 81fe2e8 | 2020-12-04 18:59:26 +0200 | [diff] [blame] | 150 | struct MyOtherType |
Viktor Kirilov | 17d984c | 2020-12-04 15:13:33 +0200 | [diff] [blame] | 151 | { |
Viktor Kirilov | 81fe2e8 | 2020-12-04 18:59:26 +0200 | [diff] [blame] | 152 | int data; |
| 153 | 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] | 154 | }; |
| 155 | |
Viktor Kirilov | 81fe2e8 | 2020-12-04 18:59:26 +0200 | [diff] [blame] | 156 | // 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] | 157 | template <class OStream> |
Viktor Kirilov | 81fe2e8 | 2020-12-04 18:59:26 +0200 | [diff] [blame] | 158 | OStream& operator<<(OStream& stream, const MyOtherType& in) { |
| 159 | stream << "MyOtherType: " << in.data; |
Viktor Kirilov | 17d984c | 2020-12-04 15:13:33 +0200 | [diff] [blame] | 160 | return stream; |
| 161 | } |
| 162 | |
onqtam | b18680d | 2016-11-15 14:08:56 +0200 | [diff] [blame] | 163 | } // namespace Bar |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 164 | |
onqtam | 05bcc37 | 2017-03-17 02:10:38 +0200 | [diff] [blame] | 165 | // set an exception translator for MyTypeInherited<int> |
| 166 | REGISTER_EXCEPTION_TRANSLATOR(MyTypeInherited<int>& ex) { |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 167 | return doctest::String("MyTypeInherited<int>(") + doctest::toString(ex.one) + ", " + |
| 168 | doctest::toString(ex.two) + ")"; |
onqtam | 05bcc37 | 2017-03-17 02:10:38 +0200 | [diff] [blame] | 169 | } |
| 170 | |
Stefan | e8ba771 | 2022-04-28 02:47:24 +0200 | [diff] [blame] | 171 | #define CHECK_NOT_DEFAULT_STR(var) CHECK(toString(var) != "{?}") |
| 172 | |
onqtam | 7cc0e96 | 2017-04-17 23:30:36 +0300 | [diff] [blame] | 173 | TEST_CASE("all asserts should fail and show how the objects get stringified") { |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 174 | MyTypeInherited<int> bla1; |
| 175 | bla1.one = 5; |
onqtam | 02b7eb7 | 2017-10-28 23:59:49 +0300 | [diff] [blame] | 176 | bla1.two = 4u; |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 177 | |
onqtam | b18680d | 2016-11-15 14:08:56 +0200 | [diff] [blame] | 178 | Bar::Foo f1; |
Stefan | e8ba771 | 2022-04-28 02:47:24 +0200 | [diff] [blame] | 179 | MESSAGE(f1); |
onqtam | b18680d | 2016-11-15 14:08:56 +0200 | [diff] [blame] | 180 | Bar::Foo f2; |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 181 | CHECK(f1 == f2); |
| 182 | |
Stefan | 499a0fd | 2022-05-01 03:38:08 +0200 | [diff] [blame] | 183 | doctest::String str; |
| 184 | CHECK(str == doctest::toString(str)); |
| 185 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 186 | // std::string already has an operator<< working with std::ostream |
onqtam | 367098b | 2021-12-15 15:15:06 +0200 | [diff] [blame] | 187 | std::string dummy = "omg"; |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 188 | |
Stefan | e8ba771 | 2022-04-28 02:47:24 +0200 | [diff] [blame] | 189 | MESSAGE(dummy); |
| 190 | |
onqtam | 367098b | 2021-12-15 15:15:06 +0200 | [diff] [blame] | 191 | CHECK(dummy == "tralala"); // should fail |
| 192 | CHECK("tralala" == dummy); // should fail |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 193 | |
| 194 | std::vector<int> vec1; |
| 195 | vec1.push_back(1); |
| 196 | vec1.push_back(2); |
| 197 | vec1.push_back(3); |
| 198 | |
Stefan | e8ba771 | 2022-04-28 02:47:24 +0200 | [diff] [blame] | 199 | MESSAGE(vec1); |
| 200 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 201 | std::vector<int> vec2; |
| 202 | vec2.push_back(1); |
| 203 | vec2.push_back(2); |
| 204 | vec2.push_back(4); |
| 205 | |
| 206 | CHECK(vec1 == vec2); |
| 207 | |
| 208 | std::list<int> lst_1; |
| 209 | lst_1.push_back(1); |
| 210 | lst_1.push_back(42); |
| 211 | lst_1.push_back(3); |
| 212 | |
Stefan | e8ba771 | 2022-04-28 02:47:24 +0200 | [diff] [blame] | 213 | MESSAGE(lst_1); |
| 214 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 215 | std::list<int> lst_2; |
| 216 | lst_2.push_back(1); |
| 217 | lst_2.push_back(2); |
| 218 | lst_2.push_back(666); |
| 219 | |
| 220 | CHECK(lst_1 == lst_2); |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 221 | |
Viktor Kirilov | 17d984c | 2020-12-04 15:13:33 +0200 | [diff] [blame] | 222 | { |
Viktor Kirilov | 81fe2e8 | 2020-12-04 18:59:26 +0200 | [diff] [blame] | 223 | Bar::MyOtherType s1 {42}; |
| 224 | Bar::MyOtherType s2 {666}; |
Viktor Kirilov | 17d984c | 2020-12-04 15:13:33 +0200 | [diff] [blame] | 225 | INFO("s1=", s1, " s2=", s2); |
Viktor Kirilov | 17d984c | 2020-12-04 15:13:33 +0200 | [diff] [blame] | 226 | CHECK(s1 == s2); |
| 227 | CHECK_MESSAGE(s1 == s2, s1, " is not really ", s2); |
| 228 | } |
| 229 | |
Stefan | e8ba771 | 2022-04-28 02:47:24 +0200 | [diff] [blame] | 230 | CHECK_NOT_DEFAULT_STR(doctest::IsNaN<double>(0.5)); |
| 231 | CHECK_NOT_DEFAULT_STR(!doctest::IsNaN<float>(std::numeric_limits<float>::infinity())); |
| 232 | 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] | 233 | |
Menno Fraters | 8de4cf7 | 2022-03-05 23:14:35 +0000 | [diff] [blame] | 234 | CHECK("a" == doctest::Contains("aaa")); |
| 235 | |
onqtam | 05bcc37 | 2017-03-17 02:10:38 +0200 | [diff] [blame] | 236 | // lets see if this exception gets translated |
onqtam | 7cc0e96 | 2017-04-17 23:30:36 +0300 | [diff] [blame] | 237 | throw_if(true, bla1); |
onqtam | 05bcc37 | 2017-03-17 02:10:38 +0200 | [diff] [blame] | 238 | } |
| 239 | |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 240 | static doctest::String intTranslator(int ex) { |
| 241 | return doctest::String("int: ") + doctest::toString(ex); |
| 242 | } |
onqtam | 246e817 | 2017-03-17 02:48:12 +0200 | [diff] [blame] | 243 | |
onqtam | 05bcc37 | 2017-03-17 02:10:38 +0200 | [diff] [blame] | 244 | TEST_CASE("a test case that registers an exception translator for int and then throws one") { |
| 245 | // 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] | 246 | // 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] | 247 | doctest::registerExceptionTranslator(intTranslator); |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 248 | |
onqtam | 7cc0e96 | 2017-04-17 23:30:36 +0300 | [diff] [blame] | 249 | throw_if(true, 5); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 250 | } |