Changed unit testing framework from Google Test to Catch

This commit is contained in:
Benoit Blanchon
2017-04-18 18:22:24 +02:00
parent f2ef338cb8
commit df541a2a22
266 changed files with 15955 additions and 146149 deletions

View File

@ -18,5 +18,5 @@ add_executable(JsonObjectTests
subscript.cpp
)
target_link_libraries(JsonObjectTests gtest)
target_link_libraries(JsonObjectTests catch)
add_test(JsonObject JsonObjectTests)

View File

@ -6,20 +6,17 @@
// If you like this project, please add a star!
#include <ArduinoJson.h>
#include <gtest/gtest.h>
#include <catch.hpp>
#define TEST_(name) TEST(JsonObject_Basic_Tests, name)
TEST_(InitialSizeIsZero) {
TEST_CASE("JsonObject basics") {
DynamicJsonBuffer _jsonBuffer;
JsonObject& _object = _jsonBuffer.createObject();
EXPECT_EQ(0, _object.size());
}
SECTION("InitialSizeIsZero") {
REQUIRE(0 == _object.size());
}
TEST_(SuccessIsTrue) {
DynamicJsonBuffer _jsonBuffer;
JsonObject& _object = _jsonBuffer.createObject();
EXPECT_TRUE(_object.success());
SECTION("SuccessIsTrue") {
REQUIRE(_object.success());
}
}

View File

@ -6,34 +6,28 @@
// If you like this project, please add a star!
#include <ArduinoJson.h>
#include <gtest/gtest.h>
#include <catch.hpp>
#define TEST_(name) TEST(JsonObject_Basic_Tests, name)
TEST_(ContainsKeyReturnsFalseForNonExistingKey) {
TEST_CASE("JsonObject::containsKey()") {
DynamicJsonBuffer _jsonBuffer;
JsonObject& _object = _jsonBuffer.createObject();
_object.set("hello", 42);
SECTION("ContainsKeyReturnsFalseForNonExistingKey") {
_object.set("hello", 42);
EXPECT_FALSE(_object.containsKey("world"));
}
TEST_(ContainsKeyReturnsTrueForDefinedValue) {
DynamicJsonBuffer _jsonBuffer;
JsonObject& _object = _jsonBuffer.createObject();
_object.set("hello", 42);
EXPECT_TRUE(_object.containsKey("hello"));
}
TEST_(ContainsKeyReturnsFalseAfterRemove) {
DynamicJsonBuffer _jsonBuffer;
JsonObject& _object = _jsonBuffer.createObject();
_object.set("hello", 42);
_object.remove("hello");
EXPECT_FALSE(_object.containsKey("hello"));
REQUIRE(false == _object.containsKey("world"));
}
SECTION("ContainsKeyReturnsTrueForDefinedValue") {
_object.set("hello", 42);
REQUIRE(true == _object.containsKey("hello"));
}
SECTION("ContainsKeyReturnsFalseAfterRemove") {
_object.set("hello", 42);
_object.remove("hello");
REQUIRE(false == _object.containsKey("hello"));
}
}

View File

@ -6,21 +6,17 @@
// If you like this project, please add a star!
#include <ArduinoJson.h>
#include <gtest/gtest.h>
#include <catch.hpp>
class JsonObject_Get_Tests : public ::testing::Test {
public:
JsonObject_Get_Tests() : _object(_jsonBuffer.createObject()) {}
using namespace Catch::Matchers;
protected:
DynamicJsonBuffer _jsonBuffer;
JsonObject& _object;
};
TEST_CASE("JsonObject::get()") {
DynamicJsonBuffer jb;
JsonObject& obj = jb.createObject();
#define TEST_(name) TEST_F(JsonObject_Get_Tests, name)
TEST_(GetConstCharPointer_GivenStringLiteral) {
_object.set("hello", "world");
const char* value = _object.get<const char*>("hello");
EXPECT_STREQ("world", value);
SECTION("GetConstCharPointer_GivenStringLiteral") {
obj.set("hello", "world");
const char* value = obj.get<const char*>("hello");
REQUIRE_THAT(value, Equals("world"));
}
}

View File

@ -6,28 +6,33 @@
// If you like this project, please add a star!
#include <ArduinoJson.h>
#include <gtest/gtest.h>
#include <catch.hpp>
TEST(JsonObject_Invalid_Tests, SubscriptFails) {
ASSERT_FALSE(JsonObject::invalid()["key"].success());
}
using namespace Catch::Matchers;
TEST(JsonObject_Invalid_Tests, AddFails) {
JsonObject& object = JsonObject::invalid();
object.set("hello", "world");
ASSERT_EQ(0, object.size());
}
TEST_CASE("JsonObject::invalid()") {
JsonObject& obj = JsonObject::invalid();
TEST(JsonObject_Invalid_Tests, CreateNestedArrayFails) {
ASSERT_FALSE(JsonObject::invalid().createNestedArray("hello").success());
}
SECTION("SubscriptFails") {
REQUIRE_FALSE(obj["key"].success());
}
TEST(JsonObject_Invalid_Tests, CreateNestedObjectFails) {
ASSERT_FALSE(JsonObject::invalid().createNestedObject("world").success());
}
SECTION("AddFails") {
obj.set("hello", "world");
REQUIRE(0 == obj.size());
}
TEST(JsonObject_Invalid_Tests, PrintToWritesBraces) {
char buffer[32];
JsonObject::invalid().printTo(buffer, sizeof(buffer));
ASSERT_STREQ("{}", buffer);
SECTION("CreateNestedArrayFails") {
REQUIRE_FALSE(obj.createNestedArray("hello").success());
}
SECTION("CreateNestedObjectFails") {
REQUIRE_FALSE(obj.createNestedObject("world").success());
}
SECTION("PrintToWritesBraces") {
char buffer[32];
obj.printTo(buffer, sizeof(buffer));
REQUIRE_THAT(buffer, Equals("{}"));
}
}

View File

@ -6,52 +6,49 @@
// If you like this project, please add a star!
#include <ArduinoJson.h>
#include <gtest/gtest.h>
#include <catch.hpp>
class JsonObject_Iterator_Test : public testing::Test {
public:
JsonObject_Iterator_Test() : _object(_buffer.createObject()) {
_object["ab"] = 12;
_object["cd"] = 34;
using namespace Catch::Matchers;
TEST_CASE("JsonObject::begin()/end()") {
StaticJsonBuffer<JSON_OBJECT_SIZE(2)> jb;
JsonObject& obj = jb.createObject();
obj["ab"] = 12;
obj["cd"] = 34;
SECTION("NonConstIterator") {
JsonObject::iterator it = obj.begin();
REQUIRE(obj.end() != it);
REQUIRE_THAT(it->key, Equals("ab"));
REQUIRE(12 == it->value);
it->key = "a.b";
it->value = 1.2;
++it;
REQUIRE(obj.end() != it);
REQUIRE_THAT(it->key, Equals("cd"));
REQUIRE(34 == it->value);
it->key = "c.d";
it->value = 3.4;
++it;
REQUIRE(obj.end() == it);
REQUIRE(2 == obj.size());
REQUIRE(1.2 == obj["a.b"]);
REQUIRE(3.4 == obj["c.d"]);
}
protected:
StaticJsonBuffer<JSON_OBJECT_SIZE(2)> _buffer;
JsonObject& _object;
};
SECTION("ConstIterator") {
const JsonObject& const_object = obj;
JsonObject::const_iterator it = const_object.begin();
TEST_F(JsonObject_Iterator_Test, NonConstIterator) {
JsonObject::iterator it = _object.begin();
ASSERT_NE(_object.end(), it);
EXPECT_STREQ("ab", it->key);
EXPECT_EQ(12, it->value);
it->key = "a.b";
it->value = 1.2;
++it;
ASSERT_NE(_object.end(), it);
EXPECT_STREQ("cd", it->key);
EXPECT_EQ(34, it->value);
it->key = "c.d";
it->value = 3.4;
++it;
ASSERT_EQ(_object.end(), it);
ASSERT_EQ(2, _object.size());
EXPECT_EQ(1.2, _object["a.b"]);
EXPECT_EQ(3.4, _object["c.d"]);
}
TEST_F(JsonObject_Iterator_Test, ConstIterator) {
const JsonObject& const_object = _object;
JsonObject::const_iterator it = const_object.begin();
ASSERT_NE(const_object.end(), it);
EXPECT_STREQ("ab", it->key);
EXPECT_EQ(12, it->value);
++it;
ASSERT_NE(const_object.end(), it);
EXPECT_STREQ("cd", it->key);
EXPECT_EQ(34, it->value);
++it;
ASSERT_EQ(const_object.end(), it);
REQUIRE(const_object.end() != it);
REQUIRE_THAT(it->key, Equals("ab"));
REQUIRE(12 == it->value);
++it;
REQUIRE(const_object.end() != it);
REQUIRE_THAT(it->key, Equals("cd"));
REQUIRE(34 == it->value);
++it;
REQUIRE(const_object.end() == it);
}
}

View File

@ -6,77 +6,74 @@
// If you like this project, please add a star!
#include <ArduinoJson.h>
#include <gtest/gtest.h>
#include <catch.hpp>
#include <string>
class JsonObject_PrettyPrintTo_Tests : public testing::Test {
public:
JsonObject_PrettyPrintTo_Tests() : _object(_jsonBuffer.createObject()) {}
void check(const JsonObject &obj, const std::string expected) {
char json[256];
protected:
DynamicJsonBuffer _jsonBuffer;
JsonObject &_object;
size_t actualLen = obj.prettyPrintTo(json);
size_t measuredLen = obj.measurePrettyLength();
void outputMustBe(const char *expected) {
char buffer[256];
REQUIRE(json == expected);
REQUIRE(expected.size() == actualLen);
REQUIRE(expected.size() == measuredLen);
}
size_t actualLen = _object.prettyPrintTo(buffer);
size_t measuredLen = _object.measurePrettyLength();
TEST_CASE("JsonObject::prettyPrintTo()") {
DynamicJsonBuffer jb;
JsonObject &obj = jb.createObject();
EXPECT_STREQ(expected, buffer);
EXPECT_EQ(strlen(expected), actualLen);
EXPECT_EQ(strlen(expected), measuredLen);
SECTION("EmptyObject") {
check(obj, "{}");
}
};
TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyObject) {
outputMustBe("{}");
}
TEST_F(JsonObject_PrettyPrintTo_Tests, OneMember) {
_object["key"] = "value";
outputMustBe(
"{\r\n"
" \"key\": \"value\"\r\n"
"}");
}
TEST_F(JsonObject_PrettyPrintTo_Tests, TwoMembers) {
_object["key1"] = "value1";
_object["key2"] = "value2";
outputMustBe(
"{\r\n"
" \"key1\": \"value1\",\r\n"
" \"key2\": \"value2\"\r\n"
"}");
}
TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyNestedContainers) {
_object.createNestedObject("key1");
_object.createNestedArray("key2");
outputMustBe(
"{\r\n"
" \"key1\": {},\r\n"
" \"key2\": []\r\n"
"}");
}
TEST_F(JsonObject_PrettyPrintTo_Tests, NestedContainers) {
JsonObject &nested1 = _object.createNestedObject("key1");
nested1["a"] = 1;
JsonArray &nested2 = _object.createNestedArray("key2");
nested2.add(2);
outputMustBe(
"{\r\n"
" \"key1\": {\r\n"
" \"a\": 1\r\n"
" },\r\n"
" \"key2\": [\r\n"
" 2\r\n"
" ]\r\n"
"}");
SECTION("OneMember") {
obj["key"] = "value";
check(obj,
"{\r\n"
" \"key\": \"value\"\r\n"
"}");
}
SECTION("TwoMembers") {
obj["key1"] = "value1";
obj["key2"] = "value2";
check(obj,
"{\r\n"
" \"key1\": \"value1\",\r\n"
" \"key2\": \"value2\"\r\n"
"}");
}
SECTION("EmptyNestedContainers") {
obj.createNestedObject("key1");
obj.createNestedArray("key2");
check(obj,
"{\r\n"
" \"key1\": {},\r\n"
" \"key2\": []\r\n"
"}");
}
SECTION("NestedContainers") {
JsonObject &nested1 = obj.createNestedObject("key1");
nested1["a"] = 1;
JsonArray &nested2 = obj.createNestedArray("key2");
nested2.add(2);
check(obj,
"{\r\n"
" \"key1\": {\r\n"
" \"a\": 1\r\n"
" },\r\n"
" \"key2\": [\r\n"
" 2\r\n"
" ]\r\n"
"}");
}
}

View File

@ -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\":{}}");
}
}

View File

@ -6,39 +6,39 @@
// If you like this project, please add a star!
#include <ArduinoJson.h>
#include <gtest/gtest.h>
#include <catch.hpp>
#include <string>
#define TEST_(name) TEST(JsonObject_Remove_Tests, name)
TEST_CASE("JsonObject::remove()") {
DynamicJsonBuffer jb;
TEST_(SizeDecreased_WhenValuesAreRemoved) {
DynamicJsonBuffer _jsonBuffer;
JsonObject& _object = _jsonBuffer.createObject();
_object["hello"] = 1;
SECTION("SizeDecreased_WhenValuesAreRemoved") {
JsonObject& obj = jb.createObject();
obj["hello"] = 1;
_object.remove("hello");
obj.remove("hello");
EXPECT_EQ(0, _object.size());
}
TEST_(SizeUntouched_WhenRemoveIsCalledWithAWrongKey) {
DynamicJsonBuffer _jsonBuffer;
JsonObject& _object = _jsonBuffer.createObject();
_object["hello"] = 1;
_object.remove("world");
EXPECT_EQ(1, _object.size());
}
TEST_(RemoveByIterator) {
DynamicJsonBuffer _jsonBuffer;
JsonObject& _object = _jsonBuffer.parseObject("{\"a\":0,\"b\":1,\"c\":2}");
for (JsonObject::iterator it = _object.begin(); it != _object.end(); ++it) {
if (it->value == 1) _object.remove(it);
REQUIRE(0 == obj.size());
}
char result[64];
_object.printTo(result);
EXPECT_STREQ("{\"a\":0,\"c\":2}", result);
SECTION("SizeUntouched_WhenRemoveIsCalledWithAWrongKey") {
JsonObject& obj = jb.createObject();
obj["hello"] = 1;
obj.remove("world");
REQUIRE(1 == obj.size());
}
SECTION("RemoveByIterator") {
JsonObject& obj = jb.parseObject("{\"a\":0,\"b\":1,\"c\":2}");
for (JsonObject::iterator it = obj.begin(); it != obj.end(); ++it) {
if (it->value == 1) obj.remove(it);
}
std::string result;
obj.printTo(result);
REQUIRE("{\"a\":0,\"c\":2}" == result);
}
}

View File

@ -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")));
}
}

View File

@ -6,132 +6,127 @@
// If you like this project, please add a star!
#include <ArduinoJson.h>
#include <gtest/gtest.h>
#include <catch.hpp>
class JsonObject_Subscript_Tests : public ::testing::Test {
public:
JsonObject_Subscript_Tests() : _object(_jsonBuffer.createObject()) {}
protected:
TEST_CASE("JsonObject::operator[]") {
DynamicJsonBuffer _jsonBuffer;
JsonObject& _object;
};
JsonObject& _object = _jsonBuffer.createObject();
#define TEST_(name) TEST_F(JsonObject_Subscript_Tests, name)
SECTION("SizeIncreased_WhenValuesAreAdded") {
_object["hello"] = 1;
REQUIRE(1 == _object.size());
}
TEST_(SizeIncreased_WhenValuesAreAdded) {
_object["hello"] = 1;
EXPECT_EQ(1, _object.size());
}
TEST_(SizeUntouched_WhenSameValueIsAdded) {
_object["hello"] = 1;
_object["hello"] = 2;
EXPECT_EQ(1, _object.size());
}
TEST_(StoreInteger) {
_object["hello"] = 123;
EXPECT_EQ(123, _object["hello"].as<int>());
EXPECT_TRUE(_object["hello"].is<int>());
EXPECT_FALSE(_object["hello"].is<double>());
}
TEST_(StoreVolatileInteger) { // issue #415
volatile int i = 123;
_object["hello"] = i;
EXPECT_EQ(123, _object["hello"].as<int>());
EXPECT_TRUE(_object["hello"].is<int>());
EXPECT_FALSE(_object["hello"].is<double>());
}
TEST_(StoreDouble) {
_object["hello"] = 123.45;
EXPECT_TRUE(_object["hello"].is<double>());
EXPECT_FALSE(_object["hello"].is<long>());
EXPECT_EQ(123.45, _object["hello"].as<double>());
}
TEST_(StoreDoubleWithDigits) {
_object["hello"].set(123.45, 2);
EXPECT_TRUE(_object["hello"].is<double>());
EXPECT_FALSE(_object["hello"].is<long>());
EXPECT_EQ(123.45, _object["hello"].as<double>());
}
TEST_(StoreBoolean) {
_object["hello"] = true;
EXPECT_TRUE(_object["hello"].is<bool>());
EXPECT_FALSE(_object["hello"].is<long>());
EXPECT_TRUE(_object["hello"].as<bool>());
}
TEST_(StoreString) {
_object["hello"] = "h3110";
EXPECT_TRUE(_object["hello"].is<const char*>());
EXPECT_FALSE(_object["hello"].is<long>());
EXPECT_STREQ("h3110", _object["hello"].as<const char*>());
EXPECT_STREQ("h3110", _object["hello"].as<char*>()); // <- short hand
}
TEST_(StoreArray) {
JsonArray& arr = _jsonBuffer.createArray();
_object["hello"] = arr;
EXPECT_EQ(&arr, &_object["hello"].as<JsonArray&>());
EXPECT_EQ(&arr, &_object["hello"].as<JsonArray>()); // <- short hand
EXPECT_EQ(&arr, &_object["hello"].as<const JsonArray&>());
EXPECT_EQ(&arr, &_object["hello"].as<const JsonArray>()); // <- short hand
EXPECT_TRUE(_object["hello"].is<JsonArray&>());
EXPECT_TRUE(_object["hello"].is<JsonArray>());
EXPECT_TRUE(_object["hello"].is<const JsonArray&>());
EXPECT_TRUE(_object["hello"].is<const JsonArray>());
EXPECT_FALSE(_object["hello"].is<JsonObject&>());
}
TEST_(StoreObject) {
JsonObject& obj = _jsonBuffer.createObject();
_object["hello"] = obj;
EXPECT_EQ(&obj, &_object["hello"].as<JsonObject&>());
EXPECT_EQ(&obj, &_object["hello"].as<JsonObject>()); // <- short hand
EXPECT_EQ(&obj, &_object["hello"].as<const JsonObject&>());
EXPECT_EQ(&obj, &_object["hello"].as<const JsonObject>()); // <- short hand
EXPECT_TRUE(_object["hello"].is<JsonObject&>());
EXPECT_TRUE(_object["hello"].is<JsonObject>());
EXPECT_TRUE(_object["hello"].is<const JsonObject&>());
EXPECT_TRUE(_object["hello"].is<const JsonObject>());
EXPECT_FALSE(_object["hello"].is<JsonArray&>());
}
TEST_(StoreArraySubscript) {
JsonArray& arr = _jsonBuffer.createArray();
arr.add(42);
_object["a"] = arr[0];
EXPECT_EQ(42, _object["a"]);
}
TEST_(StoreObjectSubscript) {
JsonObject& obj = _jsonBuffer.createObject();
obj.set("x", 42);
_object["a"] = obj["x"];
EXPECT_EQ(42, _object["a"]);
}
TEST_(KeyAsCharArray) { // issue #423
char key[] = "hello";
_object[key] = 42;
EXPECT_EQ(42, _object[key]);
SECTION("SizeUntouched_WhenSameValueIsAdded") {
_object["hello"] = 1;
_object["hello"] = 2;
REQUIRE(1 == _object.size());
}
SECTION("StoreInteger") {
_object["hello"] = 123;
REQUIRE(123 == _object["hello"].as<int>());
REQUIRE(true == _object["hello"].is<int>());
REQUIRE(false == _object["hello"].is<double>());
}
SECTION("StoreVolatileInteger") { // issue #415
volatile int i = 123;
_object["hello"] = i;
REQUIRE(123 == _object["hello"].as<int>());
REQUIRE(true == _object["hello"].is<int>());
REQUIRE(false == _object["hello"].is<double>());
}
SECTION("StoreDouble") {
_object["hello"] = 123.45;
REQUIRE(true == _object["hello"].is<double>());
REQUIRE(false == _object["hello"].is<long>());
REQUIRE(123.45 == _object["hello"].as<double>());
}
SECTION("StoreDoubleWithDigits") {
_object["hello"].set(123.45, 2);
REQUIRE(true == _object["hello"].is<double>());
REQUIRE(false == _object["hello"].is<long>());
REQUIRE(123.45 == _object["hello"].as<double>());
}
SECTION("StoreBoolean") {
_object["hello"] = true;
REQUIRE(true == _object["hello"].is<bool>());
REQUIRE(false == _object["hello"].is<long>());
REQUIRE(true == _object["hello"].as<bool>());
}
SECTION("StoreString") {
_object["hello"] = "h3110";
REQUIRE(true == _object["hello"].is<const char*>());
REQUIRE(false == _object["hello"].is<long>());
REQUIRE(std::string("h3110") == _object["hello"].as<const char*>());
REQUIRE(std::string("h3110") ==
_object["hello"].as<char*>()); // <- short hand
}
SECTION("StoreArray") {
JsonArray& arr = _jsonBuffer.createArray();
_object["hello"] = arr;
REQUIRE(&arr == &_object["hello"].as<JsonArray&>());
REQUIRE(&arr == &_object["hello"].as<JsonArray>()); // <- short hand
REQUIRE(&arr == &_object["hello"].as<const JsonArray&>());
REQUIRE(&arr == &_object["hello"].as<const JsonArray>()); // <- short hand
REQUIRE(true == _object["hello"].is<JsonArray&>());
REQUIRE(true == _object["hello"].is<JsonArray>());
REQUIRE(true == _object["hello"].is<const JsonArray&>());
REQUIRE(true == _object["hello"].is<const JsonArray>());
REQUIRE(false == _object["hello"].is<JsonObject&>());
}
SECTION("StoreObject") {
JsonObject& obj = _jsonBuffer.createObject();
_object["hello"] = obj;
REQUIRE(&obj == &_object["hello"].as<JsonObject&>());
REQUIRE(&obj == &_object["hello"].as<JsonObject>()); // <- short hand
REQUIRE(&obj == &_object["hello"].as<const JsonObject&>());
REQUIRE(&obj == &_object["hello"].as<const JsonObject>()); // <- short hand
REQUIRE(true == _object["hello"].is<JsonObject&>());
REQUIRE(true == _object["hello"].is<JsonObject>());
REQUIRE(true == _object["hello"].is<const JsonObject&>());
REQUIRE(true == _object["hello"].is<const JsonObject>());
REQUIRE(false == _object["hello"].is<JsonArray&>());
}
SECTION("StoreArraySubscript") {
JsonArray& arr = _jsonBuffer.createArray();
arr.add(42);
_object["a"] = arr[0];
REQUIRE(42 == _object["a"]);
}
SECTION("StoreObjectSubscript") {
JsonObject& obj = _jsonBuffer.createObject();
obj.set("x", 42);
_object["a"] = obj["x"];
REQUIRE(42 == _object["a"]);
}
SECTION("KeyAsCharArray") { // issue #423
char key[] = "hello";
_object[key] = 42;
REQUIRE(42 == _object[key]);
}
}