as.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <stdint.h>
  6. #include <catch.hpp>
  7. namespace my {
  8. using ARDUINOJSON_NAMESPACE::isinf;
  9. } // namespace my
  10. static const char* null = 0;
  11. TEST_CASE("JsonVariant::as()") {
  12. DynamicJsonDocument doc(4096);
  13. JsonVariant variant = doc.to<JsonVariant>();
  14. SECTION("not set") {
  15. REQUIRE(false == variant.as<bool>());
  16. REQUIRE(0 == variant.as<int>());
  17. REQUIRE(0.0f == variant.as<float>());
  18. REQUIRE(0 == variant.as<char*>());
  19. REQUIRE("null" == variant.as<std::string>());
  20. }
  21. SECTION("set(4.2)") {
  22. variant.set(4.2);
  23. REQUIRE(variant.as<bool>());
  24. REQUIRE(0 == variant.as<const char*>());
  25. REQUIRE(variant.as<std::string>() == "4.2");
  26. REQUIRE(variant.as<long>() == 4L);
  27. REQUIRE(variant.as<unsigned>() == 4U);
  28. }
  29. SECTION("set(0.0)") {
  30. variant.set(0.0);
  31. REQUIRE(variant.as<bool>() == false);
  32. REQUIRE(variant.as<long>() == 0L);
  33. }
  34. SECTION("set(false)") {
  35. variant.set(false);
  36. REQUIRE(false == variant.as<bool>());
  37. REQUIRE(variant.as<double>() == 0.0);
  38. REQUIRE(variant.as<long>() == 0L);
  39. REQUIRE(variant.as<std::string>() == "false");
  40. }
  41. SECTION("set(true)") {
  42. variant.set(true);
  43. REQUIRE(variant.as<bool>());
  44. REQUIRE(variant.as<double>() == 1.0);
  45. REQUIRE(variant.as<long>() == 1L);
  46. REQUIRE(variant.as<std::string>() == "true");
  47. }
  48. SECTION("set(42L)") {
  49. variant.set(42L);
  50. REQUIRE(variant.as<bool>() == true);
  51. REQUIRE(variant.as<double>() == 42.0);
  52. REQUIRE(variant.as<std::string>() == "42");
  53. }
  54. SECTION("set(-42L)") {
  55. variant.set(-42L);
  56. REQUIRE(variant.as<double>() == -42.0);
  57. REQUIRE(variant.as<std::string>() == "-42");
  58. }
  59. SECTION("set(0L)") {
  60. variant.set(0L);
  61. SECTION("as<bool>()") {
  62. REQUIRE(false == variant.as<bool>());
  63. }
  64. SECTION("as<double>()") {
  65. REQUIRE(variant.as<double>() == 0.0);
  66. }
  67. }
  68. SECTION("set(null)") {
  69. variant.set(null);
  70. REQUIRE(variant.as<bool>() == false);
  71. REQUIRE(variant.as<double>() == 0.0);
  72. REQUIRE(variant.as<long>() == 0L);
  73. REQUIRE(variant.as<std::string>() == "null");
  74. }
  75. SECTION("set(\"42\")") {
  76. variant.set("42");
  77. REQUIRE(variant.as<long>() == 42L);
  78. }
  79. SECTION("set(\"hello\")") {
  80. variant.set("hello");
  81. REQUIRE(variant.as<bool>() == false);
  82. REQUIRE(variant.as<long>() == 0L);
  83. REQUIRE(variant.as<const char*>() == std::string("hello"));
  84. REQUIRE(variant.as<char*>() == std::string("hello"));
  85. REQUIRE(variant.as<std::string>() == std::string("hello"));
  86. }
  87. SECTION("set(std::string(\"4.2\"))") {
  88. variant.set(std::string("4.2"));
  89. REQUIRE(variant.as<long>() == 4L);
  90. REQUIRE(variant.as<double>() == 4.2);
  91. REQUIRE(variant.as<char*>() == std::string("4.2"));
  92. REQUIRE(variant.as<std::string>() == std::string("4.2"));
  93. }
  94. SECTION("set(\"true\")") {
  95. variant.set("true");
  96. REQUIRE(variant.as<bool>() == true);
  97. REQUIRE(variant.as<int>() == 0);
  98. }
  99. SECTION("set(-1e300)") {
  100. variant.set(-1e300);
  101. REQUIRE(variant.as<double>() == -1e300);
  102. REQUIRE(variant.as<float>() < 0);
  103. REQUIRE(my::isinf(variant.as<float>()));
  104. }
  105. SECTION("set(1e300)") {
  106. variant.set(1e300);
  107. REQUIRE(variant.as<double>() == 1e300);
  108. REQUIRE(variant.as<float>() > 0);
  109. REQUIRE(my::isinf(variant.as<float>()));
  110. }
  111. SECTION("set(1e300)") {
  112. variant.set(1e-300);
  113. REQUIRE(variant.as<double>() == 1e-300);
  114. REQUIRE(variant.as<float>() == 0);
  115. }
  116. SECTION("to<JsonObject>()") {
  117. JsonObject obj = variant.to<JsonObject>();
  118. obj["key"] = "value";
  119. SECTION("as<std::string>()") {
  120. REQUIRE(variant.as<std::string>() == std::string("{\"key\":\"value\"}"));
  121. }
  122. SECTION("ObjectAsJsonObject") {
  123. JsonObject o = variant.as<JsonObject>();
  124. REQUIRE(o.size() == 1);
  125. REQUIRE(o["key"] == std::string("value"));
  126. }
  127. }
  128. SECTION("to<JsonArray>()") {
  129. JsonArray arr = variant.to<JsonArray>();
  130. arr.add(4);
  131. arr.add(2);
  132. SECTION("as<std::string>()") {
  133. REQUIRE(variant.as<std::string>() == std::string("[4,2]"));
  134. }
  135. SECTION("as<JsonArray>()") {
  136. JsonArray a = variant.as<JsonArray>();
  137. REQUIRE(a.size() == 2);
  138. REQUIRE(a[0] == 4);
  139. REQUIRE(a[1] == 2);
  140. }
  141. }
  142. #if ARDUINOJSON_USE_LONG_LONG
  143. SECTION("Smallest int64 negative") {
  144. variant.set("-9223372036854775808");
  145. REQUIRE(variant.as<long long>() == -9223372036854775807 - 1);
  146. }
  147. SECTION("Biggerst int64 positive") {
  148. variant.set("9223372036854775807");
  149. REQUIRE(variant.as<long long>() == 9223372036854775807);
  150. }
  151. #endif
  152. SECTION("should work on JsonVariantConst") {
  153. variant.set("hello");
  154. JsonVariantConst cvar = variant;
  155. REQUIRE(cvar.as<bool>() == false);
  156. REQUIRE(cvar.as<long>() == 0L);
  157. REQUIRE(cvar.as<const char*>() == std::string("hello"));
  158. REQUIRE(cvar.as<char*>() == std::string("hello"));
  159. // REQUIRE(cvar.as<std::string>() == std::string("hello"));
  160. }
  161. }