JsonArray_PrintTo_Tests.cpp 2.4 KB

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