mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-15 19:42:12 +02:00
Changed unit testing framework from Google Test to Catch
This commit is contained in:
@ -6,118 +6,114 @@
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <catch.hpp>
|
||||
#include <string>
|
||||
|
||||
class JsonObject_PrintTo_Tests : public testing::Test {
|
||||
public:
|
||||
JsonObject_PrintTo_Tests() : _object(_jsonBuffer.createObject()) {}
|
||||
void check(const JsonObject &obj, const std::string &expected) {
|
||||
char actual[256];
|
||||
size_t actualLen = obj.printTo(actual);
|
||||
size_t measuredLen = obj.measureLength();
|
||||
|
||||
protected:
|
||||
void outputMustBe(const char *expected) {
|
||||
char actual[256];
|
||||
size_t actualLen = _object.printTo(actual);
|
||||
size_t measuredLen = _object.measureLength();
|
||||
REQUIRE(expected == actual);
|
||||
REQUIRE(expected.size() == actualLen);
|
||||
REQUIRE(expected.size() == measuredLen);
|
||||
}
|
||||
TEST_CASE("JsonObject::printTo()") {
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonObject &obj = _jsonBuffer.createObject();
|
||||
|
||||
EXPECT_STREQ(expected, actual);
|
||||
EXPECT_EQ(strlen(expected), actualLen);
|
||||
EXPECT_EQ(strlen(expected), measuredLen);
|
||||
SECTION("EmptyObject") {
|
||||
check(obj, "{}");
|
||||
}
|
||||
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonObject &_object;
|
||||
};
|
||||
SECTION("TwoStrings") {
|
||||
obj["key1"] = "value1";
|
||||
obj.set("key2", "value2");
|
||||
|
||||
TEST_F(JsonObject_PrintTo_Tests, EmptyObject) {
|
||||
outputMustBe("{}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_PrintTo_Tests, TwoStrings) {
|
||||
_object["key1"] = "value1";
|
||||
_object.set("key2", "value2");
|
||||
|
||||
outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_PrintTo_Tests, RemoveFirst) {
|
||||
_object["key1"] = "value1";
|
||||
_object["key2"] = "value2";
|
||||
_object.remove("key1");
|
||||
|
||||
outputMustBe("{\"key2\":\"value2\"}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_PrintTo_Tests, RemoveLast) {
|
||||
_object["key1"] = "value1";
|
||||
_object["key2"] = "value2";
|
||||
_object.remove("key2");
|
||||
|
||||
outputMustBe("{\"key1\":\"value1\"}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_PrintTo_Tests, RemoveUnexistingKey) {
|
||||
_object["key1"] = "value1";
|
||||
_object["key2"] = "value2";
|
||||
_object.remove("key3");
|
||||
|
||||
outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_PrintTo_Tests, ReplaceExistingKey) {
|
||||
_object["key"] = "value1";
|
||||
_object["key"] = "value2";
|
||||
|
||||
outputMustBe("{\"key\":\"value2\"}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_PrintTo_Tests, TwoIntegers) {
|
||||
_object["a"] = 1;
|
||||
_object.set("b", 2);
|
||||
outputMustBe("{\"a\":1,\"b\":2}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_PrintTo_Tests, RawJson) {
|
||||
_object["a"] = RawJson("[1,2]");
|
||||
_object.set("b", RawJson("[4,5]"));
|
||||
outputMustBe("{\"a\":[1,2],\"b\":[4,5]}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_PrintTo_Tests, TwoDoublesFourDigits) {
|
||||
_object["a"] = double_with_n_digits(3.14159265358979323846, 4);
|
||||
_object.set("b", 2.71828182845904523536, 4);
|
||||
_object.set("c", double_with_n_digits(3.14159265358979323846, 3));
|
||||
outputMustBe("{\"a\":3.1416,\"b\":2.7183,\"c\":3.142}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_PrintTo_Tests, TwoDoubleDefaultDigits) {
|
||||
_object["a"] = 3.14159265358979323846;
|
||||
_object.set("b", 2.71828182845904523536);
|
||||
outputMustBe("{\"a\":3.14,\"b\":2.72}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_PrintTo_Tests, TwoNull) {
|
||||
_object["a"] = static_cast<char *>(0);
|
||||
_object.set("b", static_cast<char *>(0));
|
||||
outputMustBe("{\"a\":null,\"b\":null}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_PrintTo_Tests, TwoBooleans) {
|
||||
_object["a"] = true;
|
||||
_object.set("b", false);
|
||||
outputMustBe("{\"a\":true,\"b\":false}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_PrintTo_Tests, ThreeNestedArrays) {
|
||||
_object.createNestedArray("a");
|
||||
_object["b"] = _jsonBuffer.createArray();
|
||||
_object.set("c", _jsonBuffer.createArray());
|
||||
|
||||
outputMustBe("{\"a\":[],\"b\":[],\"c\":[]}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_PrintTo_Tests, ThreeNestedObjects) {
|
||||
_object.createNestedObject("a");
|
||||
_object["b"] = _jsonBuffer.createObject();
|
||||
_object.set("c", _jsonBuffer.createObject());
|
||||
|
||||
outputMustBe("{\"a\":{},\"b\":{},\"c\":{}}");
|
||||
check(obj, "{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
||||
}
|
||||
|
||||
SECTION("RemoveFirst") {
|
||||
obj["key1"] = "value1";
|
||||
obj["key2"] = "value2";
|
||||
obj.remove("key1");
|
||||
|
||||
check(obj, "{\"key2\":\"value2\"}");
|
||||
}
|
||||
|
||||
SECTION("RemoveLast") {
|
||||
obj["key1"] = "value1";
|
||||
obj["key2"] = "value2";
|
||||
obj.remove("key2");
|
||||
|
||||
check(obj, "{\"key1\":\"value1\"}");
|
||||
}
|
||||
|
||||
SECTION("RemoveUnexistingKey") {
|
||||
obj["key1"] = "value1";
|
||||
obj["key2"] = "value2";
|
||||
obj.remove("key3");
|
||||
|
||||
check(obj, "{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
||||
}
|
||||
|
||||
SECTION("ReplaceExistingKey") {
|
||||
obj["key"] = "value1";
|
||||
obj["key"] = "value2";
|
||||
|
||||
check(obj, "{\"key\":\"value2\"}");
|
||||
}
|
||||
|
||||
SECTION("TwoIntegers") {
|
||||
obj["a"] = 1;
|
||||
obj.set("b", 2);
|
||||
check(obj, "{\"a\":1,\"b\":2}");
|
||||
}
|
||||
|
||||
SECTION("RawJson") {
|
||||
obj["a"] = RawJson("[1,2]");
|
||||
obj.set("b", RawJson("[4,5]"));
|
||||
check(obj, "{\"a\":[1,2],\"b\":[4,5]}");
|
||||
}
|
||||
|
||||
SECTION("TwoDoublesFourDigits") {
|
||||
obj["a"] = double_with_n_digits(3.14159265358979323846, 4);
|
||||
obj.set("b", 2.71828182845904523536, 4);
|
||||
obj.set("c", double_with_n_digits(3.14159265358979323846, 3));
|
||||
check(obj, "{\"a\":3.1416,\"b\":2.7183,\"c\":3.142}");
|
||||
}
|
||||
|
||||
SECTION("TwoDoubleDefaultDigits") {
|
||||
obj["a"] = 3.14159265358979323846;
|
||||
obj.set("b", 2.71828182845904523536);
|
||||
check(obj, "{\"a\":3.14,\"b\":2.72}");
|
||||
}
|
||||
|
||||
SECTION("TwoNull") {
|
||||
obj["a"] = static_cast<char *>(0);
|
||||
obj.set("b", static_cast<char *>(0));
|
||||
check(obj, "{\"a\":null,\"b\":null}");
|
||||
}
|
||||
|
||||
SECTION("TwoBooleans") {
|
||||
obj["a"] = true;
|
||||
obj.set("b", false);
|
||||
check(obj, "{\"a\":true,\"b\":false}");
|
||||
}
|
||||
|
||||
SECTION("ThreeNestedArrays") {
|
||||
obj.createNestedArray("a");
|
||||
obj["b"] = _jsonBuffer.createArray();
|
||||
obj.set("c", _jsonBuffer.createArray());
|
||||
|
||||
check(obj, "{\"a\":[],\"b\":[],\"c\":[]}");
|
||||
}
|
||||
|
||||
SECTION("ThreeNestedObjects") {
|
||||
obj.createNestedObject("a");
|
||||
obj["b"] = _jsonBuffer.createObject();
|
||||
obj.set("c", _jsonBuffer.createObject());
|
||||
|
||||
check(obj, "{\"a\":{},\"b\":{},\"c\":{}}");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user