subscript.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Copyright Benoit Blanchon 2014-2017
  2. // MIT License
  3. //
  4. // Arduino JSON library
  5. // https://bblanchon.github.io/ArduinoJson/
  6. // If you like this project, please add a star!
  7. #include <ArduinoJson.h>
  8. #include <gtest/gtest.h>
  9. class JsonObject_Subscript_Tests : public ::testing::Test {
  10. public:
  11. JsonObject_Subscript_Tests() : _object(_jsonBuffer.createObject()) {}
  12. protected:
  13. DynamicJsonBuffer _jsonBuffer;
  14. JsonObject& _object;
  15. };
  16. #define TEST_(name) TEST_F(JsonObject_Subscript_Tests, name)
  17. TEST_(SizeIncreased_WhenValuesAreAdded) {
  18. _object["hello"] = 1;
  19. EXPECT_EQ(1, _object.size());
  20. }
  21. TEST_(SizeUntouched_WhenSameValueIsAdded) {
  22. _object["hello"] = 1;
  23. _object["hello"] = 2;
  24. EXPECT_EQ(1, _object.size());
  25. }
  26. TEST_(StoreInteger) {
  27. _object["hello"] = 123;
  28. EXPECT_EQ(123, _object["hello"].as<int>());
  29. EXPECT_TRUE(_object["hello"].is<int>());
  30. EXPECT_FALSE(_object["hello"].is<double>());
  31. }
  32. TEST_(StoreVolatileInteger) { // issue #415
  33. volatile int i = 123;
  34. _object["hello"] = i;
  35. EXPECT_EQ(123, _object["hello"].as<int>());
  36. EXPECT_TRUE(_object["hello"].is<int>());
  37. EXPECT_FALSE(_object["hello"].is<double>());
  38. }
  39. TEST_(StoreDouble) {
  40. _object["hello"] = 123.45;
  41. EXPECT_TRUE(_object["hello"].is<double>());
  42. EXPECT_FALSE(_object["hello"].is<long>());
  43. EXPECT_EQ(123.45, _object["hello"].as<double>());
  44. }
  45. TEST_(StoreDoubleWithDigits) {
  46. _object["hello"].set(123.45, 2);
  47. EXPECT_TRUE(_object["hello"].is<double>());
  48. EXPECT_FALSE(_object["hello"].is<long>());
  49. EXPECT_EQ(123.45, _object["hello"].as<double>());
  50. }
  51. TEST_(StoreBoolean) {
  52. _object["hello"] = true;
  53. EXPECT_TRUE(_object["hello"].is<bool>());
  54. EXPECT_FALSE(_object["hello"].is<long>());
  55. EXPECT_TRUE(_object["hello"].as<bool>());
  56. }
  57. TEST_(StoreString) {
  58. _object["hello"] = "h3110";
  59. EXPECT_TRUE(_object["hello"].is<const char*>());
  60. EXPECT_FALSE(_object["hello"].is<long>());
  61. EXPECT_STREQ("h3110", _object["hello"].as<const char*>());
  62. EXPECT_STREQ("h3110", _object["hello"].as<char*>()); // <- short hand
  63. }
  64. TEST_(StoreArray) {
  65. JsonArray& arr = _jsonBuffer.createArray();
  66. _object["hello"] = arr;
  67. EXPECT_EQ(&arr, &_object["hello"].as<JsonArray&>());
  68. EXPECT_EQ(&arr, &_object["hello"].as<JsonArray>()); // <- short hand
  69. EXPECT_EQ(&arr, &_object["hello"].as<const JsonArray&>());
  70. EXPECT_EQ(&arr, &_object["hello"].as<const JsonArray>()); // <- short hand
  71. EXPECT_TRUE(_object["hello"].is<JsonArray&>());
  72. EXPECT_TRUE(_object["hello"].is<JsonArray>());
  73. EXPECT_TRUE(_object["hello"].is<const JsonArray&>());
  74. EXPECT_TRUE(_object["hello"].is<const JsonArray>());
  75. EXPECT_FALSE(_object["hello"].is<JsonObject&>());
  76. }
  77. TEST_(StoreObject) {
  78. JsonObject& obj = _jsonBuffer.createObject();
  79. _object["hello"] = obj;
  80. EXPECT_EQ(&obj, &_object["hello"].as<JsonObject&>());
  81. EXPECT_EQ(&obj, &_object["hello"].as<JsonObject>()); // <- short hand
  82. EXPECT_EQ(&obj, &_object["hello"].as<const JsonObject&>());
  83. EXPECT_EQ(&obj, &_object["hello"].as<const JsonObject>()); // <- short hand
  84. EXPECT_TRUE(_object["hello"].is<JsonObject&>());
  85. EXPECT_TRUE(_object["hello"].is<JsonObject>());
  86. EXPECT_TRUE(_object["hello"].is<const JsonObject&>());
  87. EXPECT_TRUE(_object["hello"].is<const JsonObject>());
  88. EXPECT_FALSE(_object["hello"].is<JsonArray&>());
  89. }
  90. TEST_(StoreArraySubscript) {
  91. JsonArray& arr = _jsonBuffer.createArray();
  92. arr.add(42);
  93. _object["a"] = arr[0];
  94. EXPECT_EQ(42, _object["a"]);
  95. }
  96. TEST_(StoreObjectSubscript) {
  97. JsonObject& obj = _jsonBuffer.createObject();
  98. obj.set("x", 42);
  99. _object["a"] = obj["x"];
  100. EXPECT_EQ(42, _object["a"]);
  101. }
  102. TEST_(KeyAsCharArray) { // issue #423
  103. char key[] = "hello";
  104. _object[key] = 42;
  105. EXPECT_EQ(42, _object[key]);
  106. }