ArduinoString_Tests.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Copyright Benoit Blanchon 2014-2015
  2. // MIT License
  3. //
  4. // Arduino JSON library
  5. // https://github.com/bblanchon/ArduinoJson
  6. #include <gtest/gtest.h>
  7. #include <ArduinoJson.h>
  8. class ArduinoStringTests : public ::testing::Test {
  9. protected:
  10. static void eraseString(String &str) {
  11. char *p = const_cast<char *>(str.c_str());
  12. while (*p) *p++ = '*';
  13. }
  14. DynamicJsonBuffer _jsonBuffer;
  15. };
  16. TEST_F(ArduinoStringTests, JsonBuffer_ParseArray) {
  17. String json("[\"hello\"]");
  18. JsonArray &array = _jsonBuffer.parseArray(json);
  19. eraseString(json);
  20. ASSERT_TRUE(array.success());
  21. ASSERT_STREQ("hello", array[0]);
  22. }
  23. TEST_F(ArduinoStringTests, JsonBuffer_ParseObject) {
  24. String json("{\"hello\":\"world\"}");
  25. JsonObject &object = _jsonBuffer.parseObject(json);
  26. eraseString(json);
  27. ASSERT_TRUE(object.success());
  28. ASSERT_STREQ("world", object["hello"]);
  29. }
  30. TEST_F(ArduinoStringTests, JsonObject_Subscript) {
  31. char json[] = "{\"key\":\"value\"}";
  32. JsonObject &object = _jsonBuffer.parseObject(json);
  33. ASSERT_STREQ("value", object[String("key")]);
  34. }
  35. TEST_F(ArduinoStringTests, JsonObject_ConstSubscript) {
  36. char json[] = "{\"key\":\"value\"}";
  37. const JsonObject &object = _jsonBuffer.parseObject(json);
  38. ASSERT_STREQ("value", object[String("key")]);
  39. }
  40. TEST_F(ArduinoStringTests, JsonObject_SetKey) {
  41. JsonObject &object = _jsonBuffer.createObject();
  42. String key("hello");
  43. object.set(key, "world");
  44. eraseString(key);
  45. ASSERT_STREQ("world", object["hello"]);
  46. }
  47. TEST_F(ArduinoStringTests, JsonObject_SetValue) {
  48. JsonObject &object = _jsonBuffer.createObject();
  49. String value("world");
  50. object.set("hello", value);
  51. eraseString(value);
  52. ASSERT_STREQ("world", object["hello"]);
  53. }
  54. TEST_F(ArduinoStringTests, JsonObject_SetKeyValue) {
  55. JsonObject &object = _jsonBuffer.createObject();
  56. String key("hello");
  57. String value("world");
  58. object.set(key, value);
  59. eraseString(key);
  60. eraseString(value);
  61. ASSERT_STREQ("world", object["hello"]);
  62. }
  63. TEST_F(ArduinoStringTests, JsonObject_Get) {
  64. char json[] = "{\"key\":\"value\"}";
  65. const JsonObject &object = _jsonBuffer.parseObject(json);
  66. ASSERT_STREQ("value", object.get(String("key")));
  67. }
  68. TEST_F(ArduinoStringTests, JsonObject_GetT) {
  69. char json[] = "{\"key\":\"value\"}";
  70. const JsonObject &object = _jsonBuffer.parseObject(json);
  71. ASSERT_STREQ("value", object.get<const char *>(String("key")));
  72. }
  73. TEST_F(ArduinoStringTests, JsonObject_IsT) {
  74. char json[] = "{\"key\":\"value\"}";
  75. const JsonObject &object = _jsonBuffer.parseObject(json);
  76. ASSERT_TRUE(object.is<const char *>(String("key")));
  77. }
  78. TEST_F(ArduinoStringTests, JsonObject_CreateNestedObject) {
  79. String key = "key";
  80. char json[64];
  81. JsonObject &object = _jsonBuffer.createObject();
  82. object.createNestedObject(key);
  83. eraseString(key);
  84. object.printTo(json, sizeof(json));
  85. ASSERT_STREQ("{\"key\":{}}", json);
  86. }
  87. TEST_F(ArduinoStringTests, JsonObject_CreateNestedArray) {
  88. String key = "key";
  89. char json[64];
  90. JsonObject &object = _jsonBuffer.createObject();
  91. object.createNestedArray(key);
  92. eraseString(key);
  93. object.printTo(json, sizeof(json));
  94. ASSERT_STREQ("{\"key\":[]}", json);
  95. }
  96. TEST_F(ArduinoStringTests, JsonObject_ContainsKey) {
  97. char json[] = "{\"key\":\"value\"}";
  98. const JsonObject &object = _jsonBuffer.parseObject(json);
  99. ASSERT_TRUE(object.containsKey(String("key")));
  100. }
  101. TEST_F(ArduinoStringTests, JsonObject_Remove) {
  102. char json[] = "{\"key\":\"value\"}";
  103. JsonObject &object = _jsonBuffer.parseObject(json);
  104. ASSERT_EQ(1, object.size());
  105. object.remove(String("key"));
  106. ASSERT_EQ(0, object.size());
  107. }
  108. TEST_F(ArduinoStringTests, JsonObjectSubscript_SetKey) {
  109. JsonObject &object = _jsonBuffer.createObject();
  110. String key("hello");
  111. object[key] = "world";
  112. eraseString(key);
  113. ASSERT_STREQ("world", object["hello"]);
  114. }
  115. TEST_F(ArduinoStringTests, JsonObjectSubscript_SetValue) {
  116. JsonObject &object = _jsonBuffer.createObject();
  117. String value("world");
  118. object["hello"] = value;
  119. eraseString(value);
  120. ASSERT_STREQ("world", object["hello"]);
  121. }
  122. TEST_F(ArduinoStringTests, JsonArray_Add) {
  123. JsonArray &array = _jsonBuffer.createArray();
  124. String value("hello");
  125. array.add(value);
  126. eraseString(value);
  127. ASSERT_STREQ("hello", array[0]);
  128. }
  129. TEST_F(ArduinoStringTests, JsonArray_Set) {
  130. JsonArray &array = _jsonBuffer.createArray();
  131. String value("world");
  132. array.add("hello");
  133. array.set(0, value);
  134. eraseString(value);
  135. ASSERT_STREQ("world", array[0]);
  136. }
  137. TEST_F(ArduinoStringTests, JsonArraySubscript) {
  138. JsonArray &array = _jsonBuffer.createArray();
  139. String value("world");
  140. array.add("hello");
  141. array[0] = value;
  142. eraseString(value);
  143. ASSERT_STREQ("world", array[0]);
  144. }
  145. TEST_F(ArduinoStringTests, JsonArray_PrintTo) {
  146. JsonArray &array = _jsonBuffer.createArray();
  147. array.add(4);
  148. array.add(2);
  149. String json;
  150. array.printTo(json);
  151. ASSERT_EQ(String("[4,2]"), json);
  152. }
  153. TEST_F(ArduinoStringTests, JsonArray_PrettyPrintTo) {
  154. JsonArray &array = _jsonBuffer.createArray();
  155. array.add(4);
  156. array.add(2);
  157. String json;
  158. array.prettyPrintTo(json);
  159. ASSERT_EQ(String("[\r\n 4,\r\n 2\r\n]"), json);
  160. }
  161. TEST_F(ArduinoStringTests, JsonObject_PrintTo) {
  162. JsonObject &object = _jsonBuffer.createObject();
  163. object["key"] = "value";
  164. String json;
  165. object.printTo(json);
  166. ASSERT_EQ(String("{\"key\":\"value\"}"), json);
  167. }
  168. TEST_F(ArduinoStringTests, JsonObject_PrettyPrintTo) {
  169. JsonObject &object = _jsonBuffer.createObject();
  170. object["key"] = "value";
  171. String json;
  172. object.prettyPrintTo(json);
  173. ASSERT_EQ(String("{\r\n \"key\": \"value\"\r\n}"), json);
  174. }