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/JsonArray/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								extras/tests/JsonArray/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,23 @@ | ||||
| # ArduinoJson - arduinojson.org | ||||
| # Copyright Benoit Blanchon 2014-2019 | ||||
| # MIT License | ||||
|  | ||||
| add_executable(JsonArrayTests  | ||||
| 	add.cpp | ||||
| 	copyArray.cpp | ||||
| 	createNested.cpp | ||||
| 	equals.cpp | ||||
| 	get.cpp | ||||
| 	isNull.cpp | ||||
| 	iterator.cpp | ||||
| 	memoryUsage.cpp | ||||
| 	nesting.cpp | ||||
| 	remove.cpp | ||||
| 	size.cpp | ||||
| 	std_string.cpp | ||||
| 	subscript.cpp | ||||
| 	undefined.cpp | ||||
| ) | ||||
|  | ||||
| target_link_libraries(JsonArrayTests catch) | ||||
| add_test(JsonArray JsonArrayTests) | ||||
							
								
								
									
										138
									
								
								extras/tests/JsonArray/add.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										138
									
								
								extras/tests/JsonArray/add.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,138 @@ | ||||
| // ArduinoJson - arduinojson.org | ||||
| // Copyright Benoit Blanchon 2014-2019 | ||||
| // MIT License | ||||
|  | ||||
| #include <ArduinoJson.h> | ||||
| #include <catch.hpp> | ||||
|  | ||||
| TEST_CASE("JsonArray::add()") { | ||||
|   DynamicJsonDocument doc(4096); | ||||
|   JsonArray array = doc.to<JsonArray>(); | ||||
|  | ||||
|   SECTION("int") { | ||||
|     array.add(123); | ||||
|     REQUIRE(123 == array[0].as<int>()); | ||||
|     REQUIRE(array[0].is<int>()); | ||||
|     REQUIRE(array[0].is<double>()); | ||||
|   } | ||||
|  | ||||
|   SECTION("double") { | ||||
|     array.add(123.45); | ||||
|     REQUIRE(123.45 == array[0].as<double>()); | ||||
|     REQUIRE(array[0].is<double>()); | ||||
|     REQUIRE_FALSE(array[0].is<bool>()); | ||||
|   } | ||||
|  | ||||
|   SECTION("bool") { | ||||
|     array.add(true); | ||||
|     REQUIRE(true == array[0].as<bool>()); | ||||
|     REQUIRE(array[0].is<bool>()); | ||||
|     REQUIRE_FALSE(array[0].is<int>()); | ||||
|   } | ||||
|  | ||||
|   SECTION("const char*") { | ||||
|     const char* str = "hello"; | ||||
|     array.add(str); | ||||
|     REQUIRE(str == array[0].as<std::string>()); | ||||
|     REQUIRE(array[0].is<const char*>()); | ||||
|     REQUIRE_FALSE(array[0].is<int>()); | ||||
|   } | ||||
|  | ||||
| #ifdef HAS_VARIABLE_LENGTH_ARRAY | ||||
|   SECTION("vla") { | ||||
|     int i = 16; | ||||
|     char vla[i]; | ||||
|     strcpy(vla, "world"); | ||||
|  | ||||
|     array.add(vla); | ||||
|  | ||||
|     REQUIRE(std::string("world") == array[0]); | ||||
|   } | ||||
| #endif | ||||
|  | ||||
|   SECTION("nested array") { | ||||
|     DynamicJsonDocument doc2(4096); | ||||
|     JsonArray arr = doc2.to<JsonArray>(); | ||||
|  | ||||
|     array.add(arr); | ||||
|  | ||||
|     REQUIRE(arr == array[0].as<JsonArray>()); | ||||
|     REQUIRE(array[0].is<JsonArray>()); | ||||
|     REQUIRE_FALSE(array[0].is<int>()); | ||||
|   } | ||||
|  | ||||
|   SECTION("nested object") { | ||||
|     DynamicJsonDocument doc2(4096); | ||||
|     JsonObject obj = doc2.to<JsonObject>(); | ||||
|  | ||||
|     array.add(obj); | ||||
|  | ||||
|     REQUIRE(obj == array[0].as<JsonObject>()); | ||||
|     REQUIRE(array[0].is<JsonObject>()); | ||||
|     REQUIRE_FALSE(array[0].is<int>()); | ||||
|   } | ||||
|  | ||||
|   SECTION("array subscript") { | ||||
|     const char* str = "hello"; | ||||
|     DynamicJsonDocument doc2(4096); | ||||
|     JsonArray arr = doc2.to<JsonArray>(); | ||||
|     arr.add(str); | ||||
|  | ||||
|     array.add(arr[0]); | ||||
|  | ||||
|     REQUIRE(str == array[0]); | ||||
|   } | ||||
|  | ||||
|   SECTION("object subscript") { | ||||
|     const char* str = "hello"; | ||||
|     DynamicJsonDocument doc2(4096); | ||||
|     JsonObject obj = doc2.to<JsonObject>(); | ||||
|     obj["x"] = str; | ||||
|  | ||||
|     array.add(obj["x"]); | ||||
|  | ||||
|     REQUIRE(str == array[0]); | ||||
|   } | ||||
|  | ||||
|   SECTION("should not duplicate const char*") { | ||||
|     array.add("world"); | ||||
|     const size_t expectedSize = JSON_ARRAY_SIZE(1); | ||||
|     REQUIRE(expectedSize == doc.memoryUsage()); | ||||
|   } | ||||
|  | ||||
|   SECTION("should duplicate char*") { | ||||
|     array.add(const_cast<char*>("world")); | ||||
|     const size_t expectedSize = JSON_ARRAY_SIZE(1) + JSON_STRING_SIZE(6); | ||||
|     REQUIRE(expectedSize == doc.memoryUsage()); | ||||
|   } | ||||
|  | ||||
|   SECTION("should duplicate std::string") { | ||||
|     array.add(std::string("world")); | ||||
|     const size_t expectedSize = JSON_ARRAY_SIZE(1) + JSON_STRING_SIZE(6); | ||||
|     REQUIRE(expectedSize == doc.memoryUsage()); | ||||
|   } | ||||
|  | ||||
|   SECTION("should not duplicate serialized(const char*)") { | ||||
|     array.add(serialized("{}")); | ||||
|     const size_t expectedSize = JSON_ARRAY_SIZE(1); | ||||
|     REQUIRE(expectedSize == doc.memoryUsage()); | ||||
|   } | ||||
|  | ||||
|   SECTION("should duplicate serialized(char*)") { | ||||
|     array.add(serialized(const_cast<char*>("{}"))); | ||||
|     const size_t expectedSize = JSON_ARRAY_SIZE(1) + JSON_STRING_SIZE(2); | ||||
|     REQUIRE(expectedSize == doc.memoryUsage()); | ||||
|   } | ||||
|  | ||||
|   SECTION("should duplicate serialized(std::string)") { | ||||
|     array.add(serialized(std::string("{}"))); | ||||
|     const size_t expectedSize = JSON_ARRAY_SIZE(1) + JSON_STRING_SIZE(2); | ||||
|     REQUIRE(expectedSize == doc.memoryUsage()); | ||||
|   } | ||||
|  | ||||
|   SECTION("should duplicate serialized(std::string)") { | ||||
|     array.add(serialized(std::string("\0XX", 3))); | ||||
|     const size_t expectedSize = JSON_ARRAY_SIZE(1) + JSON_STRING_SIZE(3); | ||||
|     REQUIRE(expectedSize == doc.memoryUsage()); | ||||
|   } | ||||
| } | ||||
							
								
								
									
										117
									
								
								extras/tests/JsonArray/copyArray.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										117
									
								
								extras/tests/JsonArray/copyArray.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,117 @@ | ||||
| // ArduinoJson - arduinojson.org | ||||
| // Copyright Benoit Blanchon 2014-2019 | ||||
| // MIT License | ||||
|  | ||||
| #include <ArduinoJson.h> | ||||
| #include <catch.hpp> | ||||
|  | ||||
| TEST_CASE("copyArray()") { | ||||
|   SECTION("1D -> JsonArray") { | ||||
|     DynamicJsonDocument doc(4096); | ||||
|     JsonArray array = doc.to<JsonArray>(); | ||||
|     char json[32]; | ||||
|     int source[] = {1, 2, 3}; | ||||
|  | ||||
|     bool ok = copyArray(source, array); | ||||
|     REQUIRE(ok); | ||||
|  | ||||
|     serializeJson(array, json, sizeof(json)); | ||||
|     REQUIRE(std::string("[1,2,3]") == json); | ||||
|   } | ||||
|  | ||||
|   SECTION("1D -> JsonArray, but not enough memory") { | ||||
|     const size_t SIZE = JSON_ARRAY_SIZE(2); | ||||
|     StaticJsonDocument<SIZE> doc; | ||||
|     JsonArray array = doc.to<JsonArray>(); | ||||
|     char json[32]; | ||||
|     int source[] = {1, 2, 3}; | ||||
|  | ||||
|     bool ok = copyArray(source, array); | ||||
|     REQUIRE_FALSE(ok); | ||||
|  | ||||
|     serializeJson(array, json, sizeof(json)); | ||||
|     REQUIRE(std::string("[1,2]") == json); | ||||
|   } | ||||
|  | ||||
|   SECTION("2D -> JsonArray") { | ||||
|     DynamicJsonDocument doc(4096); | ||||
|     JsonArray array = doc.to<JsonArray>(); | ||||
|     char json[32]; | ||||
|     int source[][3] = {{1, 2, 3}, {4, 5, 6}}; | ||||
|  | ||||
|     bool ok = copyArray(source, array); | ||||
|     REQUIRE(ok); | ||||
|  | ||||
|     serializeJson(array, json, sizeof(json)); | ||||
|     REQUIRE(std::string("[[1,2,3],[4,5,6]]") == json); | ||||
|   } | ||||
|  | ||||
|   SECTION("2D -> JsonArray, but not enough memory") { | ||||
|     const size_t SIZE = | ||||
|         JSON_ARRAY_SIZE(2) + JSON_ARRAY_SIZE(3) + JSON_ARRAY_SIZE(2); | ||||
|     StaticJsonDocument<SIZE> doc; | ||||
|     JsonArray array = doc.to<JsonArray>(); | ||||
|     char json[32] = ""; | ||||
|     int source[][3] = {{1, 2, 3}, {4, 5, 6}}; | ||||
|  | ||||
|     CAPTURE(SIZE) | ||||
|  | ||||
|     bool ok = copyArray(source, array); | ||||
|     CAPTURE(doc.memoryUsage()); | ||||
|     CHECK_FALSE(ok); | ||||
|  | ||||
|     serializeJson(array, json, sizeof(json)); | ||||
|     REQUIRE(std::string("[[1,2,3],[4,5]]") == json); | ||||
|   } | ||||
|  | ||||
|   SECTION("JsonArray -> 1D, with more space than needed") { | ||||
|     DynamicJsonDocument doc(4096); | ||||
|     char json[] = "[1,2,3]"; | ||||
|     DeserializationError err = deserializeJson(doc, json); | ||||
|     REQUIRE(err == DeserializationError::Ok); | ||||
|     JsonArray array = doc.as<JsonArray>(); | ||||
|  | ||||
|     int destination[4] = {0}; | ||||
|     size_t result = copyArray(array, destination); | ||||
|  | ||||
|     REQUIRE(3 == result); | ||||
|     REQUIRE(1 == destination[0]); | ||||
|     REQUIRE(2 == destination[1]); | ||||
|     REQUIRE(3 == destination[2]); | ||||
|     REQUIRE(0 == destination[3]); | ||||
|   } | ||||
|  | ||||
|   SECTION("JsonArray -> 1D, without enough space") { | ||||
|     DynamicJsonDocument doc(4096); | ||||
|     char json[] = "[1,2,3]"; | ||||
|     DeserializationError err = deserializeJson(doc, json); | ||||
|     REQUIRE(err == DeserializationError::Ok); | ||||
|     JsonArray array = doc.as<JsonArray>(); | ||||
|  | ||||
|     int destination[2] = {0}; | ||||
|     size_t result = copyArray(array, destination); | ||||
|  | ||||
|     REQUIRE(2 == result); | ||||
|     REQUIRE(1 == destination[0]); | ||||
|     REQUIRE(2 == destination[1]); | ||||
|   } | ||||
|  | ||||
|   SECTION("JsonArray -> 2D") { | ||||
|     DynamicJsonDocument doc(4096); | ||||
|     char json[] = "[[1,2],[3],[4]]"; | ||||
|  | ||||
|     DeserializationError err = deserializeJson(doc, json); | ||||
|     REQUIRE(err == DeserializationError::Ok); | ||||
|     JsonArray array = doc.as<JsonArray>(); | ||||
|  | ||||
|     int destination[3][2] = {{0}}; | ||||
|     copyArray(array, destination); | ||||
|  | ||||
|     REQUIRE(1 == destination[0][0]); | ||||
|     REQUIRE(2 == destination[0][1]); | ||||
|     REQUIRE(3 == destination[1][0]); | ||||
|     REQUIRE(0 == destination[1][1]); | ||||
|     REQUIRE(4 == destination[2][0]); | ||||
|     REQUIRE(0 == destination[2][1]); | ||||
|   } | ||||
| } | ||||
							
								
								
									
										21
									
								
								extras/tests/JsonArray/createNested.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								extras/tests/JsonArray/createNested.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,21 @@ | ||||
