| 
									
										
										
										
											2023-04-01 15:14:21 +02:00
										 |  |  | // ArduinoJson - https://arduinojson.org
 | 
					
						
							|  |  |  | // Copyright © 2014-2023, Benoit BLANCHON
 | 
					
						
							|  |  |  | // MIT License
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <ArduinoJson.h>
 | 
					
						
							|  |  |  | #include <catch.hpp>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-01 19:10:35 +02:00
										 |  |  | #include "Allocators.hpp"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-01 15:14:21 +02:00
										 |  |  | using ArduinoJson::detail::sizeofArray; | 
					
						
							|  |  |  | using ArduinoJson::detail::sizeofObject; | 
					
						
							| 
									
										
										
										
											2023-04-11 10:03:47 +02:00
										 |  |  | using ArduinoJson::detail::sizeofString; | 
					
						
							| 
									
										
										
										
											2023-04-01 15:14:21 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | TEST_CASE("JsonDocument assignment") { | 
					
						
							| 
									
										
										
										
											2023-04-01 19:10:35 +02:00
										 |  |  |   SpyingAllocator spyingAllocator; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("Copy assignment same capacity") { | 
					
						
							| 
									
										
										
										
											2023-04-11 10:03:47 +02:00
										 |  |  |     JsonDocument doc1(1024, &spyingAllocator); | 
					
						
							|  |  |  |     deserializeJson(doc1, "{\"hello\":\"world\"}"); | 
					
						
							|  |  |  |     JsonDocument doc2(1024, &spyingAllocator); | 
					
						
							|  |  |  |     spyingAllocator.clearLog(); | 
					
						
							| 
									
										
										
										
											2023-04-01 19:10:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-11 10:03:47 +02:00
										 |  |  |     doc2 = doc1; | 
					
						
							| 
									
										
										
										
											2023-04-01 19:10:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-11 10:03:47 +02:00
										 |  |  |     REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     REQUIRE(spyingAllocator.log() == | 
					
						
							|  |  |  |             AllocatorLog() << AllocatorLog::Allocate(sizeofString(5))  // hello
 | 
					
						
							|  |  |  |                            << AllocatorLog::Allocate(sizeofString(5))  // world
 | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2023-04-01 19:10:35 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-01 15:14:21 +02:00
										 |  |  |   SECTION("Copy assignment reallocates when capacity is smaller") { | 
					
						
							| 
									
										
										
										
											2023-04-11 10:03:47 +02:00
										 |  |  |     JsonDocument doc1(4096, &spyingAllocator); | 
					
						
							|  |  |  |     deserializeJson(doc1, "{\"hello\":\"world\"}"); | 
					
						
							|  |  |  |     JsonDocument doc2(8, &spyingAllocator); | 
					
						
							|  |  |  |     spyingAllocator.clearLog(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     doc2 = doc1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}"); | 
					
						
							|  |  |  |     REQUIRE(spyingAllocator.log() == | 
					
						
							|  |  |  |             AllocatorLog() << AllocatorLog::Deallocate(8) | 
					
						
							|  |  |  |                            << AllocatorLog::Allocate(4096) | 
					
						
							|  |  |  |                            << AllocatorLog::Allocate(sizeofString(5))  // hello
 | 
					
						
							|  |  |  |                            << AllocatorLog::Allocate(sizeofString(5))  // world
 | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2023-04-01 15:14:21 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("Copy assignment reallocates when capacity is larger") { | 
					
						
							| 
									
										
										
										
											2023-04-11 10:03:47 +02:00
										 |  |  |     JsonDocument doc1(1024, &spyingAllocator); | 
					
						
							|  |  |  |     deserializeJson(doc1, "{\"hello\":\"world\"}"); | 
					
						
							|  |  |  |     JsonDocument doc2(4096, &spyingAllocator); | 
					
						
							|  |  |  |     spyingAllocator.clearLog(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     doc2 = doc1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}"); | 
					
						
							|  |  |  |     REQUIRE(spyingAllocator.log() == | 
					
						
							|  |  |  |             AllocatorLog() << AllocatorLog::Deallocate(4096) | 
					
						
							|  |  |  |                            << AllocatorLog::Allocate(1024) | 
					
						
							|  |  |  |                            << AllocatorLog::Allocate(sizeofString(5))  // hello
 | 
					
						
							|  |  |  |                            << AllocatorLog::Allocate(sizeofString(5))  // world
 | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2023-04-01 19:10:35 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("Move assign") { | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       JsonDocument doc1(4096, &spyingAllocator); | 
					
						
							|  |  |  |       doc1.set(std::string("The size of this string is 32!!")); | 
					
						
							|  |  |  |       JsonDocument doc2(8, &spyingAllocator); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       doc2 = std::move(doc1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!"); | 
					
						
							|  |  |  |       REQUIRE(doc1.as<std::string>() == "null"); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-04-11 10:03:47 +02:00
										 |  |  |     REQUIRE(spyingAllocator.log() == | 
					
						
							|  |  |  |             AllocatorLog() << AllocatorLog::Allocate(4096) | 
					
						
							|  |  |  |                            << AllocatorLog::Allocate(sizeofString(31)) | 
					
						
							|  |  |  |                            << AllocatorLog::Allocate(8) | 
					
						
							|  |  |  |                            << AllocatorLog::Deallocate(8) | 
					
						
							|  |  |  |                            << AllocatorLog::Deallocate(sizeofString(31)) | 
					
						
							|  |  |  |                            << AllocatorLog::Deallocate(4096)); | 
					
						
							| 
									
										
										
										
											2023-04-01 15:14:21 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("Assign from JsonObject") { | 
					
						
							|  |  |  |     JsonDocument doc1(200); | 
					
						
							|  |  |  |     JsonObject obj = doc1.to<JsonObject>(); | 
					
						
							|  |  |  |     obj["hello"] = "world"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     JsonDocument doc2(4096); | 
					
						
							|  |  |  |     doc2 = obj; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}"); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("Assign from JsonArray") { | 
					
						
							|  |  |  |     JsonDocument doc1(200); | 
					
						
							|  |  |  |     JsonArray arr = doc1.to<JsonArray>(); | 
					
						
							|  |  |  |     arr.add("hello"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     JsonDocument doc2(4096); | 
					
						
							|  |  |  |     doc2 = arr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     REQUIRE(doc2.as<std::string>() == "[\"hello\"]"); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("Assign from JsonVariant") { | 
					
						
							|  |  |  |     JsonDocument doc1(200); | 
					
						
							|  |  |  |     deserializeJson(doc1, "42"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     JsonDocument doc2(4096); | 
					
						
							|  |  |  |     doc2 = doc1.as<JsonVariant>(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     REQUIRE(doc2.as<std::string>() == "42"); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("Assign from MemberProxy") { | 
					
						
							|  |  |  |     JsonDocument doc1(200); | 
					
						
							|  |  |  |     doc1["value"] = 42; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     JsonDocument doc2(4096); | 
					
						
							|  |  |  |     doc2 = doc1["value"]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     REQUIRE(doc2.as<std::string>() == "42"); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("Assign from ElementProxy") { | 
					
						
							|  |  |  |     JsonDocument doc1(200); | 
					
						
							|  |  |  |     doc1[0] = 42; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     JsonDocument doc2(4096); | 
					
						
							|  |  |  |     doc2 = doc1[0]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     REQUIRE(doc2.as<std::string>() == "42"); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |