Browse Source

fixed many issues discovered during the code review

Vincenz Mechler 5 years ago
parent
commit
ff1ee71b77

+ 62 - 0
SketchAssistant/SketchAssistant.Tests/UnitTest1.cs

@@ -273,4 +273,66 @@ namespace Tests
             Assert.AreEqual(currLabel, message);
         }
     }
+
+    /*
+    [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(program);
+
+            List<String> file = new List<string>();
+            file.Add("drawing");
+            file.Add("300x200");
+            for(int i = 0; i < xCoordinates.Length - 2; i++)
+            {
+                file.Add("line");
+                file.Add(xCoordinates[i - 2] + ";" + yCoordinates[i - 2]);
+                file.Add(xCoordinates[i - 1] + ";" + yCoordinates[i - 1]);
+                file.Add(xCoordinates[i] + ";" + yCoordinates[i]);
+                file.Add("endline");
+            }
+            file.Add("enddrawing");
+
+            uut.ParseISADInput(file.ToArray());
+
+            Line[] drawing = program.templatePicture.ToArray();
+
+            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]
+
+        public void ParseISADInputSuccessfulTest(String[] file)
+        {
+            Form1 program = new Form1();
+            FileImporter uut = new SketchAssistant.FileImporter(program);
+
+            Assert.IsNull(program.templatePicture);
+
+            uut.ParseISADInput(new string[] {"drawing", "300x205", "line", "40;40", "140;140", "endline", );
+
+            List<Line> oldTemplatePicture = program.templatePicture;
+
+            uut.ParseISADInput(file);
+
+            Assert.AreEqual(oldTemplatePicture, program.templatePicture);
+        }
+    }
+    */
 }

+ 26 - 46
SketchAssistant/SketchAssistant/FileImporter.cs

@@ -8,7 +8,7 @@ using System.Threading.Tasks;
 
 namespace SketchAssistant
 {
-    class FileImporter
+    public class FileImporter
     {
 
         /// <summary>
@@ -22,74 +22,59 @@ namespace SketchAssistant
         }
 
         /// <summary>
-        /// parses a drawing consisting of line objects, given in the proprietary .isad format
+        /// parses a drawing consisting of line objects, given in the application specific .isad format
         /// </summary>
-        /// <param name="allLines">the input file as an array of all lines in the .isad file</param>
-        public void ParseISADInput(String[] allLines)
+        /// <param name="fileName">the path of the input file</param>
+        /// <returns>the width and height of the left canvas and the parsed picture as a list of lines</returns>
+        public (int, int, List<Line>) ParseISADInput(String fileName)
         {
 
+            String[] allLines = System.IO.File.ReadAllLines(fileName);
 
             if (allLines.Length == 0)
             {
-                program.ShowInfoMessage("Could not import file:\n file is empty");
-                return;
+                throw new FileImporterException("file is empty", "", -1);
             }
             if (!"drawing".Equals(allLines[0]))
             {
-                program.ShowInfoMessage("Could not import file:\n file is not an interactive sketch assistant drawing\n (Hint: .isad files ave to start with the 'drawing' token)");
-                return;
+                throw new FileImporterException("file is not an interactive sketch assistant drawing", ".isad files have to start with the 'drawing' token", 1);
             }
             if (!"enddrawing".Equals(allLines[allLines.Length - 1]))
             {
-                program.ShowInfoMessage("Could not import file:\n unterminated drawing definition\n (Hint: .isad files ave to end with the 'enddrawing' token)");
-                return;
-            }
-
-            if (!parseISADHeader(allLines))
-            {
-                return;
-            }
-
-            if (!parseISADBody(allLines))
-            {
-                return;
+                throw new FileImporterException("unterminated drawing definition", ".isad files have to end with the 'enddrawing' token", allLines.Length);
             }
 
+            (int, int) dimensions = parseISADHeader(allLines);
+            List<Line> picture = parseISADBody(allLines, dimensions.Item1, dimensions.Item2);
 
-
+            return (dimensions.Item1, dimensions.Item2, picture);
         }
 
         /// <summary>
-        /// parses the first two lines of an input file .isad format
+        /// parses the first two lines of an input file in .isad format
         /// </summary>
         /// <param name="allLines">the input file as an array of lines</param>
-        /// <returns></returns>
-        private bool parseISADHeader(String[] allLines)
+        /// <returns>the width and height of the left canvas</returns>
+        private (int, int) parseISADHeader(String[] allLines)
         {
-
             int width;
             int height;
-
             if (!(allLines.Length > 1) || !Regex.Match(allLines[1], @"(\d?\d*x?\d?\d*?)?", RegexOptions.None).Success)
             {
-                program.ShowInfoMessage("Could not import file:\n invalid or missing canvas size definition\n (Line: 2)");
-                return false;
+                throw new FileImporterException("invalid or missing canvas size definition", "format: [width]x[heigth]", 2);
             }
             String[] size = allLines[1].Split('x');
             width = Convert.ToInt32(size[0]);
             height = Convert.ToInt32(size[1]);
-
-            program.DrawEmptyCanvasLeft(width, height);
-
-            return true;
+            return (width, height);
         }
 
         /// <summary>
-        /// parses all line entries of an input file .isad format
+        /// parses all line entries of an input file in .isad format
         /// </summary>
         /// <param name="allLines">the input file as an array of lines</param>
-        /// <returns></returns>
-        private bool parseISADBody(String[] allLines)
+        /// <returns>the parsed picture as a list of lines</returns>
+        private List<Line> parseISADBody(String[] allLines, int width, int height)
         {
 
             String lineStartString = "line";
@@ -107,23 +92,20 @@ namespace SketchAssistant
                 {
                     if (i == allLines.Length)
                     {
-                        program.ShowInfoMessage("Could not import file:\n unterminated line definition\n (Line: " + (i + 1) + ")");
-                        return false;
+                        throw new FileImporterException("unterminated line definition", null, (i + 1));
                     }
                     //parse single point definition
                     if (!Regex.Match(allLines[i], @"(\d?\d*;?\d?\d*?)?", RegexOptions.None).Success)
                     {
-                        program.ShowInfoMessage("Could not import file:\n invalid Point definition: wrong format\n (Line: " + (i + 1) + ")");
-                        return false;
+                        throw new FileImporterException("invalid Point definition: wrong format", "format: [xCoordinate];[yCoordinate]", (i + 1) );
                     }
                     String[] coordinates = allLines[i].Split(';');
                     //no errors possible, convertability to string already checked above
                     int xCoordinate = Convert.ToInt32(coordinates[0]);
                     int yCoordinate = Convert.ToInt32(coordinates[1]);
-                    if (xCoordinate < 0 || yCoordinate < 0 || xCoordinate > program.leftImage.Width - 1 || yCoordinate > program.leftImage.Height - 1)
+                    if (xCoordinate < 0 || yCoordinate < 0 || xCoordinate > width - 1 || yCoordinate > height - 1)
                     {
-                        program.ShowInfoMessage("Could not import file:\n invalid Point definition: point out of bounds\n (Line: " + (i + 1) + ")");
-                        return false;
+                        throw new FileImporterException("invalid Point definition: point out of bounds", null, (i + 1) );
                     }
                     newLine.Add(new Point(xCoordinate, yCoordinate));
                     i++;
@@ -134,10 +116,8 @@ namespace SketchAssistant
                 drawing.Add(new Line(newLine));
             }
 
-            //save parsed drawing to instance variable and draw it
-            program.BindTemplatePicture(drawing);
-
-            return true;
+            //return parsed picture
+            return drawing;
         }
 
     }

