mirror of
				https://github.com/bblanchon/ArduinoJson.git
				synced 2025-11-04 00:21:36 +01:00 
			
		
		
		
	Instead of storing a pointer, the function copies the `VariantData`. Benefits: * smaller code * no impact on programs that don't use this feature Drawbacks: * changes to the original variant are not always reflected on the copy * modifying the original from the shallow copy leads to UB
		
			
				
	
	
		
			48 lines
		
	
	
		
			988 B
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			48 lines
		
	
	
		
			988 B
		
	
	
	
		
			C++
		
	
	
	
	
	
#include <ArduinoJson.h>
 | 
						|
 | 
						|
#include <catch.hpp>
 | 
						|
 | 
						|
#if !ARDUINOJSON_HAS_NULLPTR
 | 
						|
#  error ARDUINOJSON_HAS_NULLPTR must be set to 1
 | 
						|
#endif
 | 
						|
 | 
						|
TEST_CASE("nullptr") {
 | 
						|
  DynamicJsonDocument doc(4096);
 | 
						|
  JsonVariant variant = doc.to<JsonVariant>();
 | 
						|
 | 
						|
  SECTION("JsonVariant == nullptr") {
 | 
						|
    REQUIRE((variant == nullptr));
 | 
						|
    REQUIRE_FALSE((variant != nullptr));
 | 
						|
  }
 | 
						|
 | 
						|
  SECTION("JsonVariant != nullptr") {
 | 
						|
    variant.set(42);
 | 
						|
 | 
						|
    REQUIRE_FALSE((variant == nullptr));
 | 
						|
    REQUIRE((variant != nullptr));
 | 
						|
  }
 | 
						|
 | 
						|
  SECTION("JsonVariant.set(nullptr)") {
 | 
						|
    variant.set(42);
 | 
						|
    variant.set(nullptr);
 | 
						|
 | 
						|
    REQUIRE(variant.isNull());
 | 
						|
  }
 | 
						|
 | 
						|
  SECTION("JsonVariant.set(nullptr) with unbound reference") {
 | 
						|
    JsonVariant unboundReference;
 | 
						|
 | 
						|
    unboundReference.set(nullptr);
 | 
						|
 | 
						|
    REQUIRE(variant.isNull());
 | 
						|
  }
 | 
						|
 | 
						|
  SECTION("JsonVariant.is<nullptr_t>()") {
 | 
						|
    variant.set(42);
 | 
						|
    REQUIRE(variant.is<std::nullptr_t>() == false);
 | 
						|
 | 
						|
    variant.clear();
 | 
						|
    REQUIRE(variant.is<std::nullptr_t>() == true);
 | 
						|
  }
 | 
						|
}
 |