JsonArray_PrintTo_Tests.cpp 2.5 KB

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