blob: 492e1ec96e0e0475b7e46530966755c6ad76a392 [file] [log] [blame]
ncihnegnc5458f22019-01-28 05:08:18 -08001#include <doctest/doctest.h>
onqtam4a655632016-05-26 14:20:52 +03002
onqtam7cc0e962017-04-17 23:30:36 +03003#include "header.h"
4
onqtamabf39d22017-10-28 21:30:45 +03005DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN
onqtam4a655632016-05-26 14:20:52 +03006#include <string>
7#include <vector>
8#include <list>
onqtam4a655632016-05-26 14:20:52 +03009#include <sstream>
onqtamabf39d22017-10-28 21:30:45 +030010DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END
onqtam4a655632016-05-26 14:20:52 +030011
onqtam190ba252019-03-23 09:58:34 +020012DOCTEST_MSVC_SUPPRESS_WARNING(5045) // Spectre mitigation diagnostics
13
onqtam4a655632016-05-26 14:20:52 +030014// the standard forbids writing in the std namespace but it works on all compilers
15namespace std
16{
17template <typename T>
18ostream& operator<<(ostream& stream, const vector<T>& in) {
19 stream << "[";
20 for(size_t i = 0; i < in.size(); ++i)
21 if(i < in.size() - 1)
22 stream << in[i] << ", ";
23 else
24 stream << in[i];
25 stream << "]";
26 return stream;
27}
28}
29
30// as an alternative you may write a specialization of doctest::StringMaker
31namespace doctest
32{
33template <typename T>
34struct StringMaker<std::list<T> >
35{
36 static String convert(const std::list<T>& in) {
37 std::ostringstream oss;
38
39 oss << "[";
40 for(typename std::list<T>::const_iterator it = in.begin(); it != in.end(); ++it)
41 oss << *it << ", ";
42 oss << "]";
43
44 return oss.str().c_str();
45 }
46};
47}
48
onqtam4a655632016-05-26 14:20:52 +030049template <typename T, typename K>
50struct MyType
51{
52 T one;
53 K two;
54};
55
56template <typename T>
onqtam02b7eb72017-10-28 23:59:49 +030057struct MyTypeInherited : MyType<T, unsigned>
onqtam4a655632016-05-26 14:20:52 +030058{};
59
60template <typename T, typename K>
61bool operator==(const MyType<T, K>& lhs, const MyType<T, K>& rhs) {
62 return lhs.one == rhs.one && lhs.two == rhs.two;
63}
64
65template <typename T, typename K>
66std::ostream& operator<<(std::ostream& stream, const MyType<T, K>& in) {
67 stream << "[" << in.one << ", " << in.two << "]";
68 return stream;
69}
70
onqtam12d55982017-04-16 22:35:27 +030071namespace Bar
72{
onqtam4a655632016-05-26 14:20:52 +030073struct Foo
onqtam12d55982017-04-16 22:35:27 +030074{
75 friend bool operator==(const Foo&, const Foo&) { return false; }
76};
onqtam4a655632016-05-26 14:20:52 +030077
onqtamb18680d2016-11-15 14:08:56 +020078// as a third option you may provide an overload of toString()
onqtam7cc0e962017-04-17 23:30:36 +030079inline doctest::String toString(const Foo&) { return "Foo{}"; }
Viktor Kirilov17d984c2020-12-04 15:13:33 +020080
Viktor Kirilov81fe2e82020-12-04 18:59:26 +020081struct MyOtherType
Viktor Kirilov17d984c2020-12-04 15:13:33 +020082{
Viktor Kirilov81fe2e82020-12-04 18:59:26 +020083 int data;
84 friend bool operator==(const MyOtherType& l, const MyOtherType& r) { return l.data == r.data; }
Viktor Kirilov17d984c2020-12-04 15:13:33 +020085};
86
Viktor Kirilov81fe2e82020-12-04 18:59:26 +020087// you also can use a template operator<< if your code does not use std::ostream
Viktor Kirilov17d984c2020-12-04 15:13:33 +020088template <class OStream>
Viktor Kirilov81fe2e82020-12-04 18:59:26 +020089OStream& operator<<(OStream& stream, const MyOtherType& in) {
90 stream << "MyOtherType: " << in.data;
Viktor Kirilov17d984c2020-12-04 15:13:33 +020091 return stream;
92}
93
onqtamb18680d2016-11-15 14:08:56 +020094} // namespace Bar
onqtam4a655632016-05-26 14:20:52 +030095
onqtam05bcc372017-03-17 02:10:38 +020096// set an exception translator for MyTypeInherited<int>
97REGISTER_EXCEPTION_TRANSLATOR(MyTypeInherited<int>& ex) {
onqtam12d55982017-04-16 22:35:27 +030098 return doctest::String("MyTypeInherited<int>(") + doctest::toString(ex.one) + ", " +
99 doctest::toString(ex.two) + ")";
onqtam05bcc372017-03-17 02:10:38 +0200100}
101
onqtam7cc0e962017-04-17 23:30:36 +0300102TEST_CASE("all asserts should fail and show how the objects get stringified") {
onqtam4a655632016-05-26 14:20:52 +0300103 MyTypeInherited<int> bla1;
104 bla1.one = 5;
onqtam02b7eb72017-10-28 23:59:49 +0300105 bla1.two = 4u;
onqtam4a655632016-05-26 14:20:52 +0300106
onqtamb18680d2016-11-15 14:08:56 +0200107 Bar::Foo f1;
108 Bar::Foo f2;
onqtam4a655632016-05-26 14:20:52 +0300109 CHECK(f1 == f2);
110
111 // std::string already has an operator<< working with std::ostream
112 std::string dummy1 = "omg";
113 std::string dummy2 = "tralala";
114
115 CHECK(dummy1 == dummy2);
116
117 std::vector<int> vec1;
118 vec1.push_back(1);
119 vec1.push_back(2);
120 vec1.push_back(3);
121
122 std::vector<int> vec2;
123 vec2.push_back(1);
124 vec2.push_back(2);
125 vec2.push_back(4);
126
127 CHECK(vec1 == vec2);
128
129 std::list<int> lst_1;
130 lst_1.push_back(1);
131 lst_1.push_back(42);
132 lst_1.push_back(3);
133
134 std::list<int> lst_2;
135 lst_2.push_back(1);
136 lst_2.push_back(2);
137 lst_2.push_back(666);
138
139 CHECK(lst_1 == lst_2);
onqtam12d55982017-04-16 22:35:27 +0300140
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200141 {
Viktor Kirilov81fe2e82020-12-04 18:59:26 +0200142 Bar::MyOtherType s1 {42};
143 Bar::MyOtherType s2 {666};
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200144 INFO("s1=", s1, " s2=", s2);
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200145 CHECK(s1 == s2);
146 CHECK_MESSAGE(s1 == s2, s1, " is not really ", s2);
147 }
148
onqtam05bcc372017-03-17 02:10:38 +0200149 // lets see if this exception gets translated
onqtam7cc0e962017-04-17 23:30:36 +0300150 throw_if(true, bla1);
onqtam05bcc372017-03-17 02:10:38 +0200151}
152
onqtam12d55982017-04-16 22:35:27 +0300153static doctest::String intTranslator(int ex) {
154 return doctest::String("int: ") + doctest::toString(ex);
155}
onqtam246e8172017-03-17 02:48:12 +0200156
onqtam05bcc372017-03-17 02:10:38 +0200157TEST_CASE("a test case that registers an exception translator for int and then throws one") {
158 // set an exception translator for int - note that this shouldn't be done in a test case but
onqtam7cc0e962017-04-17 23:30:36 +0300159 // in main() or somewhere before executing the tests - but here I'm just lazy...
onqtam246e8172017-03-17 02:48:12 +0200160 doctest::registerExceptionTranslator(intTranslator);
onqtam12d55982017-04-16 22:35:27 +0300161
onqtam7cc0e962017-04-17 23:30:36 +0300162 throw_if(true, 5);
onqtam4a655632016-05-26 14:20:52 +0300163}