Commits

Christian Specht committed 825190c Draft

add enums to use as commands in LauncherCommand

Comments (0)

Files changed (9)

src/MissileSharp.Tests/CommandCenterTests.cs

         [Test]
         public void RunCommand_Up_SendsUpCommand()
         {
-            cmd.RunCommand(new LauncherCommand("up", 0));
+            cmd.RunCommand(new LauncherCommand(Command.Up, 0));
             Assert.AreEqual(launcher.Up, device.ReceivedCommands[0]);
         }
 
         [Test]
         public void RunCommand_Down_SendsDownCommand()
         {
-            cmd.RunCommand(new LauncherCommand("down", 0));
+            cmd.RunCommand(new LauncherCommand(Command.Down, 0));
             Assert.AreEqual(launcher.Down, device.ReceivedCommands[0]);
         }
 
         [Test]
         public void RunCommand_Left_SendsLeftCommand()
         {
-            cmd.RunCommand(new LauncherCommand("left", 0));
+            cmd.RunCommand(new LauncherCommand(Command.Left, 0));
             Assert.AreEqual(launcher.Left, device.ReceivedCommands[0]);
         }
 
         [Test]
         public void RunCommand_Right_SendsRightCommand()
         {
-            cmd.RunCommand(new LauncherCommand("right", 0));
+            cmd.RunCommand(new LauncherCommand(Command.Right, 0));
             Assert.AreEqual(launcher.Right, device.ReceivedCommands[0]);
         }
 
         [Test]
-        public void RunCommand_InvalidValue_SendsNoCommand()
-        {
-            cmd.RunCommand(new LauncherCommand("unknown", 0));
-            Assert.AreEqual(0, device.ReceivedCommands.Count);
-        }
-
-        [Test]
         public void RunCommand_Fire_SendsFireCommand()
         {
-            cmd.RunCommand(new LauncherCommand("fire", 0));
+            cmd.RunCommand(new LauncherCommand(Command.Fire, 0));
             Assert.AreEqual(launcher.Fire, device.ReceivedCommands[0]);
         }
 

src/MissileSharp.Tests/CommandSetListTests.cs

         [Test]
         public void Add_CommandSetNameIsEmpty_ThrowsException()
         {
-            Assert.Throws<InvalidOperationException>(() => list.Add(string.Empty, "cmd", 1));
+            Assert.Throws<InvalidOperationException>(() => list.Add(string.Empty, Command.Up, 1));
         }
 
         [Test]
         public void Add_CommandSetNameIsNull_ThrowsException()
         {
-            Assert.Throws<InvalidOperationException>(() => list.Add(null, "cmd", 1));
+            Assert.Throws<InvalidOperationException>(() => list.Add(null, Command.Up, 1));
         }
 
         [Test]
         [Test]
         public void Add_CommandIsValid_ListContainsOneItem()
         {
-            list.Add("name", "cmd", 1);
+            list.Add("name", Command.Up, 1);
             Assert.AreEqual(1, list.GetCommandSet("name").Count);
         }
         
         [Test]
         public void Add_CommandIsValid_SavedCommandIsCorrect()
         {
-            list.Add("name", "cmd", 1);
-            Assert.AreEqual("cmd", list.GetCommandSet("name")[0].Command);
+            list.Add("name", Command.Up, 1);
+            Assert.AreEqual(Command.Up, list.GetCommandSet("name")[0].Command);
         }
         
         [Test]
         public void Add_CommandIsValid_SavedValueIsCorrect()
         {
-            list.Add("name", "cmd", 1);
+            list.Add("name", Command.Up, 1);
             Assert.AreEqual(1, list.GetCommandSet("name")[0].Value);
         }
         
         [Test]
         public void Add_TwoValidCommands_ListContainsTwoItems()
         {
-            list.Add("name", "cmd1", 1);
-            list.Add("name", "cmd2", 1);
+            list.Add("name", Command.Up, 1);
+            list.Add("name", Command.Up, 1);
             Assert.AreEqual(2, list.GetCommandSet("name").Count);
         }
 
         [Test]
         public void Add_CommandSetWithUpperCase_IsSavedInLowerCase()
         {
-            list.Add("NAME", "cmd", 1);
+            list.Add("NAME", Command.Up, 1);
             Assert.That(list.ContainsCommandSet("name"));
         }
 
         [Test]
         public void Add_SameCommandSetInUpperAndLowerCase_IsSavedInOneCommandSet()
         {
-            list.Add("NAME", "cmd", 1);
-            list.Add("name", "cmd", 1);
+            list.Add("NAME", Command.Up, 1);
+            list.Add("name", Command.Up, 1);
             Assert.AreEqual(2, list.CountCommands("name"));
         }
 
         [Test]
         public void CountSets_ListWithOneItem_ReturnsOne()
         {
-            list.Add("name", "cmd", 1);
+            list.Add("name", Command.Up, 1);
             Assert.AreEqual(1, list.CountSets());
         }
 
         [Test]
         public void CountCommands_ExistingCommandSet_ReturnsCorrectNumberOfItems()
         {
-            list.Add("name","cmd",1);
+            list.Add("name", Command.Up, 1);
             Assert.AreEqual(1, list.CountCommands("name"));
         }
 
         [Test]
         public void CountCommands_CommandSetWithUpperCase_ReturnsCorrectNumberOfItems()
         {
-            list.Add("name", "cmd", 1);
+            list.Add("name", Command.Up, 1);
             Assert.AreEqual(1, list.CountCommands("NAME"));
         }
 
         [Test]
         public void GetCommandSet_ExistingCommandSet_ReturnsListWithCorrectNumberOfItems()
         {
-            list.Add("name", "cmd", 1);
+            list.Add("name", Command.Up, 1);
             Assert.AreEqual(1, list.GetCommandSet("name").Count);
         }
 
         [Test]
         public void GetCommandSet_ExistingCommandSet_ReturnsValidCommand()
         {
-            list.Add("name", "cmd", 1);
-            Assert.AreEqual("cmd", list.GetCommandSet("name")[0].Command);
+            list.Add("name", Command.Up, 1);
+            Assert.AreEqual(Command.Up, list.GetCommandSet("name")[0].Command);
         }
 
         [Test]
         public void GetCommandSet_ExistingCommandSet_ReturnsValidValue()
         {
-            list.Add("name", "cmd", 1);
+            list.Add("name", Command.Up, 1);
             Assert.AreEqual(1, list.GetCommandSet("name")[0].Value);
         }
 
         [Test]
         public void GetCommandSet_CommandSetWithUpperCase_IsFound()
         {
-            list.Add("name", "cmd", 1);
+            list.Add("name", Command.Up, 1);
             Assert.AreEqual(1, list.GetCommandSet("NAME").Count);
         }
 
         [Test]
         public void ContainsCommandSet_ExistingCommandSet_ReturnsTrue()
         {
-            list.Add("name", "cmd", 1);
+            list.Add("name", Command.Up, 1);
             Assert.True(list.ContainsCommandSet("name"));            
         }
 
         [Test]
         public void ContainsCommandSet_CommandSetWithUpperCase_ReturnsTrue()
         {
-            list.Add("name", "cmd", 1);
+            list.Add("name", Command.Up, 1);
             Assert.True(list.ContainsCommandSet("NAME"));
         }
     }

