Просмотр исходного кода

Test that a double can be stored in a JsonObject

Benoit Blanchon 11 лет назад
Родитель
Сommit
5fa446d3f5
5 измененных файлов с 31 добавлено и 10 удалено
  1. 1 0
      srcs/JsonNode.h
  2. 1 1
      srcs/JsonObject.cpp
  3. 13 8
      srcs/JsonValue.cpp
  4. 2 0
      srcs/JsonValue.h
  5. 14 1
      tests/JsonObjectTests.cpp

+ 1 - 0
srcs/JsonNode.h

@@ -24,6 +24,7 @@ struct JsonNode
 
     union
     {
+        double asDouble;
         int asInteger;
 
         struct 

+ 1 - 1
srcs/JsonObject.cpp

@@ -2,7 +2,7 @@
 #include "JsonObject.h"
 #include "JsonValue.h"
 #include "JsonNode.h"
-#include <string.h>
+#include <string.h> // for strcmp
 
 //JsonValue& JsonObject::operator[](char const* key)
 //{

+ 13 - 8
srcs/JsonValue.cpp

@@ -2,10 +2,13 @@
 #include "JsonNode.h"
 #include "JsonValue.h"
 
-//void JsonValue::operator=(JsonObject const& object)
-//{
-//    _node = object._node;
-//}
+void JsonValue::operator=(double value)
+{
+    if (!_node) return;
+
+    _node->type = JSON_DOUBLE_2_DECIMALS;
+    _node->content.asDouble = value;
+}
 
 void JsonValue::operator=(int value)
 {
@@ -15,10 +18,12 @@ void JsonValue::operator=(int value)
     _node->content.asInteger = value;
 }
 
-//JsonValue::operator JsonObject()
-//{
-//    return JsonObject(_buffer, _node);
-//}
+JsonValue::operator double()
+{
+    if (!_node || _node->type < JSON_DOUBLE_0_DECIMALS) return 0;
+
+    return _node->content.asDouble;
+}
 
 JsonValue::operator int()
 {

+ 2 - 0
srcs/JsonValue.h

@@ -14,9 +14,11 @@ public:
     }
 
    // void operator=(const JsonObject& object);
+    void operator=(double);
     void operator=(int);
     
    // operator JsonObject();
+    operator double();
     operator int();
 
 private:

+ 14 - 1
tests/JsonObjectTests.cpp

@@ -28,7 +28,7 @@ TEST(JsonObjectTests, WhenTheSameValueIsAddedTwice_ThenSizeIsOnlyIncreasedByOne)
     EXPECT_EQ(1, object.size());
 }
 
-TEST(JsonObjectTests, WhenAnIntegerIsStore_TheSameIntegerIsRetreived)
+TEST(JsonObjectTests, GivenAnIntegerStored_WhenRetreivingTheValue_ThenTheValueIsTheSame)
 {
     StaticJsonBuffer<42> json;
 
@@ -39,4 +39,17 @@ TEST(JsonObjectTests, WhenAnIntegerIsStore_TheSameIntegerIsRetreived)
 
     EXPECT_EQ(123, (int) object["hello"]);
     EXPECT_EQ(456, (int) object["world"]);
+}
+
+TEST(JsonObjectTests, GivenAnDoubleStored_WhenRetreivingTheValue_ThenTheValueIsTheSame)
+{
+    StaticJsonBuffer<42> json;
+
+    JsonObject object = json.createObject();
+
+    object["hello"] = 123.45;
+    object["world"] = 456.78;
+
+    EXPECT_EQ(123.45, (double) object["hello"]);
+    EXPECT_EQ(456.78, (double) object["world"]);
 }