Browse Source

added missing comments as requested by code review and added basic tests for error throwing behaviour of parseSVGInput method, although currently not working due to problems with deploying external files for testing

Vincenz Mechler 5 years ago
parent
commit
e24cc22479

+ 30 - 0
SketchAssistant/SketchAssistant.Tests/SketchAssistant.Tests.csproj

@@ -67,6 +67,32 @@
       <Name>SketchAssistant</Name>
     </ProjectReference>
   </ItemGroup>
+  <ItemGroup>
+    <Folder Include="test_input_files\blacklisted\" />
+  </ItemGroup>
+  <ItemGroup>
+    <Content Include="test_input_files\whitelisted\all.svg">
+      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+    </Content>
+    <Content Include="test_input_files\whitelisted\circle_ellipse.svg">
+      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+    </Content>
+    <Content Include="test_input_files\whitelisted\line.svg">
+      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+    </Content>
+    <Content Include="test_input_files\whitelisted\path.svg">
+      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+    </Content>
+    <Content Include="test_input_files\whitelisted\polygon.svg">
+      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+    </Content>
+    <Content Include="test_input_files\whitelisted\polyline.svg">
+      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+    </Content>
+    <Content Include="test_input_files\whitelisted\rect.svg">
+      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+    </Content>
+  </ItemGroup>
   <Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
@@ -77,4 +103,8 @@
     <Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.4.0\build\net45\MSTest.TestAdapter.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.4.0\build\net45\MSTest.TestAdapter.targets'))" />
   </Target>
   <Import Project="..\packages\MSTest.TestAdapter.1.4.0\build\net45\MSTest.TestAdapter.targets" Condition="Exists('..\packages\MSTest.TestAdapter.1.4.0\build\net45\MSTest.TestAdapter.targets')" />
+  <PropertyGroup>
+    <PostBuildEvent>
+    </PostBuildEvent>
+  </PropertyGroup>
 </Project>

+ 157 - 82
SketchAssistant/SketchAssistant.Tests/UnitTest1.cs

@@ -4,6 +4,7 @@ using System.Drawing;
 using System.Collections.Generic;
 using SketchAssistant;
 using System.Windows.Forms;
+using System.IO;
 
 namespace Tests
 {
@@ -271,104 +272,178 @@ namespace Tests
             Assert.AreEqual(message, lastActionLabel);
         }
     }
