the first steps toward a working robot

A previous series of posts illustrated how I got to grips with acceptance testing for a robocode robot. I'm now ready to start  coding the robot itself so I want to refer back to the bottom of a previous post: starting the robocode robot with an anti-corruption layer :

"Note that the service locator purposefully returns null for the time being and causes a null reference exception when the Robot is run. This is so I can observe what happens when a robot does not function correctly and I'll be covering this in one of the next few posts."

By leaving my robot in an exception state, I observed that the robot is disabled when the exception is thrown:

disabledRobot

I therefore want to write an acceptance test that will always check that my robot doesn't throw an exception at the start of the battle. So here's how I want to express that in an acceptance test:

    public class in_a_one_round_battle_that_the_slayer_robot_takes_part : acceptance_test
    {
        Establish context = () => battleName = "in_a_one_round_battle";

        It should_not_disable_the_robot_at_the_start_of_battle = () =>
            robotStatistics.ReachedTurn50.ShouldBeTrue();
    }

I've introduced a new member that does not yet exist - the robotStatistics field and a property that ensures we have reached a reasonable number of turns within a battle.

Here's my idea: if I can write out an xml file during the battle, my acceptance tests can determine what occurred - so I create an xml file by hand and place it in my acceptance test battle results folder:

xmlStatistics
The robot will write out the information I need to establish it reached a certain number of turns and it also writes the date and time these statistics were written so I can establish in the acceptance test that it is a fresh file and I'm not asserting against a previous run.

So the first thing to do is add the reading of the statistics into my base acceptance test - the new lines are highlighted below:

    public class acceptance_test
    {
        Establish context = () => battleRunner = new BattleRunner(new ProcessRunner(), 
                  new BattleResultParser(new BattleResultsFileReader()));

        Because of = () =>
                         {
                             battleName.ShouldNotEqual(string.Empty);
                             battle = new Battle(battleName);
                             returnedResults = battleRunner.Run(battle);
                             robotStatistics = Statistics.RetrieveStatistics();   <---
                         };

        protected static string battleName = string.Empty;
        protected static IEnumerable<BattleResult> returnedResults;
        protected static RobotStatistics robotStatistics;                         <---
        static Battle battle;
        static BattleRunner battleRunner;   
    }

I also create a quick "Statistics" class solely for reading within the acceptance test:

    internal static class Statistics
    {
        public static RobotStatistics RetrieveStatistics()
        {
            var statistics = XElement.Load(@"C:\dev\Projects\RoboCodeWars\TeamSlayer\
                              Specifications\AcceptanceSpecs\battles\results\Statistics.xml");

            string reachedTurn50Value = statistics.Element("reachedTurn50").Value;

            var startDateAndTime = Convert.ToDateTime(
                               statistics.Element("statisticsWritten").Value);

            if (startDateAndTime < DateTime.Now.AddMinutes(-1))
                throw new Exception(string.Format("Statistics file looks old - start 
                                time in file was {0}", startDateAndTime));

            return new RobotStatistics {ReachedTurn50 = Convert.ToBoolean(reachedTurn50Value)};
        }
    }

    internal class RobotStatistics
    {
        public bool ReachedTurn50 { get; set; }
    }

I now re-save my xml file, run my acceptance test and see that it fails because the statistics say the robot did not reach turn 50:

failedToTeachTurn50

If I then run it a few minutes later, I get the exception stating that the file looks old so my acceptance test is up and running.
I'm now ready to start on my robot implementation to write these statistics out and I'll cover this on my next post.



0 comments: