blob: abce6bb7200e4f062605dfd0442b42a3e7137129 [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{}"; }
onqtamb18680d2016-11-15 14:08:56 +020080} // namespace Bar
onqtam4a655632016-05-26 14:20:52 +030081
onqtam05bcc372017-03-17 02:10:38 +020082// set an exception translator for MyTypeInherited<int>
83REGISTER_EXCEPTION_TRANSLATOR(MyTypeInherited<int>& ex) {
onqtam12d55982017-04-16 22:35:27 +030084 return doctest::String("MyTypeInherited<int>(") + doctest::toString(ex.one) + ", " +
85 doctest::toString(ex.two) + ")";
onqtam05bcc372017-03-17 02:10:38 +020086}
87
onqtam7cc0e962017-04-17 23:30:36 +030088TEST_CASE("all asserts should fail and show how the objects get stringified") {
onqtam4a655632016-05-26 14:20:52 +030089 MyTypeInherited<int> bla1;
90 bla1.one = 5;
onqtam02b7eb72017-10-28 23:59:49 +030091 bla1.two = 4u;
onqtam4a655632016-05-26 14:20:52 +030092 MyTypeInherited<int> bla2;
93 bla2.one = 5;
onqtam02b7eb72017-10-28 23:59:49 +030094 bla2.two = 6u;
onqtam4a655632016-05-26 14:20:52 +030095
onqtamb18680d2016-11-15 14:08:56 +020096 Bar::Foo f1;
97 Bar::Foo f2;
onqtam4a655632016-05-26 14:20:52 +030098 CHECK(f1 == f2);
99
100 // std::string already has an operator<< working with std::ostream
101 std::string dummy1 = "omg";
102 std::string dummy2 = "tralala";
103
104 CHECK(dummy1 == dummy2);
105
106 std::vector<int> vec1;
107 vec1.push_back(1);
108 vec1.push_back(2);
109 vec1.push_back(3);
110
111 std::vector<int> vec2;
112 vec2.push_back(1);
113 vec2.push_back(2);
114 vec2.push_back(4);
115
116 CHECK(vec1 == vec2);
117
118 std::list<int> lst_1;
119 lst_1.push_back(1);
120 lst_1.push_back(42);
121 lst_1.push_back(3);
122
123 std::list<int> lst_2;
124 lst_2.push_back(1);
125 lst_2.push_back(2);
126 lst_2.push_back(666);
127
128 CHECK(lst_1 == lst_2);
onqtam12d55982017-04-16 22:35:27 +0300129
onqtam05bcc372017-03-17 02:10:38 +0200130 // lets see if this exception gets translated
onqtam7cc0e962017-04-17 23:30:36 +0300131 throw_if(true, bla1);
onqtam05bcc372017-03-17 02:10:38 +0200132}
133
onqtam12d55982017-04-16 22:35:27 +0300134static doctest::String intTranslator(int ex) {
135 return doctest::String("int: ") + doctest::toString(ex);
136}
onqtam246e8172017-03-17 02:48:12 +0200137
onqtam05bcc372017-03-17 02:10:38 +0200138TEST_CASE("a test case that registers an exception translator for int and then throws one") {
139 // set an exception translator for int - note that this shouldn't be done in a test case but
onqtam7cc0e962017-04-17 23:30:36 +0300140 // in main() or somewhere before executing the tests - but here I'm just lazy...
onqtam246e8172017-03-17 02:48:12 +0200141 doctest::registerExceptionTranslator(intTranslator);
onqtam12d55982017-04-16 22:35:27 +0300142
onqtam7cc0e962017-04-17 23:30:36 +0300143 throw_if(true, 5);
onqtam4a655632016-05-26 14:20:52 +0300144}