Ver Fonte

Fixed return type of `JsonArray::is<T>()` and some others (issue #121)

Benoit Blanchon há 10 anos atrás
pai
commit
7cf6fe6d62

+ 1 - 0
CHANGELOG.md

@@ -5,6 +5,7 @@ v5.0.3
 ------
 
 * Fixed `printTo(String)` which wrote numbers instead of strings (issue #120)
+* Fixed return type of `JsonArray::is<T>()` and some others (issue #121)
 
 v5.0.2
 ------

+ 1 - 1
include/ArduinoJson/JsonArray.hpp

@@ -92,7 +92,7 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
 
   // Check the type of the value at specified index.
   template <typename T>
-  FORCE_INLINE T is(size_t index) const;
+  FORCE_INLINE bool is(size_t index) const;
 
   // Creates a JsonArray and adds a reference at the end of the array.
   // It's a shortcut for JsonBuffer::createArray() and JsonArray::add()

+ 1 - 1
include/ArduinoJson/JsonArray.ipp

@@ -182,7 +182,7 @@ inline T JsonArray::get(size_t index) const {
 }
 
 template <typename T>
-inline T JsonArray::is(size_t index) const {
+inline bool JsonArray::is(size_t index) const {
   node_type *node = getNodeAt(index);
   return node ? node->content.is<T>() : false;
 }

+ 1 - 1
include/ArduinoJson/JsonArraySubscript.hpp

@@ -26,7 +26,7 @@ class JsonArraySubscript : public JsonSubscriptBase<JsonArraySubscript> {
   }
 
   template <typename T>
-  FORCE_INLINE T is() const {
+  FORCE_INLINE bool is() const {
     return _array.is<T>(_index);
   }
 

+ 1 - 1
include/ArduinoJson/JsonObjectSubscript.hpp

@@ -29,7 +29,7 @@ class JsonObjectSubscript
   }
 
   template <typename TValue>
-  FORCE_INLINE TValue is() const {
+  FORCE_INLINE bool is() const {
     return _object.is<TValue>(_key);
   }
 

+ 2 - 0
test/JsonArray_Container_Tests.cpp

@@ -39,11 +39,13 @@ class JsonArray_Container_Tests : public ::testing::Test {
  private:
   template <typename T>
   void itemMustEqual(int index, T expected) {
+    EXPECT_TRUE(_array[index].is<T>());
     EXPECT_EQ(expected, _array[index].as<T>());
   }
 
   template <typename T>
   void itemMustReference(int index, const T& expected) {
+    EXPECT_TRUE(_array[index].is<T&>());
     EXPECT_EQ(&expected, &_array[index].as<T&>());
   }
 };

+ 12 - 0
test/JsonObject_Container_Tests.cpp

@@ -61,6 +61,8 @@ TEST_F(JsonObject_Container_Tests, CanStoreIntegers) {
   _object["hello"] = 123;
   _object.set("world", 456);
 
+  EXPECT_TRUE(_object["hello"].is<int>());
+  EXPECT_FALSE(_object["hello"].is<double>());
   EXPECT_EQ(123, _object["hello"].as<int>());
   EXPECT_EQ(456, _object["world"].as<int>());
 }
@@ -69,6 +71,8 @@ TEST_F(JsonObject_Container_Tests, CanStoreDoubles) {
   _object["hello"] = 123.45;
   _object.set("world", 456.78);
 
+  EXPECT_TRUE(_object["hello"].is<double>());
+  EXPECT_FALSE(_object["hello"].is<long>());
   EXPECT_EQ(123.45, _object["hello"].as<double>());
   EXPECT_EQ(456.78, _object["world"].as<double>());
 }
@@ -77,6 +81,8 @@ TEST_F(JsonObject_Container_Tests, CanStoreBooleans) {
   _object["hello"] = true;
   _object.set("world", false);
 
+  EXPECT_TRUE(_object["hello"].is<bool>());
+  EXPECT_FALSE(_object["hello"].is<long>());
   EXPECT_TRUE(_object["hello"].as<bool>());
   EXPECT_FALSE(_object["world"].as<bool>());
 }
@@ -85,6 +91,8 @@ TEST_F(JsonObject_Container_Tests, CanStoreStrings) {
   _object["hello"] = "h3110";
   _object.set("world", "w0r1d");
 
+  EXPECT_TRUE(_object["hello"].is<const char*>());
+  EXPECT_FALSE(_object["hello"].is<long>());
   EXPECT_STREQ("h3110", _object["hello"].as<const char*>());
   EXPECT_STREQ("w0r1d", _object["world"].as<const char*>());
 }
@@ -96,6 +104,8 @@ TEST_F(JsonObject_Container_Tests, CanStoreArrays) {
   _object["hello"] = array1;
   _object.set("world", array2);
 
+  EXPECT_TRUE(_object["hello"].is<JsonArray&>());
+  EXPECT_FALSE(_object["hello"].is<JsonObject&>());
   EXPECT_EQ(&array1, &_object["hello"].asArray());
   EXPECT_EQ(&array2, &_object["world"].asArray());
 }
@@ -107,6 +117,8 @@ TEST_F(JsonObject_Container_Tests, CanStoreObjects) {
   _object["hello"] = object1;
   _object.set("world", object2);
 
+  EXPECT_TRUE(_object["hello"].is<JsonObject&>());
+  EXPECT_FALSE(_object["hello"].is<JsonArray&>());
   EXPECT_EQ(&object1, &_object["hello"].asObject());
   EXPECT_EQ(&object2, &_object["world"].asObject());
 }