JsonArray_PrintTo_Tests.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright Benoit Blanchon 2014-2016
  2. // MIT License
  3. //
  4. // Arduino JSON library
  5. // https://github.com/bblanchon/ArduinoJson
  6. // If you like this project, please add a star!
  7. #include <gtest/gtest.h>
  8. #include <ArduinoJson.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, sizeof(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) { outputMustBe("[]"); }
  26. TEST_F(JsonArray_PrintTo_Tests, Null) {
  27. array.add(static_cast<char *>(0));
  28. outputMustBe("[null]");
  29. }
  30. TEST_F(JsonArray_PrintTo_Tests, OneString) {
  31. array.add("hello");
  32. outputMustBe("[\"hello\"]");
  33. }
  34. TEST_F(JsonArray_PrintTo_Tests, TwoStrings) {
  35. array.add("hello");
  36. array.add("world");
  37. outputMustBe("[\"hello\",\"world\"]");
  38. }
  39. TEST_F(JsonArray_PrintTo_Tests, OneStringOverCapacity) {
  40. array.add("hello");
  41. array.add("world");
  42. array.add("lost");
  43. outputMustBe("[\"hello\",\"world\"]");
  44. }
  45. TEST_F(JsonArray_PrintTo_Tests, OneDoubleDefaultDigits) {
  46. array.add(3.14159265358979323846);
  47. outputMustBe("[3.14]");
  48. }
  49. TEST_F(JsonArray_PrintTo_Tests, OneDoubleFourDigits) {
  50. array.add(3.14159265358979323846, 4);
  51. outputMustBe("[3.1416]");
  52. }
  53. TEST_F(JsonArray_PrintTo_Tests, OneDoubleFourDigits_AlternativeSyntax) {
  54. array.add(double_with_n_digits(3.14159265358979323846, 4));
  55. outputMustBe("[3.1416]");
  56. }
  57. TEST_F(JsonArray_PrintTo_Tests, OneFloatDefaultDigits) {
  58. array.add(3.14159f);
  59. outputMustBe("[3.14]");
  60. }
  61. TEST_F(JsonArray_PrintTo_Tests, OneFloatFourDigits) {
  62. array.add(3.14159f, 4);
  63. outputMustBe("[3.1416]");
  64. }
  65. TEST_F(JsonArray_PrintTo_Tests, OneInteger) {
  66. array.add(1);
  67. outputMustBe("[1]");
  68. }
  69. TEST_F(JsonArray_PrintTo_Tests, TwoIntegers) {
  70. array.add(1);
  71. array.add(2);
  72. outputMustBe("[1,2]");
  73. }
  74. TEST_F(JsonArray_PrintTo_Tests, RawJson) {
  75. array.add(RawJson("{\"key\":\"value\"}"));
  76. outputMustBe("[{\"key\":\"value\"}]");
  77. }
  78. TEST_F(JsonArray_PrintTo_Tests, OneIntegerOverCapacity) {
  79. array.add(1);
  80. array.add(2);
  81. array.add(3);
  82. outputMustBe("[1,2]");
  83. }
  84. TEST_F(JsonArray_PrintTo_Tests, OneTrue) {
  85. array.add(true);
  86. outputMustBe("[true]");
  87. }
  88. TEST_F(JsonArray_PrintTo_Tests, OneFalse) {
  89. array.add(false);
  90. outputMustBe("[false]");
  91. }
  92. TEST_F(JsonArray_PrintTo_Tests, TwoBooleans) {
  93. array.add(false);
  94. array.add(true);
  95. outputMustBe("[false,true]");
  96. }
  97. TEST_F(JsonArray_PrintTo_Tests, OneBooleanOverCapacity) {
  98. array.add(false);
  99. array.add(true);
  100. array.add(false);
  101. outputMustBe("[false,true]");
  102. }
  103. TEST_F(JsonArray_PrintTo_Tests, OneEmptyNestedArray) {
  104. array.createNestedArray();
  105. outputMustBe("[[]]");
  106. }
  107. TEST_F(JsonArray_PrintTo_Tests, OneEmptyNestedHash) {
  108. array.createNestedObject();
  109. outputMustBe("[{}]");
  110. }