as.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  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. enum MY_ENUM { ONE = 1, TWO = 2 };
  11. TEST_CASE("JsonVariant::as()") {
  12. static const char* null = 0;
  13. DynamicJsonDocument doc(4096);
  14. JsonVariant variant = doc.to<JsonVariant>();
  15. SECTION("not set") {
  16. REQUIRE(false == variant.as<bool>());
  17. REQUIRE(0 == variant.as<int>());
  18. REQUIRE(0.0f == variant.as<float>());
  19. REQUIRE(0 == variant.as<const char*>());
  20. REQUIRE("null" == variant.as<std::string>());
  21. REQUIRE(variant.as<JsonString>().isNull());
  22. }
  23. SECTION("set(4.2)") {
  24. variant.set(4.2);
  25. REQUIRE(variant.as<bool>());
  26. REQUIRE(0 == variant.as<const char*>());
  27. REQUIRE(variant.as<std::string>() == "4.2");
  28. REQUIRE(variant.as<long>() == 4L);
  29. REQUIRE(variant.as<unsigned>() == 4U);
  30. REQUIRE(variant.as<JsonString>().isNull());
  31. }
  32. SECTION("set(0.0)") {
  33. variant.set(0.0);
  34. REQUIRE(variant.as<bool>() == false);
  35. REQUIRE(variant.as<long>() == 0L);
  36. REQUIRE(variant.as<JsonString>().isNull());
  37. }
  38. SECTION("set(false)") {
  39. variant.set(false);
  40. REQUIRE(false == variant.as<bool>());
  41. REQUIRE(variant.as<double>() == 0.0);
  42. REQUIRE(variant.as<long>() == 0L);
  43. REQUIRE(variant.as<std::string>() == "false");
  44. REQUIRE(variant.as<JsonString>().isNull());
  45. }
  46. SECTION("set(true)") {
  47. variant.set(true);
  48. REQUIRE(variant.as<bool>());
  49. REQUIRE(variant.as<double>() == 1.0);
  50. REQUIRE(variant.as<long>() == 1L);
  51. REQUIRE(variant.as<std::string>() == "true");
  52. REQUIRE(variant.as<JsonString>().isNull());
  53. }
  54. SECTION("set(42)") {
  55. variant.set(42);
  56. REQUIRE(variant.as<bool>() == true);
  57. REQUIRE(variant.as<double>() == 42.0);
  58. REQUIRE(variant.as<int>() == 42);
  59. REQUIRE(variant.as<unsigned int>() == 42U); // issue #1601
  60. REQUIRE(variant.as<std::string>() == "42");
  61. REQUIRE(variant.as<JsonString>().isNull());
  62. }
  63. SECTION("set(42L)") {
  64. variant.set(42L);
  65. REQUIRE(variant.as<bool>() == true);
  66. REQUIRE(variant.as<double>() == 42.0);
  67. REQUIRE(variant.as<std::string>() == "42");
  68. REQUIRE(variant.as<JsonString>().isNull());
  69. }
  70. SECTION("set(-42L)") {
  71. variant.set(-42L);
  72. REQUIRE(variant.as<double>() == -42.0);
  73. REQUIRE(variant.as<std::string>() == "-42");
  74. REQUIRE(variant.as<JsonString>().isNull());
  75. }
  76. SECTION("set(42UL)") {
  77. variant.set(42UL);
  78. REQUIRE(variant.as<bool>() == true);
  79. REQUIRE(variant.as<double>() == 42.0);
  80. REQUIRE(variant.as<std::string>() == "42");
  81. REQUIRE(variant.as<JsonString>().isNull());
  82. }
  83. SECTION("set(0L)") {
  84. variant.set(0L);
  85. REQUIRE(variant.as<bool>() == false);
  86. REQUIRE(variant.as<double>() == 0.0);
  87. REQUIRE(variant.as<std::string>() == "0");
  88. REQUIRE(variant.as<JsonString>().isNull());
  89. }
  90. SECTION("set(0UL)") {
  91. variant.set(0UL);
  92. REQUIRE(variant.as<bool>() == false);
  93. REQUIRE(variant.as<double>() == 0.0);
  94. REQUIRE(variant.as<std::string>() == "0");
  95. REQUIRE(variant.as<JsonString>().isNull());
  96. }
  97. SECTION("set(null)") {
  98. variant.set(null);
  99. REQUIRE(variant.as<bool>() == false);
  100. REQUIRE(variant.as<double>() == 0.0);
  101. REQUIRE(variant.as<long>() == 0L);
  102. REQUIRE(variant.as<std::string>() == "null");
  103. REQUIRE(variant.as<JsonString>().isNull());
  104. }
  105. SECTION("set(\"42\")") {
  106. variant.set("42");
  107. REQUIRE(variant.as<long>() == 42L);
  108. REQUIRE(variant.as<JsonString>() == "42");
  109. REQUIRE(variant.as<JsonString>().isLinked() == true);
  110. }
  111. SECTION("set(\"hello\")") {
  112. variant.set("hello");
  113. REQUIRE(variant.as<bool>() == true);
  114. REQUIRE(variant.as<long>() == 0L);
  115. REQUIRE(variant.as<const char*>() == std::string("hello"));
  116. REQUIRE(variant.as<const char*>() == std::string("hello"));
  117. REQUIRE(variant.as<std::string>() == std::string("hello"));
  118. REQUIRE(variant.as<JsonString>() == "hello");
  119. }
  120. SECTION("set(std::string(\"4.2\"))") {
  121. variant.set(std::string("4.2"));
  122. REQUIRE(variant.as<bool>() == true);
  123. REQUIRE(variant.as<long>() == 4L);
  124. REQUIRE(variant.as<double>() == 4.2);
  125. REQUIRE(variant.as<const char*>() == std::string("4.2"));
  126. REQUIRE(variant.as<std::string>() == std::string("4.2"));
  127. REQUIRE(variant.as<JsonString>() == "4.2");
  128. REQUIRE(variant.as<JsonString>().isLinked() == false);
  129. }
  130. SECTION("set(\"true\")") {
  131. variant.set("true");
  132. REQUIRE(variant.as<bool>() == true);
  133. REQUIRE(variant.as<int>() == 0);
  134. REQUIRE(variant.as<JsonString>() == "true");
  135. }
  136. SECTION("set(-1e300)") {
  137. variant.set(-1e300);
  138. REQUIRE(variant.as<bool>() == true);
  139. REQUIRE(variant.as<double>() == -1e300);
  140. REQUIRE(variant.as<float>() < 0);
  141. REQUIRE(my::isinf(variant.as<float>()));
  142. REQUIRE(variant.as<JsonString>().isNull());
  143. }
  144. SECTION("set(1e300)") {
  145. variant.set(1e300);
  146. REQUIRE(variant.as<bool>() == true);
  147. REQUIRE(variant.as<double>() == 1e300);
  148. REQUIRE(variant.as<float>() > 0);
  149. REQUIRE(my::isinf(variant.as<float>()));
  150. REQUIRE(variant.as<JsonString>().isNull());
  151. }
  152. SECTION("set(1e-300)") {
  153. variant.set(1e-300);
  154. REQUIRE(variant.as<bool>() == true);
  155. REQUIRE(variant.as<double>() == 1e-300);
  156. REQUIRE(variant.as<float>() == 0);
  157. REQUIRE(variant.as<JsonString>().isNull());
  158. }
  159. SECTION("to<JsonObject>()") {
  160. JsonObject obj = variant.to<JsonObject>();
  161. obj["key"] = "value";
  162. SECTION("as<bool>()") {
  163. REQUIRE(variant.as<bool>() == true);
  164. }
  165. SECTION("as<std::string>()") {
  166. REQUIRE(variant.as<std::string>() == std::string("{\"key\":\"value\"}"));
  167. }
  168. SECTION("ObjectAsJsonObject") {
  169. JsonObject o = variant.as<JsonObject>();
  170. REQUIRE(o.size() == 1);
  171. REQUIRE(o["key"] == std::string("value"));
  172. }
  173. }
  174. SECTION("to<JsonArray>()") {
  175. JsonArray arr = variant.to<JsonArray>();
  176. arr.add(4);
  177. arr.add(2);
  178. SECTION("as<bool>()") {
  179. REQUIRE(variant.as<bool>() == true);
  180. }
  181. SECTION("as<std::string>()") {
  182. REQUIRE(variant.as<std::string>() == std::string("[4,2]"));
  183. }
  184. SECTION("as<JsonArray>()") {
  185. JsonArray a = variant.as<JsonArray>();
  186. REQUIRE(a.size() == 2);
  187. REQUIRE(a[0] == 4);
  188. REQUIRE(a[1] == 2);
  189. }
  190. }
  191. #if ARDUINOJSON_USE_LONG_LONG
  192. SECTION("Smallest int64 negative") {
  193. variant.set("-9223372036854775808");
  194. REQUIRE(variant.as<long long>() == -9223372036854775807 - 1);
  195. }
  196. SECTION("Biggerst int64 positive") {
  197. variant.set("9223372036854775807");
  198. REQUIRE(variant.as<long long>() == 9223372036854775807);
  199. }
  200. #endif
  201. SECTION("should work on JsonVariantConst") {
  202. variant.set("hello");
  203. JsonVariantConst cvar = variant;
  204. REQUIRE(cvar.as<bool>() == true);
  205. REQUIRE(cvar.as<long>() == 0L);
  206. REQUIRE(cvar.as<const char*>() == std::string("hello"));
  207. REQUIRE(cvar.as<std::string>() == std::string("hello"));
  208. }
  209. SECTION("as<enum>()") {
  210. variant.set(1);
  211. REQUIRE(variant.as<MY_ENUM>() == ONE);
  212. }
  213. }