JsonArray_PrintTo_Tests.cpp 2.4 KB

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