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:
		
							
								
								
									
										49
									
								
								extras/tests/JsonDocument/BasicJsonDocument.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								extras/tests/JsonDocument/BasicJsonDocument.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,49 @@ | ||||
| // ArduinoJson - arduinojson.org | ||||
| // Copyright Benoit Blanchon 2014-2019 | ||||
| // MIT License | ||||
|  | ||||
| #include <ArduinoJson.h> | ||||
| #include <stdlib.h>  // malloc, free | ||||
| #include <catch.hpp> | ||||
| #include <sstream> | ||||
|  | ||||
| using ARDUINOJSON_NAMESPACE::addPadding; | ||||
|  | ||||
| class SpyingAllocator { | ||||
|  public: | ||||
|   SpyingAllocator(std::ostream& log) : _log(log) {} | ||||
|  | ||||
|   void* allocate(size_t n) { | ||||
|     _log << "A" << n; | ||||
|     return malloc(n); | ||||
|   } | ||||
|   void deallocate(void* p) { | ||||
|     _log << "F"; | ||||
|     free(p); | ||||
|   } | ||||
|  | ||||
|  private: | ||||
|   SpyingAllocator& operator=(const SpyingAllocator& src); | ||||
|  | ||||
|   std::ostream& _log; | ||||
| }; | ||||
|  | ||||
| typedef BasicJsonDocument<SpyingAllocator> MyJsonDocument; | ||||
|  | ||||
| TEST_CASE("BasicJsonDocument") { | ||||
|   std::stringstream log; | ||||
|  | ||||
|   SECTION("Construct/Destruct") { | ||||
|     { MyJsonDocument doc(4096, log); } | ||||
|     REQUIRE(log.str() == "A4096F"); | ||||
|   } | ||||
|  | ||||
|   SECTION("Copy construct") { | ||||
|     { | ||||
|       MyJsonDocument doc1(4096, log); | ||||
|       doc1.set(std::string("The size of this string is 32!!")); | ||||
|       MyJsonDocument doc2(doc1); | ||||
|     } | ||||
|     REQUIRE(log.str() == "A4096A32FF"); | ||||
|   } | ||||
| } | ||||
							
								
								
									
										21
									
								
								extras/tests/JsonDocument/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								extras/tests/JsonDocument/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,21 @@ | ||||
| # ArduinoJson - arduinojson.org | ||||
| # Copyright Benoit Blanchon 2014-2019 | ||||
| # MIT License | ||||
|  | ||||
| add_executable(JsonDocumentTests | ||||
| 	add.cpp | ||||
| 	BasicJsonDocument.cpp | ||||
| 	compare.cpp | ||||
| 	containsKey.cpp | ||||
| 	createNested.cpp | ||||
| 	DynamicJsonDocument.cpp | ||||
| 	isNull.cpp | ||||
| 	nesting.cpp | ||||
| 	remove.cpp | ||||
| 	size.cpp | ||||
| 	StaticJsonDocument.cpp | ||||
| 	subscript.cpp | ||||
| ) | ||||
|  | ||||
| target_link_libraries(JsonDocumentTests catch) | ||||
| add_test(JsonDocument JsonDocumentTests) | ||||
							
								
								
									
										209
									
								
								extras/tests/JsonDocument/DynamicJsonDocument.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										209
									
								
								extras/tests/JsonDocument/DynamicJsonDocument.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,209 @@ | ||||
