forked from bblanchon/ArduinoJson
Redesigned JsonVariant to leverage converting constructors instead of assignment operators
This commit is contained in:
@ -13,12 +13,12 @@ TEST(DynamicJsonBuffer_Object_Tests, GrowsWithObject) {
|
||||
JsonObject &obj = json.createObject();
|
||||
ASSERT_EQ(JSON_OBJECT_SIZE(0), json.size());
|
||||
|
||||
obj["hello"];
|
||||
obj["hello"] = 1;
|
||||
ASSERT_EQ(JSON_OBJECT_SIZE(1), json.size());
|
||||
|
||||
obj["world"];
|
||||
obj["world"] = 2;
|
||||
ASSERT_EQ(JSON_OBJECT_SIZE(2), json.size());
|
||||
|
||||
obj["world"]; // <- same value, should not grow
|
||||
obj["world"] = 3; // <- same key, should not grow
|
||||
ASSERT_EQ(JSON_OBJECT_SIZE(2), json.size());
|
||||
}
|
||||
|
@ -37,7 +37,7 @@ class GbathreeBug : public testing::Test {
|
||||
TEST_F(GbathreeBug, Success) { EXPECT_TRUE(_object.success()); }
|
||||
|
||||
TEST_F(GbathreeBug, ProtocolName) {
|
||||
EXPECT_STREQ("fluorescence", _object.at("protocol_name").asString());
|
||||
EXPECT_STREQ("fluorescence", _object["protocol_name"]);
|
||||
}
|
||||
|
||||
TEST_F(GbathreeBug, Repeats) { EXPECT_EQ(1, _object["repeats"]); }
|
||||
@ -69,7 +69,7 @@ TEST_F(GbathreeBug, Calintensity) { EXPECT_EQ(255, _object["calintensity"]); }
|
||||
TEST_F(GbathreeBug, Pulses) {
|
||||
// "pulses":[50,50,50]
|
||||
|
||||
JsonArray& array = _object.at("pulses");
|
||||
JsonArray& array = _object["pulses"];
|
||||
EXPECT_TRUE(array.success());
|
||||
|
||||
EXPECT_EQ(3, array.size());
|
||||
@ -82,7 +82,7 @@ TEST_F(GbathreeBug, Pulses) {
|
||||
TEST_F(GbathreeBug, Act) {
|
||||
// "act":[2,1,2,2]
|
||||
|
||||
JsonArray& array = _object.at("act");
|
||||
JsonArray& array = _object["act"];
|
||||
EXPECT_TRUE(array.success());
|
||||
|
||||
EXPECT_EQ(4, array.size());
|
||||
@ -95,7 +95,7 @@ TEST_F(GbathreeBug, Act) {
|
||||
TEST_F(GbathreeBug, Detectors) {
|
||||
// "detectors":[[34,34,34,34],[34,34,34,34],[34,34,34,34],[34,34,34,34]]
|
||||
|
||||
JsonArray& array = _object.at("detectors");
|
||||
JsonArray& array = _object["detectors"];
|
||||
EXPECT_TRUE(array.success());
|
||||
EXPECT_EQ(4, array.size());
|
||||
|
||||
@ -110,7 +110,7 @@ TEST_F(GbathreeBug, Detectors) {
|
||||
TEST_F(GbathreeBug, Alta) {
|
||||
// alta:[2,2,2,2]
|
||||
|
||||
JsonArray& array = _object.at("alta");
|
||||
JsonArray& array = _object["alta"];
|
||||
EXPECT_TRUE(array.success());
|
||||
|
||||
EXPECT_EQ(4, array.size());
|
||||
@ -123,7 +123,7 @@ TEST_F(GbathreeBug, Alta) {
|
||||
TEST_F(GbathreeBug, Altb) {
|
||||
// altb:[2,2,2,2]
|
||||
|
||||
JsonArray& array = _object.at("altb");
|
||||
JsonArray& array = _object["altb"];
|
||||
EXPECT_TRUE(array.success());
|
||||
|
||||
EXPECT_EQ(4, array.size());
|
||||
@ -136,7 +136,7 @@ TEST_F(GbathreeBug, Altb) {
|
||||
TEST_F(GbathreeBug, Measlights) {
|
||||
// "measlights":[[15,15,15,15],[15,15,15,15],[15,15,15,15],[15,15,15,15]]
|
||||
|
||||
JsonArray& array = _object.at("measlights");
|
||||
JsonArray& array = _object["measlights"];
|
||||
EXPECT_TRUE(array.success());
|
||||
EXPECT_EQ(4, array.size());
|
||||
|
||||
@ -152,7 +152,7 @@ TEST_F(GbathreeBug, Measlights) {
|
||||
TEST_F(GbathreeBug, Measlights2) {
|
||||
// "measlights2":[[15,15,15,15],[15,15,15,15],[15,15,15,15],[15,15,15,15]]
|
||||
|
||||
JsonArray& array = _object.at("measlights2");
|
||||
JsonArray& array = _object["measlights2"];
|
||||
EXPECT_TRUE(array.success());
|
||||
EXPECT_EQ(4, array.size());
|
||||
|
||||
@ -167,7 +167,7 @@ TEST_F(GbathreeBug, Measlights2) {
|
||||
TEST_F(GbathreeBug, Altc) {
|
||||
// altc:[2,2,2,2]
|
||||
|
||||
JsonArray& array = _object.at("altc");
|
||||
JsonArray& array = _object["altc"];
|
||||
EXPECT_TRUE(array.success());
|
||||
|
||||
EXPECT_EQ(4, array.size());
|
||||
@ -180,7 +180,7 @@ TEST_F(GbathreeBug, Altc) {
|
||||
TEST_F(GbathreeBug, Altd) {
|
||||
// altd:[2,2,2,2]
|
||||
|
||||
JsonArray& array = _object.at("altd");
|
||||
JsonArray& array = _object["altd"];
|
||||
EXPECT_TRUE(array.success());
|
||||
|
||||
EXPECT_EQ(4, array.size());
|
||||
|
@ -67,6 +67,12 @@ TEST_F(JsonArray_Container_Tests, Grow_WhenValuesAreAdded) {
|
||||
sizeMustBe(2);
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Container_Tests, DontGrow_WhenValuesAreReplaced) {
|
||||
_array.add("hello");
|
||||
_array[0] = "world";
|
||||
sizeMustBe(1);
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Container_Tests, CanStoreIntegers) {
|
||||
_array.add(123);
|
||||
_array.add(456);
|
||||
|
@ -7,10 +7,6 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
TEST(JsonArray_Invalid_Tests, AtFails) {
|
||||
ASSERT_FALSE(JsonArray::invalid().at(0).success());
|
||||
}
|
||||
|
||||
TEST(JsonArray_Invalid_Tests, SubscriptFails) {
|
||||
ASSERT_FALSE(JsonArray::invalid()[0].success());
|
||||
}
|
||||
@ -33,4 +29,4 @@ TEST(JsonArray_Invalid_Tests, PrintToWritesBrackets) {
|
||||
char buffer[32];
|
||||
JsonArray::invalid().printTo(buffer, sizeof(buffer));
|
||||
ASSERT_STREQ("[]", buffer);
|
||||
}
|
||||
}
|
||||
|
@ -63,7 +63,7 @@ TEST_F(JsonArray_PrintTo_Tests, OneDoubleDefaultDigits) {
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrintTo_Tests, OneDoubleFourDigits) {
|
||||
array.add(3.14159265358979323846, 4);
|
||||
array.add(double_with_n_digits(3.14159265358979323846, 4));
|
||||
outputMustBe("[3.1416]");
|
||||
}
|
||||
|
||||
|
@ -21,24 +21,24 @@ TEST_F(JsonObject_Container_Tests, InitialSizeIsZero) {
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_Container_Tests, Grow_WhenValuesAreAdded) {
|
||||
_object["hello"];
|
||||
_object["hello"] = 1;
|
||||
EXPECT_EQ(1, _object.size());
|
||||
|
||||
_object["world"];
|
||||
_object.set("world", 2);
|
||||
EXPECT_EQ(2, _object.size());
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_Container_Tests, DoNotGrow_WhenSameValueIsAdded) {
|
||||
_object["hello"];
|
||||
_object["hello"] = 1;
|
||||
EXPECT_EQ(1, _object.size());
|
||||
|
||||
_object["hello"];
|
||||
_object["hello"] = 2;
|
||||
EXPECT_EQ(1, _object.size());
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_Container_Tests, Shrink_WhenValuesAreRemoved) {
|
||||
_object["hello"];
|
||||
_object["world"];
|
||||
_object["hello"] = 1;
|
||||
_object["world"] = 2;
|
||||
|
||||
_object.remove("hello");
|
||||
EXPECT_EQ(1, _object.size());
|
||||
@ -49,8 +49,8 @@ TEST_F(JsonObject_Container_Tests, Shrink_WhenValuesAreRemoved) {
|
||||
|
||||
TEST_F(JsonObject_Container_Tests,
|
||||
DoNotShrink_WhenRemoveIsCalledWithAWrongKey) {
|
||||
_object["hello"];
|
||||
_object["world"];
|
||||
_object["hello"] = 1;
|
||||
_object["world"] = 2;
|
||||
|
||||
_object.remove(":-P");
|
||||
|
||||
@ -111,16 +111,11 @@ TEST_F(JsonObject_Container_Tests, CanStoreInnerObjects) {
|
||||
EXPECT_EQ(&innerObject2, &_object["world"].asObject());
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_Container_Tests, ContainsKeyReturnFalseForNonExistingKey) {
|
||||
TEST_F(JsonObject_Container_Tests, ContainsKeyReturnsFalseForNonExistingKey) {
|
||||
EXPECT_FALSE(_object.containsKey("hello"));
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_Container_Tests, ContainsKeyReturnTrueForDefinedValue) {
|
||||
_object.add("hello", 42);
|
||||
TEST_F(JsonObject_Container_Tests, ContainsKeyReturnsTrueForDefinedValue) {
|
||||
_object.set("hello", 42);
|
||||
EXPECT_TRUE(_object.containsKey("hello"));
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_Container_Tests, ContainsKeyReturnFalseForUndefinedValue) {
|
||||
_object.add("hello");
|
||||
EXPECT_FALSE(_object.containsKey("hello"));
|
||||
}
|
||||
|
@ -7,18 +7,14 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
TEST(JsonObject_Invalid_Tests, AtFails) {
|
||||
ASSERT_FALSE(JsonObject::invalid().at(0).success());
|
||||
}
|
||||
|
||||
TEST(JsonObject_Invalid_Tests, SubscriptFails) {
|
||||
ASSERT_FALSE(JsonObject::invalid()[0].success());
|
||||
}
|
||||
|
||||
TEST(JsonObject_Invalid_Tests, AddFails) {
|
||||
JsonObject& array = JsonObject::invalid();
|
||||
array.add("hello", "world");
|
||||
ASSERT_EQ(0, array.size());
|
||||
JsonObject& object = JsonObject::invalid();
|
||||
object.set("hello", "world");
|
||||
ASSERT_EQ(0, object.size());
|
||||
}
|
||||
|
||||
TEST(JsonObject_Invalid_Tests, CreateNestedArrayFails) {
|
||||
@ -33,4 +29,4 @@ TEST(JsonObject_Invalid_Tests, PrintToWritesBraces) {
|
||||
char buffer[32];
|
||||
JsonObject::invalid().printTo(buffer, sizeof(buffer));
|
||||
ASSERT_STREQ("{}", buffer);
|
||||
}
|
||||
}
|
||||
|
@ -88,7 +88,7 @@ TEST_F(JsonObject_PrintTo_Tests, OneInteger) {
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_PrintTo_Tests, OneDoubleFourDigits) {
|
||||
object["key"].set(3.14159265358979323846, 4);
|
||||
object["key"] = double_with_n_digits(3.14159265358979323846, 4);
|
||||
outputMustBe("{\"key\":3.1416}");
|
||||
}
|
||||
|
||||
|
@ -35,11 +35,11 @@ class JsonParser_Array_Tests : public testing::Test {
|
||||
|
||||
template <typename T>
|
||||
void elementAtIndexMustBe(int index, T expected) {
|
||||
EXPECT_EQ(expected, _array->at(index).as<T>());
|
||||
EXPECT_EQ(expected, (*_array)[index].as<T>());
|
||||
}
|
||||
|
||||
void elementAtIndexMustBe(int index, const char *expected) {
|
||||
EXPECT_STREQ(expected, _array->at(index).as<const char *>());
|
||||
EXPECT_STREQ(expected, (*_array)[index].as<const char *>());
|
||||
}
|
||||
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
|
@ -21,12 +21,12 @@ class JsonParser_Object_Test : public testing::Test {
|
||||
void sizeMustBe(int expected) { EXPECT_EQ(expected, _object->size()); }
|
||||
|
||||
void keyMustHaveValue(const char *key, const char *expected) {
|
||||
EXPECT_STREQ(expected, _object->at(key).as<const char *>());
|
||||
EXPECT_STREQ(expected, (*_object)[key]);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void keyMustHaveValue(const char *key, T expected) {
|
||||
EXPECT_EQ(expected, _object->at(key).as<T>());
|
||||
EXPECT_EQ(expected, (*_object)[key].as<T>());
|
||||
}
|
||||
|
||||
private:
|
||||
|
@ -1,75 +0,0 @@
|
||||
// Copyright Benoit Blanchon 2014-2015
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include "Printers.hpp"
|
||||
|
||||
class JsonVariant_Invalid_Tests : public ::testing::Test {
|
||||
public:
|
||||
JsonVariant_Invalid_Tests() : variant(JsonVariant::invalid()) {}
|
||||
|
||||
protected:
|
||||
JsonVariant variant;
|
||||
};
|
||||
|
||||
TEST_F(JsonVariant_Invalid_Tests, SuccessReturnsFalse) {
|
||||
EXPECT_FALSE(variant.success());
|
||||
}
|
||||
|
||||
TEST_F(JsonVariant_Invalid_Tests, AsLongReturns0) {
|
||||
EXPECT_EQ(0, variant.as<long>());
|
||||
}
|
||||
|
||||
TEST_F(JsonVariant_Invalid_Tests, AsStringReturnsNull) {
|
||||
EXPECT_EQ(0, variant.asString());
|
||||
}
|
||||
|
||||
TEST_F(JsonVariant_Invalid_Tests, AsDoubleReturns0) {
|
||||
EXPECT_EQ(0, variant.as<double>());
|
||||
}
|
||||
|
||||
TEST_F(JsonVariant_Invalid_Tests, AsBoolReturnsFalse) {
|
||||
EXPECT_FALSE(variant.as<bool>());
|
||||
}
|
||||
|
||||
TEST_F(JsonVariant_Invalid_Tests, AsArrayReturnInvalid) {
|
||||
EXPECT_EQ(JsonArray::invalid(), variant.asArray());
|
||||
}
|
||||
|
||||
TEST_F(JsonVariant_Invalid_Tests, AsObjectReturnInvalid) {
|
||||
EXPECT_EQ(JsonObject::invalid(), variant.asObject());
|
||||
}
|
||||
|
||||
TEST_F(JsonVariant_Invalid_Tests, CanNotBeSetToLong) {
|
||||
variant = 0L;
|
||||
EXPECT_FALSE(variant.success());
|
||||
}
|
||||
|
||||
TEST_F(JsonVariant_Invalid_Tests, CanNotBeSetToDouble) {
|
||||
variant = 0.0;
|
||||
EXPECT_FALSE(variant.success());
|
||||
}
|
||||
|
||||
TEST_F(JsonVariant_Invalid_Tests, CanNotBeSetToString) {
|
||||
variant = static_cast<const char*>(NULL);
|
||||
EXPECT_FALSE(variant.success());
|
||||
}
|
||||
|
||||
TEST_F(JsonVariant_Invalid_Tests, CanNotBeSetToBool) {
|
||||
variant = false;
|
||||
EXPECT_FALSE(variant.success());
|
||||
}
|
||||
|
||||
TEST_F(JsonVariant_Invalid_Tests, CanNotBeSetToArray) {
|
||||
variant = JsonArray::invalid();
|
||||
EXPECT_FALSE(variant.success());
|
||||
}
|
||||
|
||||
TEST_F(JsonVariant_Invalid_Tests, CanNotBeSetToObject) {
|
||||
variant = JsonObject::invalid();
|
||||
EXPECT_FALSE(variant.success());
|
||||
}
|
@ -42,7 +42,7 @@ TEST_F(JsonVariant_PrintTo_Tests, DoubleDefaultDigits) {
|
||||
}
|
||||
|
||||
TEST_F(JsonVariant_PrintTo_Tests, DoubleFourDigits) {
|
||||
variant.set(3.14159265358979323846, 4);
|
||||
variant = JsonVariant(3.14159265358979323846, 4);
|
||||
outputMustBe("3.1416");
|
||||
}
|
||||
|
||||
|
@ -11,13 +11,13 @@ class JsonVariant_Storage_Tests : public ::testing::Test {
|
||||
protected:
|
||||
template <typename T>
|
||||
void testValue(T expected) {
|
||||
_actual.set(expected);
|
||||
_actual = expected;
|
||||
EXPECT_EQ(expected, _actual.as<T>());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void testReference(T &expected) {
|
||||
_actual.set(expected);
|
||||
_actual = expected;
|
||||
EXPECT_EQ(expected, _actual.as<T &>());
|
||||
}
|
||||
|
||||
|
@ -49,13 +49,6 @@ TEST_F(JsonVariant_Subscript_Tests, Undefined) {
|
||||
EXPECT_FALSE(_variant[0].success());
|
||||
}
|
||||
|
||||
TEST_F(JsonVariant_Subscript_Tests, Invalid) {
|
||||
_variant = JsonVariant::invalid();
|
||||
EXPECT_EQ(0, _variant.size());
|
||||
EXPECT_FALSE(_variant["0"].success());
|
||||
EXPECT_FALSE(_variant[0].success());
|
||||
}
|
||||
|
||||
TEST_F(JsonVariant_Subscript_Tests, String) {
|
||||
_variant = "hello world";
|
||||
EXPECT_EQ(0, _variant.size());
|
||||
|
@ -13,10 +13,6 @@ class JsonVariant_Undefined_Tests : public ::testing::Test {
|
||||
JsonVariant variant;
|
||||
};
|
||||
|
||||
TEST_F(JsonVariant_Undefined_Tests, SuccessReturnsFalse) {
|
||||
EXPECT_FALSE(variant.success());
|
||||
}
|
||||
|
||||
TEST_F(JsonVariant_Undefined_Tests, AsLongReturns0) {
|
||||
EXPECT_EQ(0, variant.as<long>());
|
||||
}
|
||||
@ -40,35 +36,3 @@ TEST_F(JsonVariant_Undefined_Tests, AsArrayReturnInvalid) {
|
||||
TEST_F(JsonVariant_Undefined_Tests, AsObjectReturnInvalid) {
|
||||
EXPECT_EQ(JsonObject::invalid(), variant.asObject());
|
||||
}
|
||||
|
||||
TEST_F(JsonVariant_Undefined_Tests, CanBeSetToLong) {
|
||||
variant = 0L;
|
||||
EXPECT_TRUE(variant.success());
|
||||
}
|
||||
|
||||
TEST_F(JsonVariant_Undefined_Tests, CanBeSetToDouble) {
|
||||
variant = 0.0;
|
||||
EXPECT_TRUE(variant.success());
|
||||
}
|
||||
|
||||
TEST_F(JsonVariant_Undefined_Tests, CanBeSetToString) {
|
||||
variant = static_cast<const char*>(NULL);
|
||||
EXPECT_TRUE(variant.success());
|
||||
}
|
||||
|
||||
TEST_F(JsonVariant_Undefined_Tests, CanBeSetToBool) {
|
||||
variant = false;
|
||||
EXPECT_TRUE(variant.success());
|
||||
}
|
||||
|
||||
TEST_F(JsonVariant_Undefined_Tests, CanBeSetToArray) {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
variant = jsonBuffer.createArray();
|
||||
EXPECT_TRUE(variant.success());
|
||||
}
|
||||
|
||||
TEST_F(JsonVariant_Undefined_Tests, CanBeSetToObject) {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
variant = jsonBuffer.createObject();
|
||||
EXPECT_TRUE(variant.success());
|
||||
}
|
||||
|
@ -36,3 +36,15 @@ std::ostream& ArduinoJson::operator<<(std::ostream& os,
|
||||
v.printTo(adapter);
|
||||
return os;
|
||||
}
|
||||
|
||||
std::ostream& ArduinoJson::operator<<(
|
||||
std::ostream& os, const ArduinoJson::JsonObjectSubscript& v) {
|
||||
JsonVariant value = v;
|
||||
return os << value;
|
||||
}
|
||||
|
||||
std::ostream& ArduinoJson::operator<<(
|
||||
std::ostream& os, const ArduinoJson::JsonArraySubscript& v) {
|
||||
JsonVariant value = v;
|
||||
return os << value;
|
||||
}
|
||||
|
@ -6,10 +6,12 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ArduinoJson/JsonVariant.hpp>
|
||||
#include <ArduinoJson.h>
|
||||
#include <ostream>
|
||||
|
||||
namespace ArduinoJson {
|
||||
std::ostream& operator<<(std::ostream& os, const ArduinoJson::JsonVariant& v);
|
||||
std::ostream& operator<<(std::ostream& os, const ArduinoJson::JsonArray& v);
|
||||
std::ostream& operator<<(std::ostream& os, const JsonVariant& v);
|
||||
std::ostream& operator<<(std::ostream& os, const JsonArray& v);
|
||||
std::ostream& operator<<(std::ostream& os, const JsonObjectSubscript& v);
|
||||
std::ostream& operator<<(std::ostream& os, const JsonArraySubscript& v);
|
||||
}
|
||||
|
@ -14,12 +14,15 @@ TEST(StaticJsonBuffer_CreateObject_Tests, GrowsWithObject) {
|
||||
ASSERT_EQ(JSON_OBJECT_SIZE(0), json.size());
|
||||
|
||||
obj["hello"];
|
||||
ASSERT_EQ(JSON_OBJECT_SIZE(0), json.size());
|
||||
|
||||
obj["hello"] = 1;
|
||||
ASSERT_EQ(JSON_OBJECT_SIZE(1), json.size());
|
||||
|
||||
obj["world"];
|
||||
obj["world"] = 2;
|
||||
ASSERT_EQ(JSON_OBJECT_SIZE(2), json.size());
|
||||
|
||||
obj["world"]; // <- same value, should not grow
|
||||
obj["world"] = 3; // <- same key, should not grow
|
||||
ASSERT_EQ(JSON_OBJECT_SIZE(2), json.size());
|
||||
}
|
||||
|
||||
@ -41,8 +44,8 @@ TEST(StaticJsonBuffer_CreateObject_Tests, ObjectDoesntGrowWhenFull) {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(1)> json;
|
||||
|
||||
JsonObject &obj = json.createObject();
|
||||
obj["hello"];
|
||||
obj["world"];
|
||||
obj["hello"] = 1;
|
||||
obj["world"] = 2;
|
||||
|
||||
ASSERT_EQ(JSON_OBJECT_SIZE(1), json.size());
|
||||
}
|
||||
|
Reference in New Issue
Block a user