| // ArduinoJson - arduinojson.org | ||||
| // Copyright Benoit Blanchon 2014-2019 | ||||
| // MIT License | ||||
|  | ||||
| #include <ArduinoJson.h> | ||||
| #include <catch.hpp> | ||||
|  | ||||
| TEST_CASE("JsonArray basics") { | ||||
|   DynamicJsonDocument doc(4096); | ||||
|   JsonArray array = doc.to<JsonArray>(); | ||||
|  | ||||
|   SECTION("CreateNestedArray") { | ||||
|     JsonArray arr = array.createNestedArray(); | ||||
|     REQUIRE(arr == array[0].as<JsonArray>()); | ||||
|   } | ||||
|  | ||||
|   SECTION("CreateNestedObject") { | ||||
|     JsonObject obj = array.createNestedObject(); | ||||
|     REQUIRE(obj == array[0].as<JsonObject>()); | ||||
|   } | ||||
| } | ||||
							
								
								
									
										50
									
								
								extras/tests/JsonArray/equals.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								extras/tests/JsonArray/equals.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,50 @@ | ||||
| // ArduinoJson - arduinojson.org | ||||
| // Copyright Benoit Blanchon 2014-2019 | ||||
| // MIT License | ||||
|  | ||||
| #include <ArduinoJson.h> | ||||
| #include <catch.hpp> | ||||
|  | ||||
| TEST_CASE("JsonArray::operator==()") { | ||||
|   DynamicJsonDocument doc1(4096); | ||||
|   JsonArray array1 = doc1.to<JsonArray>(); | ||||
|   JsonArrayConst array1c = array1; | ||||
|  | ||||
|   DynamicJsonDocument doc2(4096); | ||||
|   JsonArray array2 = doc2.to<JsonArray>(); | ||||
|   JsonArrayConst array2c = array2; | ||||
|  | ||||
|   SECTION("should return false when arrays differ") { | ||||
|     array1.add("coucou"); | ||||
|     array2.add(1); | ||||
|  | ||||
|     REQUIRE_FALSE(array1 == array2); | ||||
|     REQUIRE_FALSE(array1c == array2c); | ||||
|   } | ||||
|  | ||||
|   SECTION("should return false when LHS has more elements") { | ||||
|     array1.add(1); | ||||
|     array1.add(2); | ||||
|     array2.add(1); | ||||
|  | ||||
|     REQUIRE_FALSE(array1 == array2); | ||||
|     REQUIRE_FALSE(array1c == array2c); | ||||
|   } | ||||
|  | ||||
|   SECTION("should return false when RKS has more elements") { | ||||
|     array1.add(1); | ||||
|     array2.add(1); | ||||
|     array2.add(2); | ||||
|  | ||||
|     REQUIRE_FALSE(array1 == array2); | ||||
|     REQUIRE_FALSE(array1c == array2c); | ||||
|   } | ||||
|  | ||||
|   SECTION("should return true when arrays equal") { | ||||
|     array1.add("coucou"); | ||||
|     array2.add("coucou"); | ||||
|  | ||||
|     REQUIRE(array1 == array2); | ||||
|     REQUIRE(array1c == array2c); | ||||
|   } | ||||
| } | ||||
							
								
								
									
										16
									
								
								extras/tests/JsonArray/get.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								extras/tests/JsonArray/get.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,16 @@ | ||||
