JsonVariant.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. #include <limits>
  7. template <typename T>
  8. void check(T value, const std::string &expected) {
  9. DynamicJsonDocument doc(4096);
  10. doc.to<JsonVariant>().set(value);
  11. char buffer[256] = "";
  12. size_t returnValue = serializeJson(doc, buffer, sizeof(buffer));
  13. REQUIRE(expected == buffer);
  14. REQUIRE(expected.size() == returnValue);
  15. }
  16. TEST_CASE("serializeJson(JsonVariant)") {
  17. SECTION("Undefined") {
  18. check(JsonVariant(), "null");
  19. }
  20. SECTION("Null string") {
  21. check(static_cast<char *>(0), "null");
  22. }
  23. SECTION("const char*") {
  24. check("hello", "\"hello\"");
  25. }
  26. SECTION("string") {
  27. check(std::string("hello"), "\"hello\"");
  28. }
  29. SECTION("SerializedValue<const char*>") {
  30. check(serialized("[1,2]"), "[1,2]");
  31. }
  32. SECTION("SerializedValue<std::string>") {
  33. check(serialized(std::string("[1,2]")), "[1,2]");
  34. }
  35. SECTION("Double") {
  36. check(3.1415927, "3.1415927");
  37. }
  38. SECTION("Zero") {
  39. check(0, "0");
  40. }
  41. SECTION("Integer") {
  42. check(42, "42");
  43. }
  44. SECTION("NegativeLong") {
  45. check(-42, "-42");
  46. }
  47. SECTION("UnsignedLong") {
  48. check(4294967295UL, "4294967295");
  49. }
  50. SECTION("True") {
  51. check(true, "true");
  52. }
  53. SECTION("OneFalse") {
  54. check(false, "false");
  55. }
  56. #if ARDUINOJSON_USE_LONG_LONG
  57. SECTION("NegativeInt64") {
  58. check(-9223372036854775807 - 1, "-9223372036854775808");
  59. }
  60. SECTION("PositiveInt64") {
  61. check(9223372036854775807, "9223372036854775807");
  62. }
  63. SECTION("UInt64") {
  64. check(18446744073709551615U, "18446744073709551615");
  65. }
  66. #endif
  67. }