From 42ce5ab31ff3cec84bfed5b7945a72fa5169ad15 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Sun, 28 Sep 2014 21:18:43 +0200 Subject: [PATCH] Test that integers in JsonValue are copied --- srcs/JsonValue.cpp | 42 ++++++++++++++++++++++++++++++++++++---- srcs/JsonValue.h | 2 ++ tests/JsonValueTests.cpp | 40 ++++++++++++++++++++++++-------------- 3 files changed, 66 insertions(+), 18 deletions(-) diff --git a/srcs/JsonValue.cpp b/srcs/JsonValue.cpp index 4be9ff15..0fa5454d 100644 --- a/srcs/JsonValue.cpp +++ b/srcs/JsonValue.cpp @@ -36,13 +36,36 @@ void JsonValue::operator=(int value) void JsonValue::operator=(const JsonObject& object) { - if (_node) + setAsProxyTo(object._node); +} + +void JsonValue::operator=(JsonValue const& value) +{ + if (!_node) { - _node->type = JSON_PROXY; - _node->content.asProxy.target = object._node; + _node = value._node; + return; } - _node = object._node; + JsonNodeType nodeType = value._node ? value._node->type : JSON_UNDEFINED; + + + switch (nodeType) + { + case JSON_UNDEFINED: + _node->type = JSON_UNDEFINED; + break; + + case JSON_INTEGER: + _node->type = JSON_INTEGER; + _node->content.asInteger = value._node->content.asInteger; + break; + + case JSON_OBJECT: + case JSON_ARRAY: + case JSON_PROXY: + setAsProxyTo(value._node); + } } JsonValue::operator bool() const @@ -86,6 +109,17 @@ JsonValue::operator JsonObject() const return JsonObject(getActualNode()); } +void JsonValue::setAsProxyTo(JsonNode* target) +{ + if (_node) + { + _node->type = JSON_PROXY; + _node->content.asProxy.target = target; + } + + _node = target; +} + JsonNode* JsonValue::getActualNode() const { JsonNode* target = _node; diff --git a/srcs/JsonValue.h b/srcs/JsonValue.h index d3232d63..86ff287d 100644 --- a/srcs/JsonValue.h +++ b/srcs/JsonValue.h @@ -21,6 +21,7 @@ public: void operator=(double); void operator=(int); void operator=(const JsonObject&); + void operator=(const JsonValue&); operator bool() const; operator const char*() const; @@ -31,6 +32,7 @@ public: private: JsonNode* _node; + void setAsProxyTo(JsonNode*); JsonNode* getActualNode() const; }; diff --git a/tests/JsonValueTests.cpp b/tests/JsonValueTests.cpp index c6be6b07..0392968b 100644 --- a/tests/JsonValueTests.cpp +++ b/tests/JsonValueTests.cpp @@ -7,52 +7,64 @@ class JsonValueTests : public ::testing::Test protected: virtual void SetUp() { - jsonValue = json.createValue(); + jsonValue1 = json.createValue(); + jsonValue2 = json.createValue(); } StaticJsonBuffer<42> json; - JsonValue jsonValue; + JsonValue jsonValue1; + JsonValue jsonValue2; }; TEST_F(JsonValueTests, CanStoreInteger) { - jsonValue = 123; + jsonValue1 = 123; - EXPECT_EQ(123, (int) jsonValue); + EXPECT_EQ(123, (int) jsonValue1); } TEST_F(JsonValueTests, CanStoreDouble) { - jsonValue = 123.45; + jsonValue1 = 123.45; - EXPECT_EQ(123.45, (double) jsonValue); + EXPECT_EQ(123.45, (double) jsonValue1); } TEST_F(JsonValueTests, CanStoreTrue) { - jsonValue = true; - EXPECT_TRUE((bool) jsonValue); + jsonValue1 = true; + EXPECT_TRUE((bool) jsonValue1); } TEST_F(JsonValueTests, CanStoreFalse) { - jsonValue = false; - EXPECT_FALSE((bool) jsonValue); + jsonValue1 = false; + EXPECT_FALSE((bool) jsonValue1); } TEST_F(JsonValueTests, CanStoreString) { - jsonValue = "hello"; + jsonValue1 = "hello"; - EXPECT_STREQ("hello", (const char*) jsonValue); + EXPECT_STREQ("hello", (const char*) jsonValue1); } TEST_F(JsonValueTests, CanStoreObject) { JsonObject innerObject1 = json.createObject(); - jsonValue = innerObject1; + jsonValue1 = innerObject1; - EXPECT_EQ(innerObject1, (JsonObject) jsonValue); + EXPECT_EQ(innerObject1, (JsonObject) jsonValue1); +} + +TEST_F(JsonValueTests, CanCopyInteger) +{ + jsonValue1 = 123; + jsonValue2 = jsonValue1; + jsonValue1 = 456; + + EXPECT_EQ(456, (int) jsonValue1); + EXPECT_EQ(123, (int) jsonValue2); } \ No newline at end of file