blob: 46798d3a71013176543905db9ffa4f8ad4fca19e [file] [log] [blame]
onqtam4a655632016-05-26 14:20:52 +03001#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
2#include "doctest.h"
3
4#include <string>
5#include <vector>
6#include <list>
7
8#include <sstream>
9
10// the standard forbids writing in the std namespace but it works on all compilers
11namespace std
12{
13template <typename T>
14ostream& operator<<(ostream& stream, const vector<T>& in) {
15 stream << "[";
16 for(size_t i = 0; i < in.size(); ++i)
17 if(i < in.size() - 1)
18 stream << in[i] << ", ";
19 else
20 stream << in[i];
21 stream << "]";
22 return stream;
23}
24}
25
26// as an alternative you may write a specialization of doctest::StringMaker
27namespace doctest
28{
29template <typename T>
30struct StringMaker<std::list<T> >
31{
32 static String convert(const std::list<T>& in) {
33 std::ostringstream oss;
34
35 oss << "[";
36 for(typename std::list<T>::const_iterator it = in.begin(); it != in.end(); ++it)
37 oss << *it << ", ";
38 oss << "]";
39
40 return oss.str().c_str();
41 }
42};
43}
44
45// to silence GCC warnings when inheriting from the class MyType which has no virtual destructor
46#if defined(__GNUC__) && !defined(__clang__)
47#pragma GCC diagnostic ignored "-Weffc++"
48#endif // __GNUC__
49
50template <typename T, typename K>
51struct MyType
52{
53 T one;
54 K two;
55};
56
57template <typename T>
58struct MyTypeInherited : MyType<T, float>
59{};
60
61template <typename T, typename K>
62bool operator==(const MyType<T, K>& lhs, const MyType<T, K>& rhs) {
63 return lhs.one == rhs.one && lhs.two == rhs.two;
64}
65
66template <typename T, typename K>
67std::ostream& operator<<(std::ostream& stream, const MyType<T, K>& in) {
68 stream << "[" << in.one << ", " << in.two << "]";
69 return stream;
70}
71
onqtamb18680d2016-11-15 14:08:56 +020072namespace Bar {
onqtam4a655632016-05-26 14:20:52 +030073struct Foo
74{};
onqtam4a655632016-05-26 14:20:52 +030075static bool operator==(const Foo&, const Foo&) { return false; }
76
onqtamb18680d2016-11-15 14:08:56 +020077doctest::String toString(const Foo&); // to silence -Wmissing-declarations
78// as a third option you may provide an overload of toString()
79doctest::String toString(const Foo&) {
onqtam4a655632016-05-26 14:20:52 +030080 return "Foo{}";
81}
onqtamb18680d2016-11-15 14:08:56 +020082} // namespace Bar
onqtam4a655632016-05-26 14:20:52 +030083
onqtam05bcc372017-03-17 02:10:38 +020084// set an exception translator for MyTypeInherited<int>
85REGISTER_EXCEPTION_TRANSLATOR(MyTypeInherited<int>& ex) {
86 return doctest::String("MyTypeInherited<int>(") + doctest::toString(ex.one) + ", " + doctest::toString(ex.two) + ")";
87}
88
onqtam4a655632016-05-26 14:20:52 +030089TEST_CASE("the only test") {
90 MyTypeInherited<int> bla1;
91 bla1.one = 5;
92 bla1.two = 4.0f;
93 MyTypeInherited<int> bla2;
94 bla2.one = 5;
95 bla2.two = 6.0f;
96
onqtamb18680d2016-11-15 14:08:56 +020097 Bar::Foo f1;
98 Bar::Foo f2;
onqtam4a655632016-05-26 14:20:52 +030099 CHECK(f1 == f2);
100
101 // std::string already has an operator<< working with std::ostream
102 std::string dummy1 = "omg";
103 std::string dummy2 = "tralala";
104
105 CHECK(dummy1 == dummy2);
106
107 std::vector<int> vec1;
108 vec1.push_back(1);
109 vec1.push_back(2);
110 vec1.push_back(3);
111
112 std::vector<int> vec2;
113 vec2.push_back(1);
114 vec2.push_back(2);
115 vec2.push_back(4);
116
117 CHECK(vec1 == vec2);
118
119 std::list<int> lst_1;
120 lst_1.push_back(1);
121 lst_1.push_back(42);
122 lst_1.push_back(3);
123
124 std::list<int> lst_2;
125 lst_2.push_back(1);
126 lst_2.push_back(2);
127 lst_2.push_back(666);
128
129 CHECK(lst_1 == lst_2);
onqtam05bcc372017-03-17 02:10:38 +0200130
131 // lets see if this exception gets translated
132 throw bla1;
133}
134
135TEST_CASE("a test case that registers an exception translator for int and then throws one") {
136 // set an exception translator for int - note that this shouldn't be done in a test case but
137 // in main() or somewhere before executing the tests - but here I'm lazy to write my own main...
138 doctest::registerExceptionTranslator<int>([](int in){ return doctest::String("int: ") + doctest::toString(in); });
139
140 throw 42;
onqtam4a655632016-05-26 14:20:52 +0300141}