示例#1
0
 public void RunAndValidate(ToyRobot robot)
 {
     foreach (string c in Commands)
     {
         try
         {
             robot.InterpretCommand(c);
         }
         // robot exceptions are count in the console app, therefore catch them here
         catch (RobotException) { }
     }
     Assert.AreEqual(robot.Report(), this.ExpectedResult);
 }
示例#2
0
        public void InterpretCommandTest_Output()
        {
            // init
            ToyTable tb    = ToyTableTests.ToyTableCreate_successtest(300, 500);
            ToyRobot robot = new ToyRobot(tb);

            using (MemoryStream ms = new MemoryStream())
                using (StreamWriter sw = new StreamWriter(ms))
                {
                    // place report check
                    robot.InterpretCommand("PLACE 99,230,EAST", sw);
                    robot.InterpretCommand("MOVE", sw);
                    robot.InterpretCommand("MOVE", sw);
                    robot.InterpretCommand("LEFT", sw);
                    robot.InterpretCommand("LEFT", sw);
                    robot.InterpretCommand("RIGHT", sw);
                    TestLocation(robot, new CoordinateXY(101, 230), Direction.NORTH);
                    robot.InterpretCommand("REPORT", sw);

                    ms.Seek(0, SeekOrigin.Begin);
                    StreamReader sr = new StreamReader(ms);
                    Assert.AreEqual("101,230,NORTH", sr.ReadToEnd().Trim());
                }

            // when report is not called nothing is written
            using (MemoryStream ms = new MemoryStream())
                using (StreamWriter sw = new StreamWriter(ms))
                {
                    // place report check
                    robot.InterpretCommand("PLACE 99,230,EAST", sw);
                    robot.InterpretCommand("MOVE", sw);
                    robot.InterpretCommand("MOVE", sw);
                    robot.InterpretCommand("LEFT", sw);
                    robot.InterpretCommand("LEFT", sw);
                    robot.InterpretCommand("RIGHT", sw);
                    TestLocation(robot, new CoordinateXY(101, 230), Direction.NORTH);

                    ms.Seek(0, SeekOrigin.Begin);
                    StreamReader sr = new StreamReader(ms);
                    Assert.AreEqual("", sr.ReadToEnd());
                }
        }
示例#3
0
        public void InterpretCommandTest_Success()
        {
            // init
            ToyTable tb    = ToyTableTests.ToyTableCreate_successtest(300, 500);
            ToyRobot robot = new ToyRobot(tb);

            // place report check
            robot.InterpretCommand("PLACE 99,230,EAST");
            TestLocation(robot, new CoordinateXY(99, 230), Direction.EAST);
            robot.InterpretCommand("REPORT");
            TestLocation(robot, new CoordinateXY(99, 230), Direction.EAST);
            robot.InterpretCommand("PLACE 99,230,WEST");
            TestLocation(robot, new CoordinateXY(99, 230), Direction.WEST);
            robot.InterpretCommand("REPORT");
            TestLocation(robot, new CoordinateXY(99, 230), Direction.WEST);
            robot.InterpretCommand("PLACE 230,99,NORTH");
            TestLocation(robot, new CoordinateXY(230, 99), Direction.NORTH);
            robot.InterpretCommand("REPORT");
            TestLocation(robot, new CoordinateXY(230, 99), Direction.NORTH);
            robot.InterpretCommand("PLACE 230,99,SOUTH");
            TestLocation(robot, new CoordinateXY(230, 99), Direction.SOUTH);
            robot.InterpretCommand("REPORT");
            TestLocation(robot, new CoordinateXY(230, 99), Direction.SOUTH);

            //move tests
            robot.InterpretCommand("MOVE");
            TestLocation(robot, new CoordinateXY(230, 98), Direction.SOUTH);
            robot.InterpretCommand("MOVE");
            TestLocation(robot, new CoordinateXY(230, 97), Direction.SOUTH);
            robot.InterpretCommand("LEFT");
            TestLocation(robot, new CoordinateXY(230, 97), Direction.EAST);
            robot.InterpretCommand("RIGHT");
            TestLocation(robot, new CoordinateXY(230, 97), Direction.SOUTH);
        }
示例#4
0
        public void InterpretCommandTest_Throw()
        {
            ToyTable tb    = ToyTableTests.ToyTableCreate_successtest(300, 500);
            ToyRobot robot = new ToyRobot(tb);

            // test in valid commands
            AssertInvalidRobotCommandException(() => { robot.InterpretCommand(""); });
            AssertInvalidRobotCommandException(() => { robot.InterpretCommand("blahblah asd ad 123e 123 312 ,1,1,,1"); });
            AssertInvalidRobotCommandException(() => { robot.InterpretCommand("PLACE123"); });
            AssertInvalidRobotCommandException(() => { robot.InterpretCommand("PLACE 123"); });
            AssertInvalidRobotCommandException(() => { robot.InterpretCommand("PLACE 123,2,NROTH"); });
            AssertInvalidRobotCommandException(() => { robot.InterpretCommand("PLACE X, ,NORTH"); });
            AssertInvalidRobotCommandException(() => { robot.InterpretCommand("PLACE ,,"); });
            AssertInvalidRobotCommandException(() => { robot.InterpretCommand("PLACE 3xs2,xxa,SOUTH"); });
            //negative values
            AssertOutofBoundException(() => { robot.InterpretCommand("PLACE -123,-23,NORTH"); });
            //testing limits
            AssertOutofBoundException(() => { robot.InterpretCommand($"PLACE {int.MaxValue},{int.MinValue},NORTH"); });
            AssertOutofBoundException(() => { robot.InterpretCommand($"PLACE {int.MinValue},{int.MinValue},NORTH"); });
            AssertOutofBoundException(() => { robot.InterpretCommand($"PLACE {int.MinValue},{int.MaxValue},NORTH"); });
            AssertOutofBoundException(() => { robot.InterpretCommand($"PLACE {int.MaxValue},{int.MaxValue},NORTH"); });
            //make sure interpreter throws for overflow
            AssertInvalidRobotCommandException(() => { robot.InterpretCommand($"PLACE {int.MaxValue}3213,{int.MinValue}3213,NORTH"); });
            AssertInvalidRobotCommandException(() => { robot.InterpretCommand("PLACE     !@#!@#()_@!#(<<<mmm,,,,,,,"); });
            //out of bound
            AssertOutofBoundException(() => { robot.InterpretCommand("PLACE 4123,23,NORTH"); });
            AssertInvalidRobotCommandException(() => { robot.InterpretCommand("   PLACE      "); });
            AssertInvalidRobotCommandException(() => { robot.InterpretCommand(" MOVE bassdasd "); });
            AssertInvalidRobotCommandException(() => { robot.InterpretCommand("REPORT bassdasd "); });
            AssertInvalidRobotCommandException(() => { robot.InterpretCommand("LEFTbassdasd "); });
            AssertInvalidRobotCommandException(() => { robot.InterpretCommand(" LEFTba  s  sdasd "); });
            AssertInvalidRobotCommandException(() => { robot.InterpretCommand("RIGHT  sdsad "); });
        }