src/MissileSharp.Tests/ConfigReaderTests.cs

         public void Read_ValidConfig_Name1TheCommandIsCorrect()
         {
             var config = GetValidConfig();
-            Assert.AreEqual("up", config.GetCommandSet("name1")[0].Command);
+            Assert.AreEqual(Command.Up, config.GetCommandSet("name1")[0].Command);
         }
 
         [Test]
         public void Read_ValidConfig_Name2TheFirstCommandIsCorrect()
         {
             var config = GetValidConfig();
-            Assert.AreEqual("right", config.GetCommandSet("name2")[0].Command);
+            Assert.AreEqual(Command.Right, config.GetCommandSet("name2")[0].Command);
         }
 
         [Test]
         public void Read_ValidConfig_Name2TheSecondCommandIsCorrect()
         {
             var config = GetValidConfig();
-            Assert.AreEqual("fire", config.GetCommandSet("name2")[1].Command);
+            Assert.AreEqual(Command.Fire, config.GetCommandSet("name2")[1].Command);
         }
 
         [Test]

src/MissileSharp.Tests/LauncherCommandTests.cs

         [Test]
         public void ValueIsSmallerThanZero_ThrowsException()
         {
-            Assert.Throws<InvalidOperationException>(() => new LauncherCommand("cmd", -1));
+            Assert.Throws<InvalidOperationException>(() => new LauncherCommand(Command.Up, -1));
         }
 
         [Test]
-        public void ConstructorParametersAreSavedInProperties()
+        public void ValidCommandAsEnum_IsSavedWithValue()
         {
-            var cmd = new LauncherCommand("up", 100);
-            Assert.AreEqual("up", cmd.Command);
+            var cmd = new LauncherCommand(Command.Up, 100);
+            Assert.AreEqual(Command.Up, cmd.Command);
             Assert.AreEqual(100, cmd.Value);
         }
 
         [Test]
-        public void CommandIsAlwaysSavedInLowerCase()
+        public void CommandAsString_IsConvertedToEnum()
         {
-            var cmd = new LauncherCommand("UP",0);
-            Assert.AreEqual("up", cmd.Command);
-        } 
+            var cmd = new LauncherCommand("up", 100);
+            Assert.AreEqual(Command.Up, cmd.Command);
+        }
+
+        [Test]
+        public void CommandAsUppercaseString_IsConvertedToEnum()
+        {
+            var cmd = new LauncherCommand("UP", 100);
+            Assert.AreEqual(Command.Up, cmd.Command);
+        }
+
+        [Test]
+        public void InvalidCommandAsString_ThrowsException()
+        {
+            Assert.Throws<ArgumentOutOfRangeException>(() => new LauncherCommand("invalid", 100));
+        }
     }
 }

