Commit a3fe013

unknown <mkhan@.arcresources.ca>
2009-10-05 19:04:03
created command registry to register and load console commands to process requests.
1 parent 9a557b5
product/application/application.csproj
@@ -49,6 +49,9 @@
     <Compile Include="Console.cs" />
     <Compile Include="ConsoleApplication.cs" />
     <Compile Include="CommandRegistry.cs" />
+    <Compile Include="ConsoleArgumentsCommandRegistry.cs" />
+    <Compile Include="ConsoleCommand.cs" />
+    <Compile Include="HelpCommand.cs" />
     <Compile Include="ParameterizedCommand.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
   </ItemGroup>
product/application/CommandRegistry.cs
@@ -3,5 +3,6 @@ namespace simple.migrations
     public interface CommandRegistry
     {
         ParameterizedCommand<string[]> command_for(string[] arguments);
+        void register(ConsoleCommand command);
     }
 }
\ No newline at end of file
product/application/ConsoleArgumentsCommandRegistry.cs
@@ -0,0 +1,22 @@
+using System.Collections.Generic;
+using System.Linq;
+
+namespace simple.migrations
+{
+    public class ConsoleArgumentsCommandRegistry : CommandRegistry
+    {
+        readonly IList<ConsoleCommand> all_commands = new List<ConsoleCommand>();
+
+        public ParameterizedCommand<string[]> command_for(string[] arguments)
+        {
+            if(all_commands.Any(x => x.can_handle(arguments)))
+                return all_commands.Single(x => x.can_handle(arguments));
+            return new HelpCommand();
+        }
+
+        public void register(ConsoleCommand command)
+        {
+            all_commands.Add(command);
+        }
+    }
+}
\ No newline at end of file
product/application/ConsoleCommand.cs
@@ -0,0 +1,7 @@
+namespace simple.migrations
+{
+    public interface ConsoleCommand : ParameterizedCommand<string[]>
+    {
+        bool can_handle(string[] arguments);
+    }
+}
\ No newline at end of file
product/application/HelpCommand.cs
@@ -0,0 +1,17 @@
+using System;
+
+namespace simple.migrations
+{
+    public class HelpCommand : ConsoleCommand
+    {
+        public void run_against(string[] item)
+        {
+            throw new NotImplementedException();
+        }
+
+        public bool can_handle(string[] arguments)
+        {
+            throw new NotImplementedException();
+        }
+    }
+}
\ No newline at end of file
product/application.tests/application.tests.csproj
@@ -69,6 +69,7 @@
     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="CommandRegistrySpecs.cs" />
     <Compile Include="ConsoleSpecs.cs" />
     <Compile Include="EmptyTestFixture.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
product/application.tests/CommandRegistrySpecs.cs
@@ -0,0 +1,54 @@
+using developwithpassion.bdd.contexts;
+using developwithpassion.bdd.harnesses.mbunit;
+using Rhino.Mocks;
+using simple.migrations;
+
+namespace tests
+{
+    public class CommandRegistrySpecs
+    {
+        public class concern : observations_for_a_sut_with_a_contract<CommandRegistry, ConsoleArgumentsCommandRegistry>
+        {
+        }
+
+        public class when_looking_up_the_appropriate_command_for_some_console_arguments : concern
+        {
+            it should_return_the_command_that_can_process_the_request = () => { result.should_be_equal_to(command); };
+
+            context c = () =>
+                            {
+                                command = an<ConsoleCommand>();
+                                command.Stub(x => x.can_handle(arguments)).Return(true);
+                            };
+
+            after_the_sut_has_been_created a = () => { sut.register(command); };
+
+            because b = () => { result = controller.sut.command_for(arguments); };
+
+            static readonly string[] arguments = new[] {""};
+            static ParameterizedCommand<string[]> result;
+            static ConsoleCommand command;
+        }
+
+        public class when_attempting_to_find_a_command_that_can_process_a_set_of_arguments_that_is_not_recognized :
+            concern
+        {
+            it should_return_a_command_that_can_inform_the_user_of_the_error =
+                () => { result.should_be_an_instance_of<HelpCommand>(); };
+
+            context c = () =>
+                            {
+                                command = an<ConsoleCommand>();
+                                command.Stub(x => x.can_handle(arguments)).Return(false);
+                            };
+
+            after_the_sut_has_been_created a = () => { sut.register(command); };
+
+            because b = () => { result = controller.sut.command_for(arguments); };
+
+            static readonly string[] arguments = new[] {""};
+            static ParameterizedCommand<string[]> result;
+            static ConsoleCommand command;
+        }
+    }
+}
\ No newline at end of file