onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 1 | #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 |
| 11 | namespace std |
| 12 | { |
| 13 | template <typename T> |
| 14 | ostream& 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 |
| 27 | namespace doctest |
| 28 | { |
| 29 | template <typename T> |
| 30 | struct 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 | |
| 50 | template <typename T, typename K> |
| 51 | struct MyType |
| 52 | { |
| 53 | T one; |
| 54 | K two; |
| 55 | }; |
| 56 | |
| 57 | template <typename T> |
| 58 | struct MyTypeInherited : MyType<T, float> |
| 59 | {}; |
| 60 | |
| 61 | template <typename T, typename K> |
| 62 | bool operator==(const MyType<T, K>& lhs, const MyType<T, K>& rhs) { |
| 63 | return lhs.one == rhs.one && lhs.two == rhs.two; |
| 64 | } |
| 65 | |
| 66 | template <typename T, typename K> |
| 67 | std::ostream& operator<<(std::ostream& stream, const MyType<T, K>& in) { |
| 68 | stream << "[" << in.one << ", " << in.two << "]"; |
| 69 | return stream; |
| 70 | } |
| 71 | |
onqtam | b18680d | 2016-11-15 14:08:56 +0200 | [diff] [blame] | 72 | namespace Bar { |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 73 | struct Foo |
| 74 | {}; |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 75 | static bool operator==(const Foo&, const Foo&) { return false; } |
| 76 | |
onqtam | b18680d | 2016-11-15 14:08:56 +0200 | [diff] [blame] | 77 | doctest::String toString(const Foo&); // to silence -Wmissing-declarations |
| 78 | // as a third option you may provide an overload of toString() |
| 79 | doctest::String toString(const Foo&) { |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 80 | return "Foo{}"; |
| 81 | } |
onqtam | b18680d | 2016-11-15 14:08:56 +0200 | [diff] [blame] | 82 | } // namespace Bar |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 83 | |
onqtam | 05bcc37 | 2017-03-17 02:10:38 +0200 | [diff] [blame^] | 84 | // set an exception translator for MyTypeInherited<int> |
| 85 | REGISTER_EXCEPTION_TRANSLATOR(MyTypeInherited<int>& ex) { |
| 86 | return doctest::String("MyTypeInherited<int>(") + doctest::toString(ex.one) + ", " + doctest::toString(ex.two) + ")"; |
| 87 | } |
| 88 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 89 | TEST_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 | |
onqtam | b18680d | 2016-11-15 14:08:56 +0200 | [diff] [blame] | 97 | Bar::Foo f1; |
| 98 | Bar::Foo f2; |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 99 | 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); |
onqtam | 05bcc37 | 2017-03-17 02:10:38 +0200 | [diff] [blame^] | 130 | |
| 131 | // lets see if this exception gets translated |
| 132 | throw bla1; |
| 133 | } |
| 134 | |
| 135 | TEST_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; |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 141 | } |