mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 16:14:11 +01:00 
			
		
		
		
	Moved ancillary files to extras/ (fixes #1011)
				
					
				
			This commit is contained in:
		
							
								
								
									
										23
									
								
								extras/tests/JsonObject/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								extras/tests/JsonObject/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,23 @@ | ||||
| # ArduinoJson - arduinojson.org | ||||
| # Copyright Benoit Blanchon 2014-2019 | ||||
| # MIT License | ||||
|  | ||||
| add_executable(JsonObjectTests  | ||||
| 	containsKey.cpp | ||||
| 	copy.cpp | ||||
| 	createNestedArray.cpp | ||||
| 	createNestedObject.cpp | ||||
| 	equals.cpp | ||||
| 	invalid.cpp | ||||
| 	isNull.cpp | ||||
| 	iterator.cpp | ||||
| 	memoryUsage.cpp | ||||
| 	nesting.cpp | ||||
| 	remove.cpp | ||||
| 	size.cpp | ||||
| 	std_string.cpp | ||||
| 	subscript.cpp | ||||
| ) | ||||
|  | ||||
| target_link_libraries(JsonObjectTests catch) | ||||
| add_test(JsonObject JsonObjectTests) | ||||
							
								
								
									
										39
									
								
								extras/tests/JsonObject/containsKey.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								extras/tests/JsonObject/containsKey.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,39 @@ | ||||
| // ArduinoJson - arduinojson.org | ||||
| // Copyright Benoit Blanchon 2014-2019 | ||||
| // MIT License | ||||
|  | ||||
| #include <ArduinoJson.h> | ||||
| #include <catch.hpp> | ||||
|  | ||||
| TEST_CASE("JsonObject::containsKey()") { | ||||
|   DynamicJsonDocument doc(4096); | ||||
|   JsonObject obj = doc.to<JsonObject>(); | ||||
|   obj["hello"] = 42; | ||||
|  | ||||
|   SECTION("returns true only if key is present") { | ||||
|     REQUIRE(false == obj.containsKey("world")); | ||||
|     REQUIRE(true == obj.containsKey("hello")); | ||||
|   } | ||||
|  | ||||
|   SECTION("works with JsonObjectConst") { | ||||
|     JsonObjectConst cobj = obj; | ||||
|     REQUIRE(false == cobj.containsKey("world")); | ||||
|     REQUIRE(true == cobj.containsKey("hello")); | ||||
|   } | ||||
|  | ||||
|   SECTION("returns false after remove()") { | ||||
|     obj.remove("hello"); | ||||
|  | ||||
|     REQUIRE(false == obj.containsKey("hello")); | ||||
|   } | ||||
|  | ||||
| #ifdef HAS_VARIABLE_LENGTH_ARRAY | ||||
|   SECTION("key is a VLA") { | ||||
|     int i = 16; | ||||
|     char vla[i]; | ||||
|     strcpy(vla, "hello"); | ||||
|  | ||||
|     REQUIRE(true == obj.containsKey(vla)); | ||||
|   } | ||||
| #endif | ||||
| } | ||||
							
								
								
									
										68
									
								
								extras/tests/JsonObject/copy.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										68
									
								
								extras/tests/JsonObject/copy.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,68 @@ | ||||
| // ArduinoJson - arduinojson.org | ||||
| // Copyright Benoit Blanchon 2014-2019 | ||||
| // MIT License | ||||
|  | ||||
| #include <ArduinoJson.h> | ||||
| #include <catch.hpp> | ||||
|  | ||||
| TEST_CASE("JsonObject::set()") { | ||||
|   DynamicJsonDocument doc1(4096); | ||||
|   DynamicJsonDocument doc2(4096); | ||||
|  | ||||
|   JsonObject obj1 = doc1.to<JsonObject>(); | ||||
|   JsonObject obj2 = doc2.to<JsonObject>(); | ||||
|  | ||||
|   SECTION("doesn't copy static string in key or value") { | ||||
|     obj1["hello"] = "world"; | ||||
|  | ||||
|     obj2.set(obj1); | ||||
|  | ||||
|     REQUIRE(doc1.memoryUsage() == doc2.memoryUsage()); | ||||
|     REQUIRE(obj2["hello"] == std::string("world")); | ||||
|   } | ||||
|  | ||||
|   SECTION("copy local string value") { | ||||
|     obj1["hello"] = std::string("world"); | ||||
|  | ||||
|     obj2.set(obj1); | ||||
|  | ||||
|     REQUIRE(doc1.memoryUsage() == doc2.memoryUsage()); | ||||
|     REQUIRE(obj2["hello"] == std::string("world")); | ||||
|   } | ||||
|  | ||||
|   SECTION("copy local key") { | ||||
|     obj1[std::string("hello")] = "world"; | ||||
|  | ||||
|     obj2.set(obj1); | ||||
|  | ||||
|     REQUIRE(doc1.memoryUsage() == doc2.memoryUsage()); | ||||
|     REQUIRE(obj2["hello"] == std::string("world")); | ||||
|   } | ||||
|  | ||||
|   SECTION("copy string from deserializeJson()") { | ||||
|     deserializeJson(doc1, "{'hello':'world'}"); | ||||
|  | ||||
|     obj2.set(obj1); | ||||
|  | ||||
|     REQUIRE(doc1.memoryUsage() == doc2.memoryUsage()); | ||||
|     REQUIRE(obj2["hello"] == std::string("world")); | ||||
|   } | ||||
|  | ||||
|   SECTION("copy string from deserializeMsgPack()") { | ||||
|     deserializeMsgPack(doc1, "\x81\xA5hello\xA5world"); | ||||
|  | ||||
|     obj2.set(obj1); | ||||
|  | ||||
|     REQUIRE(doc1.memoryUsage() == doc2.memoryUsage()); | ||||
|     REQUIRE(obj2["hello"] == std::string("world")); | ||||
|   } | ||||
|  | ||||
|   SECTION("should work with JsonObjectConst") { | ||||
|     obj1["hello"] = "world"; | ||||
|  | ||||
|     obj2.set(static_cast<JsonObjectConst>(obj1)); | ||||
|  | ||||
|     REQUIRE(doc1.memoryUsage() == doc2.memoryUsage()); | ||||
|     REQUIRE(obj2["hello"] == std::string("world")); | ||||
|   } | ||||
| } | ||||
							
								
								
									
										27
									
								
								extras/tests/JsonObject/createNestedArray.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								extras/tests/JsonObject/createNestedArray.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,27 @@ | ||||
| // ArduinoJson - arduinojson.org | ||||
| // Copyright Benoit Blanchon 2014-2019 | ||||
| // MIT License | ||||
|  | ||||
| #include <ArduinoJson.h> | ||||
| #include <catch.hpp> | ||||
|  | ||||
| TEST_CASE("JsonObject::createNestedArray()") { | ||||
|   DynamicJsonDocument doc(4096); | ||||
|   JsonObject obj = doc.to<JsonObject>(); | ||||
|  | ||||
|   SECTION("key is a const char*") { | ||||
|     JsonArray arr = obj.createNestedArray("hello"); | ||||
|     REQUIRE(arr.isNull() == false); | ||||
|   } | ||||
|  | ||||
| #ifdef HAS_VARIABLE_LENGTH_ARRAY | ||||
|   SECTION("key is a VLA") { | ||||
|     int i = 16; | ||||
|     char vla[i]; | ||||
|     strcpy(vla, "hello"); | ||||
|  | ||||
|     JsonArray arr = obj.createNestedArray(vla); | ||||
|     REQUIRE(arr.isNull() == false); | ||||
|   } | ||||
| #endif | ||||
| } | ||||
							
								
								
									
										25
									
								
								extras/tests/JsonObject/createNestedObject.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								extras/tests/JsonObject/createNestedObject.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,25 @@ | ||||
| // ArduinoJson - arduinojson.org | ||||
| // Copyright Benoit Blanchon 2014-2019 | ||||
| // MIT License | ||||
|  | ||||
| #include <ArduinoJson.h> | ||||
| #include <catch.hpp> | ||||
|  | ||||
| TEST_CASE("JsonObject::createNestedObject()") { | ||||
|   DynamicJsonDocument doc(4096); | ||||
|   JsonObject obj = doc.to<JsonObject>(); | ||||
|  | ||||
|   SECTION("key is a const char*") { | ||||
|     obj.createNestedObject("hello"); | ||||
|   } | ||||
|  | ||||
| #ifdef HAS_VARIABLE_LENGTH_ARRAY | ||||
|   SECTION("key is a VLA") { | ||||
|     int i = 16; | ||||
|     char vla[i]; | ||||
|     strcpy(vla, "hello"); | ||||
|  | ||||
|     obj.createNestedObject(vla); | ||||
|   } | ||||
| #endif | ||||
| } | ||||
							
								
								
									
										53
									
								
								extras/tests/JsonObject/equals.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										53
									
								
								extras/tests/JsonObject/equals.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,53 @@ | ||||
| // ArduinoJson - arduinojson.org | ||||
| // Copyright Benoit Blanchon 2014-2019 | ||||
| // MIT License | ||||
|  | ||||
| #include <ArduinoJson.h> | ||||
| #include <catch.hpp> | ||||
|  | ||||
| TEST_CASE("JsonObject::operator==()") { | ||||
|   DynamicJsonDocument doc1(4096); | ||||
|   JsonObject obj1 = doc1.to<JsonObject>(); | ||||
|   JsonObjectConst obj1c = obj1; | ||||
|  | ||||
|   DynamicJsonDocument doc2(4096); | ||||
|   JsonObject obj2 = doc2.to<JsonObject>(); | ||||
|   JsonObjectConst obj2c = obj2; | ||||
|  | ||||
|   SECTION("should return false when objs differ") { | ||||
|     obj1["hello"] = "coucou"; | ||||
|     obj2["world"] = 1; | ||||
|  | ||||
|     REQUIRE_FALSE(obj1 == obj2); | ||||
|     REQUIRE_FALSE(obj1c == obj2c); | ||||
|   } | ||||
|  | ||||
|   SECTION("should return false when LHS has more elements") { | ||||
|     obj1["hello"] = "coucou"; | ||||
|     obj1["world"] = 666; | ||||
|     obj2["hello"] = "coucou"; | ||||
|  | ||||
|     REQUIRE_FALSE(obj1 == obj2); | ||||
|     REQUIRE_FALSE(obj1c == obj2c); | ||||
|   } | ||||
|  | ||||
|   SECTION("should return false when RKS has more elements") { | ||||
|     obj1["hello"] = "coucou"; | ||||
|     obj2["hello"] = "coucou"; | ||||
|     obj2["world"] = 666; | ||||
|  | ||||
|     REQUIRE_FALSE(obj1 == obj2); | ||||
|     REQUIRE_FALSE(obj1c == obj2c); | ||||
|   } | ||||
|  | ||||
|   SECTION("should return true when objs equal") { | ||||
|     obj1["hello"] = "world"; | ||||
|     obj1["anwser"] = 42; | ||||
|     // insert in different order | ||||
|     obj2["anwser"] = 42; | ||||
|     obj2["hello"] = "world"; | ||||
|  | ||||
|     REQUIRE(obj1 == obj2); | ||||
|     REQUIRE(obj1c == obj2c); | ||||
|   } | ||||
| } | ||||
							
								
								
									
										35
									
								
								extras/tests/JsonObject/invalid.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								extras/tests/JsonObject/invalid.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,35 @@ | ||||
| // ArduinoJson - arduinojson.org | ||||
| // Copyright Benoit Blanchon 2014-2019 | ||||
| // MIT License | ||||
|  | ||||
| #include <ArduinoJson.h> | ||||
| #include <catch.hpp> | ||||
|  | ||||
| using namespace Catch::Matchers; | ||||
|  | ||||
| TEST_CASE("JsonObject::invalid()") { | ||||
|   JsonObject obj; | ||||
|  | ||||
|   SECTION("SubscriptFails") { | ||||
|     REQUIRE(obj["key"].isNull()); | ||||
|   } | ||||
|  | ||||
|   SECTION("AddFails") { | ||||
|     obj["hello"] = "world"; | ||||
|     REQUIRE(0 == obj.size()); | ||||
|   } | ||||
|  | ||||
|   SECTION("CreateNestedArrayFails") { | ||||
|     REQUIRE(obj.createNestedArray("hello").isNull()); | ||||
|   } | ||||
|  | ||||
|   SECTION("CreateNestedObjectFails") { | ||||
|     REQUIRE(obj.createNestedObject("world").isNull()); | ||||
|   } | ||||
|  | ||||
|   SECTION("serialize to 'null'") { | ||||
|     char buffer[32]; | ||||
|     serializeJson(obj, buffer, sizeof(buffer)); | ||||
|     REQUIRE_THAT(buffer, Equals("null")); | ||||
|   } | ||||
| } | ||||
							
								
								
									
										34
									
								
								extras/tests/JsonObject/isNull.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								extras/tests/JsonObject/isNull.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,34 @@ | ||||
| // ArduinoJson - arduinojson.org | ||||
| // Copyright Benoit Blanchon 2014-2019 | ||||
| // MIT License | ||||
|  | ||||
| #include <ArduinoJson.h> | ||||
| #include <catch.hpp> | ||||
|  | ||||
| TEST_CASE("JsonObject::isNull()") { | ||||
|   DynamicJsonDocument doc(4096); | ||||
|  | ||||
|   SECTION("returns true") { | ||||
|     JsonObject obj; | ||||
|     REQUIRE(obj.isNull() == true); | ||||
|   } | ||||
|  | ||||
|   SECTION("returns false") { | ||||
|     JsonObject obj = doc.to<JsonObject>(); | ||||
|     REQUIRE(obj.isNull() == false); | ||||
|   } | ||||
| } | ||||
|  | ||||
| TEST_CASE("JsonObjectConst::isNull()") { | ||||
|   DynamicJsonDocument doc(4096); | ||||
|  | ||||
|   SECTION("returns true") { | ||||
|     JsonObjectConst obj; | ||||
|     REQUIRE(obj.isNull() == true); | ||||
|   } | ||||
|  | ||||
|   SECTION("returns false") { | ||||
|     JsonObjectConst obj = doc.to<JsonObject>(); | ||||
|     REQUIRE(obj.isNull() == false); | ||||
|   } | ||||
| } | ||||
							
								
								
									
										60
									
								
								extras/tests/JsonObject/iterator.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								extras/tests/JsonObject/iterator.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,60 @@ | ||||
| // ArduinoJson - arduinojson.org | ||||
| // Copyright Benoit Blanchon 2014-2019 | ||||
| // MIT License | ||||
|  | ||||
| #include <ArduinoJson.h> | ||||
| #include <catch.hpp> | ||||
|  | ||||
| using namespace Catch::Matchers; | ||||
|  | ||||
| TEST_CASE("JsonObject::begin()/end()") { | ||||
|   StaticJsonDocument<JSON_OBJECT_SIZE(2)> doc; | ||||
|   JsonObject obj = doc.to<JsonObject>(); | ||||
|   obj["ab"] = 12; | ||||
|   obj["cd"] = 34; | ||||
|  | ||||
|   SECTION("NonConstIterator") { | ||||
|     JsonObject::iterator it = obj.begin(); | ||||
|     REQUIRE(obj.end() != it); | ||||
|     REQUIRE(it->key() == "ab"); | ||||
|     REQUIRE(12 == it->value()); | ||||
|     ++it; | ||||
|     REQUIRE(obj.end() != it); | ||||
|     REQUIRE(it->key() == "cd"); | ||||
|     REQUIRE(34 == it->value()); | ||||
|     ++it; | ||||
|     REQUIRE(obj.end() == it); | ||||
|   } | ||||
|  | ||||
|   SECTION("Dereferencing end() is safe") { | ||||
|     REQUIRE(obj.end()->key().isNull()); | ||||
|     REQUIRE(obj.end()->value().isNull()); | ||||
|   } | ||||
| } | ||||
|  | ||||
| TEST_CASE("JsonObjectConst::begin()/end()") { | ||||
|   StaticJsonDocument<JSON_OBJECT_SIZE(2)> doc; | ||||
|   JsonObject obj = doc.to<JsonObject>(); | ||||
|   obj["ab"] = 12; | ||||
|   obj["cd"] = 34; | ||||
|  | ||||
|   JsonObjectConst cobj = obj; | ||||
|  | ||||
|   SECTION("NonConstIterator") { | ||||
|     JsonObjectConst::iterator it = cobj.begin(); | ||||
|     REQUIRE(cobj.end() != it); | ||||
|     REQUIRE(it->key() == "ab"); | ||||
|     REQUIRE(12 == it->value()); | ||||
|     ++it; | ||||
|     REQUIRE(cobj.end() != it); | ||||
|     REQUIRE(it->key() == "cd"); | ||||
|     REQUIRE(34 == it->value()); | ||||
|     ++it; | ||||
|     REQUIRE(cobj.end() == it); | ||||
|   } | ||||
|  | ||||
|   SECTION("Dereferencing end() is safe") { | ||||
|     REQUIRE(cobj.end()->key().isNull()); | ||||
|     REQUIRE(cobj.end()->value().isNull()); | ||||
|   } | ||||
| } | ||||
							
								
								
									
										43
									
								
								extras/tests/JsonObject/memoryUsage.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								extras/tests/JsonObject/memoryUsage.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,43 @@ | ||||
| // ArduinoJson - arduinojson.org | ||||
| // Copyright Benoit Blanchon 2014-2019 | ||||
| // MIT License | ||||
|  | ||||
| #include <ArduinoJson.h> | ||||
| #include <catch.hpp> | ||||
| #include <string> | ||||
|  | ||||
| TEST_CASE("JsonObject::memoryUsage()") { | ||||
|   DynamicJsonDocument doc(4096); | ||||
|   JsonObject obj = doc.to<JsonObject>(); | ||||
|  | ||||
|   SECTION("return 0 if uninitialized") { | ||||
|     JsonObject unitialized; | ||||
|     REQUIRE(unitialized.memoryUsage() == 0); | ||||
|   } | ||||
|  | ||||
|   SECTION("JSON_OBJECT_SIZE(0) for empty object") { | ||||
|     REQUIRE(obj.memoryUsage() == JSON_OBJECT_SIZE(0)); | ||||
|   } | ||||
|  | ||||
|   SECTION("JSON_OBJECT_SIZE(1) after add") { | ||||
|     obj["hello"] = 42; | ||||
|     REQUIRE(obj.memoryUsage() == JSON_OBJECT_SIZE(1)); | ||||
|   } | ||||
|  | ||||
|   SECTION("includes the size of the key") { | ||||
|     obj[std::string("hello")] = 42; | ||||
|     REQUIRE(obj.memoryUsage() == JSON_OBJECT_SIZE(1) + 6); | ||||
|   } | ||||
|  | ||||
|   SECTION("includes the size of the nested array") { | ||||
|     JsonArray nested = obj.createNestedArray("nested"); | ||||
|     nested.add(42); | ||||
|     REQUIRE(obj.memoryUsage() == JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(1)); | ||||
|   } | ||||
|  | ||||
|   SECTION("includes the size of the nested object") { | ||||
|     JsonObject nested = obj.createNestedObject("nested"); | ||||
|     nested["hello"] = "world"; | ||||
|     REQUIRE(obj.memoryUsage() == 2 * JSON_OBJECT_SIZE(1)); | ||||
|   } | ||||
| } | ||||
							
								
								
									
										35
									
								
								extras/tests/JsonObject/nesting.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								extras/tests/JsonObject/nesting.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,35 @@ | ||||
| // ArduinoJson - arduinojson.org | ||||
| // Copyright Benoit Blanchon 2014-2019 | ||||
| // MIT License | ||||
|  | ||||
| #include <ArduinoJson.h> | ||||
| #include <catch.hpp> | ||||
|  | ||||
| TEST_CASE("JsonObject::nesting()") { | ||||
|   DynamicJsonDocument doc(4096); | ||||
|   JsonObject obj = doc.to<JsonObject>(); | ||||
|  | ||||
|   SECTION("return 0 if uninitialized") { | ||||
|     JsonObject unitialized; | ||||
|     REQUIRE(unitialized.nesting() == 0); | ||||
|   } | ||||
|  | ||||
|   SECTION("returns 1 for empty object") { | ||||
|     REQUIRE(obj.nesting() == 1); | ||||
|   } | ||||
|  | ||||
|   SECTION("returns 1 for flat object") { | ||||
|     obj["hello"] = "world"; | ||||
|     REQUIRE(obj.nesting() == 1); | ||||
|   } | ||||
|  | ||||
|   SECTION("returns 2 with nested array") { | ||||
|     obj.createNestedArray("nested"); | ||||
|     REQUIRE(obj.nesting() == 2); | ||||
|   } | ||||
|  | ||||
|   SECTION("returns 2 with nested object") { | ||||
|     obj.createNestedObject("nested"); | ||||
|     REQUIRE(obj.nesting() == 2); | ||||
|   } | ||||
| } | ||||
							
								
								
									
										72
									
								
								extras/tests/JsonObject/remove.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										72
									
								
								extras/tests/JsonObject/remove.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,72 @@ | ||||
| // ArduinoJson - arduinojson.org | ||||
| // Copyright Benoit Blanchon 2014-2019 | ||||
| // MIT License | ||||
|  | ||||
| #include <ArduinoJson.h> | ||||
| #include <catch.hpp> | ||||
| #include <string> | ||||
|  | ||||
| TEST_CASE("JsonObject::remove()") { | ||||
|   DynamicJsonDocument doc(4096); | ||||
|   JsonObject obj = doc.to<JsonObject>(); | ||||
|   obj["a"] = 0; | ||||
|   obj["b"] = 1; | ||||
|   obj["c"] = 2; | ||||
|   std::string result; | ||||
|  | ||||
|   SECTION("remove(key)") { | ||||
|     SECTION("Remove first") { | ||||
|       obj.remove("a"); | ||||
|       serializeJson(obj, result); | ||||
|       REQUIRE("{\"b\":1,\"c\":2}" == result); | ||||
|     } | ||||
|  | ||||
|     SECTION("Remove middle") { | ||||
|       obj.remove("b"); | ||||
|       serializeJson(obj, result); | ||||
|       REQUIRE("{\"a\":0,\"c\":2}" == result); | ||||
|     } | ||||
|  | ||||
|     SECTION("Remove last") { | ||||
|       obj.remove("c"); | ||||
|       serializeJson(obj, result); | ||||
|       REQUIRE("{\"a\":0,\"b\":1}" == result); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   SECTION("remove(iterator)") { | ||||
|     JsonObject::iterator it = obj.begin(); | ||||
|  | ||||
|     SECTION("Remove first") { | ||||
|       obj.remove(it); | ||||
|       serializeJson(obj, result); | ||||
|       REQUIRE("{\"b\":1,\"c\":2}" == result); | ||||
|     } | ||||
|  | ||||
|     SECTION("Remove middle") { | ||||
|       ++it; | ||||
|       obj.remove(it); | ||||
|       serializeJson(obj, result); | ||||
|       REQUIRE("{\"a\":0,\"c\":2}" == result); | ||||
|     } | ||||
|  | ||||
|     SECTION("Remove last") { | ||||
|       it += 2; | ||||
|       obj.remove(it); | ||||
|       serializeJson(obj, result); | ||||
|       REQUIRE("{\"a\":0,\"b\":1}" == result); | ||||
|     } | ||||
|   } | ||||
|  | ||||
| #ifdef HAS_VARIABLE_LENGTH_ARRAY | ||||
|   SECTION("key is a vla") { | ||||
|     int i = 16; | ||||
|     char vla[i]; | ||||
|     strcpy(vla, "b"); | ||||
|     obj.remove(vla); | ||||
|  | ||||
|     serializeJson(obj, result); | ||||
|     REQUIRE("{\"a\":0,\"c\":2}" == result); | ||||
|   } | ||||
| #endif | ||||
| } | ||||
							
								
								
									
										39
									
								
								extras/tests/JsonObject/size.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								extras/tests/JsonObject/size.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,39 @@ | ||||
| // ArduinoJson - arduinojson.org | ||||
| // Copyright Benoit Blanchon 2014-2019 | ||||
| // MIT License | ||||
|  | ||||
| #include <ArduinoJson.h> | ||||
| #include <catch.hpp> | ||||
| #include <string> | ||||
|  | ||||
| TEST_CASE("JsonObject::size()") { | ||||
|   DynamicJsonDocument doc(4096); | ||||
|   JsonObject obj = doc.to<JsonObject>(); | ||||
|  | ||||
|   SECTION("initial size is zero") { | ||||
|     REQUIRE(0 == obj.size()); | ||||
|   } | ||||
|  | ||||
|   SECTION("increases when values are added") { | ||||
|     obj["hello"] = 42; | ||||
|     REQUIRE(1 == obj.size()); | ||||
|   } | ||||
|  | ||||
|   SECTION("decreases when values are removed") { | ||||
|     obj["hello"] = 42; | ||||
|     obj.remove("hello"); | ||||
|     REQUIRE(0 == obj.size()); | ||||
|   } | ||||
|  | ||||
|   SECTION("doesn't increase when the same key is added twice") { | ||||
|     obj["hello"] = 1; | ||||
|     obj["hello"] = 2; | ||||
|     REQUIRE(1 == obj.size()); | ||||
|   } | ||||
|  | ||||
|   SECTION("doesn't decrease when another key is removed") { | ||||
|     obj["hello"] = 1; | ||||
|     obj.remove("world"); | ||||
|     REQUIRE(1 == obj.size()); | ||||
|   } | ||||
| } | ||||
							
								
								
									
										109
									
								
								extras/tests/JsonObject/std_string.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										109
									
								
								extras/tests/JsonObject/std_string.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,109 @@ | ||||
| // ArduinoJson - arduinojson.org | ||||
| // Copyright Benoit Blanchon 2014-2019 | ||||
| // MIT License | ||||
|  | ||||
| #include <ArduinoJson.h> | ||||
| #include <catch.hpp> | ||||
|  | ||||
| static void eraseString(std::string &str) { | ||||
|   char *p = const_cast<char *>(str.c_str()); | ||||
|   while (*p) *p++ = '*'; | ||||
| } | ||||
|  | ||||
| TEST_CASE("std::string") { | ||||
|   DynamicJsonDocument doc(4096); | ||||
|  | ||||
|   SECTION("operator[]") { | ||||
|     char json[] = "{\"key\":\"value\"}"; | ||||
|  | ||||
|     deserializeJson(doc, json); | ||||
|     JsonObject obj = doc.as<JsonObject>(); | ||||
|  | ||||
|     REQUIRE(std::string("value") == obj[std::string("key")]); | ||||
|   } | ||||
|  | ||||
|   SECTION("operator[] const") { | ||||
|     char json[] = "{\"key\":\"value\"}"; | ||||
|  | ||||
|     deserializeJson(doc, json); | ||||
|     JsonObject obj = doc.as<JsonObject>(); | ||||
|  | ||||
|     REQUIRE(std::string("value") == obj[std::string("key")]); | ||||
|   } | ||||
|  | ||||
|   SECTION("createNestedObject()") { | ||||
|     JsonObject obj = doc.to<JsonObject>(); | ||||
|     std::string key = "key"; | ||||
|     char json[64]; | ||||
|     obj.createNestedObject(key); | ||||
|     eraseString(key); | ||||
|     serializeJson(doc, json, sizeof(json)); | ||||
|     REQUIRE(std::string("{\"key\":{}}") == json); | ||||
|   } | ||||
|  | ||||
|   SECTION("createNestedArray()") { | ||||
|     JsonObject obj = doc.to<JsonObject>(); | ||||
|     std::string key = "key"; | ||||
|     char json[64]; | ||||
|     obj.createNestedArray(key); | ||||
|     eraseString(key); | ||||
|     serializeJson(doc, json, sizeof(json)); | ||||
|     REQUIRE(std::string("{\"key\":[]}") == json); | ||||
|   } | ||||
|  | ||||
|   SECTION("containsKey()") { | ||||
|     char json[] = "{\"key\":\"value\"}"; | ||||
|     deserializeJson(doc, json); | ||||
|     JsonObject obj = doc.as<JsonObject>(); | ||||
|     REQUIRE(true == obj.containsKey(std::string("key"))); | ||||
|   } | ||||
|  | ||||
|   SECTION("remove()") { | ||||
|     JsonObject obj = doc.to<JsonObject>(); | ||||
|     obj["key"] = "value"; | ||||
|  | ||||
|     obj.remove(std::string("key")); | ||||
|  | ||||
|     REQUIRE(0 == obj.size()); | ||||
|   } | ||||
|  | ||||
|   SECTION("operator[], set key") { | ||||
|     std::string key("hello"); | ||||
|     JsonObject obj = doc.to<JsonObject>(); | ||||
|     obj[key] = "world"; | ||||
|     eraseString(key); | ||||
|     REQUIRE(std::string("world") == obj["hello"]); | ||||
|   } | ||||
|  | ||||
|   SECTION("operator[], set value") { | ||||
|     std::string value("world"); | ||||
|     JsonObject obj = doc.to<JsonObject>(); | ||||
|     obj["hello"] = value; | ||||
|     eraseString(value); | ||||
|     REQUIRE(std::string("world") == obj["hello"]); | ||||
|   } | ||||
|  | ||||
|   SECTION("memoryUsage() increases when adding a new key") { | ||||
|     std::string key1("hello"), key2("world"); | ||||
|     JsonObject obj = doc.to<JsonObject>(); | ||||
|  | ||||
|     obj[key1] = 1; | ||||
|     size_t sizeBefore = doc.memoryUsage(); | ||||
|     obj[key2] = 2; | ||||
|     size_t sizeAfter = doc.memoryUsage(); | ||||
|  | ||||
|     REQUIRE(sizeAfter - sizeBefore >= key2.size()); | ||||
|   } | ||||
|  | ||||
|   SECTION("memoryUsage() remains when adding the same key") { | ||||
|     std::string key("hello"); | ||||
|     JsonObject obj = doc.to<JsonObject>(); | ||||
|  | ||||
|     obj[key] = 1; | ||||
|     size_t sizeBefore = doc.memoryUsage(); | ||||
|     obj[key] = 2; | ||||
|     size_t sizeAfter = doc.memoryUsage(); | ||||
|  | ||||
|     REQUIRE(sizeBefore == sizeAfter); | ||||
|   } | ||||
| } | ||||
							
								
								
									
										234
									
								
								extras/tests/JsonObject/subscript.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										234
									
								
								extras/tests/JsonObject/subscript.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,234 @@ | ||||
| // ArduinoJson - arduinojson.org | ||||
| // Copyright Benoit Blanchon 2014-2019 | ||||
| // MIT License | ||||
|  | ||||
| #include <ArduinoJson.h> | ||||
| #include <catch.hpp> | ||||
|  | ||||
| TEST_CASE("JsonObject::operator[]") { | ||||
|   DynamicJsonDocument doc(4096); | ||||
|   JsonObject obj = doc.to<JsonObject>(); | ||||
|  | ||||
|   SECTION("int") { | ||||
|     obj["hello"] = 123; | ||||
|  | ||||
|     REQUIRE(123 == obj["hello"].as<int>()); | ||||
|     REQUIRE(true == obj["hello"].is<int>()); | ||||
|     REQUIRE(false == obj["hello"].is<bool>()); | ||||
|   } | ||||
|  | ||||
|   SECTION("volatile int") {  // issue #415 | ||||
|     volatile int i = 123; | ||||
|     obj["hello"] = i; | ||||
|  | ||||
|     REQUIRE(123 == obj["hello"].as<int>()); | ||||
|     REQUIRE(true == obj["hello"].is<int>()); | ||||
|     REQUIRE(false == obj["hello"].is<bool>()); | ||||
|   } | ||||
|  | ||||
|   SECTION("double") { | ||||
|     obj["hello"] = 123.45; | ||||
|  | ||||
|     REQUIRE(true == obj["hello"].is<double>()); | ||||
|     REQUIRE(false == obj["hello"].is<long>()); | ||||
|     REQUIRE(123.45 == obj["hello"].as<double>()); | ||||
|   } | ||||
|  | ||||
|   SECTION("bool") { | ||||
|     obj["hello"] = true; | ||||
|  | ||||
|     REQUIRE(true == obj["hello"].is<bool>()); | ||||
|     REQUIRE(false == obj["hello"].is<long>()); | ||||
|     REQUIRE(true == obj["hello"].as<bool>()); | ||||
|   } | ||||
|  | ||||
|   SECTION("const char*") { | ||||
|     obj["hello"] = "h3110"; | ||||
|  | ||||
|     REQUIRE(true == obj["hello"].is<const char*>()); | ||||
|     REQUIRE(false == obj["hello"].is<long>()); | ||||
|     REQUIRE(std::string("h3110") == obj["hello"].as<const char*>()); | ||||
|     REQUIRE(std::string("h3110") == obj["hello"].as<char*>());  // <- short hand | ||||
|   } | ||||
|  | ||||
|   SECTION("array") { | ||||
|     DynamicJsonDocument doc2(4096); | ||||
|     JsonArray arr = doc2.to<JsonArray>(); | ||||
|  | ||||
|     obj["hello"] = arr; | ||||
|  | ||||
|     REQUIRE(arr == obj["hello"].as<JsonArray>()); | ||||
|     REQUIRE(true == obj["hello"].is<JsonArray>()); | ||||
|     REQUIRE(false == obj["hello"].is<JsonObject>()); | ||||
|   } | ||||
|  | ||||
|   SECTION("object") { | ||||
|     DynamicJsonDocument doc2(4096); | ||||
|     JsonObject obj2 = doc2.to<JsonObject>(); | ||||
|  | ||||
|     obj["hello"] = obj2; | ||||
|  | ||||
|     REQUIRE(obj2 == obj["hello"].as<JsonObject>()); | ||||
|     REQUIRE(true == obj["hello"].is<JsonObject>()); | ||||
|     REQUIRE(false == obj["hello"].is<JsonArray>()); | ||||
|   } | ||||
|  | ||||
|   SECTION("array subscript") { | ||||
|     DynamicJsonDocument doc2(4096); | ||||
|     JsonArray arr = doc2.to<JsonArray>(); | ||||
|     arr.add(42); | ||||
|  | ||||
|     obj["a"] = arr[0]; | ||||
|  | ||||
|     REQUIRE(42 == obj["a"]); | ||||
|   } | ||||
|  | ||||
|   SECTION("object subscript") { | ||||
|     DynamicJsonDocument doc2(4096); | ||||
|     JsonObject obj2 = doc2.to<JsonObject>(); | ||||
|     obj2["x"] = 42; | ||||
|  | ||||
|     obj["a"] = obj2["x"]; | ||||
|  | ||||
|     REQUIRE(42 == obj["a"]); | ||||
|   } | ||||
|  | ||||
|   SECTION("char key[]") {  // issue #423 | ||||
|     char key[] = "hello"; | ||||
|     obj[key] = 42; | ||||
|     REQUIRE(42 == obj[key]); | ||||
|   } | ||||
|  | ||||
|   SECTION("should not duplicate const char*") { | ||||
|     obj["hello"] = "world"; | ||||
|     const size_t expectedSize = JSON_OBJECT_SIZE(1); | ||||
|     REQUIRE(expectedSize == doc.memoryUsage()); | ||||
|   } | ||||
|  | ||||
|   SECTION("should duplicate char* value") { | ||||
|     obj["hello"] = const_cast<char*>("world"); | ||||
|     const size_t expectedSize = JSON_OBJECT_SIZE(1) + JSON_STRING_SIZE(6); | ||||
|     REQUIRE(expectedSize == doc.memoryUsage()); | ||||
|   } | ||||
|  | ||||
|   SECTION("should duplicate char* key") { | ||||
|     obj[const_cast<char*>("hello")] = "world"; | ||||
|     const size_t expectedSize = JSON_OBJECT_SIZE(1) + JSON_STRING_SIZE(6); | ||||
|     REQUIRE(expectedSize == doc.memoryUsage()); | ||||
|   } | ||||
|  | ||||
|   SECTION("should duplicate char* key&value") { | ||||
|     obj[const_cast<char*>("hello")] = const_cast<char*>("world"); | ||||
|     const size_t expectedSize = JSON_OBJECT_SIZE(1) + 2 * JSON_STRING_SIZE(6); | ||||
|     REQUIRE(expectedSize <= doc.memoryUsage()); | ||||
|   } | ||||
|  | ||||
|   SECTION("should duplicate std::string value") { | ||||
|     obj["hello"] = std::string("world"); | ||||
|     const size_t expectedSize = JSON_OBJECT_SIZE(1) + JSON_STRING_SIZE(6); | ||||
|     REQUIRE(expectedSize == doc.memoryUsage()); | ||||
|   } | ||||
|  | ||||
|   SECTION("should duplicate std::string key") { | ||||
|     obj[std::string("hello")] = "world"; | ||||
|     const size_t expectedSize = JSON_OBJECT_SIZE(1) + JSON_STRING_SIZE(6); | ||||
|     REQUIRE(expectedSize == doc.memoryUsage()); | ||||
|   } | ||||
|  | ||||
|   SECTION("should duplicate std::string key&value") { | ||||
|     obj[std::string("hello")] = std::string("world"); | ||||
|     const size_t expectedSize = JSON_OBJECT_SIZE(1) + 2 * JSON_STRING_SIZE(6); | ||||
|     REQUIRE(expectedSize <= doc.memoryUsage()); | ||||
|   } | ||||
|  | ||||
|   SECTION("should duplicate a non-static JsonString key") { | ||||
|     obj[JsonString("hello", false)] = "world"; | ||||
|     const size_t expectedSize = JSON_OBJECT_SIZE(1) + JSON_STRING_SIZE(6); | ||||
|     REQUIRE(expectedSize == doc.memoryUsage()); | ||||
|   } | ||||
|  | ||||
|   SECTION("should not duplicate a static JsonString key") { | ||||
|     obj[JsonString("hello", true)] = "world"; | ||||
|     const size_t expectedSize = JSON_OBJECT_SIZE(1); | ||||
|     REQUIRE(expectedSize == doc.memoryUsage()); | ||||
|   } | ||||
|  | ||||
|   SECTION("should ignore null key") { | ||||
|     // object must have a value to make a call to strcmp() | ||||
|     obj["dummy"] = 42; | ||||
|  | ||||
|     const char* null = 0; | ||||
|     obj[null] = 666; | ||||
|  | ||||
|     REQUIRE(obj.size() == 1); | ||||
|     REQUIRE(obj[null] == null); | ||||
|   } | ||||
|  | ||||
|   SECTION("obj[key].to<JsonArray>()") { | ||||
|     JsonArray arr = obj["hello"].to<JsonArray>(); | ||||
|  | ||||
|     REQUIRE(arr.isNull() == false); | ||||
|   } | ||||
|  | ||||
| #if defined(HAS_VARIABLE_LENGTH_ARRAY) && \ | ||||
|     !defined(SUBSCRIPT_CONFLICTS_WITH_BUILTIN_OPERATOR) | ||||
|   SECTION("obj[VLA] = str") { | ||||
|     int i = 16; | ||||
|     char vla[i]; | ||||
|     strcpy(vla, "hello"); | ||||
|  | ||||
|     obj[vla] = "world"; | ||||
|  | ||||
|     REQUIRE(std::string("world") == obj["hello"]); | ||||
|   } | ||||
|  | ||||
|   SECTION("obj[str] = VLA") {  // issue #416 | ||||
|     int i = 32; | ||||
|     char vla[i]; | ||||
|     strcpy(vla, "world"); | ||||
|  | ||||
|     obj["hello"] = vla; | ||||
|  | ||||
|     REQUIRE(std::string("world") == obj["hello"].as<char*>()); | ||||
|   } | ||||
|  | ||||
|   SECTION("obj.set(VLA, str)") { | ||||
|     int i = 16; | ||||
|     char vla[i]; | ||||
|     strcpy(vla, "hello"); | ||||
|  | ||||
|     obj[vla] = "world"; | ||||
|  | ||||
|     REQUIRE(std::string("world") == obj["hello"]); | ||||
|   } | ||||
|  | ||||
|   SECTION("obj.set(str, VLA)") { | ||||
|     int i = 32; | ||||
|     char vla[i]; | ||||
|     strcpy(vla, "world"); | ||||
|  | ||||
|     obj["hello"].set(vla); | ||||
|  | ||||
|     REQUIRE(std::string("world") == obj["hello"].as<char*>()); | ||||
|   } | ||||
|  | ||||
|   SECTION("obj[VLA]") { | ||||
|     int i = 16; | ||||
|     char vla[i]; | ||||
|     strcpy(vla, "hello"); | ||||
|  | ||||
|     deserializeJson(doc, "{\"hello\":\"world\"}"); | ||||
|  | ||||
|     obj = doc.as<JsonObject>(); | ||||
|     REQUIRE(std::string("world") == obj[vla]); | ||||
|   } | ||||
| #endif | ||||
|  | ||||
|   SECTION("chain") { | ||||
|     obj.createNestedObject("hello")["world"] = 123; | ||||
|  | ||||
|     REQUIRE(123 == obj["hello"]["world"].as<int>()); | ||||
|     REQUIRE(true == obj["hello"]["world"].is<int>()); | ||||
|     REQUIRE(false == obj["hello"]["world"].is<bool>()); | ||||
|   } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user