Пример #1
0
        static void Main(string[] args)
        {
            ICanvas  canvas = null;
            ICommand command;

            while (true)
            {
                Console.Write("enter command : ");
                var userInput = Console.ReadLine();
                var input     = InputParser.ParseInput(userInput);
                switch (input.Command.ToUpper())
                {
                case "C":
                    command = new CreateCanvas();
                    ExecuteCommandAndDrawCanvas();
                    break;

                case "L":
                    command = new CreateLine(canvas);
                    ExecuteCommandAndDrawCanvas();
                    break;

                case "R":
                    command = new CreateRectangle(canvas);
                    ExecuteCommandAndDrawCanvas();
                    break;

                case "B":
                    command = new FillBucket(canvas);
                    ExecuteCommandAndDrawCanvas();
                    break;

                case "Q":
                    command = new ExitCommand();
                    command.ExecuteCommand(input.Args);
                    break;

                default:
                    break;

                    void ExecuteCommandAndDrawCanvas()
                    {
                        try
                        {
                            canvas = command.ExecuteCommand(input.Args);
                            Console.WriteLine(canvas.ToString());
                            //canvas.DrawCanvas();
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine($"Error: {e.Message}");
                        }
                    }
                }
            }
        }
Пример #2
0
        public void ExecuteCommand_Canvas_Not_Exists()
        {
            string[]   args       = new string[] { "1", "2", "o" };
            FillBucket fillBucket = new FillBucket(null);
            var        exception  = Record.Exception(() => fillBucket.ExecuteCommand(args));

            Assert.NotNull(exception);
            Assert.IsType <ArgumentException>(exception);
            Assert.Equal($"{Constants.Command_Canvas_Not_Exists}", exception.Message);
        }
Пример #3
0
        public void ExecuteCommand_Invalid_Argument(string CommandName, string[] args)
        {
            FillBucket fillBucket = new FillBucket(_canvas.Object);
            var        exception  = Record.Exception(() => fillBucket.ExecuteCommand(args));

            Assert.NotNull(exception);
            Assert.IsType <ArgumentException>(exception);
            Assert.Equal($"{Constants.Command_Expect_Two_Positive_One_Alphanumeric_Arguments}", exception.Message);
            Assert.Equal("B", CommandName);
        }
Пример #4
0
        public void ExecuteCommand_Invalid_No_Of_Arguments_Exception(string CommandName, string[] args)
        {
            FillBucket fillBucket = new FillBucket(_canvas.Object);
            var        exception  = Record.Exception(() => fillBucket.ExecuteCommand(args));

            Assert.NotNull(exception);
            Assert.IsType <ArgumentException>(exception);
            Assert.Equal($"{Constants.Command_Expect_Three_Arguments} {args.Length}", exception.Message);
            Assert.Equal("B", CommandName);
        }
Пример #5
0
        public void ExecuteCommand_Fill_Bucket_Success()
        {
            CreateCanvas createCanvas = new CreateCanvas();

            string[] args1  = new string[] { "20", "4" };
            var      canvas = createCanvas.ExecuteCommand(args1);

            string[]   args       = new string[] { "10", "2", "o" };
            FillBucket fillBucket = new FillBucket(canvas);
            var        result     = fillBucket.ExecuteCommand(args);

            Assert.NotNull(result);
            for (int i = 1; i < 21; i++)
            {
                for (int j = 1; j < 5; j++)
                {
                    Assert.Equal('o', canvas.Cells[i, j]);
                }
            }
        }
Пример #6
0
        public void CanvasApp_Test()
        {
            List <string> commands = new List <string>
            {
                new string("C 20 4"),
                new string("L 1 2 6 2"),
                new string("L 6 3 6 4"),
                new string("R 14 1 18 3"),
                new string("B 10 3 o"),
            };

            ICanvas  canvas = null;
            ICommand command;

            foreach (string com in commands)
            {
                var input = InputParser.ParseInput(com);
                switch (input.Command.ToUpper())
                {
                case "C":
                    command = new CreateCanvas();
                    ExecuteCommandAndDrawCanvas();
                    break;

                case "L":
                    command = new CreateLine(canvas);
                    ExecuteCommandAndDrawCanvas();
                    break;

                case "R":
                    command = new CreateRectangle(canvas);
                    ExecuteCommandAndDrawCanvas();
                    break;

                case "B":
                    command = new FillBucket(canvas);
                    ExecuteCommandAndDrawCanvas();
                    break;

                case "Q":
                    command = new ExitCommand();
                    command.ExecuteCommand(input.Args);
                    break;

                default:
                    break;
                    void ExecuteCommandAndDrawCanvas()
                    {
                        try
                        {
                            canvas = command.ExecuteCommand(input.Args);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine($"Error: {e.Message}");
                        }
                    }
                }
            }

            string[] expected = new string[6]
            {
                "----------------------",
                "|oooooooooooooxxxxxoo|",
                "|xxxxxxooooooox   xoo|",
                "|     xoooooooxxxxxoo|",
                "|     xoooooooooooooo|",
                "----------------------"
            };

            for (int i = 0; i < 6; i++)
            {
                StringBuilder sb = new StringBuilder();
                for (int j = 0; j < 22; j++)
                {
                    sb.Append(canvas.Cells[j, i]);
                }
                sb.Replace('\0', ' ');
                Assert.Equal(expected[i], sb.ToString());
            }
        }
Пример #7
0
 void Start()
 {
     bucket        = GetComponent <FillBucket>();
     usescript     = GetComponent <UseDelegator>();
     usescript.use = CleanDirty;
 }