Browse Source

fixed an issue in xml parsing and added tests

Kolja Strohm 2 years ago
parent
commit
374565ffc8

+ 94 - 94
Framework Tests/Cache.cpp

@@ -6,102 +6,102 @@ using namespace Microsoft::VisualStudio::CppUnitTestFramework;
 
 namespace FrameworkTests
 {
-    TEST_CLASS( CacheTests )
-    {
-    public:
+	TEST_CLASS(CacheTests)
+	{
+	public:
 
-        TEST_METHOD( EmtptyTest )
-        {
-            Framework::Cache<int, int> cache( 100, []( int i ) {
-                return i;
-            }, Framework::CacheCleanupStrategy::RANDOM );
-            Assert::IsTrue( cache.getCurrentSize() == 0, L"getCurrentSize() on empty cache should be 0" );
-            Assert::IsTrue( cache.getMaxSize() == 100, L"getMaxSize() on empty cache" );
-        }
+		TEST_METHOD(EmtptyTest)
+		{
+			Framework::Cache<int, int> cache(100, [](int i) {
+				return i;
+				}, Framework::CacheCleanupStrategy::RANDOM);
+			Assert::IsTrue(cache.getCurrentSize() == 0, L"getCurrentSize() on empty cache should be 0");
+			Assert::IsTrue(cache.getMaxSize() == 100, L"getMaxSize() on empty cache");
+		}
 
-        TEST_METHOD( PutTest )
-        {
-            Framework::Cache<int, int> cache( 2, []( int i ) {
-                return i;
-            }, Framework::CacheCleanupStrategy::RANDOM );
-            cache.put( 0, 100 );
-            cache.put( 1, 10 );
-            cache.put( 2, 1000 );
-            Assert::IsTrue( cache.getCurrentSize() == 2, L"unexpected count of elements in cache" );
-            Assert::IsTrue( cache.has( 0 ) ? cache.get( 0 ) == 100 : 1, L"invalid value at key 0 in cache after adding elements" );
-            Assert::IsTrue( cache.has( 1 ) ? cache.get( 1 ) == 10 : 1, L"invalid value at key 1 in cache after adding elements" );
-            Assert::IsTrue( cache.get( 2 ) == 1000, L"invalid value at key 2 in cache after adding elements" );
-            int count = 0;
-            for( Framework::MapEntry< int, int > i : cache )
-            {
-                if( i.getKey() == 0 )
-                    Assert::IsTrue( i.getValue() == 100, L"invalid value at key 0 in cache after adding elements" );
-                else if( i.getKey() == 1 )
-                    Assert::IsTrue( i.getValue() == 10, L"invalid value at key 1 in cache after adding elements" );
-                else if( i.getKey() == 2 )
-                    Assert::IsTrue( i.getValue() == 1000, L"invalid value at key 2 in cache after adding elements" );
-                else
-                    Assert::Fail( L"invalid key in cache after adding elements" );
-                count++;
-            }
-            Assert::IsTrue( count == 2, L"unexpected count of elements in cache" );
-        }
+		TEST_METHOD(PutTest)
+		{
+			Framework::Cache<int, int> cache(2, [](int i) {
+				return i;
+				}, Framework::CacheCleanupStrategy::RANDOM);
+			cache.put(0, 100);
+			cache.put(1, 10);
+			cache.put(2, 1000);
+			Assert::IsTrue(cache.getCurrentSize() == 2, L"unexpected count of elements in cache");
+			Assert::IsTrue(cache.has(0) ? cache.get(0) == 100 : 1, L"invalid value at key 0 in cache after adding elements");
+			Assert::IsTrue(cache.has(1) ? cache.get(1) == 10 : 1, L"invalid value at key 1 in cache after adding elements");
+			Assert::IsTrue(cache.get(2) == 1000, L"invalid value at key 2 in cache after adding elements");
+			int count = 0;
+			for (Framework::MapEntry< int, int > i : cache)
+			{
+				if (i.getKey() == 0)
+					Assert::IsTrue(i.getValue() == 100, L"invalid value at key 0 in cache after adding elements");
+				else if (i.getKey() == 1)
+					Assert::IsTrue(i.getValue() == 10, L"invalid value at key 1 in cache after adding elements");
+				else if (i.getKey() == 2)
+					Assert::IsTrue(i.getValue() == 1000, L"invalid value at key 2 in cache after adding elements");
+				else
+					Assert::Fail(L"invalid key in cache after adding elements");
+				count++;
+			}
+			Assert::IsTrue(count == 2, L"unexpected count of elements in cache");
+		}
 
-        TEST_METHOD( OldestTest )
-        {
-            Framework::Cache<int, int> cache( 2, []( int i ) {
-                return i;
-            }, Framework::CacheCleanupStrategy::OLDEST );
-            cache.put( 0, 100 );
-            Sleep( 1000 );
-            cache.put( 1, 10 );
-            Sleep( 1000 );
-            cache.put( 2, 1000 );
-            Assert::IsTrue( cache.getCurrentSize() == 2, L"unexpected count of elements in cache" );
-            Assert::IsFalse( cache.has( 0 ), L"invalid value at key 0 in cache after adding elements" );
-            Assert::IsTrue( cache.has( 1 ) && cache.get( 1 ) == 10, L"invalid value at key 1 in cache after adding elements" );
-            Assert::IsTrue( cache.get( 2 ) == 1000, L"invalid value at key 2 in cache after adding elements" );
-            int count = 0;
-            for( Framework::MapEntry< int, int > i : cache )
-            {
-                if( i.getKey() == 1 )
-                    Assert::IsTrue( i.getValue() == 10, L"invalid value at key 1 in cache after adding elements" );
-                else if( i.getKey() == 2 )
-                    Assert::IsTrue( i.getValue() == 1000, L"invalid value at key 2 in cache after adding elements" );
-                else
-                    Assert::Fail( L"invalid key in cache after adding elements" );
-                count++;
-            }
-            Assert::IsTrue( count == 2, L"unexpected count of elements in cache" );
-        }
+		TEST_METHOD(OldestTest)
+		{
+			Framework::Cache<int, int> cache(2, [](int i) {
+				return i;
+				}, Framework::CacheCleanupStrategy::OLDEST);
+			cache.put(0, 100);
+			Sleep(1000);
+			cache.put(1, 10);
+			Sleep(1000);
+			cache.put(2, 1000);
+			Assert::IsTrue(cache.getCurrentSize() == 2, L"unexpected count of elements in cache");
+			Assert::IsFalse(cache.has(0), L"invalid value at key 0 in cache after adding elements");
+			Assert::IsTrue(cache.has(1) && cache.get(1) == 10, L"invalid value at key 1 in cache after adding elements");
+			Assert::IsTrue(cache.get(2) == 1000, L"invalid value at key 2 in cache after adding elements");
+			int count = 0;
+			for (Framework::MapEntry< int, int > i : cache)
+			{
+				if (i.getKey() == 1)
+					Assert::IsTrue(i.getValue() == 10, L"invalid value at key 1 in cache after adding elements");
+				else if (i.getKey() == 2)
+					Assert::IsTrue(i.getValue() == 1000, L"invalid value at key 2 in cache after adding elements");
+				else
+					Assert::Fail(L"invalid key in cache after adding elements");
+				count++;
+			}
+			Assert::IsTrue(count == 2, L"unexpected count of elements in cache");
+		}
 
-        TEST_METHOD( LongestNotUsedTest )
-        {
-            Framework::Cache<int, int> cache( 2, []( int i ) {
-                return i;
-            }, Framework::CacheCleanupStrategy::LONGEST_NOT_USED );
-            cache.put( 0, 100 );
-            Sleep( 1000 );
-            cache.put( 1, 10 );
-            Sleep( 1000 );
-            cache.get( 0 );
-            cache.put( 2, 1000 );
-            Assert::IsTrue( cache.getCurrentSize() == 2, L"unexpected count of elements in cache" );
-            Assert::IsTrue( cache.has( 0 ) && cache.get( 0 ) == 100, L"invalid value at key 0 in cache after adding elements" );
-            Assert::IsFalse( cache.has( 1 ), L"invalid value at key 1 in cache after adding elements" );
-            Assert::IsTrue( cache.get( 2 ) == 1000, L"invalid value at key 2 in cache after adding elements" );
-            int count = 0;
-            for( Framework::MapEntry< int, int > i : cache )
-            {
-                if( i.getKey() == 0 )
-                    Assert::IsTrue( i.getValue() == 100, L"invalid value at key 0 in cache after adding elements" );
-                else if( i.getKey() == 2 )
-                    Assert::IsTrue( i.getValue() == 1000, L"invalid value at key 2 in cache after adding elements" );
-                else
-                    Assert::Fail( L"invalid key in cache after adding elements" );
-                count++;
-            }
-            Assert::IsTrue( count == 2, L"unexpected count of elements in cache" );
-        }
-    };
+		TEST_METHOD(LongestNotUsedTest)
+		{
+			Framework::Cache<int, int> cache(2, [](int i) {
+				return i;
+				}, Framework::CacheCleanupStrategy::LONGEST_NOT_USED);
+			cache.put(0, 100);
+			Sleep(1000);
+			cache.put(1, 10);
+			Sleep(1000);
+			cache.get(0);
+			cache.put(2, 1000);
+			Assert::IsTrue(cache.getCurrentSize() == 2, L"unexpected count of elements in cache");
+			Assert::IsTrue(cache.has(0) && cache.get(0) == 100, L"invalid value at key 0 in cache after adding elements");
+			Assert::IsFalse(cache.has(1), L"invalid value at key 1 in cache after adding elements");
+			Assert::IsTrue(cache.get(2) == 1000, L"invalid value at key 2 in cache after adding elements");
+			int count = 0;
+			for (Framework::MapEntry< int, int > i : cache)
+			{
+				if (i.getKey() == 0)
+					Assert::IsTrue(i.getValue() == 100, L"invalid value at key 0 in cache after adding elements");
+				else if (i.getKey() == 2)
+					Assert::IsTrue(i.getValue() == 1000, L"invalid value at key 2 in cache after adding elements");
+				else
+					Assert::Fail(L"invalid key in cache after adding elements");
+				count++;
+			}
+			Assert::IsTrue(count == 2, L"unexpected count of elements in cache");
+		}
+	};
 }

