|
| 1 | +/* |
| 2 | + This file is part of libhttpserver |
| 3 | + Copyright (C) 2011-2019 Sebastiano Merlino |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 18 | + USA |
| 19 | +*/ |
| 20 | + |
| 21 | +#include <stdexcept> |
| 22 | +#include <string> |
| 23 | +#include <type_traits> |
| 24 | + |
| 25 | +#include "./httpserver.hpp" |
| 26 | +#include "./littletest.hpp" |
| 27 | + |
| 28 | +// AC #1: feature_unavailable derives from std::runtime_error. This compile-time |
| 29 | +// assertion runs at TU scope and fires if the inheritance is ever broken. |
| 30 | +static_assert( |
| 31 | + std::is_base_of_v<std::runtime_error, httpserver::feature_unavailable>, |
| 32 | + "feature_unavailable must derive from std::runtime_error"); |
| 33 | + |
| 34 | +LT_BEGIN_SUITE(feature_unavailable_suite) |
| 35 | + void set_up() { |
| 36 | + } |
| 37 | + |
| 38 | + void tear_down() { |
| 39 | + } |
| 40 | +LT_END_SUITE(feature_unavailable_suite) |
| 41 | + |
| 42 | +// AC #2: a unit test catches the exception as std::runtime_error and asserts |
| 43 | +// that what() contains both the feature name and the build flag. |
| 44 | +LT_BEGIN_AUTO_TEST(feature_unavailable_suite, |
| 45 | + catches_as_runtime_error_with_feature_and_flag) |
| 46 | + std::string msg; |
| 47 | + try { |
| 48 | + throw httpserver::feature_unavailable("tls", "HAVE_GNUTLS"); |
| 49 | + } catch (const std::runtime_error& e) { |
| 50 | + msg = e.what(); |
| 51 | + } |
| 52 | + LT_CHECK(msg.find("tls") != std::string::npos); |
| 53 | + LT_CHECK(msg.find("HAVE_GNUTLS") != std::string::npos); |
| 54 | +LT_END_AUTO_TEST(catches_as_runtime_error_with_feature_and_flag) |
| 55 | + |
| 56 | +// Catching the concrete type still produces a runtime_error-shaped what(). |
| 57 | +LT_BEGIN_AUTO_TEST(feature_unavailable_suite, catches_as_feature_unavailable_directly) |
| 58 | + std::string msg; |
| 59 | + try { |
| 60 | + throw httpserver::feature_unavailable("tls", "HAVE_GNUTLS"); |
| 61 | + } catch (const httpserver::feature_unavailable& e) { |
| 62 | + const std::runtime_error* base = &e; |
| 63 | + msg = base->what(); |
| 64 | + } |
| 65 | + LT_CHECK(msg.find("tls") != std::string::npos); |
| 66 | + LT_CHECK(msg.find("HAVE_GNUTLS") != std::string::npos); |
| 67 | +LT_END_AUTO_TEST(catches_as_feature_unavailable_directly) |
| 68 | + |
| 69 | +// Guard against a hard-coded message: a different (feature, flag) pair must |
| 70 | +// also propagate verbatim into what(). |
| 71 | +LT_BEGIN_AUTO_TEST(feature_unavailable_suite, composes_message_for_websocket) |
| 72 | + std::string msg; |
| 73 | + try { |
| 74 | + throw httpserver::feature_unavailable("websocket", "HAVE_WEBSOCKET"); |
| 75 | + } catch (const std::runtime_error& e) { |
| 76 | + msg = e.what(); |
| 77 | + } |
| 78 | + LT_CHECK(msg.find("websocket") != std::string::npos); |
| 79 | + LT_CHECK(msg.find("HAVE_WEBSOCKET") != std::string::npos); |
| 80 | +LT_END_AUTO_TEST(composes_message_for_websocket) |
| 81 | + |
| 82 | +LT_BEGIN_AUTO_TEST_ENV() |
| 83 | + AUTORUN_TESTS() |
| 84 | +LT_END_AUTO_TEST_ENV() |
0 commit comments