+ 22 - 0
SketchAssistant/SketchAssistant/FileImporterException.cs

@@ -0,0 +1,22 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace SketchAssistant
+{
+    class FileImporterException : Exception
+    {
+        String showMessage;
+        public FileImporterException(String message, String hint, int lineNumber) : base (message)
+        {
+            showMessage = "Could not import file:\n\n" + message + (hint == null ? "" : "\n(Hint: " + hint + ")") + (lineNumber == -1 ? "" : "\n\n-line: " + lineNumber );
+        }
+
+        public override string ToString()
+        {
+            return showMessage;
+        }
+    }
+}

+ 16 - 14
SketchAssistant/SketchAssistant/Form1.cs

@@ -49,7 +49,7 @@ namespace SketchAssistant
         /// <summary>
         /// the graphic shown in the left window, represented as a list of polylines
         /// </summary>
-        List<Line> templatePicture;
+        public List<Line> templatePicture { get; private set; }
         //Image on the right
         Image rightImage = null;
         //Current Line being Drawn
@@ -112,18 +112,21 @@ namespace SketchAssistant
         /// </summary>
         private void examplePictureToolStripMenuItem_Click(object sender, EventArgs e)
         {
-
-            
             openFileDialogLeft.Filter = "Interactive Sketch-Assistant Drawing|*.isad";
             if (openFileDialogLeft.ShowDialog() == DialogResult.OK)
             {
                 toolStripLoadStatus.Text = openFileDialogLeft.SafeFileName;
-
-                string[] allLines = System.IO.File.ReadAllLines(openFileDialogLeft.FileName);
-
-                fileImporter.ParseISADInput(allLines);
-
-                this.Refresh();
+                try
+                {
+                    (int, int, List<Line>) values = fileImporter.ParseISADInput(openFileDialogLeft.FileName);
+                    DrawEmptyCanvasLeft(values.Item1, values.Item2);
+                    BindAndDrawLeftImage(values.Item3);
+                    this.Refresh();
+                }
+                catch(FileImporterException ex)
+                {
+                    ShowInfoMessage(ex.ToString());
+                }
             }
         }
 
@@ -348,7 +351,7 @@ namespace SketchAssistant
         /// </summary>
         /// <param name="width"> width of the new canvas in pixels </param>
         /// <param name="height"> height of the new canvas in pixels </param>
-        public void DrawEmptyCanvasLeft(int width, int height)
+        private void DrawEmptyCanvasLeft(int width, int height)
         {
             if (width == 0)
             {
@@ -546,21 +549,20 @@ namespace SketchAssistant
         /// </summary>
         /// <param name="newTemplatePicture"> the new template picture, represented as a list of polylines </param>
         /// <returns></returns>
-        public Boolean BindTemplatePicture(List<Line> newTemplatePicture)
+        private void BindAndDrawLeftImage(List<Line> newTemplatePicture)
         {
             templatePicture = newTemplatePicture;
             foreach(Line l in templatePicture)
             {
                 l.DrawLine(Graphics.FromImage(leftImage));
             }
-            return true;
         }
 
         /// <summary>
         /// shows the given info message in a popup and asks the user to aknowledge it
         /// </summary>
-        /// <param name="message">teh message to show</param>
-        public void ShowInfoMessage(String message)
+        /// <param name="message">the message to show</param>
+        private void ShowInfoMessage(String message)
         {
             MessageBox.Show(message);
         }

+ 1 - 0
SketchAssistant/SketchAssistant/SketchAssistant.csproj

@@ -70,6 +70,7 @@
   </ItemGroup>
   <ItemGroup>
     <Compile Include="FileImporter.cs" />
+    <Compile Include="FileImporterException.cs" />
     <Compile Include="SketchAction.cs" />
     <Compile Include="ActionHistory.cs" />
     <Compile Include="Line.cs" />