| // ArduinoJson - arduinojson.org | ||||
| // Copyright Benoit Blanchon 2014-2019 | ||||
| // MIT License | ||||
|  | ||||
| #include <ArduinoJson.h> | ||||
| #include <catch.hpp> | ||||
|  | ||||
| using ARDUINOJSON_NAMESPACE::addPadding; | ||||
|  | ||||
| static void REQUIRE_JSON(JsonDocument& doc, const std::string& expected) { | ||||
|   std::string json; | ||||
|   serializeJson(doc, json); | ||||
|   REQUIRE(json == expected); | ||||
| } | ||||
|  | ||||
| TEST_CASE("DynamicJsonDocument") { | ||||
|   DynamicJsonDocument doc(4096); | ||||
|  | ||||
|   SECTION("serializeJson()") { | ||||
|     JsonObject obj = doc.to<JsonObject>(); | ||||
|     obj["hello"] = "world"; | ||||
|  | ||||
|     std::string json; | ||||
|     serializeJson(doc, json); | ||||
|  | ||||
|     REQUIRE(json == "{\"hello\":\"world\"}"); | ||||
|   } | ||||
|  | ||||
|   SECTION("memoryUsage()") { | ||||
|     SECTION("starts at zero") { | ||||
|       REQUIRE(doc.memoryUsage() == 0); | ||||
|     } | ||||
|  | ||||
|     SECTION("JSON_ARRAY_SIZE(0)") { | ||||
|       doc.to<JsonArray>(); | ||||
|       REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(0)); | ||||
|     } | ||||
|  | ||||
|     SECTION("JSON_ARRAY_SIZE(1)") { | ||||
|       doc.to<JsonArray>().add(42); | ||||
|       REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(1)); | ||||
|     } | ||||
|  | ||||
|     SECTION("JSON_ARRAY_SIZE(1) + JSON_ARRAY_SIZE(0)") { | ||||
|       doc.to<JsonArray>().createNestedArray(); | ||||
|       REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(1) + JSON_ARRAY_SIZE(0)); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   SECTION("capacity()") { | ||||
|     SECTION("matches constructor argument") { | ||||
|       DynamicJsonDocument doc2(256); | ||||
|       REQUIRE(doc2.capacity() == 256); | ||||
|     } | ||||
|  | ||||
|     SECTION("rounds up constructor argument") { | ||||
|       DynamicJsonDocument doc2(253); | ||||
|       REQUIRE(doc2.capacity() == 256); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   SECTION("memoryUsage()") { | ||||
|     SECTION("Increases after adding value to array") { | ||||
|       JsonArray arr = doc.to<JsonArray>(); | ||||
|  | ||||
|       REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(0)); | ||||
|       arr.add(42); | ||||
|       REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(1)); | ||||
|       arr.add(43); | ||||
|       REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(2)); | ||||
|     } | ||||
|  | ||||
|     SECTION("Increases after adding value to object") { | ||||
|       JsonObject obj = doc.to<JsonObject>(); | ||||
|  | ||||
|       REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(0)); | ||||
|       obj["a"] = 1; | ||||
|       REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(1)); | ||||
|       obj["b"] = 2; | ||||
|       REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(2)); | ||||
|     } | ||||
|   } | ||||
| } | ||||
|  | ||||
| TEST_CASE("DynamicJsonDocument constructor") { | ||||
|   SECTION("Copy constructor") { | ||||
|     DynamicJsonDocument doc1(1234); | ||||
|     deserializeJson(doc1, "{\"hello\":\"world\"}"); | ||||
|  | ||||
|     DynamicJsonDocument doc2 = doc1; | ||||
|  | ||||
|     REQUIRE_JSON(doc2, "{\"hello\":\"world\"}"); | ||||
|  | ||||
|     REQUIRE(doc2.capacity() == addPadding(doc1.memoryUsage())); | ||||
|   } | ||||
|  | ||||
|   SECTION("Construct from StaticJsonDocument") { | ||||
|     StaticJsonDocument<200> doc1; | ||||
|     deserializeJson(doc1, "{\"hello\":\"world\"}"); | ||||
|  | ||||
|     DynamicJsonDocument doc2 = doc1; | ||||
|  | ||||
|     REQUIRE_JSON(doc2, "{\"hello\":\"world\"}"); | ||||
|     REQUIRE(doc2.capacity() == addPadding(doc1.memoryUsage())); | ||||
|   } | ||||
|  | ||||
|   SECTION("Construct from JsonObject") { | ||||
|     StaticJsonDocument<200> doc1; | ||||
|     JsonObject obj = doc1.to<JsonObject>(); | ||||
|     obj["hello"] = "world"; | ||||
|  | ||||
|     DynamicJsonDocument doc2 = obj; | ||||
|  | ||||
|     REQUIRE_JSON(doc2, "{\"hello\":\"world\"}"); | ||||
|     REQUIRE(doc2.capacity() == addPadding(doc1.memoryUsage())); | ||||
|   } | ||||
|  | ||||
|   SECTION("Construct from JsonArray") { | ||||
|     StaticJsonDocument<200> doc1; | ||||
|     JsonArray arr = doc1.to<JsonArray>(); | ||||
|     arr.add("hello"); | ||||
|  | ||||
|     DynamicJsonDocument doc2 = arr; | ||||
|  | ||||
|     REQUIRE_JSON(doc2, "[\"hello\"]"); | ||||
|     REQUIRE(doc2.capacity() == addPadding(doc1.memoryUsage())); | ||||
|   } | ||||
|  | ||||
|   SECTION("Construct from JsonVariant") { | ||||
|     StaticJsonDocument<200> doc1; | ||||
|     deserializeJson(doc1, "42"); | ||||
|  | ||||
|     DynamicJsonDocument doc2 = doc1.as<JsonVariant>(); | ||||
|  | ||||
|     REQUIRE_JSON(doc2, "42"); | ||||
|     REQUIRE(doc2.capacity() == addPadding(doc1.memoryUsage())); | ||||
|   } | ||||
| } | ||||
|  | ||||
| TEST_CASE("DynamicJsonDocument assignment") { | ||||
|   SECTION("Copy assignment preserves the buffer when capacity is sufficient") { | ||||
|     DynamicJsonDocument doc1(1234); | ||||
|     deserializeJson(doc1, "{\"hello\":\"world\"}"); | ||||
|  | ||||
|     DynamicJsonDocument doc2(doc1.capacity()); | ||||
|     doc2 = doc1; | ||||
|  | ||||
|     REQUIRE_JSON(doc2, "{\"hello\":\"world\"}"); | ||||
|     REQUIRE(doc2.capacity() == doc1.capacity()); | ||||
|   } | ||||
|  | ||||
|   SECTION("Copy assignment realloc the buffer when capacity is insufficient") { | ||||
|     DynamicJsonDocument doc1(1234); | ||||
|     deserializeJson(doc1, "{\"hello\":\"world\"}"); | ||||
|     DynamicJsonDocument doc2(8); | ||||
|  | ||||
|     REQUIRE(doc2.capacity() < doc1.memoryUsage()); | ||||
|     doc2 = doc1; | ||||
|     REQUIRE(doc2.capacity() >= doc1.memoryUsage()); | ||||
|  | ||||
|     REQUIRE_JSON(doc2, "{\"hello\":\"world\"}"); | ||||
|   } | ||||
|  | ||||
|   SECTION("Assign from StaticJsonDocument") { | ||||
|     StaticJsonDocument<200> doc1; | ||||
|     deserializeJson(doc1, "{\"hello\":\"world\"}"); | ||||
|     DynamicJsonDocument doc2(4096); | ||||
|     doc2.to<JsonVariant>().set(666); | ||||
|  | ||||
|     doc2 = doc1; | ||||
|  | ||||
|     REQUIRE_JSON(doc2, "{\"hello\":\"world\"}"); | ||||
|   } | ||||
|  | ||||
|   SECTION("Assign from JsonObject") { | ||||
|     StaticJsonDocument<200> doc1; | ||||
|     JsonObject obj = doc1.to<JsonObject>(); | ||||
|     obj["hello"] = "world"; | ||||
|  | ||||
|     DynamicJsonDocument doc2(4096); | ||||
|     doc2 = obj; | ||||
|  | ||||
|     REQUIRE_JSON(doc2, "{\"hello\":\"world\"}"); | ||||
|     REQUIRE(doc2.capacity() == 4096); | ||||
|   } | ||||
|  | ||||
|   SECTION("Assign from JsonArray") { | ||||
|     StaticJsonDocument<200> doc1; | ||||
|     JsonArray arr = doc1.to<JsonArray>(); | ||||
|     arr.add("hello"); | ||||
|  | ||||
|     DynamicJsonDocument doc2(4096); | ||||
|     doc2 = arr; | ||||
|  | ||||
|     REQUIRE_JSON(doc2, "[\"hello\"]"); | ||||
|     REQUIRE(doc2.capacity() == 4096); | ||||
|   } | ||||
|  | ||||
|   SECTION("Assign from JsonVariant") { | ||||
|     StaticJsonDocument<200> doc1; | ||||
|     deserializeJson(doc1, "42"); | ||||
|  | ||||
|     DynamicJsonDocument doc2(4096); | ||||
|     doc2 = doc1.as<JsonVariant>(); | ||||
|  | ||||
|     REQUIRE_JSON(doc2, "42"); | ||||
|     REQUIRE(doc2.capacity() == 4096); | ||||
|   } | ||||
| } | ||||
							
								
								
									
										212
									
								
								extras/tests/JsonDocument/StaticJsonDocument.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										212
									
								
								extras/tests/JsonDocument/StaticJsonDocument.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,212 @@ | ||||