| // ArduinoJson - arduinojson.org | ||||
| // Copyright Benoit Blanchon 2014-2019 | ||||
| // MIT License | ||||
|  | ||||
| #include <ArduinoJson.h> | ||||
| #include <catch.hpp> | ||||
|  | ||||
| TEST_CASE("JsonArray::get()") { | ||||
|   DynamicJsonDocument doc(4096); | ||||
|   deserializeJson(doc, "[1,2,3]"); | ||||
|   JsonArray array = doc.as<JsonArray>(); | ||||
|  | ||||
|   SECTION("Overflow") { | ||||
|     REQUIRE(array.getElement(3).isNull()); | ||||
|   } | ||||
| } | ||||
							
								
								
									
										34
									
								
								extras/tests/JsonArray/isNull.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								extras/tests/JsonArray/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("JsonArray::isNull()") { | ||||
|   DynamicJsonDocument doc(4096); | ||||
|  | ||||
|   SECTION("returns true") { | ||||
|     JsonArray arr; | ||||
|     REQUIRE(arr.isNull() == true); | ||||
|   } | ||||
|  | ||||
|   SECTION("returns false") { | ||||
|     JsonArray arr = doc.to<JsonArray>(); | ||||
|     REQUIRE(arr.isNull() == false); | ||||
|   } | ||||
| } | ||||
|  | ||||
| TEST_CASE("JsonArrayConst::isNull()") { | ||||
|   DynamicJsonDocument doc(4096); | ||||
|  | ||||
|   SECTION("returns true") { | ||||
|     JsonArrayConst arr; | ||||
|     REQUIRE(arr.isNull() == true); | ||||
|   } | ||||
|  | ||||
|   SECTION("returns false") { | ||||
|     JsonArrayConst arr = doc.to<JsonArray>(); | ||||
|     REQUIRE(arr.isNull() == false); | ||||
|   } | ||||
| } | ||||
							
								
								
									
										36
									
								
								extras/tests/JsonArray/iterator.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								extras/tests/JsonArray/iterator.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,36 @@ | ||||
| // ArduinoJson - arduinojson.org | ||||
| // Copyright Benoit Blanchon 2014-2019 | ||||
| // MIT License | ||||
|  | ||||
| #include <ArduinoJson.h> | ||||
| #include <catch.hpp> | ||||
|  | ||||
| template <typename TArray> | ||||
| static void run_iterator_test() { | ||||
|   StaticJsonDocument<JSON_ARRAY_SIZE(2)> doc; | ||||
|   JsonArray tmp = doc.to<JsonArray>(); | ||||
|   tmp.add(12); | ||||
|   tmp.add(34); | ||||
|  | ||||
|   TArray array = tmp; | ||||
|   typename TArray::iterator it = array.begin(); | ||||
|   typename TArray::iterator end = array.end(); | ||||
|  | ||||
|   REQUIRE(end != it); | ||||
|   REQUIRE(12 == it->template as<int>()); | ||||
|   REQUIRE(12 == static_cast<int>(*it)); | ||||
|   ++it; | ||||
|   REQUIRE(end != it); | ||||
|   REQUIRE(34 == it->template as<int>()); | ||||
|   REQUIRE(34 == static_cast<int>(*it)); | ||||
|   ++it; | ||||
|   REQUIRE(end == it); | ||||
| } | ||||
|  | ||||
| TEST_CASE("JsonArray::begin()/end()") { | ||||
|   run_iterator_test<JsonArray>(); | ||||
| } | ||||
|  | ||||
| TEST_CASE("JsonArrayConst::begin()/end()") { | ||||
|   run_iterator_test<JsonArrayConst>(); | ||||
| } | ||||
							
								
								
									
										42
									
								
								extras/tests/JsonArray/memoryUsage.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								extras/tests/JsonArray/memoryUsage.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,42 @@ | ||||
