[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 <ArduinoJson.h>
|
---|
| 9 | #include <gtest/gtest.h>
|
---|
| 10 |
|
---|
| 11 | using namespace ArduinoJson::Internals;
|
---|
| 12 |
|
---|
| 13 | class JsonObject_PrintTo_Tests : public testing::Test {
|
---|
| 14 | public:
|
---|
| 15 | JsonObject_PrintTo_Tests() : _object(_jsonBuffer.createObject()) {}
|
---|
| 16 |
|
---|
| 17 | protected:
|
---|
| 18 | void outputMustBe(const char *expected) {
|
---|
| 19 | char actual[256];
|
---|
| 20 | size_t actualLen = _object.printTo(actual, sizeof(actual));
|
---|
| 21 | size_t measuredLen = _object.measureLength();
|
---|
| 22 |
|
---|
| 23 | EXPECT_STREQ(expected, actual);
|
---|
| 24 | EXPECT_EQ(strlen(expected), actualLen);
|
---|
| 25 | EXPECT_EQ(strlen(expected), measuredLen);
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | DynamicJsonBuffer _jsonBuffer;
|
---|
| 29 | JsonObject &_object;
|
---|
| 30 | };
|
---|
| 31 |
|
---|
| 32 | TEST_F(JsonObject_PrintTo_Tests, EmptyObject) { outputMustBe("{}"); }
|
---|
| 33 |
|
---|
| 34 | TEST_F(JsonObject_PrintTo_Tests, TwoStrings) {
|
---|
| 35 | _object["key1"] = "value1";
|
---|
| 36 | _object.set("key2", "value2");
|
---|
| 37 |
|
---|
| 38 | outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | TEST_F(JsonObject_PrintTo_Tests, RemoveFirst) {
|
---|
| 42 | _object["key1"] = "value1";
|
---|
| 43 | _object["key2"] = "value2";
|
---|
| 44 | _object.remove("key1");
|
---|
| 45 |
|
---|
| 46 | outputMustBe("{\"key2\":\"value2\"}");
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | TEST_F(JsonObject_PrintTo_Tests, RemoveLast) {
|
---|
| 50 | _object["key1"] = "value1";
|
---|
| 51 | _object["key2"] = "value2";
|
---|
| 52 | _object.remove("key2");
|
---|
| 53 |
|
---|
| 54 | outputMustBe("{\"key1\":\"value1\"}");
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | TEST_F(JsonObject_PrintTo_Tests, RemoveUnexistingKey) {
|
---|
| 58 | _object["key1"] = "value1";
|
---|
| 59 | _object["key2"] = "value2";
|
---|
| 60 | _object.remove("key3");
|
---|
| 61 |
|
---|
| 62 | outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | TEST_F(JsonObject_PrintTo_Tests, ReplaceExistingKey) {
|
---|
| 66 | _object["key"] = "value1";
|
---|
| 67 | _object["key"] = "value2";
|
---|
| 68 |
|
---|
| 69 | outputMustBe("{\"key\":\"value2\"}");
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | TEST_F(JsonObject_PrintTo_Tests, TwoIntegers) {
|
---|
| 73 | _object["a"] = 1;
|
---|
| 74 | _object.set("b", 2);
|
---|
| 75 | outputMustBe("{\"a\":1,\"b\":2}");
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | TEST_F(JsonObject_PrintTo_Tests, TwoDoublesFourDigits) {
|
---|
| 79 | _object["a"] = double_with_n_digits(3.14159265358979323846, 4);
|
---|
| 80 | _object.set("b", 2.71828182845904523536, 4);
|
---|
| 81 | _object.set("c", double_with_n_digits(3.14159265358979323846, 3));
|
---|
| 82 | outputMustBe("{\"a\":3.1416,\"b\":2.7183,\"c\":3.142}");
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | TEST_F(JsonObject_PrintTo_Tests, TwoDoubleDefaultDigits) {
|
---|
| 86 | _object["a"] = 3.14159265358979323846;
|
---|
| 87 | _object.set("b", 2.71828182845904523536);
|
---|
| 88 | outputMustBe("{\"a\":3.14,\"b\":2.72}");
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | TEST_F(JsonObject_PrintTo_Tests, TwoNull) {
|
---|
| 92 | _object["a"] = static_cast<char *>(0);
|
---|
| 93 | _object.set("b", static_cast<char *>(0));
|
---|
| 94 | outputMustBe("{\"a\":null,\"b\":null}");
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | TEST_F(JsonObject_PrintTo_Tests, TwoBooleans) {
|
---|
| 98 | _object["a"] = true;
|
---|
| 99 | _object.set("b", false);
|
---|
| 100 | outputMustBe("{\"a\":true,\"b\":false}");
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | TEST_F(JsonObject_PrintTo_Tests, ThreeNestedArrays) {
|
---|
| 104 | _object.createNestedArray("a");
|
---|
| 105 | _object["b"] = _jsonBuffer.createArray();
|
---|
| 106 | _object.set("c", _jsonBuffer.createArray());
|
---|
| 107 |
|
---|
| 108 | outputMustBe("{\"a\":[],\"b\":[],\"c\":[]}");
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | TEST_F(JsonObject_PrintTo_Tests, ThreeNestedObjects) {
|
---|
| 112 | _object.createNestedObject("a");
|
---|
| 113 | _object["b"] = _jsonBuffer.createObject();
|
---|
| 114 | _object.set("c", _jsonBuffer.createObject());
|
---|
| 115 |
|
---|
| 116 | outputMustBe("{\"a\":{},\"b\":{},\"c\":{}}");
|
---|
| 117 | }
|
---|