DeserializationError.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  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(bool(error) == expected);
  12. CHECK(bool(error) != !expected);
  13. CHECK(!bool(error) == !expected);
  14. // DeserializationError on right-hand side
  15. CHECK(expected == bool(error));
  16. CHECK(!expected != bool(error));
  17. CHECK(!expected == !bool(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(EmptyInput);
  27. TEST_STRINGIFICATION(IncompleteInput);
  28. TEST_STRINGIFICATION(InvalidInput);
  29. TEST_STRINGIFICATION(NoMemory);
  30. TEST_STRINGIFICATION(TooDeep);
  31. }
  32. SECTION("as boolean") {
  33. TEST_BOOLIFICATION(Ok, false);
  34. TEST_BOOLIFICATION(EmptyInput, true);
  35. TEST_BOOLIFICATION(IncompleteInput, true);
  36. TEST_BOOLIFICATION(InvalidInput, true);
  37. TEST_BOOLIFICATION(NoMemory, true);
  38. TEST_BOOLIFICATION(TooDeep, 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("switch") {
  51. DeserializationError err = DeserializationError::InvalidInput;
  52. switch (err.code()) {
  53. case DeserializationError::InvalidInput:
  54. SUCCEED();
  55. break;
  56. default:
  57. FAIL();
  58. break;
  59. }
  60. }
  61. SECTION("Use in a condition") {
  62. DeserializationError invalidInput(DeserializationError::InvalidInput);
  63. DeserializationError ok(DeserializationError::Ok);
  64. SECTION("if (!err)") {
  65. if (!invalidInput)
  66. FAIL();
  67. }
  68. SECTION("if (err)") {
  69. if (ok)
  70. FAIL();
  71. }
  72. }
  73. SECTION("Comparisons") {
  74. DeserializationError invalidInput(DeserializationError::InvalidInput);
  75. DeserializationError ok(DeserializationError::Ok);
  76. SECTION("DeserializationError == Code") {
  77. REQUIRE(invalidInput == DeserializationError::InvalidInput);
  78. REQUIRE(ok == DeserializationError::Ok);
  79. }
  80. SECTION("Code == DeserializationError") {
  81. REQUIRE(DeserializationError::InvalidInput == invalidInput);
  82. REQUIRE(DeserializationError::Ok == ok);
  83. }
  84. SECTION("DeserializationError != Code") {
  85. REQUIRE(invalidInput != DeserializationError::Ok);
  86. REQUIRE(ok != DeserializationError::InvalidInput);
  87. }
  88. SECTION("Code != DeserializationError") {
  89. REQUIRE(DeserializationError::Ok != invalidInput);
  90. REQUIRE(DeserializationError::InvalidInput != ok);
  91. }
  92. SECTION("DeserializationError == DeserializationError") {
  93. REQUIRE_FALSE(invalidInput == ok);
  94. }
  95. SECTION("DeserializationError != DeserializationError") {
  96. REQUIRE(invalidInput != ok);
  97. }
  98. }
  99. }