JsonArray_PrintTo_Tests.cpp 2.6 KB

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