printTo.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright Benoit Blanchon 2014-2017
  2. // MIT License
  3. //
  4. // Arduino JSON library
  5. // https://bblanchon.github.io/ArduinoJson/
  6. // If you like this project, please add a star!
  7. #include <ArduinoJson.h>
  8. #include <gtest/gtest.h>
  9. class JsonArray_PrintTo_Tests : public testing::Test {
  10. public:
  11. JsonArray_PrintTo_Tests() : array(json.createArray()) {}
  12. protected:
  13. StaticJsonBuffer<JSON_ARRAY_SIZE(2)> json;
  14. JsonArray &array;
  15. void outputMustBe(const char *expected) {
  16. size_t actualLen = array.printTo(buffer);
  17. size_t measuredLen = array.measureLength();
  18. EXPECT_STREQ(expected, buffer);
  19. EXPECT_EQ(strlen(expected), actualLen);
  20. EXPECT_EQ(strlen(expected), measuredLen);
  21. }
  22. private:
  23. char buffer[256];
  24. };
  25. TEST_F(JsonArray_PrintTo_Tests, Empty) {
  26. outputMustBe("[]");
  27. }
  28. TEST_F(JsonArray_PrintTo_Tests, Null) {
  29. array.add(static_cast<char *>(0));
  30. outputMustBe("[null]");
  31. }
  32. TEST_F(JsonArray_PrintTo_Tests, OneString) {
  33. array.add("hello");
  34. outputMustBe("[\"hello\"]");
  35. }
  36. TEST_F(JsonArray_PrintTo_Tests, TwoStrings) {
  37. array.add("hello");
  38. array.add("world");
  39. outputMustBe("[\"hello\",\"world\"]");
  40. }
  41. TEST_F(JsonArray_PrintTo_Tests, OneStringOverCapacity) {
  42. array.add("hello");
  43. array.add("world");
  44. array.add("lost");
  45. outputMustBe("[\"hello\",\"world\"]");
  46. }
  47. TEST_F(JsonArray_PrintTo_Tests, OneDoubleDefaultDigits) {
  48. array.add(3.14159265358979323846);
  49. outputMustBe("[3.14]");
  50. }
  51. TEST_F(JsonArray_PrintTo_Tests, OneDoubleFourDigits) {
  52. array.add(3.14159265358979323846, 4);
  53. outputMustBe("[3.1416]");
  54. }
  55. TEST_F(JsonArray_PrintTo_Tests, OneDoubleFourDigits_AlternativeSyntax) {
  56. array.add(double_with_n_digits(3.14159265358979323846, 4));
  57. outputMustBe("[3.1416]");
  58. }
  59. TEST_F(JsonArray_PrintTo_Tests, OneFloatDefaultDigits) {
  60. array.add(3.14159f);
  61. outputMustBe("[3.14]");
  62. }
  63. TEST_F(JsonArray_PrintTo_Tests, OneFloatFourDigits) {
  64. array.add(3.14159f, 4);
  65. outputMustBe("[3.1416]");
  66. }
  67. TEST_F(JsonArray_PrintTo_Tests, OneInteger) {
  68. array.add(1);
  69. outputMustBe("[1]");
  70. }
  71. TEST_F(JsonArray_PrintTo_Tests, TwoIntegers) {
  72. array.add(1);
  73. array.add(2);
  74. outputMustBe("[1,2]");
  75. }
  76. TEST_F(JsonArray_PrintTo_Tests, RawJson) {
  77. array.add(RawJson("{\"key\":\"value\"}"));
  78. outputMustBe("[{\"key\":\"value\"}]");
  79. }
  80. TEST_F(JsonArray_PrintTo_Tests, OneIntegerOverCapacity) {
  81. array.add(1);
  82. array.add(2);
  83. array.add(3);
  84. outputMustBe("[1,2]");
  85. }
  86. TEST_F(JsonArray_PrintTo_Tests, OneTrue) {
  87. array.add(true);
  88. outputMustBe("[true]");
  89. }
  90. TEST_F(JsonArray_PrintTo_Tests, OneFalse) {
  91. array.add(false);
  92. outputMustBe("[false]");
  93. }
  94. TEST_F(JsonArray_PrintTo_Tests, TwoBooleans) {
  95. array.add(false);
  96. array.add(true);
  97. outputMustBe("[false,true]");
  98. }
  99. TEST_F(JsonArray_PrintTo_Tests, OneBooleanOverCapacity) {
  100. array.add(false);
  101. array.add(true);
  102. array.add(false);
  103. outputMustBe("[false,true]");
  104. }
  105. TEST_F(JsonArray_PrintTo_Tests, OneEmptyNestedArray) {
  106. array.createNestedArray();
  107. outputMustBe("[[]]");
  108. }
  109. TEST_F(JsonArray_PrintTo_Tests, OneEmptyNestedHash) {
  110. array.createNestedObject();
  111. outputMustBe("[{}]");
  112. }