DeserializationError.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. void testStringification(DeserializationError error, std::string expected) {
  7. REQUIRE(error.c_str() == expected);
  8. }
  9. void testBoolification(DeserializationError error, bool expected) {
  10. // DeserializationError on left-hand side
  11. CHECK(error == expected);
  12. CHECK(error != !expected);
  13. CHECK(!error == !expected);
  14. // DeserializationError on right-hand side
  15. CHECK(expected == error);
  16. CHECK(!expected != error);
  17. CHECK(!expected == !error);
  18. }
  19. #define TEST_STRINGIFICATION(symbol) \
  20. testStringification(DeserializationError::symbol, #symbol)
  21. #define TEST_BOOLIFICATION(symbol, expected) \
  22. testBoolification(DeserializationError::symbol, expected)
  23. TEST_CASE("DeserializationError") {
  24. SECTION("c_str()") {
  25. TEST_STRINGIFICATION(Ok);
  26. TEST_STRINGIFICATION(TooDeep);
  27. TEST_STRINGIFICATION(NoMemory);
  28. TEST_STRINGIFICATION(InvalidInput);
  29. TEST_STRINGIFICATION(IncompleteInput);
  30. TEST_STRINGIFICATION(NotSupported);
  31. }
  32. SECTION("as boolean") {
  33. TEST_BOOLIFICATION(Ok, false);
  34. TEST_BOOLIFICATION(TooDeep, true);
  35. TEST_BOOLIFICATION(NoMemory, true);
  36. TEST_BOOLIFICATION(InvalidInput, true);
  37. TEST_BOOLIFICATION(IncompleteInput, true);
  38. TEST_BOOLIFICATION(NotSupported, true);
  39. }
  40. SECTION("ostream DeserializationError") {
  41. std::stringstream s;
  42. s << DeserializationError(DeserializationError::InvalidInput);
  43. REQUIRE(s.str() == "InvalidInput");
  44. }
  45. SECTION("ostream DeserializationError::Code") {
  46. std::stringstream s;
  47. s << DeserializationError::InvalidInput;
  48. REQUIRE(s.str() == "InvalidInput");
  49. }
  50. SECTION("out of range") {
  51. int code = 666;
  52. DeserializationError err(
  53. *reinterpret_cast<DeserializationError::Code*>(&code));
  54. REQUIRE(err.c_str() == std::string("???"));
  55. }
  56. SECTION("switch") {
  57. DeserializationError err = DeserializationError::InvalidInput;
  58. switch (err.code()) {
  59. case DeserializationError::InvalidInput:
  60. SUCCEED();
  61. break;
  62. default:
  63. FAIL();
  64. break;
  65. }
  66. }
  67. SECTION("Comparisons") {
  68. DeserializationError invalidInput(DeserializationError::InvalidInput);
  69. DeserializationError ok(DeserializationError::Ok);
  70. SECTION("DeserializationError == bool") {
  71. REQUIRE(invalidInput == true);
  72. REQUIRE(ok == false);
  73. }
  74. SECTION("bool == DeserializationError") {
  75. REQUIRE(true == invalidInput);
  76. REQUIRE(false == ok);
  77. }
  78. SECTION("DeserializationError != bool") {
  79. REQUIRE(invalidInput != false);
  80. REQUIRE(ok != true);
  81. }
  82. SECTION("bool != DeserializationError") {
  83. REQUIRE(false != invalidInput);
  84. REQUIRE(true != ok);
  85. }
  86. SECTION("Negations") {
  87. REQUIRE(!invalidInput == false);
  88. REQUIRE(!ok == true);
  89. }
  90. SECTION("DeserializationError == Code") {
  91. REQUIRE(invalidInput == DeserializationError::InvalidInput);
  92. REQUIRE(ok == DeserializationError::Ok);
  93. }
  94. SECTION("Code == DeserializationError") {
  95. REQUIRE(DeserializationError::InvalidInput == invalidInput);
  96. REQUIRE(DeserializationError::Ok == ok);
  97. }
  98. SECTION("DeserializationError != Code") {
  99. REQUIRE(invalidInput != DeserializationError::Ok);
  100. REQUIRE(ok != DeserializationError::InvalidInput);
  101. }
  102. SECTION("Code != DeserializationError") {
  103. REQUIRE(DeserializationError::Ok != invalidInput);
  104. REQUIRE(DeserializationError::InvalidInput != ok);
  105. }
  106. SECTION("DeserializationError == DeserializationError") {
  107. REQUIRE_FALSE(invalidInput == ok);
  108. }
  109. SECTION("DeserializationError != DeserializationError") {
  110. REQUIRE(invalidInput != ok);
  111. }
  112. }
  113. }