mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 16:14:11 +01:00 
			
		
		
		
	Generator: added a test of one string in an array
This commit is contained in:
		
							
								
								
									
										1
									
								
								JsonGeneratorTests/.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								JsonGeneratorTests/.gitignore
									
									
									
									
										vendored
									
									
								
							| @@ -3,3 +3,4 @@ | |||||||
| /*.suo | /*.suo | ||||||
| /Debug | /Debug | ||||||
| /ipch | /ipch | ||||||
|  | /*.opensdf | ||||||
|   | |||||||
| @@ -6,9 +6,3 @@ | |||||||
| #include <string.h> | #include <string.h> | ||||||
|  |  | ||||||
| #include "JsonArray.h" | #include "JsonArray.h" | ||||||
|  |  | ||||||
| void JsonArray::writeTo(char* buffer, size_t bufferSize) |  | ||||||
| { |  | ||||||
|     strncpy(buffer, "[]", bufferSize); |  | ||||||
|  |  | ||||||
| } |  | ||||||
| @@ -5,9 +5,45 @@ | |||||||
|  |  | ||||||
| #pragma once | #pragma once | ||||||
|  |  | ||||||
|  | template<int N> | ||||||
| class JsonArray | class JsonArray | ||||||
| { | { | ||||||
| public: | public: | ||||||
|     void writeTo(char* buffer, size_t bufferSize); |     JsonArray() | ||||||
|  |     { | ||||||
|  |         itemCount = 0; | ||||||
|  |     } | ||||||
|  |      | ||||||
|  |     void add(const char* data) | ||||||
|  |     { | ||||||
|  |         if (itemCount <= N) | ||||||
|  |             items[itemCount++] = data; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     void writeTo(char* buffer, size_t bufferSize) | ||||||
|  |     { | ||||||
|  |         buffer[0] = 0; | ||||||
|  |  | ||||||
|  |         append("[", buffer, bufferSize); | ||||||
|  |  | ||||||
|  |         for (int i = 0; i < itemCount; i++) | ||||||
|  |         { | ||||||
|  |             append("'", buffer, bufferSize); | ||||||
|  |             append(items[i], buffer, bufferSize); | ||||||
|  |             append("'", buffer, bufferSize); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         append("]", buffer, bufferSize);        | ||||||
|  |     } | ||||||
|  |  | ||||||
|  | private: | ||||||
|  |     const char* items[N]; | ||||||
|  |     int itemCount; | ||||||
|  |  | ||||||
|  |     void append(const char* source, char* dest, size_t destSize) | ||||||
|  |     { | ||||||
|  |         int len = strlen(dest); | ||||||
|  |         strncpy(dest + len, source, destSize - len); | ||||||
|  |     } | ||||||
| }; | }; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -7,16 +7,29 @@ namespace JsonGeneratorTests | |||||||
| {		 | {		 | ||||||
|     TEST_CLASS(JsonArrayTests) |     TEST_CLASS(JsonArrayTests) | ||||||
|     { |     { | ||||||
|  |         JsonArray<32> arr; | ||||||
|  |          | ||||||
|     public: |     public: | ||||||
|          |          | ||||||
|         TEST_METHOD(EmptyArray) |         TEST_METHOD(EmptyArray) | ||||||
|         { |         { | ||||||
|             JsonArray arr; |             AssertJsonIs("[]"); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         TEST_METHOD(OneString) | ||||||
|  |         { | ||||||
|  |             arr.add("hello"); | ||||||
|  |  | ||||||
|  |             AssertJsonIs("['hello']"); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         void AssertJsonIs(const char* expected) | ||||||
|  |         {       | ||||||
|             char buffer[256]; |             char buffer[256]; | ||||||
|  |  | ||||||
|             arr.writeTo(buffer, sizeof(buffer)); |             arr.writeTo(buffer, sizeof(buffer)); | ||||||
|  |  | ||||||
|             Assert::AreEqual("[]", buffer); |             Assert::AreEqual(expected, buffer); | ||||||
|         } |         } | ||||||
|     }; |     }; | ||||||
| } | } | ||||||
							
								
								
									
										1
									
								
								JsonParserTests/.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								JsonParserTests/.gitignore
									
									
									
									
										vendored
									
									
								
							| @@ -3,3 +3,4 @@ | |||||||
| /*.suo | /*.suo | ||||||
| /Debug | /Debug | ||||||
| /ipch | /ipch | ||||||
|  | /*.opensdf | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user