JsonArray_PrintTo_Tests.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright Benoit Blanchon 2014
  2. // MIT License
  3. //
  4. // Arduino JSON library
  5. // https://github.com/bblanchon/ArduinoJson
  6. #include <gtest/gtest.h>
  7. #include <ArduinoJson/JsonArray.hpp>
  8. #include <ArduinoJson/JsonObject.hpp>
  9. #include <ArduinoJson/StaticJsonBuffer.hpp>
  10. using namespace ArduinoJson;
  11. class JsonArray_PrintTo_Tests : public testing::Test {
  12. public:
  13. JsonArray_PrintTo_Tests() : array(json.createArray()) {}
  14. protected:
  15. StaticJsonBuffer<JSON_ARRAY_SIZE(2)> json;
  16. JsonArray &array;
  17. void outputMustBe(const char *expected) {
  18. size_t n = array.printTo(buffer, sizeof(buffer));
  19. EXPECT_STREQ(expected, buffer);
  20. EXPECT_EQ(strlen(expected), n);
  21. }
  22. private:
  23. char buffer[256];
  24. };
  25. TEST_F(JsonArray_PrintTo_Tests, Empty) { outputMustBe("[]"); }
  26. TEST_F(JsonArray_PrintTo_Tests, Null) {
  27. array.add(static_cast<char *>(0));
  28. outputMustBe("[null]");
  29. }
  30. TEST_F(JsonArray_PrintTo_Tests, OneString) {
  31. array.add("hello");
  32. outputMustBe("[\"hello\"]");
  33. }
  34. TEST_F(JsonArray_PrintTo_Tests, TwoStrings) {
  35. array.add("hello");
  36. array.add("world");
  37. outputMustBe("[\"hello\",\"world\"]");
  38. }
  39. TEST_F(JsonArray_PrintTo_Tests, OneStringOverCapacity) {
  40. array.add("hello");
  41. array.add("world");
  42. array.add("lost");
  43. outputMustBe("[\"hello\",\"world\"]");
  44. }
  45. TEST_F(JsonArray_PrintTo_Tests, OneDoubleDefaultDigits) {
  46. array.add(3.14159265358979323846);
  47. outputMustBe("[3.14]");
  48. }
  49. TEST_F(JsonArray_PrintTo_Tests, OneDoubleFourDigits) {
  50. array.add(3.14159265358979323846, 4);
  51. outputMustBe("[3.1416]");
  52. }
  53. TEST_F(JsonArray_PrintTo_Tests, OneInteger) {
  54. array.add(1);
  55. outputMustBe("[1]");
  56. }
  57. TEST_F(JsonArray_PrintTo_Tests, TwoIntegers) {
  58. array.add(1);
  59. array.add(2);
  60. outputMustBe("[1,2]");
  61. }
  62. TEST_F(JsonArray_PrintTo_Tests, OneIntegerOverCapacity) {
  63. array.add(1);
  64. array.add(2);
  65. array.add(3);
  66. outputMustBe("[1,2]");
  67. }
  68. TEST_F(JsonArray_PrintTo_Tests, OneTrue) {
  69. array.add(true);
  70. outputMustBe("[true]");
  71. }
  72. TEST_F(JsonArray_PrintTo_Tests, OneFalse) {
  73. array.add(false);
  74. outputMustBe("[false]");
  75. }
  76. TEST_F(JsonArray_PrintTo_Tests, TwoBooleans) {
  77. array.add(false);
  78. array.add(true);
  79. outputMustBe("[false,true]");
  80. }
  81. TEST_F(JsonArray_PrintTo_Tests, OneBooleanOverCapacity) {
  82. array.add(false);
  83. array.add(true);
  84. array.add(false);
  85. outputMustBe("[false,true]");
  86. }
  87. TEST_F(JsonArray_PrintTo_Tests, OneEmptyNestedArray) {
  88. array.createNestedArray();
  89. outputMustBe("[[]]");
  90. }
  91. TEST_F(JsonArray_PrintTo_Tests, OneEmptyNestedHash) {
  92. array.createNestedObject();
  93. outputMustBe("[{}]");
  94. }