as.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. REQUIRE(variant.as<bool>() == false);
  62. REQUIRE(variant.as<double>() == 0.0);
  63. }
  64. SECTION("set(null)") {
  65. variant.set(null);
  66. REQUIRE(variant.as<bool>() == false);
  67. REQUIRE(variant.as<double>() == 0.0);
  68. REQUIRE(variant.as<long>() == 0L);
  69. REQUIRE(variant.as<std::string>() == "null");
  70. }
  71. SECTION("set(\"42\")") {
  72. variant.set("42");
  73. REQUIRE(variant.as<long>() == 42L);
  74. }
  75. SECTION("set(\"hello\")") {
  76. variant.set("hello");
  77. REQUIRE(variant.as<bool>() == true);
  78. REQUIRE(variant.as<long>() == 0L);
  79. REQUIRE(variant.as<const char*>() == std::string("hello"));
  80. REQUIRE(variant.as<char*>() == std::string("hello"));
  81. REQUIRE(variant.as<std::string>() == std::string("hello"));
  82. }
  83. SECTION("set(std::string(\"4.2\"))") {
  84. variant.set(std::string("4.2"));
  85. REQUIRE(variant.as<bool>() == true);
  86. REQUIRE(variant.as<long>() == 4L);
  87. REQUIRE(variant.as<double>() == 4.2);
  88. REQUIRE(variant.as<char*>() == std::string("4.2"));
  89. REQUIRE(variant.as<std::string>() == std::string("4.2"));
  90. }
  91. SECTION("set(\"true\")") {
  92. variant.set("true");
  93. REQUIRE(variant.as<bool>() == true);
  94. REQUIRE(variant.as<int>() == 0);
  95. }
  96. SECTION("set(-1e300)") {
  97. variant.set(-1e300);
  98. REQUIRE(variant.as<bool>() == true);
  99. REQUIRE(variant.as<double>() == -1e300);
  100. REQUIRE(variant.as<float>() < 0);
  101. REQUIRE(my::isinf(variant.as<float>()));
  102. }
  103. SECTION("set(1e300)") {
  104. variant.set(1e300);
  105. REQUIRE(variant.as<bool>() == true);
  106. REQUIRE(variant.as<double>() == 1e300);
  107. REQUIRE(variant.as<float>() > 0);
  108. REQUIRE(my::isinf(variant.as<float>()));
  109. }
  110. SECTION("set(1e-300)") {
  111. variant.set(1e-300);
  112. REQUIRE(variant.as<bool>() == true);
  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<bool>()") {
  120. REQUIRE(variant.as<bool>() == true);
  121. }
  122. SECTION("as<std::string>()") {
  123. REQUIRE(variant.as<std::string>() == std::string("{\"key\":\"value\"}"));
  124. }
  125. SECTION("ObjectAsJsonObject") {
  126. JsonObject o = variant.as<JsonObject>();
  127. REQUIRE(o.size() == 1);
  128. REQUIRE(o["key"] == std::string("value"));
  129. }
  130. }
  131. SECTION("to<JsonArray>()") {
  132. JsonArray arr = variant.to<JsonArray>();
  133. arr.add(4);
  134. arr.add(2);
  135. SECTION("as<bool>()") {
  136. REQUIRE(variant.as<bool>() == true);
  137. }
  138. SECTION("as<std::string>()") {
  139. REQUIRE(variant.as<std::string>() == std::string("[4,2]"));
  140. }
  141. SECTION("as<JsonArray>()") {
  142. JsonArray a = variant.as<JsonArray>();
  143. REQUIRE(a.size() == 2);
  144. REQUIRE(a[0] == 4);
  145. REQUIRE(a[1] == 2);
  146. }
  147. }
  148. #if ARDUINOJSON_USE_LONG_LONG
  149. SECTION("Smallest int64 negative") {
  150. variant.set("-9223372036854775808");
  151. REQUIRE(variant.as<long long>() == -9223372036854775807 - 1);
  152. }
  153. SECTION("Biggerst int64 positive") {
  154. variant.set("9223372036854775807");
  155. REQUIRE(variant.as<long long>() == 9223372036854775807);
  156. }
  157. #endif
  158. SECTION("should work on JsonVariantConst") {
  159. variant.set("hello");
  160. JsonVariantConst cvar = variant;
  161. REQUIRE(cvar.as<bool>() == true);
  162. REQUIRE(cvar.as<long>() == 0L);
  163. REQUIRE(cvar.as<const char*>() == std::string("hello"));
  164. REQUIRE(cvar.as<char*>() == std::string("hello"));
  165. // REQUIRE(cvar.as<std::string>() == std::string("hello"));
  166. }
  167. }