blob: a9518a3d270fa6a48aad7b08ec10ffe844bae066 [file] [log] [blame]
Stefane8ba7712022-04-28 02:47:24 +02001#ifdef _MSC_VER
2__pragma(warning(push))
3__pragma(warning(disable : 4643))
4namespace std {
5 template <typename> struct char_traits;
6 template <typename, typename> class basic_ostream;
Salvage7c2737c2022-05-29 16:10:28 +02007 typedef basic_ostream<char, char_traits<char>> ostream; // NOLINT(modernize-use-using)
Stefane8ba7712022-04-28 02:47:24 +02008 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
16namespace 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
25static std::ostream& operator<<(std::ostream& os, const N::A&) { return os << "A"; }
26
ncihnegnc5458f22019-01-28 05:08:18 -080027#include <doctest/doctest.h>
onqtam4a655632016-05-26 14:20:52 +030028
Stefane8ba7712022-04-28 02:47:24 +020029#include <utility>
30
31TEST_CASE("operator<<") {
32 MESSAGE(N::A{ });
33 MESSAGE(N::B{ });
34 MESSAGE(N::C{ });
35}
36
onqtam7cc0e962017-04-17 23:30:36 +030037#include "header.h"
38
Stefane8ba7712022-04-28 02:47:24 +020039// 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
46TEST_CASE("no headers") {
Salvage1c941372022-05-28 13:40:20 +020047 char chs[] = { '1', 'a', 's' }; // NOLINT(*-avoid-c-arrays)
Stefane8ba7712022-04-28 02:47:24 +020048 MESSAGE(chs); CHECK(chs == nullptr);
49 MESSAGE("1as"); CHECK("1as" == nullptr);
50
Salvage1c941372022-05-28 13:40:20 +020051 int ints[] = { 0, 1, 1, 2, 3, 5, 8, 13 }; // NOLINT(*-avoid-c-arrays)
Stefane8ba7712022-04-28 02:47:24 +020052 MESSAGE(ints); CHECK(ints == nullptr);
Salvage1c941372022-05-28 13:40:20 +020053 MESSAGE(MOVE(ints)); // NOLINT(*-move-const-arg)
Stefane8ba7712022-04-28 02:47:24 +020054
Salvage1c941372022-05-28 13:40:20 +020055 char* cptr = reinterpret_cast<char*>(ints + 4); // NOLINT
56 const char* ccptr = cptr;
Stefane8ba7712022-04-28 02:47:24 +020057 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
onqtamabf39d22017-10-28 21:30:45 +030072DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN
onqtam4a655632016-05-26 14:20:52 +030073#include <string>
74#include <vector>
75#include <list>
onqtam4a655632016-05-26 14:20:52 +030076#include <sstream>
Stefan58f3ec82022-01-13 21:03:37 +010077#include <limits>
onqtamabf39d22017-10-28 21:30:45 +030078DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END
onqtam4a655632016-05-26 14:20:52 +030079
onqtam190ba252019-03-23 09:58:34 +020080DOCTEST_MSVC_SUPPRESS_WARNING(5045) // Spectre mitigation diagnostics
81
onqtam4a655632016-05-26 14:20:52 +030082// the standard forbids writing in the std namespace but it works on all compilers
Salvage1c941372022-05-28 13:40:20 +020083namespace std // NOLINT(cert-dcl58-cpp)
onqtam4a655632016-05-26 14:20:52 +030084{
85template <typename T>
86ostream& operator<<(ostream& stream, const vector<T>& in) {
87 stream << "[";
Stefane8ba7712022-04-28 02:47:24 +020088 for (size_t i = 0; i < in.size(); ++i) {
89 if (i != 0) { stream << ", "; }
90 stream << in[i];
91 }
onqtam4a655632016-05-26 14:20:52 +030092 stream << "]";
93 return stream;
94}
95}
96
97// as an alternative you may write a specialization of doctest::StringMaker
98namespace doctest
99{
100template <typename T>
Stefane8ba7712022-04-28 02:47:24 +0200101struct StringMaker<std::list<T>>
onqtam4a655632016-05-26 14:20:52 +0300102{
103 static String convert(const std::list<T>& in) {
104 std::ostringstream oss;
105
106 oss << "[";
Salvage1c941372022-05-28 13:40:20 +0200107 // NOLINTNEXTLINE(*-use-auto)
Stefane8ba7712022-04-28 02:47:24 +0200108 for (typename std::list<T>::const_iterator it = in.begin(); it != in.end();) {
109 oss << *it;
110 if (++it != in.end()) { oss << ", "; }
111 }
onqtam4a655632016-05-26 14:20:52 +0300112 oss << "]";
onqtam4a655632016-05-26 14:20:52 +0300113 return oss.str().c_str();
114 }
115};
116}
117
onqtam4a655632016-05-26 14:20:52 +0300118template <typename T, typename K>
119struct MyType
120{
121 T one;
122 K two;
123};
124
125template <typename T>
onqtam02b7eb72017-10-28 23:59:49 +0300126struct MyTypeInherited : MyType<T, unsigned>
onqtam4a655632016-05-26 14:20:52 +0300127{};
128
129template <typename T, typename K>
130bool operator==(const MyType<T, K>& lhs, const MyType<T, K>& rhs) {
131 return lhs.one == rhs.one && lhs.two == rhs.two;
132}
133
134template <typename T, typename K>
135std::ostream& operator<<(std::ostream& stream, const MyType<T, K>& in) {
136 stream << "[" << in.one << ", " << in.two << "]";
137 return stream;
138}
139
onqtam12d55982017-04-16 22:35:27 +0300140namespace Bar
141{
onqtam4a655632016-05-26 14:20:52 +0300142struct Foo
onqtam12d55982017-04-16 22:35:27 +0300143{
144 friend bool operator==(const Foo&, const Foo&) { return false; }
145};
onqtam4a655632016-05-26 14:20:52 +0300146
onqtamb18680d2016-11-15 14:08:56 +0200147// as a third option you may provide an overload of toString()
onqtam7cc0e962017-04-17 23:30:36 +0300148inline doctest::String toString(const Foo&) { return "Foo{}"; }
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200149
Viktor Kirilov81fe2e82020-12-04 18:59:26 +0200150struct MyOtherType
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200151{
Viktor Kirilov81fe2e82020-12-04 18:59:26 +0200152 int data;
153 friend bool operator==(const MyOtherType& l, const MyOtherType& r) { return l.data == r.data; }
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200154};
155
Viktor Kirilov81fe2e82020-12-04 18:59:26 +0200156// you also can use a template operator<< if your code does not use std::ostream
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200157template <class OStream>
Viktor Kirilov81fe2e82020-12-04 18:59:26 +0200158OStream& operator<<(OStream& stream, const MyOtherType& in) {
159 stream << "MyOtherType: " << in.data;
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200160 return stream;
161}
162
onqtamb18680d2016-11-15 14:08:56 +0200163} // namespace Bar
onqtam4a655632016-05-26 14:20:52 +0300164
onqtam05bcc372017-03-17 02:10:38 +0200165// set an exception translator for MyTypeInherited<int>
166REGISTER_EXCEPTION_TRANSLATOR(MyTypeInherited<int>& ex) {
onqtam12d55982017-04-16 22:35:27 +0300167 return doctest::String("MyTypeInherited<int>(") + doctest::toString(ex.one) + ", " +
168 doctest::toString(ex.two) + ")";
onqtam05bcc372017-03-17 02:10:38 +0200169}
170
Stefane8ba7712022-04-28 02:47:24 +0200171#define CHECK_NOT_DEFAULT_STR(var) CHECK(toString(var) != "{?}")
172
onqtam7cc0e962017-04-17 23:30:36 +0300173TEST_CASE("all asserts should fail and show how the objects get stringified") {
onqtam4a655632016-05-26 14:20:52 +0300174 MyTypeInherited<int> bla1;
175 bla1.one = 5;
onqtam02b7eb72017-10-28 23:59:49 +0300176 bla1.two = 4u;
onqtam4a655632016-05-26 14:20:52 +0300177
onqtamb18680d2016-11-15 14:08:56 +0200178 Bar::Foo f1;
Stefane8ba7712022-04-28 02:47:24 +0200179 MESSAGE(f1);
onqtamb18680d2016-11-15 14:08:56 +0200180 Bar::Foo f2;
onqtam4a655632016-05-26 14:20:52 +0300181 CHECK(f1 == f2);
182
Stefan499a0fd2022-05-01 03:38:08 +0200183 doctest::String str;
184 CHECK(str == doctest::toString(str));
185
onqtam4a655632016-05-26 14:20:52 +0300186 // std::string already has an operator<< working with std::ostream
onqtam367098b2021-12-15 15:15:06 +0200187 std::string dummy = "omg";
onqtam4a655632016-05-26 14:20:52 +0300188
Stefane8ba7712022-04-28 02:47:24 +0200189 MESSAGE(dummy);
190
onqtam367098b2021-12-15 15:15:06 +0200191 CHECK(dummy == "tralala"); // should fail
192 CHECK("tralala" == dummy); // should fail
onqtam4a655632016-05-26 14:20:52 +0300193
194 std::vector<int> vec1;
195 vec1.push_back(1);
196 vec1.push_back(2);
197 vec1.push_back(3);
198
Stefane8ba7712022-04-28 02:47:24 +0200199 MESSAGE(vec1);
200
onqtam4a655632016-05-26 14:20:52 +0300201 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
Stefane8ba7712022-04-28 02:47:24 +0200213 MESSAGE(lst_1);
214
onqtam4a655632016-05-26 14:20:52 +0300215 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);
onqtam12d55982017-04-16 22:35:27 +0300221
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200222 {
Viktor Kirilov81fe2e82020-12-04 18:59:26 +0200223 Bar::MyOtherType s1 {42};
224 Bar::MyOtherType s2 {666};
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200225 INFO("s1=", s1, " s2=", s2);
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200226 CHECK(s1 == s2);
227 CHECK_MESSAGE(s1 == s2, s1, " is not really ", s2);
228 }
229
Stefane8ba7712022-04-28 02:47:24 +0200230 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()));
Stefan58f3ec82022-01-13 21:03:37 +0100233
Menno Fraters8de4cf72022-03-05 23:14:35 +0000234 CHECK("a" == doctest::Contains("aaa"));
235
onqtam05bcc372017-03-17 02:10:38 +0200236 // lets see if this exception gets translated
onqtam7cc0e962017-04-17 23:30:36 +0300237 throw_if(true, bla1);
onqtam05bcc372017-03-17 02:10:38 +0200238}
239
onqtam12d55982017-04-16 22:35:27 +0300240static doctest::String intTranslator(int ex) {
241 return doctest::String("int: ") + doctest::toString(ex);
242}
onqtam246e8172017-03-17 02:48:12 +0200243
onqtam05bcc372017-03-17 02:10:38 +0200244TEST_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
onqtam7cc0e962017-04-17 23:30:36 +0300246 // in main() or somewhere before executing the tests - but here I'm just lazy...
onqtam246e8172017-03-17 02:48:12 +0200247 doctest::registerExceptionTranslator(intTranslator);
onqtam12d55982017-04-16 22:35:27 +0300248
onqtam7cc0e962017-04-17 23:30:36 +0300249 throw_if(true, 5);
onqtam4a655632016-05-26 14:20:52 +0300250}