+ 1 - 0
Framework Tests/Framework Tests.vcxproj

@@ -174,6 +174,7 @@
       <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
       <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
     </ClCompile>
+    <ClCompile Include="XML.cpp" />
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="pch.h" />

+ 3 - 0
Framework Tests/Framework Tests.vcxproj.filters

@@ -27,6 +27,9 @@
     <ClCompile Include="Json.cpp">
       <Filter>Quelldateien</Filter>
     </ClCompile>
+    <ClCompile Include="XML.cpp">
+      <Filter>Quelldateien</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="pch.h">

BIN
Framework Tests/Framwork.dll


+ 116 - 0
Framework Tests/XML.cpp

@@ -0,0 +1,116 @@
+#include "pch.h"
+#include "CppUnitTest.h"
+#include <XML.h>
+
+using namespace Microsoft::VisualStudio::CppUnitTestFramework;
+
+namespace FrameworkTests
+{
+    TEST_CLASS(XMLParserTests)
+    {
+    public:
+        TEST_METHOD(EmptyElementTest)
+        {
+            Framework::XML::Element* element = new Framework::XML::Element("<xml attr attr2=\"\" attr3=\"true\"/>");
+            Assert::IsTrue(element->getAttributeCount() == 3, L"One ore more xml attributes are missing");
+            Assert::IsTrue(element->getName().istGleich("xml"), L"Der Element Name ist falsch");
+            Assert::IsTrue(element->getChildCount() == 0, L"Das Element sollte keine Kinder haben");
+            Assert::IsTrue(element->hasAttribute("attr"), L"Das Attribut attr fehlt");
+            Assert::IsTrue(element->hasAttribute("attr2"), L"Das Attribut attr2 fehlt");
+            Assert::IsTrue(element->hasAttribute("attr3"), L"Das Attribut attr3 fehlt");
+            Assert::IsTrue(element->hasAttribute("") == 0, L"Es gibt ein falsches Attribut");
+            Assert::IsTrue(element->getAttributeName(0).istGleich("attr"), L"Der Attribut name ist falsch");
+            Assert::IsTrue(element->getAttributeName(1).istGleich("attr2"), L"Der Attribut name ist falsch");
+            Assert::IsTrue(element->getAttributeName(2).istGleich("attr3"), L"Der Attribut name ist falsch");
+            Assert::IsTrue(element->getAttributeValue("attr").istGleich(""), L"Der Wert des Attributes ist falsch");
+            Assert::IsTrue(element->getAttributeValue("attr2").istGleich(""), L"Der Wert des Attributes ist falsch");
+            Assert::IsTrue(element->getAttributeValue("attr3").istGleich("true"), L"Der Wert des Attributes ist falsch");
+            element->release();
+        }
+
+        TEST_METHOD(ChildrenElementTest)
+        {
+            Framework::XML::Element* element = new Framework::XML::Element("<xml><a1/><a2/></xml>");
+            Assert::IsTrue(element->getAttributeCount() == 0, L"Die Anzahl der Attribute stimmt nicht");
+            Assert::IsTrue(element->getName().istGleich("xml"), L"Der Element Name ist falsch");
+            Assert::IsTrue(element->getChildCount() == 2, L"Die Anzahl der Kinder ist falsch");
+            Assert::IsTrue(element->zChild(0)->getAttributeCount() == 0, L"Die Anzahl der Attribute stimmt nicht");
+            Assert::IsTrue(element->zChild(1)->getAttributeCount() == 0, L"Die Anzahl der Attribute stimmt nicht");
+            Assert::IsTrue(element->zChild(0)->getName().istGleich("a1"), L"Der Element Name ist falsch");
+            Assert::IsTrue(element->zChild(1)->getName().istGleich("a2"), L"Der Element Name ist falsch");
+            element->release();
+        }
+
+        TEST_METHOD(WhitespaceTest)
+        {
+            Framework::XML::Element* element = new Framework::XML::Element("<xml \t \r\n \t > \r\n <a1 x y z=\"\"/> <a2/>\t</xml     >");
+            Assert::IsTrue(element->getAttributeCount() == 0, L"Die Anzahl der Attribute stimmt nicht");
+            Assert::IsTrue(element->getName().istGleich("xml"), L"Der Element Name ist falsch");
+            Assert::IsTrue(element->getChildCount() == 2, L"Die Anzahl der Kinder ist falsch");
+            Assert::IsTrue(element->zChild(0)->getAttributeCount() == 3, L"Die Anzahl der Attribute stimmt nicht");
+            Assert::IsTrue(element->zChild(1)->getAttributeCount() == 0, L"Die Anzahl der Attribute stimmt nicht");
+            Assert::IsTrue(element->zChild(0)->getName().istGleich("a1"), L"Der Element Name ist falsch");
+            Assert::IsTrue(element->zChild(1)->getName().istGleich("a2"), L"Der Element Name ist falsch");
+            element->release();
+        }
+    };
+
+    TEST_CLASS(XMLEditorTests)
+    {
+    public:
+        TEST_METHOD(SetAttributeTest)
+        {
+            Framework::XML::Element* element = new Framework::XML::Element("<xml><a1 x y z=\"\"/><a2/></xml>");
+            element->selectChildren().setAttribute("x", "true");
+            Assert::IsTrue(element->getName().istGleich("xml"), L"Der Element Name ist falsch");
+            Assert::IsTrue(element->getChildCount() == 2, L"Die Anzahl der Kinder ist falsch");
+            Assert::IsTrue(element->zChild(0)->getAttributeCount() == 3, L"Die Anzahl der Attribute stimmt nicht");
+            Assert::IsTrue(element->zChild(1)->getAttributeCount() == 1, L"Die Anzahl der Attribute stimmt nicht");
+            Assert::IsTrue(element->zChild(0)->getName().istGleich("a1"), L"Der Element Name ist falsch");
+            Assert::IsTrue(element->zChild(1)->getName().istGleich("a2"), L"Der Element Name ist falsch");
+            Assert::IsTrue(element->zChild(0)->getAttributeValue("x").istGleich("true"), L"Der Attribut wert ist falsch");
+            Assert::IsTrue(element->zChild(1)->getAttributeValue("x").istGleich("true"), L"Der Attribut wert ist falsch");
+            element->release();
+        }
+
+        TEST_METHOD(RemoveAttributeTest)
+        {
+            Framework::XML::Element* element = new Framework::XML::Element("<xml><a1 x y z=\"\"/><a2/></xml>");
+            element->selectChildren().removeAttribute("x");
+            Assert::IsTrue(element->getName().istGleich("xml"), L"Der Element Name ist falsch");
+            Assert::IsTrue(element->getChildCount() == 2, L"Die Anzahl der Kinder ist falsch");
+            Assert::IsTrue(element->zChild(0)->getAttributeCount() == 2, L"Die Anzahl der Attribute stimmt nicht");
+            Assert::IsTrue(element->zChild(1)->getAttributeCount() == 0, L"Die Anzahl der Attribute stimmt nicht");
+            Assert::IsTrue(element->zChild(0)->getName().istGleich("a1"), L"Der Element Name ist falsch");
+            Assert::IsTrue(element->zChild(1)->getName().istGleich("a2"), L"Der Element Name ist falsch");
+            Assert::IsTrue(element->zChild(0)->hasAttribute("x") == 0, L"Ein entferntes Attibut ist noch vorhanden");
+            Assert::IsTrue(element->zChild(1)->hasAttribute("x") == 0, L"Ein entferntes Attibut ist noch vorhanden");
+            element->release();
+        }
+
+        TEST_METHOD(AddChildrenTest)
+        {
+            Framework::XML::Element* element = new Framework::XML::Element("<xml><a1 x y z=\"\"/><a2/></xml>");
+            element->selectChildren().whereAttributeExists("x").addChild(element->dublicate());
+            Assert::IsTrue(element->getName().istGleich("xml"), L"Der Element Name ist falsch");
+            Assert::IsTrue(element->getChildCount() == 2, L"Die Anzahl der Kinder ist falsch");
+            Assert::IsTrue(element->zChild(0)->getAttributeCount() == 3, L"Die Anzahl der Attribute stimmt nicht");
+            Assert::IsTrue(element->zChild(1)->getAttributeCount() == 0, L"Die Anzahl der Attribute stimmt nicht");
+            Assert::IsTrue(element->zChild(0)->getName().istGleich("a1"), L"Der Element Name ist falsch");
+            Assert::IsTrue(element->zChild(1)->getName().istGleich("a2"), L"Der Element Name ist falsch");
+            Assert::IsTrue(element->zChild(0)->getChildCount() == 1, L"Anzahl der Kinder ist Falsch");
+            Assert::IsTrue(element->zChild(1)->getChildCount() == 0, L"Anzahl der Kinder ist Falsch");
+            element->release();
+        }
+
+        TEST_METHOD(RemoveTest)
+        {
+            Framework::XML::Element* element = new Framework::XML::Element("<xml><a1 x y z=\"\"/><a2/></xml>");
+            element->selectChildren().whereAttributeExists("x").remove();
+            Assert::IsTrue(element->getName().istGleich("xml"), L"Der Element Name ist falsch");
+            Assert::IsTrue(element->getChildCount() == 1, L"Die Anzahl der Kinder ist falsch");
+            Assert::IsTrue(element->zChild(0)->getAttributeCount() == 0, L"Die Anzahl der Attribute stimmt nicht");
+            element->release();
+        }
+    };
+}

+ 4 - 1
XML.cpp

@@ -127,8 +127,11 @@ Element::Element( Text string, Element *zParent )
                         if( text->getText()[ 0 ] == '<' && text->getText()[ text->getLength() - 1 ] == '>' )
                         {
                             int start = 0;
+                            int lastStart = -1;
                             while( start < text->getLength() )
                             {
+                                if (lastStart == start) break;
+                                lastStart = start;
                                 bool esc = 0;
                                 bool inString1 = 0;
                                 bool inString2 = 0;
@@ -166,6 +169,7 @@ Element::Element( Text string, Element *zParent )
                                         lastSlash = 0;
                                         break;
                                     case '/':
+                                        lastSlash = 0;
                                         if( !inString1 && !inString2 )
                                         {
                                             lastSlash = 1;
@@ -173,7 +177,6 @@ Element::Element( Text string, Element *zParent )
                                                 openSlash = 1;
                                         }
                                         esc = 0;
-                                        lastSlash = 0;
                                         lastOpen = 0;
                                         break;
                                     case '>':