+    
+    [TestClass]
+    public class FileImporterTests
+    { 
 
-    /*
-[TestClass]
-public class FileImporterTests
-{
-    [DataTestMethod]
-    [DataRow(new int[] { 54, 43, 57, 11, 145, 34, 113, 299, 0 }, new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 })]
-    [DataRow(new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 }, new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 })]
-    [DataRow(new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 }, new int[] { 54, 43, 57, 11, 145, 34, 113, 199, 0 })]
-    public void ParseISADInputSuccessfulTest(int[] xCoordinates, int[] yCoordinates)
-    {
-        Form1 program = new Form1();
-        FileImporter uut = new SketchAssistant.FileImporter();
-
-        List<String> file = new List<string>();
-        file.Add("drawing");
-        file.Add("300x200");
-        for (int i = 0; i < xCoordinates.Length - 2; i += 3)
+        /// <summary>
+        /// instance of TestContext to be able to access deployed files
+        /// </summary>
+         private TestContext testContextInstance;
+        /// <summary>
+        ///Gets or sets the test context which provides
+        ///information about and functionality for the current test run.
+        ///</summary>
+        public TestContext TestContext
         {
-            file.Add("line");
-            file.Add(xCoordinates[i] + ";" + yCoordinates[i]);
-            file.Add(xCoordinates[i + 1] + ";" + yCoordinates[i + 1]);
-            file.Add(xCoordinates[i + 2] + ";" + yCoordinates[i + 2]);
-            file.Add("endline");
+            get
+            {
+                return testContextInstance;
+            }
+            set
+            {
+                testContextInstance = value;
+            }
         }
-        file.Add("enddrawing");
+        /*
+        [DataTestMethod]
+        [DataRow(new int[] { 54, 43, 57, 11, 145, 34, 113, 299, 0 }, new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 })]
+        [DataRow(new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 }, new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 })]
+        [DataRow(new int[] { 33, 42, 140, 30, 30, 30, 32, 145, 2 }, new int[] { 54, 43, 57, 11, 145, 34, 113, 199, 0 })]
+        public void ParseISADInputSuccessfulTest(int[] xCoordinates, int[] yCoordinates)
+        {
+            Form1 program = new Form1();
+            FileImporter uut = new SketchAssistant.FileImporter();
+
+            List<String> file = new List<string>();
+            file.Add("drawing");
+            file.Add("300x200");
+            for (int i = 0; i < xCoordinates.Length - 2; i += 3)
+            {
+                file.Add("line");
+                file.Add(xCoordinates[i] + ";" + yCoordinates[i]);
+                file.Add(xCoordinates[i + 1] + ";" + yCoordinates[i + 1]);
+                file.Add(xCoordinates[i + 2] + ";" + yCoordinates[i + 2]);
+                file.Add("endline");
+            }
+            file.Add("enddrawing");
 
-        (int, int, List<Line>) values = uut.ParseISADInputForTesting(file.ToArray());
-        program.CreateCanvasAndSetPictureForTesting(values.Item1, values.Item2, values.Item3);
+            (int, int, List<Line>) values = uut.ParseISADInputForTesting(file.ToArray());
+            program.CreateCanvasAndSetPictureForTesting(values.Item1, values.Item2, values.Item3);
 
-        Line[] drawing = GetLeftImage(program).ToArray();
+            Line[] drawing = GetLeftImage(program).ToArray();
 
-        Assert.AreEqual(xCoordinates.Length / 3, drawing.Length);
-        for (int i = 0; i < xCoordinates.Length - 2; i += 3)
+            Assert.AreEqual(xCoordinates.Length / 3, drawing.Length);
+            for (int i = 0; i < xCoordinates.Length - 2; i += 3)
+            {
+                Point[] currentLine = drawing[i / 3].GetPoints().ToArray();
+                Assert.AreEqual(3, currentLine.Length);
+                for (int j = 0; j < 3; j++)
+                {
+                    Assert.IsTrue(currentLine[j].X == xCoordinates[i + j] && currentLine[j].Y == yCoordinates[i + j]);
+                }
+            }
+        }
+
+        [DataTestMethod]
+        [DataRow(new String[] {})]
+        [DataRow(new String[] { "begindrawing", "300x300", "line", "50;50", "100;50", "endline", "enddrawing" })]
+        [DataRow(new String[] { "drawing", "300;300", "line", "50;50", "100;50", "endline", "enddrawing" })]
+        [DataRow(new String[] { "drawing", "30.5x300", "line", "50;50", "100;50", "endline", "enddrawing" })]
+        [DataRow(new String[] { "drawing", "line", "50;50", "100;50", "endline", "enddrawing" })]
+        [DataRow(new String[] { "drawing", "300x300", "beginline", "50;50", "100;50", "endline", "enddrawing" })]
+        [DataRow(new String[] { "drawing", "300x300", "line", "500;50", "100;50", "endline", "enddrawing" })]
+        [DataRow(new String[] { "drawing", "300x300", "line", "50x50", "100;50", "endline", "enddrawing" })]
+        [DataRow(new String[] { "drawing", "300x300", "line", "50", "100", "endline", "enddrawing" })]
+        [DataRow(new String[] { "drawing", "300x300", "line", "50;50", "line", "endline", "enddrawing" })]
+        [DataRow(new String[] { "drawing", "300x300", "line", "50;50", "100;50", "stopline", "enddrawing" })]
+        [DataRow(new String[] { "drawing", "300x300", "line", "50;50", "100;50", "enddrawing" })]
+        [DataRow(new String[] { "drawing", "300x300", "line", "50;50", "100;50", "endline", "endrawing" })]
+        [DataRow(new String[] { "drawing", "300x300", "line", "50;50", "100;50", "endline" })]
+        public void ParseISADInputExceptionTest(String[] file)
         {
-            Point[] currentLine = drawing[i / 3].GetPoints().ToArray();
-            Assert.AreEqual(3, currentLine.Length);
-            for (int j = 0; j < 3; j++)
+            bool exceptionThrown = false;
+            Form1 program = new Form1();
+            FileImporter uut = new SketchAssistant.FileImporter();
+            //check that left image initially is uninitialized
+            Assert.IsNull(GetLeftImage(program));
+            //initialize left image with a valid isad drawing
+            (int, int, List<Line>) values = uut.ParseISADInputForTesting(new string[] { "drawing", "300x205", "line", "40;40", "140;140", "endline", "enddrawing" });
+            program.CreateCanvasAndSetPictureForTesting(values.Item1, values.Item2, values.Item3);
+            //save left image for later comparison
+            List<Line> oldLeftImage = GetLeftImage(program);
+            try
+            {
+                //try to initialize the left image with an invalid isad drawing
+                (int, int, List<Line>) values1 = uut.ParseISADInputForTesting(file);
+                program.CreateCanvasAndSetPictureForTesting(values1.Item1, values1.Item2, values1.Item3);
+            }
+            catch(FileImporterException)
             {
-                Assert.IsTrue(currentLine[j].X == xCoordinates[i + j] && currentLine[j].Y == yCoordinates[i + j]);
+                //save the occurence of an exception
+                exceptionThrown = true;
             }
+            //check that an exception has been thrown
+            Assert.IsTrue(exceptionThrown);
+            //check that the left image has not been changed by the failed image import
+            Assert.AreEqual(oldLeftImage, GetLeftImage(program));
         }
-    }
 
-    [DataTestMethod]
-    [DataRow(new String[] {})]
-    [DataRow(new String[] { "begindrawing", "300x300", "line", "50;50", "100;50", "endline", "enddrawing" })]
-    [DataRow(new String[] { "drawing", "300;300", "line", "50;50", "100;50", "endline", "enddrawing" })]
-    [DataRow(new String[] { "drawing", "30.5x300", "line", "50;50", "100;50", "endline", "enddrawing" })]
-    [DataRow(new String[] { "drawing", "line", "50;50", "100;50", "endline", "enddrawing" })]
-    [DataRow(new String[] { "drawing", "300x300", "beginline", "50;50", "100;50", "endline", "enddrawing" })]
-    [DataRow(new String[] { "drawing", "300x300", "line", "500;50", "100;50", "endline", "enddrawing" })]
-    [DataRow(new String[] { "drawing", "300x300", "line", "50x50", "100;50", "endline", "enddrawing" })]
-    [DataRow(new String[] { "drawing", "300x300", "line", "50", "100", "endline", "enddrawing" })]
-    [DataRow(new String[] { "drawing", "300x300", "line", "50;50", "line", "endline", "enddrawing" })]
-    [DataRow(new String[] { "drawing", "300x300", "line", "50;50", "100;50", "stopline", "enddrawing" })]
-    [DataRow(new String[] { "drawing", "300x300", "line", "50;50", "100;50", "enddrawing" })]
-    [DataRow(new String[] { "drawing", "300x300", "line", "50;50", "100;50", "endline", "endrawing" })]
-    [DataRow(new String[] { "drawing", "300x300", "line", "50;50", "100;50", "endline" })]
-    public void ParseISADInputExceptionTest(String[] file)
-    {
-        bool exceptionThrown = false;
-        Form1 program = new Form1();
-        FileImporter uut = new SketchAssistant.FileImporter();
-        //check that left image initially is uninitialized
-        Assert.IsNull(GetLeftImage(program));
-        //initialize left image with a valid isad drawing
-        (int, int, List<Line>) values = uut.ParseISADInputForTesting(new string[] { "drawing", "300x205", "line", "40;40", "140;140", "endline", "enddrawing" });
-        program.CreateCanvasAndSetPictureForTesting(values.Item1, values.Item2, values.Item3);
-        //save left image for later comparison
-        List<Line> oldLeftImage = GetLeftImage(program);
-        try
+        /// <summary>
+        /// local helper method retrieving the left image from a Form1 instance
+        /// </summary>
+        /// <returns>the left image of the given Form1 instance</returns>
+        private List<Line> GetLeftImage(Form1 program)
         {
-            //try to initialize the left image with an invalid isad drawing
-            (int, int, List<Line>) values1 = uut.ParseISADInputForTesting(file);
-            program.CreateCanvasAndSetPictureForTesting(values1.Item1, values1.Item2, values1.Item3);
+            //cast is save as long as Form1#GetAllVariables() is conform to its contract
+            return (List<Line>) program.GetAllVariables()[4];
         }
-        catch(FileImporterException)
+        */
+
+        [TestMethod]
+        [DeploymentItem(@"test_input_files")]
+        public void parseSVGInputNoErrorForWhitelistedFilesTest()
         {
-            //save the occurence of an exception
-            exceptionThrown = true;
+            FileImporter uut = new FileImporter();
+
+            string[] files = Directory.GetFiles(TestContext.DeploymentDirectory, " *.svg", SearchOption.AllDirectories);
+            foreach(string s in files) //parse each of the whitelisted files
+            {
+                bool noExceptionThrown = true;
+                try
+                {
+                    uut.ParseSVGInputFile(s, 10000, 10000);
+                }
+                catch (FileImporterException e)
+                {
+                    noExceptionThrown = false;
+                }
+                catch (Exception e)
+                {
+                    noExceptionThrown = false;
+                }
+                Assert.IsTrue(noExceptionThrown);
+            }
+            Assert.IsTrue(files.Length > 0);
         }
-        //check that an exception has been thrown
-        Assert.IsTrue(exceptionThrown);
-        //check that the left image has not been changed by the failed image import
-        Assert.AreEqual(oldLeftImage, GetLeftImage(program));
-    }
 
