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 | |
onqtam | 036fa31 | 2017-03-17 11:44:30 +0200 | [diff] [blame] | 50 | template<typename T> |
onqtam | 4b68df3 | 2017-03-17 20:45:54 +0200 | [diff] [blame] | 51 | static int conditional_throw(bool in, const T& ex) { |
| 52 | if(in) |
| 53 | #ifndef DOCTEST_CONFIG_NO_EXCEPTIONS |
| 54 | throw ex; |
| 55 | #else // DOCTEST_CONFIG_NO_EXCEPTIONS |
| 56 | ((void)ex); |
| 57 | #endif // DOCTEST_CONFIG_NO_EXCEPTIONS |
| 58 | return 42; |
| 59 | } |
onqtam | 8327c6c | 2017-03-17 11:18:45 +0200 | [diff] [blame] | 60 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 61 | template <typename T, typename K> |
| 62 | struct MyType |
| 63 | { |
| 64 | T one; |
| 65 | K two; |
| 66 | }; |
| 67 | |
| 68 | template <typename T> |
| 69 | struct MyTypeInherited : MyType<T, float> |
| 70 | {}; |
| 71 | |
| 72 | template <typename T, typename K> |
| 73 | bool operator==(const MyType<T, K>& lhs, const MyType<T, K>& rhs) { |
| 74 | return lhs.one == rhs.one && lhs.two == rhs.two; |
| 75 | } |
| 76 | |
| 77 | template <typename T, typename K> |
| 78 | std::ostream& operator<<(std::ostream& stream, const MyType<T, K>& in) { |
| 79 | stream << "[" << in.one << ", " << in.two << "]"; |
| 80 | return stream; |
| 81 | } |
| 82 | |
onqtam | b18680d | 2016-11-15 14:08:56 +0200 | [diff] [blame] | 83 | namespace Bar { |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 84 | struct Foo |
| 85 | {}; |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 86 | static bool operator==(const Foo&, const Foo&) { return false; } |
| 87 | |
onqtam | b18680d | 2016-11-15 14:08:56 +0200 | [diff] [blame] | 88 | doctest::String toString(const Foo&); // to silence -Wmissing-declarations |
| 89 | // as a third option you may provide an overload of toString() |
| 90 | doctest::String toString(const Foo&) { |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 91 | return "Foo{}"; |
| 92 | } |
onqtam | b18680d | 2016-11-15 14:08:56 +0200 | [diff] [blame] | 93 | } // namespace Bar |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 94 | |
onqtam | 05bcc37 | 2017-03-17 02:10:38 +0200 | [diff] [blame] | 95 | // set an exception translator for MyTypeInherited<int> |
| 96 | REGISTER_EXCEPTION_TRANSLATOR(MyTypeInherited<int>& ex) { |
| 97 | return doctest::String("MyTypeInherited<int>(") + doctest::toString(ex.one) + ", " + doctest::toString(ex.two) + ")"; |
| 98 | } |
| 99 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 100 | TEST_CASE("the only test") { |
| 101 | MyTypeInherited<int> bla1; |
| 102 | bla1.one = 5; |
| 103 | bla1.two = 4.0f; |
| 104 | MyTypeInherited<int> bla2; |
| 105 | bla2.one = 5; |
| 106 | bla2.two = 6.0f; |
| 107 | |
onqtam | b18680d | 2016-11-15 14:08:56 +0200 | [diff] [blame] | 108 | Bar::Foo f1; |
| 109 | Bar::Foo f2; |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 110 | CHECK(f1 == f2); |
| 111 | |
| 112 | // std::string already has an operator<< working with std::ostream |
| 113 | std::string dummy1 = "omg"; |
| 114 | std::string dummy2 = "tralala"; |
| 115 | |
| 116 | CHECK(dummy1 == dummy2); |
| 117 | |
| 118 | std::vector<int> vec1; |
| 119 | vec1.push_back(1); |
| 120 | vec1.push_back(2); |
| 121 | vec1.push_back(3); |
| 122 | |
| 123 | std::vector<int> vec2; |
| 124 | vec2.push_back(1); |
| 125 | vec2.push_back(2); |
| 126 | vec2.push_back(4); |
| 127 | |
| 128 | CHECK(vec1 == vec2); |
| 129 | |
| 130 | std::list<int> lst_1; |
| 131 | lst_1.push_back(1); |
| 132 | lst_1.push_back(42); |
| 133 | lst_1.push_back(3); |
| 134 | |
| 135 | std::list<int> lst_2; |
| 136 | lst_2.push_back(1); |
| 137 | lst_2.push_back(2); |
| 138 | lst_2.push_back(666); |
| 139 | |
| 140 | CHECK(lst_1 == lst_2); |
onqtam | 05bcc37 | 2017-03-17 02:10:38 +0200 | [diff] [blame] | 141 | |
| 142 | // lets see if this exception gets translated |
onqtam | 8327c6c | 2017-03-17 11:18:45 +0200 | [diff] [blame] | 143 | conditional_throw(true, bla1); |
onqtam | 05bcc37 | 2017-03-17 02:10:38 +0200 | [diff] [blame] | 144 | } |
| 145 | |
onqtam | 246e817 | 2017-03-17 02:48:12 +0200 | [diff] [blame] | 146 | static doctest::String intTranslator(int ex) { return doctest::String("int: ") + doctest::toString(ex); } |
| 147 | |
onqtam | 05bcc37 | 2017-03-17 02:10:38 +0200 | [diff] [blame] | 148 | TEST_CASE("a test case that registers an exception translator for int and then throws one") { |
| 149 | // set an exception translator for int - note that this shouldn't be done in a test case but |
| 150 | // in main() or somewhere before executing the tests - but here I'm lazy to write my own main... |
onqtam | 246e817 | 2017-03-17 02:48:12 +0200 | [diff] [blame] | 151 | doctest::registerExceptionTranslator(intTranslator); |
onqtam | 05bcc37 | 2017-03-17 02:10:38 +0200 | [diff] [blame] | 152 | |
onqtam | 036fa31 | 2017-03-17 11:44:30 +0200 | [diff] [blame] | 153 | conditional_throw(true, 5); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 154 | } |