JsonArray_PrintTo_Tests.cpp 2.5 KB

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