-    /// <summary>
-    /// local helper method retrieving the left image from a Form1 instance
-    /// </summary>
-    /// <returns>the left image of the given Form1 instance</returns>
-    private List<Line> GetLeftImage(Form1 program)
-    {
-        //cast is save as long as Form1#GetAllVariables() is conform to its contract
-        return (List<Line>) program.GetAllVariables()[4];
+        [TestMethod]
+        [DeploymentItem(@"test_input_files")]
+        public void parseSVGInputNoErrorForBlacklistedFilesTest()
+        {
+            FileImporter uut = new FileImporter();
+
+            string[] files = Directory.GetFiles(TestContext.DeploymentDirectory, " *.svg", SearchOption.AllDirectories);
+            foreach (string s in files) //parse each of the whitelisted files
+            {
+                bool correctExceptionThrown = false;
+                try
+                {
+                    uut.ParseSVGInputFile(s, 10000, 10000);
+                }
+                catch(FileImporterException e)
+                {
+                    correctExceptionThrown = true;
+                }
+                catch(Exception e)
+                {
+
+                }
+                Assert.IsTrue(correctExceptionThrown);
+            }
+            Assert.IsTrue(files.Length > 0);
+        }
     }
-}
-*/
 
 }

+ 11 - 0
SketchAssistant/SketchAssistant/FileImporter.cs

