Files
ArduinoJson/test/JsonObject_PrintTo_Tests.cpp

140 lines
3.2 KiB
C++
Raw Normal View History

2015-02-07 16:05:48 +01:00
// Copyright Benoit Blanchon 2014-2015
2014-10-23 23:39:22 +02:00
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
2014-10-16 16:23:24 +02:00
#include <gtest/gtest.h>
#include <ArduinoJson.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:
2014-10-30 21:51:59 +01:00
JsonObject_PrintTo_Tests() : object(json.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];
int result = object.printTo(actual, sizeof(actual));
EXPECT_STREQ(expected, actual);
EXPECT_EQ(strlen(expected), result);
}
2014-10-30 21:51:59 +01:00
StaticJsonBuffer<JSON_OBJECT_SIZE(2)> json;
2014-10-30 14:03:33 +01:00
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, OneString) {
2014-10-23 19:54:00 +02:00
object["key"] = "value";
2014-10-16 16:23:24 +02:00
2014-10-23 19:54:00 +02:00
outputMustBe("{\"key\":\"value\"}");
2014-10-16 16:23:24 +02:00
}
2014-10-30 21:51:59 +01:00
TEST_F(JsonObject_PrintTo_Tests, TwoStrings) {
2014-10-23 19:54:00 +02:00
object["key1"] = "value1";
object["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) {
2014-10-23 19:54:00 +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) {
2014-10-23 19:54:00 +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) {
2014-10-23 19:54:00 +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) {
2014-10-23 19:54:00 +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
}
2014-10-30 21:51:59 +01:00
TEST_F(JsonObject_PrintTo_Tests, OneStringOverCapacity) {
2014-10-23 19:54:00 +02:00
object["key1"] = "value1";
object["key2"] = "value2";
object["key3"] = "value3";
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, OneInteger) {
2014-10-23 19:54:00 +02:00
object["key"] = 1;
outputMustBe("{\"key\":1}");
2014-10-16 16:23:24 +02:00
}
2014-10-30 21:51:59 +01:00
TEST_F(JsonObject_PrintTo_Tests, OneDoubleFourDigits) {
2014-10-23 19:54:00 +02:00
object["key"].set(3.14159265358979323846, 4);
outputMustBe("{\"key\":3.1416}");
2014-10-16 16:23:24 +02:00
}
2014-10-30 21:51:59 +01:00
TEST_F(JsonObject_PrintTo_Tests, OneDoubleDefaultDigits) {
2014-10-23 19:54:00 +02:00
object["key"] = 3.14159265358979323846;
outputMustBe("{\"key\":3.14}");
2014-10-16 16:23:24 +02:00
}
2014-10-30 21:51:59 +01:00
TEST_F(JsonObject_PrintTo_Tests, OneNull) {
2014-10-24 00:08:25 +02:00
object["key"] = static_cast<char *>(0);
2014-10-23 19:54:00 +02:00
outputMustBe("{\"key\":null}");
2014-10-16 16:23:24 +02:00
}
2014-10-30 21:51:59 +01:00
TEST_F(JsonObject_PrintTo_Tests, OneTrue) {
2014-10-23 19:54:00 +02:00
object["key"] = true;
outputMustBe("{\"key\":true}");
2014-10-16 16:23:24 +02:00
}
2014-10-30 21:51:59 +01:00
TEST_F(JsonObject_PrintTo_Tests, OneFalse) {
2014-10-23 19:54:00 +02:00
object["key"] = false;
outputMustBe("{\"key\":false}");
2014-10-16 16:23:24 +02:00
}
2014-10-30 21:51:59 +01:00
TEST_F(JsonObject_PrintTo_Tests, OneEmptyNestedArrayViaProxy) {
2014-10-30 14:03:33 +01:00
JsonArray &nestedArray = json.createArray();
2014-10-16 16:23:24 +02:00
2014-10-23 19:54:00 +02:00
object["key"] = nestedArray;
2014-10-16 16:23:24 +02:00
2014-10-23 19:54:00 +02:00
outputMustBe("{\"key\":[]}");
2014-10-16 16:23:24 +02:00
}
2014-10-30 21:51:59 +01:00
TEST_F(JsonObject_PrintTo_Tests, OneEmptyNestedObjectViaProxy) {
2014-10-30 14:03:33 +01:00
JsonObject &nestedArray = json.createObject();
2014-10-16 16:23:24 +02:00
2014-10-23 19:54:00 +02:00
object["key"] = nestedArray;
2014-10-16 16:23:24 +02:00
2014-10-23 19:54:00 +02:00
outputMustBe("{\"key\":{}}");
2014-10-16 16:23:24 +02:00
}
2014-10-30 21:51:59 +01:00
TEST_F(JsonObject_PrintTo_Tests, OneEmptyNestedObject) {
2014-10-23 19:54:00 +02:00
object.createNestedObject("key");
2014-10-16 16:23:24 +02:00
2014-10-23 19:54:00 +02:00
outputMustBe("{\"key\":{}}");
2014-10-16 16:23:24 +02:00
}
2014-10-30 21:51:59 +01:00
TEST_F(JsonObject_PrintTo_Tests, OneEmptyNestedArray) {
2014-10-23 19:54:00 +02:00
object.createNestedArray("key");
2014-10-16 16:23:24 +02:00
2014-10-23 19:54:00 +02:00
outputMustBe("{\"key\":[]}");
2014-10-23 23:45:36 +02:00
}