| // ArduinoJson - arduinojson.org | ||||
| // Copyright Benoit Blanchon 2014-2019 | ||||
| // MIT License | ||||
|  | ||||
| #include <ArduinoJson.h> | ||||
| #include <catch.hpp> | ||||
|  | ||||
| TEST_CASE("JsonArray::memoryUsage()") { | ||||
|   DynamicJsonDocument doc(4096); | ||||
|   JsonArray arr = doc.to<JsonArray>(); | ||||
|  | ||||
|   SECTION("return 0 if uninitialized") { | ||||
|     JsonArray unitialized; | ||||
|     REQUIRE(unitialized.memoryUsage() == 0); | ||||
|   } | ||||
|  | ||||
|   SECTION("JSON_ARRAY_SIZE(0) if empty") { | ||||
|     REQUIRE(arr.memoryUsage() == JSON_ARRAY_SIZE(0)); | ||||
|   } | ||||
|  | ||||
|   SECTION("JSON_ARRAY_SIZE(1) after add") { | ||||
|     arr.add("hello"); | ||||
|     REQUIRE(arr.memoryUsage() == JSON_ARRAY_SIZE(1)); | ||||
|   } | ||||
|  | ||||
|   SECTION("includes the size of the string") { | ||||
|     arr.add(std::string("hello")); | ||||
|     REQUIRE(arr.memoryUsage() == JSON_ARRAY_SIZE(1) + 6); | ||||
|   } | ||||
|  | ||||
|   SECTION("includes the size of the nested array") { | ||||
|     JsonArray nested = arr.createNestedArray(); | ||||
|     nested.add(42); | ||||
|     REQUIRE(arr.memoryUsage() == 2 * JSON_ARRAY_SIZE(1)); | ||||
|   } | ||||
|  | ||||
|   SECTION("includes the size of the nested arrect") { | ||||
|     JsonObject nested = arr.createNestedObject(); | ||||
|     nested["hello"] = "world"; | ||||
|     REQUIRE(arr.memoryUsage() == JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(1)); | ||||
|   } | ||||
| } | ||||
							
								
								
									
										35
									
								
								extras/tests/JsonArray/nesting.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								extras/tests/JsonArray/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("JsonArray::nesting()") { | ||||
|   DynamicJsonDocument doc(4096); | ||||
|   JsonArray arr = doc.to<JsonArray>(); | ||||
|  | ||||
|   SECTION("return 0 if uninitialized") { | ||||
|     JsonArray unitialized; | ||||
|     REQUIRE(unitialized.nesting() == 0); | ||||
|   } | ||||
|  | ||||
|   SECTION("returns 1 for empty array") { | ||||
|     REQUIRE(arr.nesting() == 1); | ||||
|   } | ||||
|  | ||||
|   SECTION("returns 1 for flat array") { | ||||
|     arr.add("hello"); | ||||
|     REQUIRE(arr.nesting() == 1); | ||||
|   } | ||||
|  | ||||
|   SECTION("returns 2 with nested array") { | ||||
|     arr.createNestedArray(); | ||||
|     REQUIRE(arr.nesting() == 2); | ||||
|   } | ||||
|  | ||||
|   SECTION("returns 2 with nested object") { | ||||
|     arr.createNestedObject(); | ||||
|     REQUIRE(arr.nesting() == 2); | ||||
|   } | ||||
| } | ||||
							
								
								
									
										68
									
								
								extras/tests/JsonArray/remove.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										68
									
								
								extras/tests/JsonArray/remove.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("JsonArray::remove()") { | ||||
|   DynamicJsonDocument doc(4096); | ||||
|   JsonArray _array = doc.to<JsonArray>(); | ||||
|   _array.add(1); | ||||
|   _array.add(2); | ||||
|   _array.add(3); | ||||
|  | ||||
|   SECTION("RemoveFirstByIndex") { | ||||
|     _array.remove(0); | ||||
|  | ||||
|     REQUIRE(2 == _array.size()); | ||||
|     REQUIRE(_array[0] == 2); | ||||
|     REQUIRE(_array[1] == 3); | ||||
|   } | ||||
|  | ||||
|   SECTION("RemoveMiddleByIndex") { | ||||
|     _array.remove(1); | ||||
|  | ||||
|     REQUIRE(2 == _array.size()); | ||||
|     REQUIRE(_array[0] == 1); | ||||
|     REQUIRE(_array[1] == 3); | ||||
|   } | ||||
|  | ||||
|   SECTION("RemoveLastByIndex") { | ||||
|     _array.remove(2); | ||||
|  | ||||
|     REQUIRE(2 == _array.size()); | ||||
|     REQUIRE(_array[0] == 1); | ||||
|     REQUIRE(_array[1] == 2); | ||||
|   } | ||||
|  | ||||
|   SECTION("RemoveFirstByIterator") { | ||||
|     JsonArray::iterator it = _array.begin(); | ||||
|     _array.remove(it); | ||||
|  | ||||
|     REQUIRE(2 == _array.size()); | ||||
|     REQUIRE(_array[0] == 2); | ||||
|     REQUIRE(_array[1] == 3); | ||||
|   } | ||||
|  | ||||
|   SECTION("RemoveMiddleByIterator") { | ||||
|     JsonArray::iterator it = _array.begin(); | ||||
|     ++it; | ||||
|     _array.remove(it); | ||||
|  | ||||
|     REQUIRE(2 == _array.size()); | ||||
|     REQUIRE(_array[0] == 1); | ||||
|     REQUIRE(_array[1] == 3); | ||||
|   } | ||||
|  | ||||
|   SECTION("RemoveLastByIterator") { | ||||
|     JsonArray::iterator it = _array.begin(); | ||||
|     ++it; | ||||
|     ++it; | ||||
|     _array.remove(it); | ||||
|  | ||||
|     REQUIRE(2 == _array.size()); | ||||
|     REQUIRE(_array[0] == 1); | ||||
|     REQUIRE(_array[1] == 2); | ||||
|   } | ||||
| } | ||||
							
								
								
									
										31
									
								
								extras/tests/JsonArray/size.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								extras/tests/JsonArray/size.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,31 @@ | ||||
| // ArduinoJson - arduinojson.org | ||||
| // Copyright Benoit Blanchon 2014-2019 | ||||
| // MIT License | ||||
|  | ||||
| #include <ArduinoJson.h> | ||||
| #include <catch.hpp> | ||||
|  | ||||
| TEST_CASE("JsonArray::size()") { | ||||
|   DynamicJsonDocument doc(4096); | ||||
|   JsonArray array = doc.to<JsonArray>(); | ||||
|  | ||||
|   SECTION("returns 0 is empty") { | ||||
|     REQUIRE(0U == array.size()); | ||||
|   } | ||||
|  | ||||
|   SECTION("increases after add()") { | ||||
|     array.add("hello"); | ||||
|     REQUIRE(1U == array.size()); | ||||
|  | ||||
|     array.add("world"); | ||||
|     REQUIRE(2U == array.size()); | ||||
|   } | ||||
|  | ||||
|   SECTION("remains the same after replacing an element") { | ||||
|     array.add("hello"); | ||||
|     REQUIRE(1U == array.size()); | ||||
|  | ||||
|     array[0] = "hello"; | ||||
|     REQUIRE(1U == array.size()); | ||||
|   } | ||||
| } | ||||
							
								
								
									
										31
									
								
								extras/tests/JsonArray/std_string.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								extras/tests/JsonArray/std_string.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,31 @@ | ||||