@@ -754,6 +754,10 @@ namespace SketchAssistant
             return element;
         }
 
+        /// <summary>
+        /// normalizes the declaration of the data field of a path declaration by splitting coordinates still connected by a semicolon and command descriptors which are directly attached to the following coordinate into seperate tokens, also repeats omitted command descriptor tokens when the same command is repeated multiple times
+        /// </summary>
+        /// <param name="pathElements">the list of tokens to normalize, by splitting up existing tokens and adding new command descriptor tokens</param>
         private void NormalizePathDeclaration(List<string> pathElements)
         {
             Char lastCommand = 'M';
@@ -950,6 +954,13 @@ namespace SketchAssistant
             }
         }
 
+        /// <summary>
+        /// parses a "closeloop" path element
+        /// </summary>
+        /// <param name="pathElements">a list of all not yet parsed path element tokens and values in correct order, starting with the element to be parsed</param>
+        /// <param name="initialPositionX">absolute x coordinate of the last initial point of this subpath</param>
+        /// <param name="initialPositionY">absolute y coordinate of the last initial point of this subpath</param>
+        /// <returns></returns>
         private (Point, double, double) Parse_Z(List<string> pathElements, double initialPositionX, double initialPositionY)
         {
             pathElements.RemoveAt(0); //remove element descriptor token