فهرست منبع

Fixed bug in JsonObject::begin() and end()

Benoit Blanchon 11 سال پیش
والد
کامیت
0fe77176e1

+ 2 - 2
JsonParser/JsonObject.h

@@ -42,12 +42,12 @@ namespace ArduinoJson
 
             JsonObjectIterator begin()
             {
-                return firstChild();
+                return isObject() ? firstChild() : null();
             }
 
             JsonObjectIterator end()
             {
-                return nextSibling();
+                return isObject() ? nextSibling() : null();
             }
 
             DEPRECATED JsonArray getArray(const char* key);

+ 16 - 18
JsonParserTests/JsonArrayIteratorTests.cpp

@@ -7,28 +7,26 @@ using namespace ArduinoJson::Parser;
 
 namespace JsonParserTests
 {
-    TEST_CLASS(JsonObjectIteratorTests)
-    {
-    public:
-
-        TEST_METHOD(ThreeStrings)
-        {
-            char json [] = "{\"key1\":\"value1\",\"key2\":\"value2\",\"key3\":\"value3\"}";
-            char* expectedKeys [] = { "key1", "key2", "key3" };
-            char* expectedValues [] = { "value1", "value2", "value3" };
-            JsonParser<7> parser;
-
-            JsonHashTable a = parser.parse(json);
+	TEST_CLASS(JsonArrayIteratorTests)
+	{
+	public:
+		
+		TEST_METHOD(ThreeIntegers)
+		{
+            char json [] = "[1,2,3]";
+            long expected [] = { 1, 2, 3 };
+            JsonParser<4> parser;
+
+            JsonValue v = parser.parse(json);
+            JsonArray a = (ArduinoJson::Parser::JsonArray)v;
 
             int index = 0;
 
-            for (auto i : a)
+            for (long i : a)
             {
-                Assert::AreEqual(expectedKeys[index], i.key());
-                Assert::AreEqual(expectedValues[index], (const char*) i.value());
-                index++;
+                Assert::AreEqual(expected[index++], i);
             }
-        }
+		}
 
-    };
+	};
 }

+ 52 - 16
JsonParserTests/JsonObjectIteratorTests.cpp

@@ -7,26 +7,62 @@ using namespace ArduinoJson::Parser;
 
 namespace JsonParserTests
 {
-	TEST_CLASS(JsonArrayIteratorTests)
-	{
-	public:
-		
-		TEST_METHOD(ThreeIntegers)
-		{
-            char json [] = "[1,2,3]";
-            long expected [] = { 1, 2, 3 };
-            JsonParser<4> parser;
-
-            JsonValue v = parser.parse(json);
-            JsonArray a = (ArduinoJson::Parser::JsonArray)v;
+    TEST_CLASS(JsonObjectIteratorTests)
+    {
+    public:
+
+        TEST_METHOD(EmptyObject)
+        {
+            char json [] = "{}";
+            JsonParser<1> parser;
+
+            JsonHashTable a = parser.parse(json);           
+
+            int loopCount = 0;
+
+            for (auto i : a)
+            {
+                loopCount++;
+            }
+
+            Assert::AreEqual(0, loopCount);
+        }
+
+        TEST_METHOD(EmptyJson)
+        {
+            char json[] = "";
+            JsonParser<1> parser;
+
+            JsonHashTable a = parser.parse(json);
+
+            int loopCount = 0;
+
+            for (auto i : a)
+            {
+                loopCount++;
+            }
+
+            Assert::AreEqual(0, loopCount);
+        }
+
+        TEST_METHOD(ThreeStrings)
+        {
+            char json[] = "{\"key1\":\"value1\",\"key2\":\"value2\",\"key3\":\"value3\"}";
+            char* expectedKeys[] = { "key1", "key2", "key3" };
+            char* expectedValues[] = { "value1", "value2", "value3" };
+            JsonParser<7> parser;
+
+            JsonHashTable a = parser.parse(json);
 
             int index = 0;
 
-            for (long i : a)
+            for (auto i : a)
             {
-                Assert::AreEqual(expected[index++], i);
+                Assert::AreEqual(expectedKeys[index], i.key());
+                Assert::AreEqual(expectedValues[index], (const char*) i.value());
+                index++;
             }
-		}
+        }
 
-	};
+    };
 }

+ 2 - 2
JsonParserTests/JsonParserTests.vcxproj

@@ -91,9 +91,9 @@
     <ClCompile Include="..\JsonParser\JsonParserBase.cpp" />
     <ClCompile Include="..\JsonParser\JsonToken.cpp" />
     <ClCompile Include="..\JsonParser\JsonValue.cpp" />
-    <ClCompile Include="JsonArrayIteratorTests.cpp" />
-    <ClCompile Include="JsonArrayTests.cpp" />
     <ClCompile Include="JsonObjectIteratorTests.cpp" />
+    <ClCompile Include="JsonArrayTests.cpp" />
+    <ClCompile Include="JsonArrayIteratorTests.cpp" />
     <ClCompile Include="JsonObjectTests.cpp" />
     <ClCompile Include="GbathreeBug.cpp" />
   </ItemGroup>

+ 3 - 3
JsonParserTests/JsonParserTests.vcxproj.filters

@@ -33,9 +33,6 @@
     <ClCompile Include="..\JsonParser\JsonValue.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
-    <ClCompile Include="JsonArrayIteratorTests.cpp">
-      <Filter>Source Files</Filter>
-    </ClCompile>
     <ClCompile Include="..\JsonParser\JsonToken.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
@@ -45,6 +42,9 @@
     <ClCompile Include="JsonObjectTests.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
+    <ClCompile Include="JsonArrayIteratorTests.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
     <ClCompile Include="JsonObjectIteratorTests.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>