ncihnegn | c5458f2 | 2019-01-28 05:08:18 -0800 | [diff] [blame] | 1 | #include <doctest/doctest.h> |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 2 | |
onqtam | 7cc0e96 | 2017-04-17 23:30:36 +0300 | [diff] [blame] | 3 | #include "header.h" |
| 4 | |
onqtam | abf39d2 | 2017-10-28 21:30:45 +0300 | [diff] [blame] | 5 | DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 6 | #include <string> |
| 7 | #include <vector> |
| 8 | #include <list> |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 9 | #include <sstream> |
onqtam | abf39d2 | 2017-10-28 21:30:45 +0300 | [diff] [blame] | 10 | DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 11 | |
| 12 | // the standard forbids writing in the std namespace but it works on all compilers |
| 13 | namespace std |
| 14 | { |
| 15 | template <typename T> |
| 16 | ostream& operator<<(ostream& stream, const vector<T>& in) { |
| 17 | stream << "["; |
| 18 | for(size_t i = 0; i < in.size(); ++i) |
| 19 | if(i < in.size() - 1) |
| 20 | stream << in[i] << ", "; |
| 21 | else |
| 22 | stream << in[i]; |
| 23 | stream << "]"; |
| 24 | return stream; |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | // as an alternative you may write a specialization of doctest::StringMaker |
| 29 | namespace doctest |
| 30 | { |
| 31 | template <typename T> |
| 32 | struct StringMaker<std::list<T> > |
| 33 | { |
| 34 | static String convert(const std::list<T>& in) { |
| 35 | std::ostringstream oss; |
| 36 | |
| 37 | oss << "["; |
| 38 | for(typename std::list<T>::const_iterator it = in.begin(); it != in.end(); ++it) |
| 39 | oss << *it << ", "; |
| 40 | oss << "]"; |
| 41 | |
| 42 | return oss.str().c_str(); |
| 43 | } |
| 44 | }; |
| 45 | } |
| 46 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 47 | template <typename T, typename K> |
| 48 | struct MyType |
| 49 | { |
| 50 | T one; |
| 51 | K two; |
| 52 | }; |
| 53 | |
| 54 | template <typename T> |
onqtam | 02b7eb7 | 2017-10-28 23:59:49 +0300 | [diff] [blame] | 55 | struct MyTypeInherited : MyType<T, unsigned> |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 56 | {}; |
| 57 | |
| 58 | template <typename T, typename K> |
| 59 | bool operator==(const MyType<T, K>& lhs, const MyType<T, K>& rhs) { |
| 60 | return lhs.one == rhs.one && lhs.two == rhs.two; |
| 61 | } |
| 62 | |
| 63 | template <typename T, typename K> |
| 64 | std::ostream& operator<<(std::ostream& stream, const MyType<T, K>& in) { |
| 65 | stream << "[" << in.one << ", " << in.two << "]"; |
| 66 | return stream; |
| 67 | } |
| 68 | |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 69 | namespace Bar |
| 70 | { |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 71 | struct Foo |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 72 | { |
| 73 | friend bool operator==(const Foo&, const Foo&) { return false; } |
| 74 | }; |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 75 | |
onqtam | b18680d | 2016-11-15 14:08:56 +0200 | [diff] [blame] | 76 | // as a third option you may provide an overload of toString() |
onqtam | 7cc0e96 | 2017-04-17 23:30:36 +0300 | [diff] [blame] | 77 | inline doctest::String toString(const Foo&) { return "Foo{}"; } |
onqtam | b18680d | 2016-11-15 14:08:56 +0200 | [diff] [blame] | 78 | } // namespace Bar |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 79 | |
onqtam | 05bcc37 | 2017-03-17 02:10:38 +0200 | [diff] [blame] | 80 | // set an exception translator for MyTypeInherited<int> |
| 81 | REGISTER_EXCEPTION_TRANSLATOR(MyTypeInherited<int>& ex) { |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 82 | return doctest::String("MyTypeInherited<int>(") + doctest::toString(ex.one) + ", " + |
| 83 | doctest::toString(ex.two) + ")"; |
onqtam | 05bcc37 | 2017-03-17 02:10:38 +0200 | [diff] [blame] | 84 | } |
| 85 | |
onqtam | 7cc0e96 | 2017-04-17 23:30:36 +0300 | [diff] [blame] | 86 | TEST_CASE("all asserts should fail and show how the objects get stringified") { |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 87 | MyTypeInherited<int> bla1; |
| 88 | bla1.one = 5; |
onqtam | 02b7eb7 | 2017-10-28 23:59:49 +0300 | [diff] [blame] | 89 | bla1.two = 4u; |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 90 | MyTypeInherited<int> bla2; |
| 91 | bla2.one = 5; |
onqtam | 02b7eb7 | 2017-10-28 23:59:49 +0300 | [diff] [blame] | 92 | bla2.two = 6u; |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 93 | |
onqtam | b18680d | 2016-11-15 14:08:56 +0200 | [diff] [blame] | 94 | Bar::Foo f1; |
| 95 | Bar::Foo f2; |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 96 | CHECK(f1 == f2); |
| 97 | |
| 98 | // std::string already has an operator<< working with std::ostream |
| 99 | std::string dummy1 = "omg"; |
| 100 | std::string dummy2 = "tralala"; |
| 101 | |
| 102 | CHECK(dummy1 == dummy2); |
| 103 | |
| 104 | std::vector<int> vec1; |
| 105 | vec1.push_back(1); |
| 106 | vec1.push_back(2); |
| 107 | vec1.push_back(3); |
| 108 | |
| 109 | std::vector<int> vec2; |
| 110 | vec2.push_back(1); |
| 111 | vec2.push_back(2); |
| 112 | vec2.push_back(4); |
| 113 | |
| 114 | CHECK(vec1 == vec2); |
| 115 | |
| 116 | std::list<int> lst_1; |
| 117 | lst_1.push_back(1); |
| 118 | lst_1.push_back(42); |
| 119 | lst_1.push_back(3); |
| 120 | |
| 121 | std::list<int> lst_2; |
| 122 | lst_2.push_back(1); |
| 123 | lst_2.push_back(2); |
| 124 | lst_2.push_back(666); |
| 125 | |
| 126 | CHECK(lst_1 == lst_2); |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 127 | |
onqtam | 05bcc37 | 2017-03-17 02:10:38 +0200 | [diff] [blame] | 128 | // lets see if this exception gets translated |
onqtam | 7cc0e96 | 2017-04-17 23:30:36 +0300 | [diff] [blame] | 129 | throw_if(true, bla1); |
onqtam | 05bcc37 | 2017-03-17 02:10:38 +0200 | [diff] [blame] | 130 | } |
| 131 | |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 132 | static doctest::String intTranslator(int ex) { |
| 133 | return doctest::String("int: ") + doctest::toString(ex); |
| 134 | } |
onqtam | 246e817 | 2017-03-17 02:48:12 +0200 | [diff] [blame] | 135 | |
onqtam | 05bcc37 | 2017-03-17 02:10:38 +0200 | [diff] [blame] | 136 | TEST_CASE("a test case that registers an exception translator for int and then throws one") { |
| 137 | // set an exception translator for int - note that this shouldn't be done in a test case but |
onqtam | 7cc0e96 | 2017-04-17 23:30:36 +0300 | [diff] [blame] | 138 | // in main() or somewhere before executing the tests - but here I'm just lazy... |
onqtam | 246e817 | 2017-03-17 02:48:12 +0200 | [diff] [blame] | 139 | doctest::registerExceptionTranslator(intTranslator); |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 140 | |
onqtam | 7cc0e96 | 2017-04-17 23:30:36 +0300 | [diff] [blame] | 141 | throw_if(true, 5); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 142 | } |