unit-noexcept.cpp 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // __ _____ _____ _____
  2. // __| | __| | | | JSON for Modern C++ (supporting code)
  3. // | | |__ | | | | | | version 3.11.2
  4. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5. //
  6. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  7. // SPDX-License-Identifier: MIT
  8. #include "doctest_compatibility.h"
  9. // disable -Wnoexcept due to struct pod_bis
  10. DOCTEST_GCC_SUPPRESS_WARNING_PUSH
  11. DOCTEST_GCC_SUPPRESS_WARNING("-Wnoexcept")
  12. #include <nlohmann/json.hpp>
  13. using nlohmann::json;
  14. namespace
  15. {
  16. enum test {};
  17. struct pod {};
  18. struct pod_bis {};
  19. void to_json(json& /*unused*/, pod /*unused*/) noexcept;
  20. void to_json(json& /*unused*/, pod_bis /*unused*/);
  21. void from_json(const json& /*unused*/, pod /*unused*/) noexcept;
  22. void from_json(const json& /*unused*/, pod_bis /*unused*/);
  23. void to_json(json& /*unused*/, pod /*unused*/) noexcept {}
  24. void to_json(json& /*unused*/, pod_bis /*unused*/) {}
  25. void from_json(const json& /*unused*/, pod /*unused*/) noexcept {}
  26. void from_json(const json& /*unused*/, pod_bis /*unused*/) {}
  27. static_assert(noexcept(json{}), "");
  28. static_assert(noexcept(nlohmann::to_json(std::declval<json&>(), 2)), "");
  29. static_assert(noexcept(nlohmann::to_json(std::declval<json&>(), 2.5)), "");
  30. static_assert(noexcept(nlohmann::to_json(std::declval<json&>(), true)), "");
  31. static_assert(noexcept(nlohmann::to_json(std::declval<json&>(), test{})), "");
  32. static_assert(noexcept(nlohmann::to_json(std::declval<json&>(), pod{})), "");
  33. static_assert(!noexcept(nlohmann::to_json(std::declval<json&>(), pod_bis{})), "");
  34. static_assert(noexcept(json(2)), "");
  35. static_assert(noexcept(json(test{})), "");
  36. static_assert(noexcept(json(pod{})), "");
  37. static_assert(noexcept(std::declval<json>().get<pod>()), "");
  38. static_assert(!noexcept(std::declval<json>().get<pod_bis>()), "");
  39. static_assert(noexcept(json(pod{})), "");
  40. } // namespace
  41. TEST_CASE("noexcept")
  42. {
  43. // silence -Wunneeded-internal-declaration errors
  44. static_cast<void>(static_cast<void(*)(json&, pod)>(&to_json));
  45. static_cast<void>(static_cast<void(*)(json&, pod_bis)>(&to_json));
  46. static_cast<void>(static_cast<void(*)(const json&, pod)>(&from_json));
  47. static_cast<void>(static_cast<void(*)(const json&, pod_bis)>(&from_json));
  48. SECTION("nothrow-copy-constructible exceptions")
  49. {
  50. // for ERR60-CPP (https://github.com/nlohmann/json/issues/531):
  51. // Exceptions should be nothrow-copy-constructible. However, compilers
  52. // treat std::runtime_exception differently in this regard. Therefore,
  53. // we can only demand nothrow-copy-constructibility for our exceptions
  54. // if std::runtime_exception is.
  55. CHECK(std::is_nothrow_copy_constructible<json::exception>::value == std::is_nothrow_copy_constructible<std::runtime_error>::value);
  56. CHECK(std::is_nothrow_copy_constructible<json::parse_error>::value == std::is_nothrow_copy_constructible<std::runtime_error>::value);
  57. CHECK(std::is_nothrow_copy_constructible<json::invalid_iterator>::value == std::is_nothrow_copy_constructible<std::runtime_error>::value);
  58. CHECK(std::is_nothrow_copy_constructible<json::type_error>::value == std::is_nothrow_copy_constructible<std::runtime_error>::value);
  59. CHECK(std::is_nothrow_copy_constructible<json::out_of_range>::value == std::is_nothrow_copy_constructible<std::runtime_error>::value);
  60. CHECK(std::is_nothrow_copy_constructible<json::other_error>::value == std::is_nothrow_copy_constructible<std::runtime_error>::value);
  61. }
  62. }
  63. DOCTEST_GCC_SUPPRESS_WARNING_POP