src/MissileSharp/Command.cs

+
+namespace MissileSharp
+{
+    /// <summary>
+    /// Possible commands for the missile launcher 
+    /// </summary>
+    public enum Command
+    {
+        /// <summary>
+        /// move up
+        /// </summary>
+        Up, 
+        
+        /// <summary>
+        /// move down
+        /// </summary>
+        Down,
+        
+        /// <summary>
+        /// turn left
+        /// </summary>
+        Left,
+        
+        /// <summary>
+        /// turn right
+        /// </summary>
+        Right,
+        
+        /// <summary>
+        /// reset position
+        /// </summary>
+        Reset,
+        
+        /// <summary>
+        /// fire missiles
+        /// </summary>
+        Fire
+    }
+}

src/MissileSharp/CommandCenter.cs

         {
             switch (command.Command)
             {
-                case "up":
+                case Command.Up:
                     Up(command.Value);
                     break;
-                case "down":
+                case Command.Down:
                     Down(command.Value);
                     break;
-                case "left":
+                case Command.Left:
                     Left(command.Value);
                     break;
-                case "right":
+                case Command.Right:
                     Right(command.Value);
                     break;
-                case "reset":
+                case Command.Reset:
                     Reset();
                     break;
-                case "fire":
+                case Command.Fire:
                     Fire(command.Value);
                     break;
             }

src/MissileSharp/CommandSetList.cs

         }
 
         /// <summary>
+        /// Add a new item to a command set (command set will be created if it doesn't exist)
+        /// </summary>
+        /// <param name="commandSetName">The name of the command set</param>
+        /// <param name="command">The actual command</param>
+        /// <param name="value">Numeric value for the command (duration/number of shots)</param>
+        public void Add(string commandSetName, Command command, int value)
+        {
+            Add(commandSetName, new LauncherCommand(command, value));
+        }
+
+        /// <summary>
         /// Get a command set (=list of LauncherCommands) by name
         /// </summary>
         /// <param name="commandSetName">The name to search for</param>

src/MissileSharp/LauncherCommand.cs

     /// </summary>
     public class LauncherCommand
     {
-        private string command;
-
         /// <summary>
         /// Creates a new instance of the LauncherCommand class
         /// </summary>
-        /// <param name="command"></param>
-        /// <param name="value"></param>
+        /// <param name="command">the actual command as a string ("down", "fire" etc.)</param>
+        /// <param name="value">numeric value (movement: milliseconds / firing: number of shots)</param>
         public LauncherCommand(string command, int value)
         {
             if (string.IsNullOrEmpty(command))
                 throw new InvalidOperationException("value must be equal or greater than zero");
             }
 
+            switch (command.ToLower())
+            {
+                case "up":
+                    this.Command = Command.Up;
+                    break;
+                case "down":
+                    this.Command = Command.Down;
+                    break;
+                case "left":
+                    this.Command = Command.Left;
+                    break;
+                case "right":
+                    this.Command = Command.Right;
+                    break;
+                case "reset":
+                    this.Command = Command.Reset;
+                    break;
+                case "fire":
+                    this.Command = Command.Fire;
+                    break;
+                default:
+                    throw new ArgumentOutOfRangeException("command must be one of the following: up, down, left, right, reset, fire");
+            }
+
+            this.Value = value;
+        }
+
+        /// <summary>
+        /// Creates a new instance of the LauncherCommand class
+        /// </summary>
+        /// <param name="command">the actual command</param>
+        /// <param name="value">numeric value (movement: milliseconds / firing: number of shots)</param>
+        public LauncherCommand(Command command, int value)
+        {
+            if (value < 0)
+            {
+                throw new InvalidOperationException("value must be equal or greater than zero");
+            }
+
             this.Command = command;
             this.Value = value;
         }
 
         /// <summary>
-        /// the actual command ("left", "fire" etc.)
+        /// the actual command
         /// </summary>
-        public string Command 
-        {
-            get { return this.command; }
-            set { this.command = value.ToLower(); }
-        }
+        public Command Command { get; set; }
 
         /// <summary>
         /// numeric value (movement: milliseconds / firing: number of shots)

src/MissileSharp/MissileSharp.csproj

   </ItemGroup>
   <ItemGroup>
     <Compile Include="AssemblyVersion.cs" />
+    <Compile Include="Command.cs" />
     <Compile Include="ConfigReader.cs" />
     <Compile Include="HidLibraryDevice.cs" />
     <Compile Include="IHidDevice.cs" />