forked from bblanchon/ArduinoJson
Changed unit testing framework from Google Test to Catch
This commit is contained in:
@ -6,122 +6,113 @@
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <catch.hpp>
|
||||
#include <string>
|
||||
|
||||
class JsonObject_Set_Tests : public ::testing::Test {
|
||||
public:
|
||||
JsonObject_Set_Tests() : _object(_jsonBuffer.createObject()) {}
|
||||
TEST_CASE("JsonObject::set()") {
|
||||
DynamicJsonBuffer jb;
|
||||
JsonObject& _object = jb.createObject();
|
||||
|
||||
protected:
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonObject& _object;
|
||||
};
|
||||
SECTION("SizeIncreased_WhenValuesAreAdded") {
|
||||
_object.set("hello", 42);
|
||||
REQUIRE(1 == _object.size());
|
||||
}
|
||||
|
||||
#define TEST_(name) TEST_F(JsonObject_Set_Tests, name)
|
||||
SECTION("SizeUntouched_WhenSameValueIsAdded") {
|
||||
_object["hello"] = 1;
|
||||
_object["hello"] = 2;
|
||||
REQUIRE(1 == _object.size());
|
||||
}
|
||||
|
||||
TEST_(SizeIncreased_WhenValuesAreAdded) {
|
||||
_object.set("hello", 42);
|
||||
EXPECT_EQ(1, _object.size());
|
||||
}
|
||||
|
||||
TEST_(SizeUntouched_WhenSameValueIsAdded) {
|
||||
_object["hello"] = 1;
|
||||
_object["hello"] = 2;
|
||||
EXPECT_EQ(1, _object.size());
|
||||
}
|
||||
|
||||
TEST_(StoreInteger) {
|
||||
_object.set("hello", 123);
|
||||
|
||||
EXPECT_EQ(123, _object["hello"].as<int>());
|
||||
EXPECT_TRUE(_object["hello"].is<int>());
|
||||
EXPECT_FALSE(_object["hello"].is<double>());
|
||||
}
|
||||
|
||||
TEST_(StoreDouble) {
|
||||
_object.set("hello", 123.45);
|
||||
|
||||
EXPECT_EQ(123.45, _object["hello"].as<double>());
|
||||
EXPECT_TRUE(_object["hello"].is<double>());
|
||||
EXPECT_FALSE(_object["hello"].is<long>());
|
||||
}
|
||||
|
||||
TEST_(StoreDoubleWithDigits) {
|
||||
_object.set("hello", 123.45, 2);
|
||||
|
||||
EXPECT_EQ(123.45, _object["hello"].as<double>());
|
||||
EXPECT_TRUE(_object["hello"].is<double>());
|
||||
EXPECT_FALSE(_object["hello"].is<long>());
|
||||
}
|
||||
|
||||
TEST_(StoreBoolean) {
|
||||
_object.set("hello", true);
|
||||
|
||||
EXPECT_TRUE(_object["hello"].as<bool>());
|
||||
EXPECT_TRUE(_object["hello"].is<bool>());
|
||||
EXPECT_FALSE(_object["hello"].is<long>());
|
||||
}
|
||||
|
||||
TEST_(StoreString) {
|
||||
_object.set("hello", "h3110");
|
||||
|
||||
EXPECT_STREQ("h3110", _object["hello"].as<const char*>());
|
||||
EXPECT_TRUE(_object["hello"].is<const char*>());
|
||||
EXPECT_FALSE(_object["hello"].is<long>());
|
||||
}
|
||||
|
||||
TEST_(StoreArray) {
|
||||
JsonArray& arr = _jsonBuffer.createArray();
|
||||
|
||||
_object.set("hello", arr);
|
||||
|
||||
EXPECT_EQ(&arr, &_object["hello"].as<JsonArray>());
|
||||
EXPECT_TRUE(_object["hello"].is<JsonArray&>());
|
||||
EXPECT_FALSE(_object["hello"].is<JsonObject&>());
|
||||
}
|
||||
|
||||
TEST_(StoreObject) {
|
||||
JsonObject& obj = _jsonBuffer.createObject();
|
||||
|
||||
_object.set("hello", obj);
|
||||
|
||||
EXPECT_EQ(&obj, &_object["hello"].as<JsonObject>());
|
||||
EXPECT_TRUE(_object["hello"].is<JsonObject&>());
|
||||
EXPECT_FALSE(_object["hello"].is<JsonArray&>());
|
||||
}
|
||||
|
||||
TEST_(StoreArraySubscript) {
|
||||
JsonArray& arr = _jsonBuffer.createArray();
|
||||
arr.add(42);
|
||||
|
||||
_object.set("a", arr[0]);
|
||||
|
||||
EXPECT_EQ(42, _object["a"]);
|
||||
}
|
||||
|
||||
TEST_(StoreObjectSubscript) {
|
||||
JsonObject& obj = _jsonBuffer.createObject();
|
||||
obj.set("x", 42);
|
||||
|
||||
_object.set("a", obj["x"]);
|
||||
|
||||
EXPECT_EQ(42, _object["a"]);
|
||||
}
|
||||
|
||||
TEST_(ShouldReturnTrue_WhenAllocationSucceeds) {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(1) + 15> jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
|
||||
bool result = obj.set(std::string("hello"), std::string("world"));
|
||||
|
||||
ASSERT_TRUE(result);
|
||||
}
|
||||
|
||||
TEST_(ShouldReturnFalse_WhenAllocationFails) {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(1) + 10> jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
|
||||
bool result = obj.set(std::string("hello"), std::string("world"));
|
||||
|
||||
ASSERT_FALSE(result);
|
||||
SECTION("StoreInteger") {
|
||||
_object.set("hello", 123);
|
||||
|
||||
REQUIRE(123 == _object["hello"].as<int>());
|
||||
REQUIRE(_object["hello"].is<int>());
|
||||
REQUIRE_FALSE(_object["hello"].is<double>());
|
||||
}
|
||||
|
||||
SECTION("StoreDouble") {
|
||||
_object.set("hello", 123.45);
|
||||
|
||||
REQUIRE(123.45 == _object["hello"].as<double>());
|
||||
REQUIRE(_object["hello"].is<double>());
|
||||
REQUIRE_FALSE(_object["hello"].is<long>());
|
||||
}
|
||||
|
||||
SECTION("StoreDoubleWithDigits") {
|
||||
_object.set("hello", 123.45, 2);
|
||||
|
||||
REQUIRE(123.45 == _object["hello"].as<double>());
|
||||
REQUIRE(_object["hello"].is<double>());
|
||||
REQUIRE_FALSE(_object["hello"].is<long>());
|
||||
}
|
||||
|
||||
SECTION("StoreBoolean") {
|
||||
_object.set("hello", true);
|
||||
|
||||
REQUIRE(_object["hello"].as<bool>());
|
||||
REQUIRE(_object["hello"].is<bool>());
|
||||
REQUIRE_FALSE(_object["hello"].is<long>());
|
||||
}
|
||||
|
||||
SECTION("StoreString") {
|
||||
_object.set("hello", "h3110");
|
||||
|
||||
REQUIRE(std::string("h3110") == _object["hello"].as<const char*>());
|
||||
REQUIRE(_object["hello"].is<const char*>());
|
||||
REQUIRE_FALSE(_object["hello"].is<long>());
|
||||
}
|
||||
|
||||
SECTION("StoreArray") {
|
||||
JsonArray& arr = jb.createArray();
|
||||
|
||||
_object.set("hello", arr);
|
||||
|
||||
REQUIRE(&arr == &_object["hello"].as<JsonArray>());
|
||||
REQUIRE(_object["hello"].is<JsonArray&>());
|
||||
REQUIRE_FALSE(_object["hello"].is<JsonObject&>());
|
||||
}
|
||||
|
||||
SECTION("StoreObject") {
|
||||
JsonObject& obj = jb.createObject();
|
||||
|
||||
_object.set("hello", obj);
|
||||
|
||||
REQUIRE(&obj == &_object["hello"].as<JsonObject>());
|
||||
REQUIRE(_object["hello"].is<JsonObject&>());
|
||||
REQUIRE_FALSE(_object["hello"].is<JsonArray&>());
|
||||
}
|
||||
|
||||
SECTION("StoreArraySubscript") {
|
||||
JsonArray& arr = jb.createArray();
|
||||
arr.add(42);
|
||||
|
||||
_object.set("a", arr[0]);
|
||||
|
||||
REQUIRE(42 == _object["a"]);
|
||||
}
|
||||
|
||||
SECTION("StoreObjectSubscript") {
|
||||
JsonObject& obj = jb.createObject();
|
||||
obj.set("x", 42);
|
||||
|
||||
_object.set("a", obj["x"]);
|
||||
|
||||
REQUIRE(42 == _object["a"]);
|
||||
}
|
||||
|
||||
SECTION("ShouldReturnTrue_WhenAllocationSucceeds") {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(1) + 15> jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
|
||||
REQUIRE(true == obj.set(std::string("hello"), std::string("world")));
|
||||
}
|
||||
|
||||
SECTION("ShouldReturnFalse_WhenAllocationFails") {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(1) + 10> jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
|
||||
REQUIRE(false == obj.set(std::string("hello"), std::string("world")));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user