| // 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); | ||||
|   JsonArray array = doc.to<JsonArray>(); | ||||
|  | ||||
|   SECTION("add()") { | ||||
|     std::string value("hello"); | ||||
|     array.add(value); | ||||
|     eraseString(value); | ||||
|     REQUIRE(std::string("hello") == array[0]); | ||||
|   } | ||||
|  | ||||
|   SECTION("operator[]") { | ||||
|     std::string value("world"); | ||||
|     array.add("hello"); | ||||
|     array[0] = value; | ||||
|     eraseString(value); | ||||
|     REQUIRE(std::string("world") == array[0]); | ||||
|   } | ||||
| } | ||||
							
								
								
									
										162
									
								
								extras/tests/JsonArray/subscript.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										162
									
								
								extras/tests/JsonArray/subscript.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,162 @@ | ||||
| // ArduinoJson - arduinojson.org | ||||
| // Copyright Benoit Blanchon 2014-2019 | ||||
| // MIT License | ||||
|  | ||||
| #include <ArduinoJson.h> | ||||
| #include <stdint.h> | ||||
| #include <catch.hpp> | ||||
|  | ||||
| TEST_CASE("JsonArray::operator[]") { | ||||
|   DynamicJsonDocument doc(4096); | ||||
|   JsonArray array = doc.to<JsonArray>(); | ||||
|   array.add(0); | ||||
|  | ||||
|   SECTION("int") { | ||||
|     array[0] = 123; | ||||
|     REQUIRE(123 == array[0].as<int>()); | ||||
|     REQUIRE(true == array[0].is<int>()); | ||||
|     REQUIRE(false == array[0].is<bool>()); | ||||
|   } | ||||
|  | ||||
| #if ARDUINOJSON_USE_LONG_LONG | ||||
|   SECTION("long long") { | ||||
|     array[0] = 9223372036854775807; | ||||
|     REQUIRE(9223372036854775807 == array[0].as<int64_t>()); | ||||
|     REQUIRE(true == array[0].is<int64_t>()); | ||||
|     REQUIRE(false == array[0].is<int32_t>()); | ||||
|     REQUIRE(false == array[0].is<bool>()); | ||||
|   } | ||||
| #endif | ||||
|  | ||||
|   SECTION("double") { | ||||
|     array[0] = 123.45; | ||||
|     REQUIRE(123.45 == array[0].as<double>()); | ||||
|     REQUIRE(true == array[0].is<double>()); | ||||
|     REQUIRE(false == array[0].is<int>()); | ||||
|   } | ||||
|  | ||||
|   SECTION("bool") { | ||||
|     array[0] = true; | ||||
|     REQUIRE(true == array[0].as<bool>()); | ||||
|     REQUIRE(true == array[0].is<bool>()); | ||||
|     REQUIRE(false == array[0].is<int>()); | ||||
|   } | ||||
|  | ||||
|   SECTION("const char*") { | ||||
|     const char* str = "hello"; | ||||
|  | ||||
|     array[0] = str; | ||||
|     REQUIRE(str == array[0].as<const char*>()); | ||||
|     REQUIRE(str == array[0].as<char*>());  // <- short hand | ||||
|     REQUIRE(true == array[0].is<const char*>()); | ||||
|     REQUIRE(false == array[0].is<int>()); | ||||
|   } | ||||
|  | ||||
|   SECTION("nested array") { | ||||
|     DynamicJsonDocument doc2(4096); | ||||
|     JsonArray arr2 = doc2.to<JsonArray>(); | ||||
|  | ||||
|     array[0] = arr2; | ||||
|  | ||||
|     REQUIRE(arr2 == array[0].as<JsonArray>()); | ||||
|     REQUIRE(true == array[0].is<JsonArray>()); | ||||
|     REQUIRE(false == array[0].is<int>()); | ||||
|   } | ||||
|  | ||||
|   SECTION("nested object") { | ||||
|     DynamicJsonDocument doc2(4096); | ||||
|     JsonObject obj = doc2.to<JsonObject>(); | ||||
|  | ||||
|     array[0] = obj; | ||||
|  | ||||
|     REQUIRE(obj == array[0].as<JsonObject>()); | ||||
|     REQUIRE(true == array[0].is<JsonObject>()); | ||||
|     REQUIRE(false == array[0].is<int>()); | ||||
|   } | ||||
|  | ||||
|   SECTION("array subscript") { | ||||
|     DynamicJsonDocument doc2(4096); | ||||
|     JsonArray arr2 = doc2.to<JsonArray>(); | ||||
|     const char* str = "hello"; | ||||
|  | ||||
|     arr2.add(str); | ||||
|  | ||||
|     array[0] = arr2[0]; | ||||
|  | ||||
|     REQUIRE(str == array[0]); | ||||
|   } | ||||
|  | ||||
|   SECTION("object subscript") { | ||||
|     const char* str = "hello"; | ||||
|     DynamicJsonDocument doc2(4096); | ||||
|     JsonObject obj = doc2.to<JsonObject>(); | ||||
|  | ||||
|     obj["x"] = str; | ||||
|  | ||||
|     array[0] = obj["x"]; | ||||
|  | ||||
|     REQUIRE(str == array[0]); | ||||
|   } | ||||
|  | ||||
|   SECTION("should not duplicate const char*") { | ||||
|     array[0] = "world"; | ||||
|     const size_t expectedSize = JSON_ARRAY_SIZE(1); | ||||
|     REQUIRE(expectedSize == doc.memoryUsage()); | ||||
|   } | ||||
|  | ||||
|   SECTION("should duplicate char*") { | ||||
|     array[0] = const_cast<char*>("world"); | ||||
|     const size_t expectedSize = JSON_ARRAY_SIZE(1) + JSON_STRING_SIZE(6); | ||||
|     REQUIRE(expectedSize == doc.memoryUsage()); | ||||
|   } | ||||
|  | ||||
|   SECTION("should duplicate std::string") { | ||||
|     array[0] = std::string("world"); | ||||
|     const size_t expectedSize = JSON_ARRAY_SIZE(1) + JSON_STRING_SIZE(6); | ||||
|     REQUIRE(expectedSize == doc.memoryUsage()); | ||||
|   } | ||||
|  | ||||
|   SECTION("array[0].to<JsonObject>()") { | ||||
|     JsonObject obj = array[0].to<JsonObject>(); | ||||
|     REQUIRE(obj.isNull() == false); | ||||
|   } | ||||
|  | ||||
| #ifdef HAS_VARIABLE_LENGTH_ARRAY | ||||
|   SECTION("set(VLA)") { | ||||
|     int i = 16; | ||||
|     char vla[i]; | ||||
|     strcpy(vla, "world"); | ||||
|  | ||||
|     array.add("hello"); | ||||
|     array[0].set(vla); | ||||
|  | ||||
|     REQUIRE(std::string("world") == array[0]); | ||||
|   } | ||||
|  | ||||
|   SECTION("operator=(VLA)") { | ||||
|     int i = 16; | ||||
|     char vla[i]; | ||||
|     strcpy(vla, "world"); | ||||
|  | ||||
|     array.add("hello"); | ||||
|     array[0] = vla; | ||||
|  | ||||
|     REQUIRE(std::string("world") == array[0]); | ||||
|   } | ||||
| #endif | ||||
| } | ||||
|  | ||||
| TEST_CASE("JsonArrayConst::operator[]") { | ||||
|   DynamicJsonDocument doc(4096); | ||||
|   JsonArray array = doc.to<JsonArray>(); | ||||
|   array.add(0); | ||||
|  | ||||
|   SECTION("int") { | ||||
|     array[0] = 123; | ||||
|     JsonArrayConst carr = array; | ||||
|  | ||||
|     REQUIRE(123 == carr[0].as<int>()); | ||||
|     REQUIRE(true == carr[0].is<int>()); | ||||
|     REQUIRE(false == carr[0].is<bool>()); | ||||
|   } | ||||
| } | ||||
							
								
								
									
										35
									
								
								extras/tests/JsonArray/undefined.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								extras/tests/JsonArray/undefined.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("Undefined JsonArray") { | ||||
|   JsonArray array; | ||||
|  | ||||
|   SECTION("SubscriptFails") { | ||||
|     REQUIRE(array[0].isNull()); | ||||
|   } | ||||
|  | ||||
|   SECTION("AddFails") { | ||||
|     array.add(1); | ||||
|     REQUIRE(0 == array.size()); | ||||
|   } | ||||
|  | ||||
|   SECTION("CreateNestedArrayFails") { | ||||
|     REQUIRE(array.createNestedArray().isNull()); | ||||
|   } | ||||
|  | ||||
|   SECTION("CreateNestedObjectFails") { | ||||
|     REQUIRE(array.createNestedObject().isNull()); | ||||
|   } | ||||
|  | ||||
|   SECTION("PrintToWritesBrackets") { | ||||
|     char buffer[32]; | ||||
|     serializeJson(array, buffer, sizeof(buffer)); | ||||
|     REQUIRE_THAT(buffer, Equals("null")); | ||||
|   } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user