[209] | 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 |
|
---|
| 8 | #include <gtest/gtest.h>
|
---|
| 9 | #include <ArduinoJson.h>
|
---|
| 10 |
|
---|
| 11 | class JsonArray_PrintTo_Tests : public testing::Test {
|
---|
| 12 | public:
|
---|
| 13 | JsonArray_PrintTo_Tests() : array(json.createArray()) {}
|
---|
| 14 |
|
---|
| 15 | protected:
|
---|
| 16 | StaticJsonBuffer<JSON_ARRAY_SIZE(2)> json;
|
---|
| 17 | JsonArray &array;
|
---|
| 18 |
|
---|
| 19 | void outputMustBe(const char *expected) {
|
---|
| 20 | size_t actualLen = array.printTo(buffer, sizeof(buffer));
|
---|
| 21 | size_t measuredLen = array.measureLength();
|
---|
| 22 |
|
---|
| 23 | EXPECT_STREQ(expected, buffer);
|
---|
| 24 | EXPECT_EQ(strlen(expected), actualLen);
|
---|
| 25 | EXPECT_EQ(strlen(expected), measuredLen);
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | private:
|
---|
| 29 | char buffer[256];
|
---|
| 30 | };
|
---|
| 31 |
|
---|
| 32 | TEST_F(JsonArray_PrintTo_Tests, Empty) { outputMustBe("[]"); }
|
---|
| 33 |
|
---|
| 34 | TEST_F(JsonArray_PrintTo_Tests, Null) {
|
---|
| 35 | array.add(static_cast<char *>(0));
|
---|
| 36 |
|
---|
| 37 | outputMustBe("[null]");
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | TEST_F(JsonArray_PrintTo_Tests, OneString) {
|
---|
| 41 | array.add("hello");
|
---|
| 42 |
|
---|
| 43 | outputMustBe("[\"hello\"]");
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | TEST_F(JsonArray_PrintTo_Tests, TwoStrings) {
|
---|
| 47 | array.add("hello");
|
---|
| 48 | array.add("world");
|
---|
| 49 |
|
---|
| 50 | outputMustBe("[\"hello\",\"world\"]");
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | TEST_F(JsonArray_PrintTo_Tests, OneStringOverCapacity) {
|
---|
| 54 | array.add("hello");
|
---|
| 55 | array.add("world");
|
---|
| 56 | array.add("lost");
|
---|
| 57 |
|
---|
| 58 | outputMustBe("[\"hello\",\"world\"]");
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | TEST_F(JsonArray_PrintTo_Tests, OneDoubleDefaultDigits) {
|
---|
| 62 | array.add(3.14159265358979323846);
|
---|
| 63 | outputMustBe("[3.14]");
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | TEST_F(JsonArray_PrintTo_Tests, OneDoubleFourDigits) {
|
---|
| 67 | array.add(3.14159265358979323846, 4);
|
---|
| 68 | outputMustBe("[3.1416]");
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | TEST_F(JsonArray_PrintTo_Tests, OneDoubleFourDigits_AlternativeSyntax) {
|
---|
| 72 | array.add(double_with_n_digits(3.14159265358979323846, 4));
|
---|
| 73 | outputMustBe("[3.1416]");
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | TEST_F(JsonArray_PrintTo_Tests, OneFloatDefaultDigits) {
|
---|
| 77 | array.add(3.14159f);
|
---|
| 78 | outputMustBe("[3.14]");
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | TEST_F(JsonArray_PrintTo_Tests, OneFloatFourDigits) {
|
---|
| 82 | array.add(3.14159f, 4);
|
---|
| 83 | outputMustBe("[3.1416]");
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | TEST_F(JsonArray_PrintTo_Tests, OneInteger) {
|
---|
| 87 | array.add(1);
|
---|
| 88 |
|
---|
| 89 | outputMustBe("[1]");
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | TEST_F(JsonArray_PrintTo_Tests, TwoIntegers) {
|
---|
| 93 | array.add(1);
|
---|
| 94 | array.add(2);
|
---|
| 95 |
|
---|
| 96 | outputMustBe("[1,2]");
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | TEST_F(JsonArray_PrintTo_Tests, OneIntegerOverCapacity) {
|
---|
| 100 | array.add(1);
|
---|
| 101 | array.add(2);
|
---|
| 102 | array.add(3);
|
---|
| 103 |
|
---|
| 104 | outputMustBe("[1,2]");
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | TEST_F(JsonArray_PrintTo_Tests, OneTrue) {
|
---|
| 108 | array.add(true);
|
---|
| 109 |
|
---|
| 110 | outputMustBe("[true]");
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | TEST_F(JsonArray_PrintTo_Tests, OneFalse) {
|
---|
| 114 | array.add(false);
|
---|
| 115 |
|
---|
| 116 | outputMustBe("[false]");
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | TEST_F(JsonArray_PrintTo_Tests, TwoBooleans) {
|
---|
| 120 | array.add(false);
|
---|
| 121 | array.add(true);
|
---|
| 122 |
|
---|
| 123 | outputMustBe("[false,true]");
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | TEST_F(JsonArray_PrintTo_Tests, OneBooleanOverCapacity) {
|
---|
| 127 | array.add(false);
|
---|
| 128 | array.add(true);
|
---|
| 129 | array.add(false);
|
---|
| 130 |
|
---|
| 131 | outputMustBe("[false,true]");
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | TEST_F(JsonArray_PrintTo_Tests, OneEmptyNestedArray) {
|
---|
| 135 | array.createNestedArray();
|
---|
| 136 |
|
---|
| 137 | outputMustBe("[[]]");
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | TEST_F(JsonArray_PrintTo_Tests, OneEmptyNestedHash) {
|
---|
| 141 | array.createNestedObject();
|
---|
| 142 |
|
---|
| 143 | outputMustBe("[{}]");
|
---|
| 144 | }
|
---|