ArduinoString_Tests.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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_SetToArraySubscript) {
  64. JsonArray &arr = _jsonBuffer.createArray();
  65. arr.add("world");
  66. JsonObject &object = _jsonBuffer.createObject();
  67. object.set(String("hello"), arr[0]);
  68. ASSERT_STREQ("world", object["hello"]);
  69. }
  70. TEST_F(ArduinoStringTests, JsonObject_SetToObjectSubscript) {
  71. JsonObject &arr = _jsonBuffer.createObject();
  72. arr.set("x", "world");
  73. JsonObject &object = _jsonBuffer.createObject();
  74. object.set(String("hello"), arr["x"]);
  75. ASSERT_STREQ("world", object["hello"]);
  76. }
  77. TEST_F(ArduinoStringTests, JsonObject_Get) {
  78. char json[] = "{\"key\":\"value\"}";
  79. const JsonObject &object = _jsonBuffer.parseObject(json);
  80. ASSERT_STREQ("value", object.get(String("key")));
  81. }
  82. TEST_F(ArduinoStringTests, JsonObject_GetT) {
  83. char json[] = "{\"key\":\"value\"}";
  84. const JsonObject &object = _jsonBuffer.parseObject(json);
  85. ASSERT_STREQ("value", object.get<const char *>(String("key")));
  86. }
  87. TEST_F(ArduinoStringTests, JsonObject_IsT) {
  88. char json[] = "{\"key\":\"value\"}";
  89. const JsonObject &object = _jsonBuffer.parseObject(json);
  90. ASSERT_TRUE(object.is<const char *>(String("key")));
  91. }
  92. TEST_F(ArduinoStringTests, JsonObject_CreateNestedObject) {
  93. String key = "key";
  94. char json[64];
  95. JsonObject &object = _jsonBuffer.createObject();
  96. object.createNestedObject(key);
  97. eraseString(key);
  98. object.printTo(json, sizeof(json));
  99. ASSERT_STREQ("{\"key\":{}}", json);
  100. }
  101. TEST_F(ArduinoStringTests, JsonObject_CreateNestedArray) {
  102. String key = "key";
  103. char json[64];
  104. JsonObject &object = _jsonBuffer.createObject();
  105. object.createNestedArray(key);
  106. eraseString(key);
  107. object.printTo(json, sizeof(json));
  108. ASSERT_STREQ("{\"key\":[]}", json);
  109. }
  110. TEST_F(ArduinoStringTests, JsonObject_ContainsKey) {
  111. char json[] = "{\"key\":\"value\"}";
  112. const JsonObject &object = _jsonBuffer.parseObject(json);
  113. ASSERT_TRUE(object.containsKey(String("key")));
  114. }
  115. TEST_F(ArduinoStringTests, JsonObject_Remove) {
  116. char json[] = "{\"key\":\"value\"}";
  117. JsonObject &object = _jsonBuffer.parseObject(json);
  118. ASSERT_EQ(1, object.size());
  119. object.remove(String("key"));
  120. ASSERT_EQ(0, object.size());
  121. }
  122. TEST_F(ArduinoStringTests, JsonObjectSubscript_SetKey) {
  123. JsonObject &object = _jsonBuffer.createObject();
  124. String key("hello");
  125. object[key] = "world";
  126. eraseString(key);
  127. ASSERT_STREQ("world", object["hello"]);
  128. }
  129. TEST_F(ArduinoStringTests, JsonObjectSubscript_SetValue) {
  130. JsonObject &object = _jsonBuffer.createObject();
  131. String value("world");
  132. object["hello"] = value;
  133. eraseString(value);
  134. ASSERT_STREQ("world", object["hello"]);
  135. }
  136. TEST_F(ArduinoStringTests, JsonArray_Add) {
  137. JsonArray &array = _jsonBuffer.createArray();
  138. String value("hello");
  139. array.add(value);
  140. eraseString(value);
  141. ASSERT_STREQ("hello", array[0]);
  142. }
  143. TEST_F(ArduinoStringTests, JsonArray_Set) {
  144. JsonArray &array = _jsonBuffer.createArray();
  145. String value("world");
  146. array.add("hello");
  147. array.set(0, value);
  148. eraseString(value);
  149. ASSERT_STREQ("world", array[0]);
  150. }
  151. TEST_F(ArduinoStringTests, JsonArraySubscript) {
  152. JsonArray &array = _jsonBuffer.createArray();
  153. String value("world");
  154. array.add("hello");
  155. array[0] = value;
  156. eraseString(value);
  157. ASSERT_STREQ("world", array[0]);
  158. }
  159. TEST_F(ArduinoStringTests, JsonArray_PrintTo) {
  160. JsonArray &array = _jsonBuffer.createArray();
  161. array.add(4);
  162. array.add(2);
  163. String json;
  164. array.printTo(json);
  165. ASSERT_EQ(String("[4,2]"), json);
  166. }
  167. TEST_F(ArduinoStringTests, JsonArray_PrettyPrintTo) {
  168. JsonArray &array = _jsonBuffer.createArray();
  169. array.add(4);
  170. array.add(2);
  171. String json;
  172. array.prettyPrintTo(json);
  173. ASSERT_EQ(String("[\r\n 4,\r\n 2\r\n]"), json);
  174. }
  175. TEST_F(ArduinoStringTests, JsonObject_PrintTo) {
  176. JsonObject &object = _jsonBuffer.createObject();
  177. object["key"] = "value";
  178. String json;
  179. object.printTo(json);
  180. ASSERT_EQ(String("{\"key\":\"value\"}"), json);
  181. }
  182. TEST_F(ArduinoStringTests, JsonObject_PrettyPrintTo) {
  183. JsonObject &object = _jsonBuffer.createObject();
  184. object["key"] = "value";
  185. String json;
  186. object.prettyPrintTo(json);
  187. ASSERT_EQ(String("{\r\n \"key\": \"value\"\r\n}"), json);
  188. }