Exemplo n.º 1
0
        static void ProcessCommands(string input)
        {
            try
            {
                var allowedCommands = new string[] { "C", "L", "R", "B", "FILE" };
                string[] commands = input.Trim().Split(' ');
                string elementComand = commands.Length > 0 ? commands[0].ToUpper() : " ";

                if (allowedCommands.Contains(elementComand))
                {
                    if (elementComand == "FILE")
                    {
                        ProcessFromFile(commands[1]);
                    }
                    else if (elementComand == "C")
                    {
                        workingCanvas = new Canvas(int.Parse(commands[1]), int.Parse(commands[2]));
                    }
                    else if (workingCanvas != null)
                    {
                        switch (elementComand)
                        {
                            case "L":
                                Line line = new Line(int.Parse(commands[1]), int.Parse(commands[2]), int.Parse(commands[3]), int.Parse(commands[4]));
                                workingCanvas.Elements.Add(line);
                                break;
                            case "R":
                                Rectangle rectangle = new Rectangle(int.Parse(commands[1]), int.Parse(commands[2]), int.Parse(commands[3]), int.Parse(commands[4]));
                                workingCanvas.Elements.Add(rectangle);
                                break;
                            case "B":
                                BucketFill bucket = new BucketFill(int.Parse(commands[1]), int.Parse(commands[2]), commands[3]);
                                workingCanvas.Elements.Add(bucket);
                                break;
                        }
                    }
                    else
                    {
                        WriteErrorLine(string.Concat("Sorry, you need to create a canvas first, enter ? for help."));
                    }

                    if (workingCanvas != null && elementComand != "FILE")
                    {
                        WriteResultLine(workingCanvas.Draw(Environment.NewLine));
                    }
                }
                else
                {
                    WriteErrorLine(string.Concat("Sorry, command: ", elementComand, " is not allowed, enter ? for help."));
                }
            }
            catch (Exception ex)
            {
                WriteErrorLine(string.Concat("Sorry, something is wrong with the command input, enter ? for help."));
            }
        }
Exemplo n.º 2
0
        public void ShouldBucketFillAllCanvas()
        {
            Canvas canvas = new Canvas(20, 4);

            canvas.Elements.Add(new BucketFill(10, 3, "*"));

            var drawing = canvas.Draw();

            var expected = "----------------------" +
                           "|********************|" +
                           "|********************|" +
                           "|********************|" +
                           "|********************|" +
                           "----------------------";

            Assert.AreEqual(expected, drawing);
        }
Exemplo n.º 3
0
        public void ShouldBucketFillButInsideRectangleAreaOnly()
        {
            Canvas canvas = new Canvas(20, 4);

            //rectangle
            canvas.Elements.Add(new Rectangle(5, 1, 15, 4));

            //bucket fill
            canvas.Elements.Add(new BucketFill(6, 3, "o"));

            var drawing = canvas.Draw();

            //expected result
            var expected = "----------------------" +
                           "|    xxxxxxxxxxx     |" +
                           "|    xooooooooox     |" +
                           "|    xooooooooox     |" +
                           "|    xxxxxxxxxxx     |" +
                           "----------------------";

            Assert.AreEqual(expected, drawing);
        }
Exemplo n.º 4
0
        public void ShouldBeTheSameAsSample()
        {
            Canvas canvas = new Canvas(20, 4);

            //adds element to canvas
            canvas.Elements.Add(lineVertical);
            canvas.Elements.Add(lineHorizontal);
            canvas.Elements.Add(rectangle);

            //bucket fill
            canvas.Elements.Add(new BucketFill(10, 3, "o"));

            var drawing = canvas.Draw();

            //expected result
            var expected = "----------------------" +
                           "|oooooooooooooooxxxxx|" +
                           "|xxxxxxooooooooox   x|" +
                           "|     xoooooooooxxxxx|" +
                           "|     xoooooooooooooo|" +
                           "----------------------";

            Assert.AreEqual(expected, drawing);
        }
Exemplo n.º 5
0
        public void ShouldBucketFillButRectangleArea()
        {
            Canvas canvas = new Canvas(20, 4);

            //rectangle
            canvas.Elements.Add(rectangle);

            //bucket fill
            canvas.Elements.Add(new BucketFill(10, 3, "o"));

            var drawing = canvas.Draw();

            var expected = "----------------------" +
                            "|oooooooooooooooxxxxx|" +
                            "|ooooooooooooooox   x|" +
                            "|oooooooooooooooxxxxx|" +
                            "|oooooooooooooooooooo|" +
                            "----------------------";

            Assert.AreEqual(expected, drawing);
        }
Exemplo n.º 6
0
        public void ShouldNotFillBetweenLines()
        {
            Canvas canvas = new Canvas(20, 4);

            //adds element to canvas
            canvas.Elements.Add(lineVertical);
            canvas.Elements.Add(lineHorizontal);

            //bucket fill
            canvas.Elements.Add(new BucketFill(1, 1, "o"));

            var drawing = canvas.Draw();
            //expected result
            var expected = "----------------------" +
                           "|oooooooooooooooooooo|" +
                           "|xxxxxxoooooooooooooo|" +
                           "|     xoooooooooooooo|" +
                           "|     xoooooooooooooo|" +
                           "----------------------";

            Assert.AreEqual(expected, drawing); ;
        }
Exemplo n.º 7
0
        public void ShouldDrawARectangle()
        {
            Canvas canvas = new Canvas(20, 4);

            //adds the rectangle to the canvas
            canvas.Elements.Add(rectangle);

            //draws the canvas
            var drawing = canvas.Draw();

            //expected result
            var expected = "----------------------" +
                           "|               xxxxx|" +
                           "|               x   x|" +
                           "|               xxxxx|" +
                           "|                    |" +
                           "----------------------";

            Assert.AreEqual(expected, drawing);
        }
Exemplo n.º 8
0
        public void ShouldDrawAnEmptyCanvas()
        {
            Canvas canvas = new Canvas(20, 4);

            var drawing = canvas.Draw();
            var expected = "----------------------" +
                           "|                    |" +
                           "|                    |" +
                           "|                    |" +
                           "|                    |" +
                           "----------------------";

            Assert.AreEqual(expected, drawing);
        }
Exemplo n.º 9
0
        public void ShouldDrawALine()
        {
            Canvas canvas = new Canvas(20, 4);

            //adds a horizontal Line to canvas
            canvas.Elements.Add(lineHorizontal);

            //draws the canvas
            var drawing = canvas.Draw();

            //expected result
            var expected = "----------------------" +
                           "|                    |" +
                           "|xxxxxx              |" +
                           "|                    |" +
                           "|                    |" +
                           "----------------------";

            Assert.AreEqual(expected, drawing);

            //adds a vertical line
            canvas.Elements.Add(lineVertical);

            //draws the canvas
            drawing = canvas.Draw();

            //expected result
            expected = "----------------------" +
                       "|                    |" +
                       "|xxxxxx              |" +
                       "|     x              |" +
                       "|     x              |" +
                       "----------------------";

            Assert.AreEqual(expected, drawing);
        }
Exemplo n.º 10
0
        public void ShouldCreateACanvasWithParams()
        {
            int x = 10, y = 100;
            Canvas canvas = new Canvas(x, y);

            Assert.IsNotNull(canvas.Matrix);
        }