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 | |
onqtam | 190ba25 | 2019-03-23 09:58:34 +0200 | [diff] [blame^] | 12 | DOCTEST_MSVC_SUPPRESS_WARNING(5045) // Spectre mitigation diagnostics |
| 13 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 14 | // the standard forbids writing in the std namespace but it works on all compilers |
| 15 | namespace std |
| 16 | { |
| 17 | template <typename T> |
| 18 | ostream& 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 |
| 31 | namespace doctest |
| 32 | { |
| 33 | template <typename T> |
| 34 | struct 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 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 49 | template <typename T, typename K> |
| 50 | struct MyType |
| 51 | { |
| 52 | T one; |
| 53 | K two; |
| 54 | }; |
| 55 | |
| 56 | template <typename T> |
onqtam | 02b7eb7 | 2017-10-28 23:59:49 +0300 | [diff] [blame] | 57 | struct MyTypeInherited : MyType<T, unsigned> |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 58 | {}; |
| 59 | |
| 60 | template <typename T, typename K> |
| 61 | bool operator==(const MyType<T, K>& lhs, const MyType<T, K>& rhs) { |
| 62 | return lhs.one == rhs.one && lhs.two == rhs.two; |
| 63 | } |
| 64 | |
| 65 | template <typename T, typename K> |
| 66 | std::ostream& operator<<(std::ostream& stream, const MyType<T, K>& in) { |
| 67 | stream << "[" << in.one << ", " << in.two << "]"; |
| 68 | return stream; |
| 69 | } |
| 70 | |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 71 | namespace Bar |
| 72 | { |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 73 | struct Foo |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 74 | { |
| 75 | friend bool operator==(const Foo&, const Foo&) { return false; } |
| 76 | }; |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 77 | |
onqtam | b18680d | 2016-11-15 14:08:56 +0200 | [diff] [blame] | 78 | // as a third option you may provide an overload of toString() |
onqtam | 7cc0e96 | 2017-04-17 23:30:36 +0300 | [diff] [blame] | 79 | inline doctest::String toString(const Foo&) { return "Foo{}"; } |
onqtam | b18680d | 2016-11-15 14:08:56 +0200 | [diff] [blame] | 80 | } // namespace Bar |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 81 | |
onqtam | 05bcc37 | 2017-03-17 02:10:38 +0200 | [diff] [blame] | 82 | // set an exception translator for MyTypeInherited<int> |
| 83 | REGISTER_EXCEPTION_TRANSLATOR(MyTypeInherited<int>& ex) { |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 84 | return doctest::String("MyTypeInherited<int>(") + doctest::toString(ex.one) + ", " + |
| 85 | doctest::toString(ex.two) + ")"; |
onqtam | 05bcc37 | 2017-03-17 02:10:38 +0200 | [diff] [blame] | 86 | } |
| 87 | |
onqtam | 7cc0e96 | 2017-04-17 23:30:36 +0300 | [diff] [blame] | 88 | TEST_CASE("all asserts should fail and show how the objects get stringified") { |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 89 | MyTypeInherited<int> bla1; |
| 90 | bla1.one = 5; |
onqtam | 02b7eb7 | 2017-10-28 23:59:49 +0300 | [diff] [blame] | 91 | bla1.two = 4u; |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 92 | MyTypeInherited<int> bla2; |
| 93 | bla2.one = 5; |
onqtam | 02b7eb7 | 2017-10-28 23:59:49 +0300 | [diff] [blame] | 94 | bla2.two = 6u; |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 95 | |
onqtam | b18680d | 2016-11-15 14:08:56 +0200 | [diff] [blame] | 96 | Bar::Foo f1; |
| 97 | Bar::Foo f2; |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 98 | 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); |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 129 | |
onqtam | 05bcc37 | 2017-03-17 02:10:38 +0200 | [diff] [blame] | 130 | // lets see if this exception gets translated |
onqtam | 7cc0e96 | 2017-04-17 23:30:36 +0300 | [diff] [blame] | 131 | throw_if(true, bla1); |
onqtam | 05bcc37 | 2017-03-17 02:10:38 +0200 | [diff] [blame] | 132 | } |
| 133 | |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 134 | static doctest::String intTranslator(int ex) { |
| 135 | return doctest::String("int: ") + doctest::toString(ex); |
| 136 | } |
onqtam | 246e817 | 2017-03-17 02:48:12 +0200 | [diff] [blame] | 137 | |
onqtam | 05bcc37 | 2017-03-17 02:10:38 +0200 | [diff] [blame] | 138 | TEST_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 |
onqtam | 7cc0e96 | 2017-04-17 23:30:36 +0300 | [diff] [blame] | 140 | // 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] | 141 | doctest::registerExceptionTranslator(intTranslator); |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 142 | |
onqtam | 7cc0e96 | 2017-04-17 23:30:36 +0300 | [diff] [blame] | 143 | throw_if(true, 5); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 144 | } |