2016-01-07 22:35:12 +01:00
|
|
|
// Copyright Benoit Blanchon 2014-2016
|
2014-10-23 23:39:22 +02:00
|
|
|
// MIT License
|
|
|
|
//
|
|
|
|
// Arduino JSON library
|
|
|
|
// https://github.com/bblanchon/ArduinoJson
|
2016-01-07 22:35:12 +01:00
|
|
|
// If you like this project, please add a star!
|
2014-10-23 23:39:22 +02:00
|
|
|
|
2014-11-11 16:54:46 +01:00
|
|
|
#include <ArduinoJson.h>
|
2016-02-14 16:18:13 +01:00
|
|
|
#include <gtest/gtest.h>
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-30 21:51:59 +01:00
|
|
|
using namespace ArduinoJson::Internals;
|
2014-10-18 23:05:54 +02:00
|
|
|
|
2014-10-30 21:51:59 +01:00
|
|
|
class JsonObject_PrintTo_Tests : public testing::Test {
|
2014-10-30 14:03:33 +01:00
|
|
|
public:
|
2015-07-25 15:38:12 +02:00
|
|
|
JsonObject_PrintTo_Tests() : _object(_jsonBuffer.createObject()) {}
|
2014-10-23 19:54:00 +02:00
|
|
|
|
2014-10-30 14:03:33 +01:00
|
|
|
protected:
|
2014-10-23 19:54:00 +02:00
|
|
|
void outputMustBe(const char *expected) {
|
|
|
|
char actual[256];
|
2015-07-25 15:38:12 +02:00
|
|
|
size_t actualLen = _object.printTo(actual, sizeof(actual));
|
|
|
|
size_t measuredLen = _object.measureLength();
|
2014-10-23 19:54:00 +02:00
|
|
|
|
|
|
|
EXPECT_STREQ(expected, actual);
|
2015-05-09 16:53:48 +02:00
|
|
|
EXPECT_EQ(strlen(expected), actualLen);
|
|
|
|
EXPECT_EQ(strlen(expected), measuredLen);
|
2014-10-23 19:54:00 +02:00
|
|
|
}
|
|
|
|
|
2015-07-25 15:38:12 +02:00
|
|
|
DynamicJsonBuffer _jsonBuffer;
|
|
|
|
JsonObject &_object;
|
2014-10-16 16:23:24 +02:00
|
|
|
};
|
|
|
|
|
2014-10-30 21:51:59 +01:00
|
|
|
TEST_F(JsonObject_PrintTo_Tests, EmptyObject) { outputMustBe("{}"); }
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-30 21:51:59 +01:00
|
|
|
TEST_F(JsonObject_PrintTo_Tests, TwoStrings) {
|
2015-07-25 15:38:12 +02:00
|
|
|
_object["key1"] = "value1";
|
|
|
|
_object.set("key2", "value2");
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-30 21:51:59 +01:00
|
|
|
TEST_F(JsonObject_PrintTo_Tests, RemoveFirst) {
|
2015-07-25 15:38:12 +02:00
|
|
|
_object["key1"] = "value1";
|
|
|
|
_object["key2"] = "value2";
|
|
|
|
_object.remove("key1");
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
outputMustBe("{\"key2\":\"value2\"}");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-30 21:51:59 +01:00
|
|
|
TEST_F(JsonObject_PrintTo_Tests, RemoveLast) {
|
2015-07-25 15:38:12 +02:00
|
|
|
_object["key1"] = "value1";
|
|
|
|
_object["key2"] = "value2";
|
|
|
|
_object.remove("key2");
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
outputMustBe("{\"key1\":\"value1\"}");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-30 21:51:59 +01:00
|
|
|
TEST_F(JsonObject_PrintTo_Tests, RemoveUnexistingKey) {
|
2015-07-25 15:38:12 +02:00
|
|
|
_object["key1"] = "value1";
|
|
|
|
_object["key2"] = "value2";
|
|
|
|
_object.remove("key3");
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-30 21:51:59 +01:00
|
|
|
TEST_F(JsonObject_PrintTo_Tests, ReplaceExistingKey) {
|
2015-07-25 15:38:12 +02:00
|
|
|
_object["key"] = "value1";
|
|
|
|
_object["key"] = "value2";
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
outputMustBe("{\"key\":\"value2\"}");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2015-07-25 15:38:12 +02:00
|
|
|
TEST_F(JsonObject_PrintTo_Tests, TwoIntegers) {
|
|
|
|
_object["a"] = 1;
|
|
|
|
_object.set("b", 2);
|
|
|
|
outputMustBe("{\"a\":1,\"b\":2}");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2015-07-25 15:38:12 +02:00
|
|
|
TEST_F(JsonObject_PrintTo_Tests, TwoDoublesFourDigits) {
|
|
|
|
_object["a"] = double_with_n_digits(3.14159265358979323846, 4);
|
|
|
|
_object.set("b", 2.71828182845904523536, 4);
|
2016-02-14 16:18:13 +01:00
|
|
|
_object.set("c", double_with_n_digits(3.14159265358979323846, 3));
|
|
|
|
outputMustBe("{\"a\":3.1416,\"b\":2.7183,\"c\":3.142}");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2015-07-25 15:38:12 +02:00
|
|
|
TEST_F(JsonObject_PrintTo_Tests, TwoDoubleDefaultDigits) {
|
|
|
|
_object["a"] = 3.14159265358979323846;
|
|
|
|
_object.set("b", 2.71828182845904523536);
|
|
|
|
outputMustBe("{\"a\":3.14,\"b\":2.72}");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2015-07-25 15:38:12 +02:00
|
|
|
TEST_F(JsonObject_PrintTo_Tests, TwoNull) {
|
|
|
|
_object["a"] = static_cast<char *>(0);
|
|
|
|
_object.set("b", static_cast<char *>(0));
|
|
|
|
outputMustBe("{\"a\":null,\"b\":null}");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2015-07-25 15:38:12 +02:00
|
|
|
TEST_F(JsonObject_PrintTo_Tests, TwoBooleans) {
|
|
|
|
_object["a"] = true;
|
|
|
|
_object.set("b", false);
|
|
|
|
outputMustBe("{\"a\":true,\"b\":false}");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2015-07-25 15:38:12 +02:00
|
|
|
TEST_F(JsonObject_PrintTo_Tests, ThreeNestedArrays) {
|
|
|
|
_object.createNestedArray("a");
|
|
|
|
_object["b"] = _jsonBuffer.createArray();
|
|
|
|
_object.set("c", _jsonBuffer.createArray());
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2015-07-25 15:38:12 +02:00
|
|
|
outputMustBe("{\"a\":[],\"b\":[],\"c\":[]}");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2015-07-25 15:38:12 +02:00
|
|
|
TEST_F(JsonObject_PrintTo_Tests, ThreeNestedObjects) {
|
|
|
|
_object.createNestedObject("a");
|
|
|
|
_object["b"] = _jsonBuffer.createObject();
|
|
|
|
_object.set("c", _jsonBuffer.createObject());
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2015-07-25 15:38:12 +02:00
|
|
|
outputMustBe("{\"a\":{},\"b\":{},\"c\":{}}");
|
2014-10-23 23:45:36 +02:00
|
|
|
}
|