| // ArduinoJson - arduinojson.org | ||||
| // Copyright Benoit Blanchon 2014-2019 | ||||
| // MIT License | ||||
|  | ||||
| #include <ArduinoJson.h> | ||||
| #include <catch.hpp> | ||||
|  | ||||
| static void REQUIRE_JSON(JsonDocument& doc, const std::string& expected) { | ||||
|   std::string json; | ||||
|   serializeJson(doc, json); | ||||
|   REQUIRE(json == expected); | ||||
| } | ||||
|  | ||||
| TEST_CASE("StaticJsonDocument") { | ||||
|   SECTION("capacity()") { | ||||
|     SECTION("matches template argument") { | ||||
|       StaticJsonDocument<256> doc; | ||||
|       REQUIRE(doc.capacity() == 256); | ||||
|     } | ||||
|  | ||||
|     SECTION("rounds up template argument") { | ||||
|       StaticJsonDocument<253> doc; | ||||
|       REQUIRE(doc.capacity() == 256); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   SECTION("serializeJson()") { | ||||
|     StaticJsonDocument<200> doc; | ||||
|     JsonObject obj = doc.to<JsonObject>(); | ||||
|     obj["hello"] = "world"; | ||||
|  | ||||
|     std::string json; | ||||
|     serializeJson(doc, json); | ||||
|  | ||||
|     REQUIRE(json == "{\"hello\":\"world\"}"); | ||||
|   } | ||||
|  | ||||
|   SECTION("Copy assignment") { | ||||
|     StaticJsonDocument<200> doc1, doc2; | ||||
|     doc1.to<JsonVariant>().set(666); | ||||
|     deserializeJson(doc2, "{\"hello\":\"world\"}"); | ||||
|  | ||||
|     doc1 = doc2; | ||||
|  | ||||
|     REQUIRE_JSON(doc2, "{\"hello\":\"world\"}"); | ||||
|   } | ||||
|  | ||||
|   SECTION("Contructor") { | ||||
|     SECTION("Copy constructor") { | ||||
|       StaticJsonDocument<200> doc1; | ||||
|       deserializeJson(doc1, "{\"hello\":\"world\"}"); | ||||
|  | ||||
|       StaticJsonDocument<200> doc2 = doc1; | ||||
|  | ||||
|       deserializeJson(doc1, "{\"HELLO\":\"WORLD\"}"); | ||||
|       REQUIRE_JSON(doc2, "{\"hello\":\"world\"}"); | ||||
|     } | ||||
|  | ||||
|     SECTION("Construct from StaticJsonDocument of different size") { | ||||
|       StaticJsonDocument<300> doc1; | ||||
|       deserializeJson(doc1, "{\"hello\":\"world\"}"); | ||||
|  | ||||
|       StaticJsonDocument<200> doc2 = doc1; | ||||
|  | ||||
|       REQUIRE_JSON(doc2, "{\"hello\":\"world\"}"); | ||||
|     } | ||||
|  | ||||
|     SECTION("Construct from DynamicJsonDocument") { | ||||
|       DynamicJsonDocument doc1(4096); | ||||
|       deserializeJson(doc1, "{\"hello\":\"world\"}"); | ||||
|  | ||||
|       StaticJsonDocument<200> doc2 = doc1; | ||||
|  | ||||
|       REQUIRE_JSON(doc2, "{\"hello\":\"world\"}"); | ||||
|     } | ||||
|  | ||||
|     SECTION("Construct from JsonObject") { | ||||
|       DynamicJsonDocument doc1(4096); | ||||
|       deserializeJson(doc1, "{\"hello\":\"world\"}"); | ||||
|  | ||||
|       StaticJsonDocument<200> doc2 = doc1.as<JsonObject>(); | ||||
|  | ||||
|       deserializeJson(doc1, "{\"HELLO\":\"WORLD\"}"); | ||||
|       REQUIRE_JSON(doc2, "{\"hello\":\"world\"}"); | ||||
|     } | ||||
|  | ||||
|     SECTION("Construct from JsonArray") { | ||||
|       DynamicJsonDocument doc1(4096); | ||||
|       deserializeJson(doc1, "[\"hello\",\"world\"]"); | ||||
|  | ||||
|       StaticJsonDocument<200> doc2 = doc1.as<JsonArray>(); | ||||
|  | ||||
|       deserializeJson(doc1, "[\"HELLO\",\"WORLD\"]"); | ||||
|       REQUIRE_JSON(doc2, "[\"hello\",\"world\"]"); | ||||
|     } | ||||
|  | ||||
|     SECTION("Construct from JsonVariant") { | ||||
|       DynamicJsonDocument doc1(4096); | ||||
|       deserializeJson(doc1, "42"); | ||||
|  | ||||
|       StaticJsonDocument<200> doc2 = doc1.as<JsonVariant>(); | ||||
|  | ||||
|       REQUIRE_JSON(doc2, "42"); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   SECTION("Assignment") { | ||||
|     SECTION("Copy assignment") { | ||||
|       StaticJsonDocument<200> doc1, doc2; | ||||
|       doc1.to<JsonVariant>().set(666); | ||||
|       deserializeJson(doc1, "{\"hello\":\"world\"}"); | ||||
|  | ||||
|       doc2 = doc1; | ||||
|  | ||||
|       deserializeJson(doc1, "{\"HELLO\":\"WORLD\"}"); | ||||
|       REQUIRE_JSON(doc2, "{\"hello\":\"world\"}"); | ||||
|     } | ||||
|  | ||||
|     SECTION("Assign from StaticJsonDocument of different capacity") { | ||||
|       StaticJsonDocument<200> doc1; | ||||
|       StaticJsonDocument<300> doc2; | ||||
|       doc1.to<JsonVariant>().set(666); | ||||
|       deserializeJson(doc1, "{\"hello\":\"world\"}"); | ||||
|  | ||||
|       doc2 = doc1; | ||||
|  | ||||
|       REQUIRE_JSON(doc2, "{\"hello\":\"world\"}"); | ||||
|     } | ||||
|  | ||||
|     SECTION("Assign from DynamicJsonDocument") { | ||||
|       StaticJsonDocument<200> doc1; | ||||
|       DynamicJsonDocument doc2(4096); | ||||
|       doc1.to<JsonVariant>().set(666); | ||||
|       deserializeJson(doc1, "{\"hello\":\"world\"}"); | ||||
|  | ||||
|       doc2 = doc1; | ||||
|  | ||||
|       deserializeJson(doc1, "{\"HELLO\":\"WORLD\"}"); | ||||
|       REQUIRE_JSON(doc2, "{\"hello\":\"world\"}"); | ||||
|     } | ||||
|  | ||||
|     SECTION("Assign from JsonArray") { | ||||
|       StaticJsonDocument<200> doc1; | ||||
|       DynamicJsonDocument doc2(4096); | ||||
|       doc1.to<JsonVariant>().set(666); | ||||
|       deserializeJson(doc1, "[\"hello\",\"world\"]"); | ||||
|  | ||||
|       doc2 = doc1.as<JsonArray>(); | ||||
|  | ||||
|       deserializeJson(doc1, "[\"HELLO\",\"WORLD\"]"); | ||||
|       REQUIRE_JSON(doc2, "[\"hello\",\"world\"]"); | ||||
|     } | ||||
|  | ||||
|     SECTION("Assign from JsonArrayConst") { | ||||
|       StaticJsonDocument<200> doc1; | ||||
|       DynamicJsonDocument doc2(4096); | ||||
|       doc1.to<JsonVariant>().set(666); | ||||
|       deserializeJson(doc1, "[\"hello\",\"world\"]"); | ||||
|  | ||||
|       doc2 = doc1.as<JsonArrayConst>(); | ||||
|  | ||||
|       deserializeJson(doc1, "[\"HELLO\",\"WORLD\"]"); | ||||
|       REQUIRE_JSON(doc2, "[\"hello\",\"world\"]"); | ||||
|     } | ||||
|  | ||||
|     SECTION("Assign from JsonObject") { | ||||
|       StaticJsonDocument<200> doc1; | ||||
|       DynamicJsonDocument doc2(4096); | ||||
|       doc1.to<JsonVariant>().set(666); | ||||
|       deserializeJson(doc1, "{\"hello\":\"world\"}"); | ||||
|  | ||||
|       doc2 = doc1.as<JsonObject>(); | ||||
|  | ||||
|       deserializeJson(doc1, "{\"HELLO\":\"WORLD\"}"); | ||||
|       REQUIRE_JSON(doc2, "{\"hello\":\"world\"}"); | ||||
|     } | ||||
|  | ||||
|     SECTION("Assign from JsonObjectConst") { | ||||
|       StaticJsonDocument<200> doc1; | ||||
|       DynamicJsonDocument doc2(4096); | ||||
|       doc1.to<JsonVariant>().set(666); | ||||
|       deserializeJson(doc1, "{\"hello\":\"world\"}"); | ||||
|  | ||||
|       doc2 = doc1.as<JsonObjectConst>(); | ||||
|  | ||||
|       deserializeJson(doc1, "{\"HELLO\":\"WORLD\"}"); | ||||
|       REQUIRE_JSON(doc2, "{\"hello\":\"world\"}"); | ||||
|     } | ||||
|  | ||||
|     SECTION("Assign from JsonVariant") { | ||||
|       DynamicJsonDocument doc1(4096); | ||||
|       doc1.to<JsonVariant>().set(666); | ||||
|       deserializeJson(doc1, "42"); | ||||
|  | ||||
|       StaticJsonDocument<200> doc2; | ||||
|       doc2 = doc1.as<JsonVariant>(); | ||||
|  | ||||
|       REQUIRE_JSON(doc2, "42"); | ||||
|     } | ||||
|  | ||||
|     SECTION("Assign from JsonVariantConst") { | ||||
|       DynamicJsonDocument doc1(4096); | ||||
|       doc1.to<JsonVariant>().set(666); | ||||
|       deserializeJson(doc1, "42"); | ||||
|  | ||||
|       StaticJsonDocument<200> doc2; | ||||
|       doc2 = doc1.as<JsonVariantConst>(); | ||||
|  | ||||
|       REQUIRE_JSON(doc2, "42"); | ||||
|     } | ||||
|   } | ||||
| } | ||||
							
								
								
									
										22
									
								
								extras/tests/JsonDocument/add.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								extras/tests/JsonDocument/add.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,22 @@ | ||||
| // ArduinoJson - arduinojson.org | ||||
| // Copyright Benoit Blanchon 2014-2019 | ||||
| // MIT License | ||||
|  | ||||
| #include <ArduinoJson.h> | ||||
| #include <catch.hpp> | ||||
|  | ||||
| TEST_CASE("JsonDocument::add()") { | ||||
|   DynamicJsonDocument doc(4096); | ||||
|  | ||||
|   SECTION("integer") { | ||||
|     doc.add(42); | ||||
|  | ||||
|     REQUIRE(doc.as<std::string>() == "[42]"); | ||||
|   } | ||||
|  | ||||
|   SECTION("const char*") { | ||||
|     doc.add("hello"); | ||||
|  | ||||
|     REQUIRE(doc.as<std::string>() == "[\"hello\"]"); | ||||
|   } | ||||
| } | ||||
							
								
								
									
										77
									
								
								extras/tests/JsonDocument/compare.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										77
									
								
								extras/tests/JsonDocument/compare.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,77 @@ | ||||
| // ArduinoJson - arduinojson.org | ||||
| // Copyright Benoit Blanchon 2014-2019 | ||||
| // MIT License | ||||
|  | ||||
| #include <ArduinoJson.h> | ||||
| #include <catch.hpp> | ||||
|  | ||||
| TEST_CASE("DynamicJsonDocument::operator==(const DynamicJsonDocument&)") { | ||||
|   DynamicJsonDocument doc1(4096); | ||||
|   DynamicJsonDocument doc2(4096); | ||||
|  | ||||
|   SECTION("Empty") { | ||||
|     REQUIRE(doc1 == doc2); | ||||
|     REQUIRE_FALSE(doc1 != doc2); | ||||
|   } | ||||
|  | ||||
|   SECTION("With same object") { | ||||
|     doc1["hello"] = "world"; | ||||
|     doc2["hello"] = "world"; | ||||
|     REQUIRE(doc1 == doc2); | ||||
|     REQUIRE_FALSE(doc1 != doc2); | ||||
|   } | ||||
|   SECTION("With different object") { | ||||
|     doc1["hello"] = "world"; | ||||
|     doc2["world"] = "hello"; | ||||
|     REQUIRE_FALSE(doc1 == doc2); | ||||
|     REQUIRE(doc1 != doc2); | ||||
|   } | ||||
| } | ||||
|  | ||||
| TEST_CASE("DynamicJsonDocument::operator==(const StaticJsonDocument&)") { | ||||
|   DynamicJsonDocument doc1(4096); | ||||
|   StaticJsonDocument<256> doc2; | ||||
|  | ||||
|   SECTION("Empty") { | ||||
|     REQUIRE(doc1 == doc2); | ||||
|     REQUIRE_FALSE(doc1 != doc2); | ||||
|   } | ||||
|  | ||||
|   SECTION("With same object") { | ||||
|     doc1["hello"] = "world"; | ||||
|     doc2["hello"] = "world"; | ||||
|     REQUIRE(doc1 == doc2); | ||||
|     REQUIRE_FALSE(doc1 != doc2); | ||||
|   } | ||||
|  | ||||
|   SECTION("With different object") { | ||||
|     doc1["hello"] = "world"; | ||||
|     doc2["world"] = "hello"; | ||||
|     REQUIRE_FALSE(doc1 == doc2); | ||||
|     REQUIRE(doc1 != doc2); | ||||
|   } | ||||
| } | ||||
|  | ||||
| TEST_CASE("StaticJsonDocument::operator==(const DynamicJsonDocument&)") { | ||||
|   StaticJsonDocument<256> doc1; | ||||
|   DynamicJsonDocument doc2(4096); | ||||
|  | ||||
|   SECTION("Empty") { | ||||
|     REQUIRE(doc1 == doc2); | ||||
|     REQUIRE_FALSE(doc1 != doc2); | ||||
|   } | ||||
|  | ||||
|   SECTION("With same object") { | ||||
|     doc1["hello"] = "world"; | ||||
|     doc2["hello"] = "world"; | ||||
|     REQUIRE(doc1 == doc2); | ||||
|     REQUIRE_FALSE(doc1 != doc2); | ||||
|   } | ||||
|  | ||||
|   SECTION("With different object") { | ||||
|     doc1["hello"] = "world"; | ||||
|     doc2["world"] = "hello"; | ||||
|     REQUIRE_FALSE(doc1 == doc2); | ||||
|     REQUIRE(doc1 != doc2); | ||||
|   } | ||||
| } | ||||
							
								
								
									
										44
									
								
								extras/tests/JsonDocument/containsKey.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								extras/tests/JsonDocument/containsKey.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,44 @@ | ||||
| // ArduinoJson - arduinojson.org | ||||
| // Copyright Benoit Blanchon 2014-2019 | ||||
| // MIT License | ||||
|  | ||||
| #include <ArduinoJson.h> | ||||
| #include <catch.hpp> | ||||
|  | ||||
| TEST_CASE("JsonDocument::containsKey()") { | ||||
|   DynamicJsonDocument doc(4096); | ||||
|  | ||||
|   SECTION("returns true on object") { | ||||
|     doc["hello"] = "world"; | ||||
|  | ||||
|     REQUIRE(doc.containsKey("hello") == true); | ||||
|   } | ||||
|  | ||||
|   SECTION("returns true when value is null") { | ||||
|     doc["hello"] = static_cast<const char*>(0); | ||||
|  | ||||
|     REQUIRE(doc.containsKey("hello") == true); | ||||
|   } | ||||
|  | ||||
|   SECTION("returns true when key is a std::string") { | ||||
|     doc["hello"] = "world"; | ||||
|  | ||||
|     REQUIRE(doc.containsKey(std::string("hello")) == true); | ||||
|   } | ||||
|  | ||||
|   SECTION("returns false  on object") { | ||||
|     doc["world"] = "hello"; | ||||
|  | ||||
|     REQUIRE(doc.containsKey("hello") == false); | ||||
|   } | ||||
|  | ||||
|   SECTION("returns false on array") { | ||||
|     doc.add("hello"); | ||||
|  | ||||
|     REQUIRE(doc.containsKey("hello") == false); | ||||
|   } | ||||
|  | ||||
|   SECTION("returns false on null") { | ||||
|     REQUIRE(doc.containsKey("hello") == false); | ||||
|   } | ||||
| } | ||||
							
								
								
									
										66
									
								
								extras/tests/JsonDocument/createNested.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										66
									
								
								extras/tests/JsonDocument/createNested.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,66 @@ | ||||
| // ArduinoJson - arduinojson.org | ||||
| // Copyright Benoit Blanchon 2014-2019 | ||||
| // MIT License | ||||
|  | ||||
| #include <ArduinoJson.h> | ||||
| #include <catch.hpp> | ||||
|  | ||||
| TEST_CASE("JsonDocument::createNestedArray()") { | ||||
|   DynamicJsonDocument doc(4096); | ||||
|  | ||||
|   SECTION("promotes to array") { | ||||
|     doc.createNestedArray(); | ||||
|  | ||||
|     REQUIRE(doc.is<JsonArray>()); | ||||
|   } | ||||
| } | ||||
|  | ||||
| TEST_CASE("JsonDocument::createNestedArray(key)") { | ||||
|   DynamicJsonDocument doc(4096); | ||||
|  | ||||
|   SECTION("key is const char*") { | ||||
|     SECTION("promotes to object") { | ||||
|       doc.createNestedArray("hello"); | ||||
|  | ||||
|       REQUIRE(doc.is<JsonObject>()); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   SECTION("key is std::string") { | ||||
|     SECTION("promotes to object") { | ||||
|       doc.createNestedArray(std::string("hello")); | ||||
|  | ||||
|       REQUIRE(doc.is<JsonObject>()); | ||||
|     } | ||||
|   } | ||||
| } | ||||
|  | ||||
| TEST_CASE("JsonDocument::createNestedObject()") { | ||||
|   DynamicJsonDocument doc(4096); | ||||
|  | ||||
|   SECTION("promotes to array") { | ||||
|     doc.createNestedObject(); | ||||
|  | ||||
|     REQUIRE(doc.is<JsonArray>()); | ||||
|   } | ||||
| } | ||||
|  | ||||
| TEST_CASE("JsonDocument::createNestedObject(key)") { | ||||
|   DynamicJsonDocument doc(4096); | ||||
|  | ||||
|   SECTION("key is const char*") { | ||||
|     SECTION("promotes to object") { | ||||
|       doc.createNestedObject("hello"); | ||||
|  | ||||
|       REQUIRE(doc.is<JsonObject>()); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   SECTION("key is std::string") { | ||||
|     SECTION("promotes to object") { | ||||
|       doc.createNestedObject(std::string("hello")); | ||||
|  | ||||
|       REQUIRE(doc.is<JsonObject>()); | ||||
|     } | ||||
|   } | ||||
| } | ||||
							
								
								
									
										39
									
								
								extras/tests/JsonDocument/isNull.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								extras/tests/JsonDocument/isNull.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("JsonDocument::isNull()") { | ||||
|   DynamicJsonDocument doc(4096); | ||||
|  | ||||
|   SECTION("returns true if uninitialized") { | ||||
|     REQUIRE(doc.isNull() == true); | ||||
|   } | ||||
|  | ||||
|   SECTION("returns false after to<JsonObject>()") { | ||||
|     doc.to<JsonObject>(); | ||||
|     REQUIRE(doc.isNull() == false); | ||||
|   } | ||||
|  | ||||
|   SECTION("returns false after to<JsonArray>()") { | ||||
|     doc.to<JsonArray>(); | ||||
|     REQUIRE(doc.isNull() == false); | ||||
|   } | ||||
|  | ||||
|   SECTION("returns true after to<JsonVariant>()") { | ||||
|     REQUIRE(doc.isNull() == true); | ||||
|   } | ||||
|  | ||||
|   SECTION("returns false after set()") { | ||||
|     doc.to<JsonVariant>().set(42); | ||||
|     REQUIRE(doc.isNull() == false); | ||||
|   } | ||||
|  | ||||
|   SECTION("returns true after clear()") { | ||||
|     doc.to<JsonObject>(); | ||||
|     doc.clear(); | ||||
|     REQUIRE(doc.isNull() == true); | ||||
|   } | ||||
| } | ||||
							
								
								
									
										30
									
								
								extras/tests/JsonDocument/nesting.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								extras/tests/JsonDocument/nesting.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,30 @@ | ||||
| // ArduinoJson - arduinojson.org | ||||
| // Copyright Benoit Blanchon 2014-2019 | ||||
| // MIT License | ||||
|  | ||||
| #include <ArduinoJson.h> | ||||
| #include <catch.hpp> | ||||
|  | ||||
| TEST_CASE("JsonDocument::nesting()") { | ||||
|   DynamicJsonDocument doc(4096); | ||||
|  | ||||
|   SECTION("return 0 if uninitialized") { | ||||
|     REQUIRE(doc.nesting() == 0); | ||||
|   } | ||||
|  | ||||
|   SECTION("returns 0 for string") { | ||||
|     JsonVariant var = doc.to<JsonVariant>(); | ||||
|     var.set("hello"); | ||||
|     REQUIRE(doc.nesting() == 0); | ||||
|   } | ||||
|  | ||||
|   SECTION("returns 1 for empty object") { | ||||
|     doc.to<JsonObject>(); | ||||
|     REQUIRE(doc.nesting() == 1); | ||||
|   } | ||||
|  | ||||
|   SECTION("returns 1 for empty array") { | ||||
|     doc.to<JsonArray>(); | ||||
|     REQUIRE(doc.nesting() == 1); | ||||
|   } | ||||
| } | ||||
							
								
								
									
										52
									
								
								extras/tests/JsonDocument/remove.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								extras/tests/JsonDocument/remove.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,52 @@ | ||||
| // ArduinoJson - arduinojson.org | ||||
| // Copyright Benoit Blanchon 2014-2019 | ||||
| // MIT License | ||||
|  | ||||
| #include <ArduinoJson.h> | ||||
| #include <catch.hpp> | ||||
|  | ||||
| TEST_CASE("JsonDocument::remove()") { | ||||
|   DynamicJsonDocument doc(4096); | ||||
|  | ||||
|   SECTION("remove(int)") { | ||||
|     doc.add(1); | ||||
|     doc.add(2); | ||||
|     doc.add(3); | ||||
|  | ||||
|     doc.remove(1); | ||||
|  | ||||
|     REQUIRE(doc.as<std::string>() == "[1,3]"); | ||||
|   } | ||||
|  | ||||
|   SECTION("remove(const char *)") { | ||||
|     doc["a"] = 1; | ||||
|     doc["b"] = 2; | ||||
|  | ||||
|     doc.remove("a"); | ||||
|  | ||||
|     REQUIRE(doc.as<std::string>() == "{\"b\":2}"); | ||||
|   } | ||||
|  | ||||
|   SECTION("remove(std::string)") { | ||||
|     doc["a"] = 1; | ||||
|     doc["b"] = 2; | ||||
|  | ||||
|     doc.remove(std::string("b")); | ||||
|  | ||||
|     REQUIRE(doc.as<std::string>() == "{\"a\":1}"); | ||||
|   } | ||||
|  | ||||
| #ifdef HAS_VARIABLE_LENGTH_ARRAY | ||||
|   SECTION("remove(vla)") { | ||||
|     doc["a"] = 1; | ||||
|     doc["b"] = 2; | ||||
|  | ||||
|     int i = 4; | ||||
|     char vla[i]; | ||||
|     strcpy(vla, "b"); | ||||
|     doc.remove(vla); | ||||
|  | ||||
|     REQUIRE(doc.as<std::string>() == "{\"a\":1}"); | ||||
|   } | ||||
| #endif | ||||
| } | ||||
							
								
								
									
										28
									
								
								extras/tests/JsonDocument/size.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								extras/tests/JsonDocument/size.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,28 @@ | ||||
| // ArduinoJson - arduinojson.org | ||||
| // Copyright Benoit Blanchon 2014-2019 | ||||
| // MIT License | ||||
|  | ||||
| #include <ArduinoJson.h> | ||||
| #include <catch.hpp> | ||||
|  | ||||
| TEST_CASE("JsonDocument::size()") { | ||||
|   DynamicJsonDocument doc(4096); | ||||
|  | ||||
|   SECTION("returns 0") { | ||||
|     REQUIRE(doc.size() == 0); | ||||
|   } | ||||
|  | ||||
|   SECTION("as an array, return 2") { | ||||
|     doc.add(1); | ||||
|     doc.add(2); | ||||
|  | ||||
|     REQUIRE(doc.size() == 2); | ||||
|   } | ||||
|  | ||||
|   SECTION("as an object, return 2") { | ||||
|     doc["a"] = 1; | ||||
|     doc["b"] = 2; | ||||
|  | ||||
|     REQUIRE(doc.size() == 2); | ||||
|   } | ||||
| } | ||||
							
								
								
									
										45
									
								
								extras/tests/JsonDocument/subscript.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								extras/tests/JsonDocument/subscript.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,45 @@ | ||||
| // ArduinoJson - arduinojson.org | ||||
| // Copyright Benoit Blanchon 2014-2019 | ||||
| // MIT License | ||||
|  | ||||
| #include <ArduinoJson.h> | ||||
| #include <catch.hpp> | ||||
|  | ||||
| TEST_CASE("JsonDocument::operator[]") { | ||||
|   DynamicJsonDocument doc(4096); | ||||
|   const JsonDocument& cdoc = doc; | ||||
|  | ||||
|   SECTION("object") { | ||||
|     deserializeJson(doc, "{\"hello\":\"world\"}"); | ||||
|  | ||||
|     SECTION("const char*") { | ||||
|       REQUIRE(doc["hello"] == "world"); | ||||
|       REQUIRE(cdoc["hello"] == "world"); | ||||
|     } | ||||
|  | ||||
|     SECTION("std::string") { | ||||
|       REQUIRE(doc[std::string("hello")] == "world"); | ||||
|       REQUIRE(cdoc[std::string("hello")] == "world"); | ||||
|     } | ||||
|  | ||||
|     SECTION("supports operator|") { | ||||
|       REQUIRE((doc["hello"] | "nope") == std::string("world")); | ||||
|       REQUIRE((doc["world"] | "nope") == std::string("nope")); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   SECTION("array") { | ||||
|     deserializeJson(doc, "[\"hello\",\"world\"]"); | ||||
|  | ||||
|     REQUIRE(doc[1] == "world"); | ||||
|     REQUIRE(cdoc[1] == "world"); | ||||
|   } | ||||
| } | ||||
|  | ||||
| TEST_CASE("JsonDocument automatically promotes to object") { | ||||
|   DynamicJsonDocument doc(4096); | ||||
|  | ||||
|   doc["one"]["two"]["three"] = 4; | ||||
|  | ||||
|   REQUIRE(doc["one"]["two"]["three"] == 4); | ||||
| } | ||||
		Reference in New Issue
	
	Block a user