mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 08:42:39 +01:00 
			
		
		
		
	Fixed ambiguous overload with JsonArraySubscript and JsonObjectSubscript (issue #122)
				
					
				
			This commit is contained in:
		
							
								
								
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| @@ -4,3 +4,5 @@ | |||||||
| /bin | /bin | ||||||
| /lib | /lib | ||||||
| /sftp-config.json | /sftp-config.json | ||||||
|  | .tags | ||||||
|  | .tags_sorted_by_file | ||||||
|   | |||||||
| @@ -1,6 +1,11 @@ | |||||||
| ArduinoJson: change log | ArduinoJson: change log | ||||||
| ======================= | ======================= | ||||||
|  |  | ||||||
|  | v5.0.4 (not released yet) | ||||||
|  | ------ | ||||||
|  |  | ||||||
|  | * Fixed ambiguous overload with `JsonArraySubscript` and `JsonObjectSubscript` (issue #122) | ||||||
|  |  | ||||||
| v5.0.3 | v5.0.3 | ||||||
| ------ | ------ | ||||||
|  |  | ||||||
|   | |||||||
| @@ -63,7 +63,8 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>, | |||||||
|   FORCE_INLINE bool add(const String &value); |   FORCE_INLINE bool add(const String &value); | ||||||
|   FORCE_INLINE bool add(JsonArray &array); |   FORCE_INLINE bool add(JsonArray &array); | ||||||
|   FORCE_INLINE bool add(JsonObject &object); |   FORCE_INLINE bool add(JsonObject &object); | ||||||
|   FORCE_INLINE bool add(const JsonVariant &object); |   template <typename T> | ||||||
|  |   FORCE_INLINE bool add(const T &value); | ||||||
|  |  | ||||||
|   // Sets the value at specified index. |   // Sets the value at specified index. | ||||||
|   FORCE_INLINE void set(size_t index, bool value); |   FORCE_INLINE void set(size_t index, bool value); | ||||||
| @@ -81,7 +82,8 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>, | |||||||
|   FORCE_INLINE void set(size_t index, const String &value); |   FORCE_INLINE void set(size_t index, const String &value); | ||||||
|   FORCE_INLINE void set(size_t index, JsonArray &array); |   FORCE_INLINE void set(size_t index, JsonArray &array); | ||||||
|   FORCE_INLINE void set(size_t index, JsonObject &object); |   FORCE_INLINE void set(size_t index, JsonObject &object); | ||||||
|   FORCE_INLINE void set(size_t index, const JsonVariant &object); |   template <typename T> | ||||||
|  |   FORCE_INLINE void set(size_t index, const T &value); | ||||||
|  |  | ||||||
|   // Gets the value at the specified index. |   // Gets the value at the specified index. | ||||||
|   FORCE_INLINE JsonVariant get(size_t index) const; |   FORCE_INLINE JsonVariant get(size_t index) const; | ||||||
|   | |||||||
| @@ -77,8 +77,9 @@ inline bool JsonArray::add(JsonObject &object) { | |||||||
|   return addNode<JsonObject &>(object); |   return addNode<JsonObject &>(object); | ||||||
| } | } | ||||||
|  |  | ||||||
| inline bool JsonArray::add(const JsonVariant &object) { | template <typename T> | ||||||
|   return addNode<const JsonVariant &>(object); | inline bool JsonArray::add(const T &variant) { | ||||||
|  |   return addNode<const JsonVariant &>(variant); | ||||||
| } | } | ||||||
|  |  | ||||||
| template <typename TValue> | template <typename TValue> | ||||||
| @@ -149,8 +150,9 @@ inline void JsonArray::set(size_t index, JsonObject &object) { | |||||||
|   return setNodeAt<JsonObject &>(index, object); |   return setNodeAt<JsonObject &>(index, object); | ||||||
| } | } | ||||||
|  |  | ||||||
| inline void JsonArray::set(size_t index, const JsonVariant &object) { | template <typename T> | ||||||
|   return setNodeAt<const JsonVariant &>(index, object); | inline void JsonArray::set(size_t index, const T &variant) { | ||||||
|  |   return setNodeAt<const JsonVariant &>(index, variant); | ||||||
| } | } | ||||||
|  |  | ||||||
| template <typename TValue> | template <typename TValue> | ||||||
|   | |||||||
| @@ -8,6 +8,11 @@ | |||||||
|  |  | ||||||
| #include "JsonSubscriptBase.hpp" | #include "JsonSubscriptBase.hpp" | ||||||
|  |  | ||||||
|  | #ifdef _MSC_VER | ||||||
|  | #pragma warning(push) | ||||||
|  | #pragma warning(disable : 4522) | ||||||
|  | #endif | ||||||
|  |  | ||||||
| namespace ArduinoJson { | namespace ArduinoJson { | ||||||
| class JsonArraySubscript : public JsonSubscriptBase<JsonArraySubscript> { | class JsonArraySubscript : public JsonSubscriptBase<JsonArraySubscript> { | ||||||
|  public: |  public: | ||||||
| @@ -16,6 +21,15 @@ class JsonArraySubscript : public JsonSubscriptBase<JsonArraySubscript> { | |||||||
|  |  | ||||||
|   using JsonSubscriptBase<JsonArraySubscript>::operator=; |   using JsonSubscriptBase<JsonArraySubscript>::operator=; | ||||||
|  |  | ||||||
|  |   JsonArraySubscript& operator=(const JsonArraySubscript& src) { | ||||||
|  |     return assign<const JsonVariant&>(src); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   template <typename T> | ||||||
|  |   JsonArraySubscript& operator=(const T& src) { | ||||||
|  |     return assign<const JsonVariant&>(src); | ||||||
|  |   } | ||||||
|  |  | ||||||
|   FORCE_INLINE bool success() const { return _index < _array.size(); } |   FORCE_INLINE bool success() const { return _index < _array.size(); } | ||||||
|  |  | ||||||
|   FORCE_INLINE operator JsonVariant() const { return _array.get(_index); } |   FORCE_INLINE operator JsonVariant() const { return _array.get(_index); } | ||||||
| @@ -52,3 +66,7 @@ inline std::ostream& operator<<(std::ostream& os, | |||||||
| #endif | #endif | ||||||
|  |  | ||||||
| }  // namespace ArduinoJson | }  // namespace ArduinoJson | ||||||
|  |  | ||||||
|  | #ifdef _MSC_VER | ||||||
|  | #pragma warning(pop) | ||||||
|  | #endif | ||||||
|   | |||||||
| @@ -65,6 +65,9 @@ class JsonObject : public Internals::JsonPrintable<JsonObject>, | |||||||
|   FORCE_INLINE bool set(const char* key, JsonArray& array); |   FORCE_INLINE bool set(const char* key, JsonArray& array); | ||||||
|   FORCE_INLINE bool set(const char* key, JsonObject& object); |   FORCE_INLINE bool set(const char* key, JsonObject& object); | ||||||
|   FORCE_INLINE bool set(const char* key, const JsonVariant& value); |   FORCE_INLINE bool set(const char* key, const JsonVariant& value); | ||||||
|  |   template <typename T> | ||||||
|  |   FORCE_INLINE bool set(const char* key, const T& value); | ||||||
|  |  | ||||||
|   FORCE_INLINE bool set(const String& key, bool value); |   FORCE_INLINE bool set(const String& key, bool value); | ||||||
|   FORCE_INLINE bool set(const String& key, float value, uint8_t decimals = 2); |   FORCE_INLINE bool set(const String& key, float value, uint8_t decimals = 2); | ||||||
|   FORCE_INLINE bool set(const String& key, double value, uint8_t decimals = 2); |   FORCE_INLINE bool set(const String& key, double value, uint8_t decimals = 2); | ||||||
| @@ -81,6 +84,8 @@ class JsonObject : public Internals::JsonPrintable<JsonObject>, | |||||||
|   FORCE_INLINE bool set(const String& key, JsonArray& array); |   FORCE_INLINE bool set(const String& key, JsonArray& array); | ||||||
|   FORCE_INLINE bool set(const String& key, JsonObject& object); |   FORCE_INLINE bool set(const String& key, JsonObject& object); | ||||||
|   FORCE_INLINE bool set(const String& key, const JsonVariant& value); |   FORCE_INLINE bool set(const String& key, const JsonVariant& value); | ||||||
|  |   template <typename T> | ||||||
|  |   FORCE_INLINE bool set(const String& key, const T& value); | ||||||
|  |  | ||||||
|   // Gets the value associated with the specified key. |   // Gets the value associated with the specified key. | ||||||
|   FORCE_INLINE JsonVariant get(JsonObjectKey) const; |   FORCE_INLINE JsonVariant get(JsonObjectKey) const; | ||||||
|   | |||||||
| @@ -116,6 +116,10 @@ inline bool JsonObject::set(const char *key, JsonObject &object) { | |||||||
| inline bool JsonObject::set(const char *key, const JsonVariant &value) { | inline bool JsonObject::set(const char *key, const JsonVariant &value) { | ||||||
|   return setNodeAt<const char *, const JsonVariant &>(key, value); |   return setNodeAt<const char *, const JsonVariant &>(key, value); | ||||||
| } | } | ||||||
|  | template <typename T> | ||||||
|  | inline bool JsonObject::set(const char *key, const T &value) { | ||||||
|  |   return setNodeAt<const char *, JsonVariant>(key, value); | ||||||
|  | } | ||||||
| inline bool JsonObject::set(const String &key, bool value) { | inline bool JsonObject::set(const String &key, bool value) { | ||||||
|   return setNodeAt<const String &, bool>(key, value); |   return setNodeAt<const String &, bool>(key, value); | ||||||
| } | } | ||||||
| @@ -166,6 +170,10 @@ inline bool JsonObject::set(const String &key, JsonObject &object) { | |||||||
| inline bool JsonObject::set(const String &key, const JsonVariant &value) { | inline bool JsonObject::set(const String &key, const JsonVariant &value) { | ||||||
|   return setNodeAt<const String &, const JsonVariant &>(key, value); |   return setNodeAt<const String &, const JsonVariant &>(key, value); | ||||||
| } | } | ||||||
|  | template <typename T> | ||||||
|  | inline bool JsonObject::set(const String &key, const T &value) { | ||||||
|  |   return setNodeAt<const String &, JsonVariant>(key, value); | ||||||
|  | } | ||||||
|  |  | ||||||
| template <typename TKey, typename TValue> | template <typename TKey, typename TValue> | ||||||
| inline bool JsonObject::setNodeAt(TKey key, TValue value) { | inline bool JsonObject::setNodeAt(TKey key, TValue value) { | ||||||
|   | |||||||
| @@ -8,6 +8,11 @@ | |||||||
|  |  | ||||||
| #include "JsonSubscriptBase.hpp" | #include "JsonSubscriptBase.hpp" | ||||||
|  |  | ||||||
|  | #ifdef _MSC_VER | ||||||
|  | #pragma warning(push) | ||||||
|  | #pragma warning(disable : 4522) | ||||||
|  | #endif | ||||||
|  |  | ||||||
| namespace ArduinoJson { | namespace ArduinoJson { | ||||||
|  |  | ||||||
| template <typename TKey> | template <typename TKey> | ||||||
| @@ -19,6 +24,17 @@ class JsonObjectSubscript | |||||||
|  |  | ||||||
|   using JsonSubscriptBase<JsonObjectSubscript<TKey> >::operator=; |   using JsonSubscriptBase<JsonObjectSubscript<TKey> >::operator=; | ||||||
|  |  | ||||||
|  |   JsonObjectSubscript<TKey>& operator=(const JsonObjectSubscript<TKey>& src) { | ||||||
|  |     return JsonSubscriptBase<JsonObjectSubscript<TKey> >::template assign< | ||||||
|  |         JsonVariant>(src); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   template <typename T> | ||||||
|  |   JsonObjectSubscript<TKey>& operator=(const T& src) { | ||||||
|  |     return JsonSubscriptBase<JsonObjectSubscript<TKey> >::template assign< | ||||||
|  |         JsonVariant>(src); | ||||||
|  |   } | ||||||
|  |  | ||||||
|   FORCE_INLINE bool success() const { return _object.containsKey(_key); } |   FORCE_INLINE bool success() const { return _object.containsKey(_key); } | ||||||
|  |  | ||||||
|   FORCE_INLINE operator JsonVariant() const { return _object.get(_key); } |   FORCE_INLINE operator JsonVariant() const { return _object.get(_key); } | ||||||
| @@ -60,4 +76,8 @@ inline std::ostream& operator<<( | |||||||
|   return source.printTo(os); |   return source.printTo(os); | ||||||
| } | } | ||||||
| #endif | #endif | ||||||
| } | }  // namespace ArduinoJson | ||||||
|  |  | ||||||
|  | #ifdef _MSC_VER | ||||||
|  | #pragma warning(pop) | ||||||
|  | #endif | ||||||
|   | |||||||
| @@ -68,10 +68,10 @@ class JsonSubscriptBase : public JsonVariantBase<TImpl> { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   FORCE_INLINE TImpl& operator=(JsonVariant value) { |   FORCE_INLINE TImpl& operator=(JsonVariant value) { | ||||||
|     return assign<JsonVariant>(value); |     return assign<const JsonVariant&>(value); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  private: |  protected: | ||||||
|   template <typename TValue> |   template <typename TValue> | ||||||
|   FORCE_INLINE TImpl& assign(TValue value) { |   FORCE_INLINE TImpl& assign(TValue value) { | ||||||
|     TImpl* that = static_cast<TImpl*>(this); |     TImpl* that = static_cast<TImpl*>(this); | ||||||
|   | |||||||
| @@ -1,5 +1,5 @@ | |||||||
| name=ArduinoJson | name=ArduinoJson | ||||||
| version=5.0.3 | version=5.0.4 | ||||||
| author=Benoit Blanchon <blog.benoitblanchon.fr> | author=Benoit Blanchon <blog.benoitblanchon.fr> | ||||||
| maintainer=Benoit Blanchon <blog.benoitblanchon.fr> | maintainer=Benoit Blanchon <blog.benoitblanchon.fr> | ||||||
| sentence=An efficient and elegant JSON library for Arduino. | sentence=An efficient and elegant JSON library for Arduino. | ||||||
|   | |||||||
| @@ -71,6 +71,26 @@ TEST_F(ArduinoStringTests, JsonObject_SetKeyValue) { | |||||||
|   ASSERT_STREQ("world", object["hello"]); |   ASSERT_STREQ("world", object["hello"]); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | TEST_F(ArduinoStringTests, JsonObject_SetToArraySubscript) { | ||||||
|  |   JsonArray &arr = _jsonBuffer.createArray(); | ||||||
|  |   arr.add("world"); | ||||||
|  |  | ||||||
|  |   JsonObject &object = _jsonBuffer.createObject(); | ||||||
|  |   object.set(String("hello"), arr[0]); | ||||||
|  |  | ||||||
|  |   ASSERT_STREQ("world", object["hello"]); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | TEST_F(ArduinoStringTests, JsonObject_SetToObjectSubscript) { | ||||||
|  |   JsonObject &arr = _jsonBuffer.createObject(); | ||||||
|  |   arr.set("x", "world"); | ||||||
|  |  | ||||||
|  |   JsonObject &object = _jsonBuffer.createObject(); | ||||||
|  |   object.set(String("hello"), arr["x"]); | ||||||
|  |  | ||||||
|  |   ASSERT_STREQ("world", object["hello"]); | ||||||
|  | } | ||||||
|  |  | ||||||
| TEST_F(ArduinoStringTests, JsonObject_Get) { | TEST_F(ArduinoStringTests, JsonObject_Get) { | ||||||
|   char json[] = "{\"key\":\"value\"}"; |   char json[] = "{\"key\":\"value\"}"; | ||||||
|   const JsonObject &object = _jsonBuffer.parseObject(json); |   const JsonObject &object = _jsonBuffer.parseObject(json); | ||||||
|   | |||||||
| @@ -22,9 +22,9 @@ TEST_F(DynamicJsonBuffer_Basic_Tests, InitialSizeIsZero) { | |||||||
|  |  | ||||||
| TEST_F(DynamicJsonBuffer_Basic_Tests, SizeIncreasesAfterAlloc) { | TEST_F(DynamicJsonBuffer_Basic_Tests, SizeIncreasesAfterAlloc) { | ||||||
|   buffer.alloc(1); |   buffer.alloc(1); | ||||||
|   ASSERT_LE(1, buffer.size()); |   ASSERT_LE(1U, buffer.size()); | ||||||
|   buffer.alloc(1); |   buffer.alloc(1); | ||||||
|   ASSERT_LE(2, buffer.size()); |   ASSERT_LE(2U, buffer.size()); | ||||||
| } | } | ||||||
|  |  | ||||||
| TEST_F(DynamicJsonBuffer_Basic_Tests, ReturnDifferentPointer) { | TEST_F(DynamicJsonBuffer_Basic_Tests, ReturnDifferentPointer) { | ||||||
|   | |||||||
							
								
								
									
										90
									
								
								test/JsonArray_Add_Tests.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										90
									
								
								test/JsonArray_Add_Tests.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,90 @@ | |||||||
|  | // Copyright Benoit Blanchon 2014-2015 | ||||||
|  | // MIT License | ||||||
|  | // | ||||||
|  | // Arduino JSON library | ||||||
|  | // https://github.com/bblanchon/ArduinoJson | ||||||
|  |  | ||||||
|  | #include <gtest/gtest.h> | ||||||
|  | #define ARDUINOJSON_ENABLE_STD_STREAM | ||||||
|  | #include <ArduinoJson.h> | ||||||
|  |  | ||||||
|  | class JsonArray_Add_Tests : public ::testing::Test { | ||||||
|  |  protected: | ||||||
|  |   JsonArray_Add_Tests() : _array(_jsonBuffer.createArray()) {} | ||||||
|  |  | ||||||
|  |   DynamicJsonBuffer _jsonBuffer; | ||||||
|  |   JsonArray& _array; | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | #define TEST_(name) TEST_F(JsonArray_Add_Tests, name) | ||||||
|  |  | ||||||
|  | TEST_(SizeIncreased_WhenValuesAreAdded) { | ||||||
|  |   _array.add("hello"); | ||||||
|  |   EXPECT_EQ(1U, _array.size()); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | TEST_(StoreInteger) { | ||||||
|  |   _array.add(123); | ||||||
|  |   EXPECT_EQ(123, _array[0].as<int>()); | ||||||
|  |   EXPECT_TRUE(_array[0].is<int>()); | ||||||
|  |   EXPECT_FALSE(_array[0].is<double>()); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | TEST_(StoreDouble) { | ||||||
|  |   _array.add(123.45); | ||||||
|  |   EXPECT_EQ(123.45, _array[0].as<double>()); | ||||||
|  |   EXPECT_TRUE(_array[0].is<double>()); | ||||||
|  |   EXPECT_FALSE(_array[0].is<int>()); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | TEST_(StoreBoolean) { | ||||||
|  |   _array.add(true); | ||||||
|  |   EXPECT_EQ(true, _array[0].as<bool>()); | ||||||
|  |   EXPECT_TRUE(_array[0].is<bool>()); | ||||||
|  |   EXPECT_FALSE(_array[0].is<int>()); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | TEST_(StoreString) { | ||||||
|  |   _array.add("hello"); | ||||||
|  |   EXPECT_STREQ("hello", _array[0].as<const char*>()); | ||||||
|  |   EXPECT_TRUE(_array[0].is<const char*>()); | ||||||
|  |   EXPECT_FALSE(_array[0].is<int>()); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | TEST_(StoreNestedArray) { | ||||||
|  |   JsonArray& arr = _jsonBuffer.createArray(); | ||||||
|  |  | ||||||
|  |   _array.add(arr); | ||||||
|  |  | ||||||
|  |   EXPECT_EQ(&arr, &_array[0].as<JsonArray&>()); | ||||||
|  |   EXPECT_TRUE(_array[0].is<JsonArray&>()); | ||||||
|  |   EXPECT_FALSE(_array[0].is<int>()); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | TEST_(StoreNestedObject) { | ||||||
|  |   JsonObject& obj = _jsonBuffer.createObject(); | ||||||
|  |  | ||||||
|  |   _array.add(obj); | ||||||
|  |  | ||||||
|  |   EXPECT_EQ(&obj, &_array[0].as<JsonObject&>()); | ||||||
|  |   EXPECT_TRUE(_array[0].is<JsonObject&>()); | ||||||
|  |   EXPECT_FALSE(_array[0].is<int>()); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | TEST_(StoreArraySubscript) { | ||||||
|  |   JsonArray& arr = _jsonBuffer.createArray(); | ||||||
|  |   arr.add("hello"); | ||||||
|  |  | ||||||
|  |   _array.add(arr[0]); | ||||||
|  |  | ||||||
|  |   EXPECT_STREQ("hello", _array[0]); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | TEST_(StoreObjectSubscript) { | ||||||
|  |   JsonObject& obj = _jsonBuffer.createObject(); | ||||||
|  |   obj["x"] = "hello"; | ||||||
|  |  | ||||||
|  |   _array.add(obj["x"]); | ||||||
|  |  | ||||||
|  |   EXPECT_STREQ("hello", _array[0]); | ||||||
|  | } | ||||||
							
								
								
									
										41
									
								
								test/JsonArray_Basic_Tests.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								test/JsonArray_Basic_Tests.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,41 @@ | |||||||
|  | // Copyright Benoit Blanchon 2014-2015 | ||||||
|  | // MIT License | ||||||
|  | // | ||||||
|  | // Arduino JSON library | ||||||
|  | // https://github.com/bblanchon/ArduinoJson | ||||||
|  |  | ||||||
|  | #include <gtest/gtest.h> | ||||||
|  | #define ARDUINOJSON_ENABLE_STD_STREAM | ||||||
|  | #include <ArduinoJson.h> | ||||||
|  |  | ||||||
|  | #define TEST_(name) TEST(JsonArray_Basic_Tests, name) | ||||||
|  |  | ||||||
|  | TEST_(SuccessIsTrue) { | ||||||
|  |   DynamicJsonBuffer _jsonBuffer; | ||||||
|  |   JsonArray& array = _jsonBuffer.createArray(); | ||||||
|  |  | ||||||
|  |   EXPECT_TRUE(array.success()); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | TEST_(InitialSizeIsZero) { | ||||||
|  |   DynamicJsonBuffer _jsonBuffer; | ||||||
|  |   JsonArray& array = _jsonBuffer.createArray(); | ||||||
|  |  | ||||||
|  |   EXPECT_EQ(0U, array.size()); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | TEST_(CreateNestedArray) { | ||||||
|  |   DynamicJsonBuffer _jsonBuffer; | ||||||
|  |   JsonArray& array = _jsonBuffer.createArray(); | ||||||
|  |  | ||||||
|  |   JsonArray& arr = array.createNestedArray(); | ||||||
|  |   EXPECT_EQ(&arr, &array[0].as<JsonArray&>()); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | TEST_(CreateNestedObject) { | ||||||
|  |   DynamicJsonBuffer _jsonBuffer; | ||||||
|  |   JsonArray& array = _jsonBuffer.createArray(); | ||||||
|  |  | ||||||
|  |   JsonObject& obj = array.createNestedObject(); | ||||||
|  |   EXPECT_EQ(&obj, &array[0].as<JsonObject&>()); | ||||||
|  | } | ||||||
| @@ -1,182 +0,0 @@ | |||||||
| // Copyright Benoit Blanchon 2014-2015 |  | ||||||
| // MIT License |  | ||||||
| // |  | ||||||
| // Arduino JSON library |  | ||||||
| // https://github.com/bblanchon/ArduinoJson |  | ||||||
|  |  | ||||||
| #include <gtest/gtest.h> |  | ||||||
| #include <ArduinoJson.h> |  | ||||||
|  |  | ||||||
| class JsonArray_Container_Tests : public ::testing::Test { |  | ||||||
|  protected: |  | ||||||
|   JsonArray_Container_Tests() : _array(_jsonBuffer.createArray()) {} |  | ||||||
|  |  | ||||||
|   template <typename T> |  | ||||||
|   void firstMustEqual(T expected) { |  | ||||||
|     itemMustEqual(0, expected); |  | ||||||
|   } |  | ||||||
|  |  | ||||||
|   template <typename T> |  | ||||||
|   void secondMustEqual(T expected) { |  | ||||||
|     itemMustEqual(1, expected); |  | ||||||
|   } |  | ||||||
|  |  | ||||||
|   template <typename T> |  | ||||||
|   void firstMustReference(const T& expected) { |  | ||||||
|     itemMustReference(0, expected); |  | ||||||
|   } |  | ||||||
|  |  | ||||||
|   template <typename T> |  | ||||||
|   void secondMustReference(const T& expected) { |  | ||||||
|     itemMustReference(1, expected); |  | ||||||
|   } |  | ||||||
|  |  | ||||||
|   void sizeMustBe(int expected) { EXPECT_EQ(expected, _array.size()); } |  | ||||||
|  |  | ||||||
|   DynamicJsonBuffer _jsonBuffer; |  | ||||||
|   JsonArray& _array; |  | ||||||
|  |  | ||||||
|  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&>()); |  | ||||||
|   } |  | ||||||
| }; |  | ||||||
|  |  | ||||||
| template <> |  | ||||||
| void JsonArray_Container_Tests::itemMustEqual(int index, const char* expected) { |  | ||||||
|   EXPECT_STREQ(expected, _array[index].asString()); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| TEST_F(JsonArray_Container_Tests, SuccessIsTrue) { |  | ||||||
|   EXPECT_TRUE(_array.success()); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| TEST_F(JsonArray_Container_Tests, InitialSizeIsZero) { sizeMustBe(0); } |  | ||||||
|  |  | ||||||
| TEST_F(JsonArray_Container_Tests, Grow_WhenValuesAreAdded) { |  | ||||||
|   _array.add("hello"); |  | ||||||
|   sizeMustBe(1); |  | ||||||
|  |  | ||||||
|   _array.add("world"); |  | ||||||
|   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); |  | ||||||
|  |  | ||||||
|   firstMustEqual(123); |  | ||||||
|   secondMustEqual(456); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| TEST_F(JsonArray_Container_Tests, CanStoreDoubles) { |  | ||||||
|   _array.add(123.45); |  | ||||||
|   _array.add(456.78); |  | ||||||
|  |  | ||||||
|   firstMustEqual(123.45); |  | ||||||
|   secondMustEqual(456.78); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| TEST_F(JsonArray_Container_Tests, CanStoreBooleans) { |  | ||||||
|   _array.add(true); |  | ||||||
|   _array.add(false); |  | ||||||
|  |  | ||||||
|   firstMustEqual(true); |  | ||||||
|   secondMustEqual(false); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| TEST_F(JsonArray_Container_Tests, CanStoreStrings) { |  | ||||||
|   _array.add("hello"); |  | ||||||
|   _array.add("world"); |  | ||||||
|  |  | ||||||
|   firstMustEqual("hello"); |  | ||||||
|   secondMustEqual("world"); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| TEST_F(JsonArray_Container_Tests, CanStoreNestedArrays) { |  | ||||||
|   JsonArray& inner_array1 = _jsonBuffer.createArray(); |  | ||||||
|   JsonArray& inner_array2 = _jsonBuffer.createArray(); |  | ||||||
|  |  | ||||||
|   _array.add(inner_array1); |  | ||||||
|   _array.add(inner_array2); |  | ||||||
|  |  | ||||||
|   firstMustReference(inner_array1); |  | ||||||
|   secondMustReference(inner_array2); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| TEST_F(JsonArray_Container_Tests, CanStoreNestedObjects) { |  | ||||||
|   JsonObject& innerObject1 = _jsonBuffer.createObject(); |  | ||||||
|   JsonObject& innerObject2 = _jsonBuffer.createObject(); |  | ||||||
|  |  | ||||||
|   _array.add(innerObject1); |  | ||||||
|   _array.add(innerObject2); |  | ||||||
|  |  | ||||||
|   firstMustReference(innerObject1); |  | ||||||
|   secondMustReference(innerObject2); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| TEST_F(JsonArray_Container_Tests, CanCreateNestedArrays) { |  | ||||||
|   JsonArray& inner_array1 = _array.createNestedArray(); |  | ||||||
|   JsonArray& inner_array2 = _array.createNestedArray(); |  | ||||||
|  |  | ||||||
|   firstMustReference(inner_array1); |  | ||||||
|   secondMustReference(inner_array2); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| TEST_F(JsonArray_Container_Tests, CanCreateNestedObjects) { |  | ||||||
|   JsonObject& innerObject1 = _array.createNestedObject(); |  | ||||||
|   JsonObject& innerObject2 = _array.createNestedObject(); |  | ||||||
|  |  | ||||||
|   firstMustReference(innerObject1); |  | ||||||
|   secondMustReference(innerObject2); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| TEST_F(JsonArray_Container_Tests, RemoveFirstElement) { |  | ||||||
|   _array.add("one"); |  | ||||||
|   _array.add("two"); |  | ||||||
|   _array.add("three"); |  | ||||||
|  |  | ||||||
|   _array.removeAt(0); |  | ||||||
|  |  | ||||||
|   sizeMustBe(2); |  | ||||||
|   firstMustEqual("two"); |  | ||||||
|   secondMustEqual("three"); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| TEST_F(JsonArray_Container_Tests, RemoveMiddleElement) { |  | ||||||
|   _array.add("one"); |  | ||||||
|   _array.add("two"); |  | ||||||
|   _array.add("three"); |  | ||||||
|  |  | ||||||
|   _array.removeAt(1); |  | ||||||
|  |  | ||||||
|   sizeMustBe(2); |  | ||||||
|   firstMustEqual("one"); |  | ||||||
|   secondMustEqual("three"); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| TEST_F(JsonArray_Container_Tests, RemoveLastElement) { |  | ||||||
|   _array.add("one"); |  | ||||||
|   _array.add("two"); |  | ||||||
|   _array.add("three"); |  | ||||||
|  |  | ||||||
|   _array.removeAt(2); |  | ||||||
|  |  | ||||||
|   sizeMustBe(2); |  | ||||||
|   firstMustEqual("one"); |  | ||||||
|   secondMustEqual("two"); |  | ||||||
| } |  | ||||||
							
								
								
									
										46
									
								
								test/JsonArray_Remove_Tests.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								test/JsonArray_Remove_Tests.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,46 @@ | |||||||
|  | // Copyright Benoit Blanchon 2014-2015 | ||||||
|  | // MIT License | ||||||
|  | // | ||||||
|  | // Arduino JSON library | ||||||
|  | // https://github.com/bblanchon/ArduinoJson | ||||||
|  |  | ||||||
|  | #include <gtest/gtest.h> | ||||||
|  | #include <ArduinoJson.h> | ||||||
|  |  | ||||||
|  | class JsonArray_Remove_Tests : public ::testing::Test { | ||||||
|  |  protected: | ||||||
|  |   JsonArray_Remove_Tests() : _array(_jsonBuffer.createArray()) { | ||||||
|  |     _array.add("one"); | ||||||
|  |     _array.add("two"); | ||||||
|  |     _array.add("three"); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   DynamicJsonBuffer _jsonBuffer; | ||||||
|  |   JsonArray& _array; | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | #define TEST_(name) TEST_F(JsonArray_Remove_Tests, name) | ||||||
|  |  | ||||||
|  | TEST_(RemoveFirstElement) { | ||||||
|  |   _array.removeAt(0); | ||||||
|  |  | ||||||
|  |   EXPECT_EQ(2, _array.size()); | ||||||
|  |   EXPECT_STREQ("two", _array[0]); | ||||||
|  |   EXPECT_STREQ("three", _array[1]); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | TEST_(RemoveMiddleElement) { | ||||||
|  |   _array.removeAt(1); | ||||||
|  |  | ||||||
|  |   EXPECT_EQ(2, _array.size()); | ||||||
|  |   EXPECT_STREQ("one", _array[0]); | ||||||
|  |   EXPECT_STREQ("three", _array[1]); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | TEST_(RemoveLastElement) { | ||||||
|  |   _array.removeAt(2); | ||||||
|  |  | ||||||
|  |   EXPECT_EQ(2, _array.size()); | ||||||
|  |   EXPECT_STREQ("one", _array[0]); | ||||||
|  |   EXPECT_STREQ("two", _array[1]); | ||||||
|  | } | ||||||
							
								
								
									
										90
									
								
								test/JsonArray_Set_Tests.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										90
									
								
								test/JsonArray_Set_Tests.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,90 @@ | |||||||
|  | // Copyright Benoit Blanchon 2014-2015 | ||||||
|  | // MIT License | ||||||
|  | // | ||||||
|  | // Arduino JSON library | ||||||
|  | // https://github.com/bblanchon/ArduinoJson | ||||||
|  |  | ||||||
|  | #include <gtest/gtest.h> | ||||||
|  | #define ARDUINOJSON_ENABLE_STD_STREAM | ||||||
|  | #include <ArduinoJson.h> | ||||||
|  |  | ||||||
|  | class JsonArray_Set_Tests : public ::testing::Test { | ||||||
|  |  protected: | ||||||
|  |   JsonArray_Set_Tests() : _array(_jsonBuffer.createArray()) { _array.add(0); } | ||||||
|  |  | ||||||
|  |   DynamicJsonBuffer _jsonBuffer; | ||||||
|  |   JsonArray& _array; | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | #define TEST_(name) TEST_F(JsonArray_Set_Tests, name) | ||||||
|  |  | ||||||
|  | TEST_(SizeIsUnchanged) { | ||||||
|  |   _array.set(0, "hello"); | ||||||
|  |   EXPECT_EQ(1U, _array.size()); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | TEST_(StoreInteger) { | ||||||
|  |   _array.set(0, 123); | ||||||
|  |   EXPECT_EQ(123, _array[0].as<int>()); | ||||||
|  |   EXPECT_TRUE(_array[0].is<int>()); | ||||||
|  |   EXPECT_FALSE(_array[0].is<double>()); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | TEST_(StoreDouble) { | ||||||
|  |   _array.set(0, 123.45); | ||||||
|  |   EXPECT_EQ(123.45, _array[0].as<double>()); | ||||||
|  |   EXPECT_TRUE(_array[0].is<double>()); | ||||||
|  |   EXPECT_FALSE(_array[0].is<int>()); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | TEST_(StoreBoolean) { | ||||||
|  |   _array.set(0, true); | ||||||
|  |   EXPECT_EQ(true, _array[0].as<bool>()); | ||||||
|  |   EXPECT_TRUE(_array[0].is<bool>()); | ||||||
|  |   EXPECT_FALSE(_array[0].is<int>()); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | TEST_(StoreString) { | ||||||
|  |   _array.set(0, "hello"); | ||||||
|  |   EXPECT_STREQ("hello", _array[0].as<const char*>()); | ||||||
|  |   EXPECT_TRUE(_array[0].is<const char*>()); | ||||||
|  |   EXPECT_FALSE(_array[0].is<int>()); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | TEST_(StoreNestedArray) { | ||||||
|  |   JsonArray& arr = _jsonBuffer.createArray(); | ||||||
|  |  | ||||||
|  |   _array.set(0, arr); | ||||||
|  |  | ||||||
|  |   EXPECT_EQ(&arr, &_array[0].as<JsonArray&>()); | ||||||
|  |   EXPECT_TRUE(_array[0].is<JsonArray&>()); | ||||||
|  |   EXPECT_FALSE(_array[0].is<int>()); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | TEST_(StoreNestedObject) { | ||||||
|  |   JsonObject& obj = _jsonBuffer.createObject(); | ||||||
|  |  | ||||||
|  |   _array.set(0, obj); | ||||||
|  |  | ||||||
|  |   EXPECT_EQ(&obj, &_array[0].as<JsonObject&>()); | ||||||
|  |   EXPECT_TRUE(_array[0].is<JsonObject&>()); | ||||||
|  |   EXPECT_FALSE(_array[0].is<int>()); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | TEST_(StoreArraySubscript) { | ||||||
|  |   JsonArray& arr = _jsonBuffer.createArray(); | ||||||
|  |   arr.add("hello"); | ||||||
|  |  | ||||||
|  |   _array.set(0, arr[0]); | ||||||
|  |  | ||||||
|  |   EXPECT_STREQ("hello", _array[0]); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | TEST_(StoreObjectSubscript) { | ||||||
|  |   JsonObject& obj = _jsonBuffer.createObject(); | ||||||
|  |   obj["x"] = "hello"; | ||||||
|  |  | ||||||
|  |   _array.set(0, obj["x"]); | ||||||
|  |  | ||||||
|  |   EXPECT_STREQ("hello", _array[0]); | ||||||
|  | } | ||||||
							
								
								
									
										92
									
								
								test/JsonArray_Subscript_Tests.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										92
									
								
								test/JsonArray_Subscript_Tests.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,92 @@ | |||||||
|  | // Copyright Benoit Blanchon 2014-2015 | ||||||
|  | // MIT License | ||||||
|  | // | ||||||
|  | // Arduino JSON library | ||||||
|  | // https://github.com/bblanchon/ArduinoJson | ||||||
|  |  | ||||||
|  | #include <gtest/gtest.h> | ||||||
|  | #define ARDUINOJSON_ENABLE_STD_STREAM | ||||||
|  | #include <ArduinoJson.h> | ||||||
|  |  | ||||||
|  | class JsonArray_Subscript_Tests : public ::testing::Test { | ||||||
|  |  protected: | ||||||
|  |   JsonArray_Subscript_Tests() : _array(_jsonBuffer.createArray()) { | ||||||
|  |     _array.add(0); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   DynamicJsonBuffer _jsonBuffer; | ||||||
|  |   JsonArray& _array; | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | #define TEST_(name) TEST_F(JsonArray_Subscript_Tests, name) | ||||||
|  |  | ||||||
|  | TEST_(SizeIsUnchanged) { | ||||||
|  |   _array[0] = "hello"; | ||||||
|  |   EXPECT_EQ(1U, _array.size()); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | TEST_(StoreInteger) { | ||||||
|  |   _array[0] = 123; | ||||||
|  |   EXPECT_EQ(123, _array[0].as<int>()); | ||||||
|  |   EXPECT_TRUE(_array[0].is<int>()); | ||||||
|  |   EXPECT_FALSE(_array[0].is<double>()); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | TEST_(StoreDouble) { | ||||||
|  |   _array[0] = 123.45; | ||||||
|  |   EXPECT_EQ(123.45, _array[0].as<double>()); | ||||||
|  |   EXPECT_TRUE(_array[0].is<double>()); | ||||||
|  |   EXPECT_FALSE(_array[0].is<int>()); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | TEST_(StoreBoolean) { | ||||||
|  |   _array[0] = true; | ||||||
|  |   EXPECT_EQ(true, _array[0].as<bool>()); | ||||||
|  |   EXPECT_TRUE(_array[0].is<bool>()); | ||||||
|  |   EXPECT_FALSE(_array[0].is<int>()); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | TEST_(StoreString) { | ||||||
|  |   _array[0] = "hello"; | ||||||
|  |   EXPECT_STREQ("hello", _array[0].as<const char*>()); | ||||||
|  |   EXPECT_TRUE(_array[0].is<const char*>()); | ||||||
|  |   EXPECT_FALSE(_array[0].is<int>()); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | TEST_(StoreNestedArray) { | ||||||
|  |   JsonArray& arr = _jsonBuffer.createArray(); | ||||||
|  |  | ||||||
|  |   _array[0] = arr; | ||||||
|  |  | ||||||
|  |   EXPECT_EQ(&arr, &_array[0].as<JsonArray&>()); | ||||||
|  |   EXPECT_TRUE(_array[0].is<JsonArray&>()); | ||||||
|  |   EXPECT_FALSE(_array[0].is<int>()); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | TEST_(StoreNestedObject) { | ||||||
|  |   JsonObject& obj = _jsonBuffer.createObject(); | ||||||
|  |  | ||||||
|  |   _array[0] = obj; | ||||||
|  |  | ||||||
|  |   EXPECT_EQ(&obj, &_array[0].as<JsonObject&>()); | ||||||
|  |   EXPECT_TRUE(_array[0].is<JsonObject&>()); | ||||||
|  |   EXPECT_FALSE(_array[0].is<int>()); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | TEST_(StoreArraySubscript) { | ||||||
|  |   JsonArray& arr = _jsonBuffer.createArray(); | ||||||
|  |   arr.add("hello"); | ||||||
|  |  | ||||||
|  |   _array[0] = arr[0]; | ||||||
|  |  | ||||||
|  |   EXPECT_STREQ("hello", _array[0]); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | TEST_(StoreObjectSubscript) { | ||||||
|  |   JsonObject& obj = _jsonBuffer.createObject(); | ||||||
|  |   obj["x"] = "hello"; | ||||||
|  |  | ||||||
|  |   _array[0] = obj["x"]; | ||||||
|  |  | ||||||
|  |   EXPECT_STREQ("hello", _array[0]); | ||||||
|  | } | ||||||
							
								
								
									
										24
									
								
								test/JsonObject_Basic_Tests.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								test/JsonObject_Basic_Tests.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,24 @@ | |||||||
|  | // Copyright Benoit Blanchon 2014-2015 | ||||||
|  | // MIT License | ||||||
|  | // | ||||||
|  | // Arduino JSON library | ||||||
|  | // https://github.com/bblanchon/ArduinoJson | ||||||
|  |  | ||||||
|  | #include <gtest/gtest.h> | ||||||
|  | #include <ArduinoJson.h> | ||||||
|  |  | ||||||
|  | #define TEST_(name) TEST(JsonObject_Basic_Tests, name) | ||||||
|  |  | ||||||
|  | TEST_(InitialSizeIsZero) { | ||||||
|  |   DynamicJsonBuffer _jsonBuffer; | ||||||
|  |   JsonObject& _object = _jsonBuffer.createObject(); | ||||||
|  |  | ||||||
|  |   EXPECT_EQ(0, _object.size()); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | TEST_(SuccessIsTrue) { | ||||||
|  |   DynamicJsonBuffer _jsonBuffer; | ||||||
|  |   JsonObject& _object = _jsonBuffer.createObject(); | ||||||
|  |  | ||||||
|  |   EXPECT_TRUE(_object.success()); | ||||||
|  | } | ||||||
| @@ -1,133 +0,0 @@ | |||||||
| // Copyright Benoit Blanchon 2014-2015 |  | ||||||
| // MIT License |  | ||||||
| // |  | ||||||
| // Arduino JSON library |  | ||||||
| // https://github.com/bblanchon/ArduinoJson |  | ||||||
|  |  | ||||||
| #include <gtest/gtest.h> |  | ||||||
| #include <ArduinoJson.h> |  | ||||||
|  |  | ||||||
| class JsonObject_Container_Tests : public ::testing::Test { |  | ||||||
|  public: |  | ||||||
|   JsonObject_Container_Tests() : _object(_jsonBuffer.createObject()) {} |  | ||||||
|  |  | ||||||
|  protected: |  | ||||||
|   DynamicJsonBuffer _jsonBuffer; |  | ||||||
|   JsonObject& _object; |  | ||||||
| }; |  | ||||||
|  |  | ||||||
| TEST_F(JsonObject_Container_Tests, InitialSizeIsZero) { |  | ||||||
|   EXPECT_EQ(0, _object.size()); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| TEST_F(JsonObject_Container_Tests, Grow_WhenValuesAreAdded) { |  | ||||||
|   _object["hello"] = 1; |  | ||||||
|   EXPECT_EQ(1, _object.size()); |  | ||||||
|  |  | ||||||
|   _object.set("world", 2); |  | ||||||
|   EXPECT_EQ(2, _object.size()); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| TEST_F(JsonObject_Container_Tests, DoNotGrow_WhenSameValueIsAdded) { |  | ||||||
|   _object["hello"] = 1; |  | ||||||
|   EXPECT_EQ(1, _object.size()); |  | ||||||
|  |  | ||||||
|   _object["hello"] = 2; |  | ||||||
|   EXPECT_EQ(1, _object.size()); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| TEST_F(JsonObject_Container_Tests, Shrink_WhenValuesAreRemoved) { |  | ||||||
|   _object["hello"] = 1; |  | ||||||
|   _object["world"] = 2; |  | ||||||
|  |  | ||||||
|   _object.remove("hello"); |  | ||||||
|   EXPECT_EQ(1, _object.size()); |  | ||||||
|  |  | ||||||
|   _object.remove("world"); |  | ||||||
|   EXPECT_EQ(0, _object.size()); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| TEST_F(JsonObject_Container_Tests, |  | ||||||
|        DoNotShrink_WhenRemoveIsCalledWithAWrongKey) { |  | ||||||
|   _object["hello"] = 1; |  | ||||||
|   _object["world"] = 2; |  | ||||||
|  |  | ||||||
|   _object.remove(":-P"); |  | ||||||
|  |  | ||||||
|   EXPECT_EQ(2, _object.size()); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| 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>()); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| 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>()); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| 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>()); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| 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*>()); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| TEST_F(JsonObject_Container_Tests, CanStoreArrays) { |  | ||||||
|   JsonArray& array1 = _jsonBuffer.createArray(); |  | ||||||
|   JsonArray& array2 = _jsonBuffer.createArray(); |  | ||||||
|  |  | ||||||
|   _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()); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| TEST_F(JsonObject_Container_Tests, CanStoreObjects) { |  | ||||||
|   JsonObject& object1 = _jsonBuffer.createObject(); |  | ||||||
|   JsonObject& object2 = _jsonBuffer.createObject(); |  | ||||||
|  |  | ||||||
|   _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()); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| TEST_F(JsonObject_Container_Tests, ContainsKeyReturnsFalseForNonExistingKey) { |  | ||||||
|   EXPECT_FALSE(_object.containsKey("hello")); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| TEST_F(JsonObject_Container_Tests, ContainsKeyReturnsTrueForDefinedValue) { |  | ||||||
|   _object.set("hello", 42); |  | ||||||
|   EXPECT_TRUE(_object.containsKey("hello")); |  | ||||||
| } |  | ||||||
							
								
								
									
										38
									
								
								test/JsonObject_ContainsKey_Tests.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								test/JsonObject_ContainsKey_Tests.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,38 @@ | |||||||
|  | // Copyright Benoit Blanchon 2014-2015 | ||||||
|  | // MIT License | ||||||
|  | // | ||||||
|  | // Arduino JSON library | ||||||
|  | // https://github.com/bblanchon/ArduinoJson | ||||||
|  |  | ||||||
|  | #include <gtest/gtest.h> | ||||||
|  | #include <ArduinoJson.h> | ||||||
|  |  | ||||||
|  | #define TEST_(name) TEST(JsonObject_Basic_Tests, name) | ||||||
|  |  | ||||||
|  | TEST_(ContainsKeyReturnsFalseForNonExistingKey) { | ||||||
|  |   DynamicJsonBuffer _jsonBuffer; | ||||||
|  |   JsonObject& _object = _jsonBuffer.createObject(); | ||||||
|  |  | ||||||
|  |   _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")); | ||||||
|  | } | ||||||
							
								
								
									
										30
									
								
								test/JsonObject_Remove_Tests.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								test/JsonObject_Remove_Tests.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,30 @@ | |||||||
|  | // Copyright Benoit Blanchon 2014-2015 | ||||||
|  | // MIT License | ||||||
|  | // | ||||||
|  | // Arduino JSON library | ||||||
|  | // https://github.com/bblanchon/ArduinoJson | ||||||
|  |  | ||||||
|  | #include <gtest/gtest.h> | ||||||
|  | #include <ArduinoJson.h> | ||||||
|  |  | ||||||
|  | #define TEST_(name) TEST(JsonObject_Remove_Tests, name) | ||||||
|  |  | ||||||
|  | TEST_(SizeDecreased_WhenValuesAreRemoved) { | ||||||
|  |   DynamicJsonBuffer _jsonBuffer; | ||||||
|  |   JsonObject& _object = _jsonBuffer.createObject(); | ||||||
|  |   _object["hello"] = 1; | ||||||
|  |  | ||||||
|  |   _object.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()); | ||||||
|  | } | ||||||
							
								
								
									
										101
									
								
								test/JsonObject_Set_Tests.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										101
									
								
								test/JsonObject_Set_Tests.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,101 @@ | |||||||
|  | // Copyright Benoit Blanchon 2014-2015 | ||||||
|  | // MIT License | ||||||
|  | // | ||||||
|  | // Arduino JSON library | ||||||
|  | // https://github.com/bblanchon/ArduinoJson | ||||||
|  |  | ||||||
|  | #include <gtest/gtest.h> | ||||||
|  | #define ARDUINOJSON_ENABLE_STD_STREAM | ||||||
|  | #include <ArduinoJson.h> | ||||||
|  |  | ||||||
|  | class JsonObject_Set_Tests : public ::testing::Test { | ||||||
|  |  public: | ||||||
|  |   JsonObject_Set_Tests() : _object(_jsonBuffer.createObject()) {} | ||||||
|  |  | ||||||
|  |  protected: | ||||||
|  |   DynamicJsonBuffer _jsonBuffer; | ||||||
|  |   JsonObject& _object; | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | #define TEST_(name) TEST_F(JsonObject_Set_Tests, name) | ||||||
|  |  | ||||||
|  | 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_(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"].asArray()); | ||||||
|  |   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"].asObject()); | ||||||
|  |   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"]); | ||||||
|  | } | ||||||
							
								
								
									
										101
									
								
								test/JsonObject_Subscript_Tests.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										101
									
								
								test/JsonObject_Subscript_Tests.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,101 @@ | |||||||
|  | // Copyright Benoit Blanchon 2014-2015 | ||||||
|  | // MIT License | ||||||
|  | // | ||||||
|  | // Arduino JSON library | ||||||
|  | // https://github.com/bblanchon/ArduinoJson | ||||||
|  |  | ||||||
|  | #include <gtest/gtest.h> | ||||||
|  | #define ARDUINOJSON_ENABLE_STD_STREAM | ||||||
|  | #include <ArduinoJson.h> | ||||||
|  |  | ||||||
|  | class JsonObject_Subscript_Tests : public ::testing::Test { | ||||||
|  |  public: | ||||||
|  |   JsonObject_Subscript_Tests() : _object(_jsonBuffer.createObject()) {} | ||||||
|  |  | ||||||
|  |  protected: | ||||||
|  |   DynamicJsonBuffer _jsonBuffer; | ||||||
|  |   JsonObject& _object; | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | #define TEST_(name) TEST_F(JsonObject_Subscript_Tests, name) | ||||||
|  |  | ||||||
|  | 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_(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_(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*>()); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | TEST_(StoreArray) { | ||||||
|  |   JsonArray& arr = _jsonBuffer.createArray(); | ||||||
|  |  | ||||||
|  |   _object["hello"] = arr; | ||||||
|  |  | ||||||
|  |   EXPECT_EQ(&arr, &_object["hello"].asArray()); | ||||||
|  |   EXPECT_TRUE(_object["hello"].is<JsonArray&>()); | ||||||
|  |   EXPECT_FALSE(_object["hello"].is<JsonObject&>()); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | TEST_(StoreObject) { | ||||||
|  |   JsonObject& obj = _jsonBuffer.createObject(); | ||||||
|  |  | ||||||
|  |   _object["hello"] = obj; | ||||||
|  |  | ||||||
|  |   EXPECT_EQ(&obj, &_object["hello"].asObject()); | ||||||
|  |   EXPECT_TRUE(_object["hello"].is<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"]); | ||||||
|  | } | ||||||
| @@ -26,9 +26,9 @@ TEST_F(StaticJsonBuffer_Basic_Tests, InitialSizeIsZero) { | |||||||
|  |  | ||||||
| TEST_F(StaticJsonBuffer_Basic_Tests, GrowsAfterAlloc) { | TEST_F(StaticJsonBuffer_Basic_Tests, GrowsAfterAlloc) { | ||||||
|   buffer.alloc(1); |   buffer.alloc(1); | ||||||
|   ASSERT_LE(1, buffer.size()); |   ASSERT_LE(1U, buffer.size()); | ||||||
|   buffer.alloc(1); |   buffer.alloc(1); | ||||||
|   ASSERT_LE(2, buffer.size()); |   ASSERT_LE(2U, buffer.size()); | ||||||
| } | } | ||||||
|  |  | ||||||
| TEST_F(StaticJsonBuffer_Basic_Tests, DoesntGrowWhenFull) { | TEST_F(StaticJsonBuffer_Basic_Tests, DoesntGrowWhenFull) { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user