Permalink
Browse files

Extracting out UserInputHandler

  • Loading branch information...
1 parent 5b4d890 commit 3171df5d16029c2ba724a6612faa4e8ee0e5eeae @codereflection committed Feb 13, 2012
@@ -0,0 +1,51 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Machine.Specifications;
+
+namespace Giles.Specs.Console
+{
+ [Subject(typeof(UserInputHandler))]
+ public class when_getting_a_list_of_user_values_and_a_new_value_is_entered
+ {
+ static List<string> result;
+ static List<string> userValues;
+ static Queue<string> stack;
+ static readonly List<string> Messages = new List<string>();
+ static readonly List<string> DefaultValues = new List<string> { "value1" };
+
+ Establish context = () =>
+ {
+ userValues = new List<string> { "newValue", Environment.NewLine };
+ stack = new Queue<string>(userValues);
+ UserInputHandler.Output = value => Messages.Add(value);
+ UserInputHandler.Input = () => stack.Dequeue();
+ };
+
+ Because of = () =>
+ result = UserInputHandler.GetUserValuesFor(DefaultValues, "The prompt");
+
+ It should_get_the_user_entered_values = () =>
+ result.ShouldContainOnly(userValues.First());
+ }
+
+ [Subject(typeof(UserInputHandler))]
+ public class when_getting_a_list_of_user_values_and_the_default_setting_is_accepted
+ {
+ static readonly List<string> Messages = new List<string>();
+ static readonly List<string> DefaultValues = new List<string> { "value1" };
+ static List<string> result;
+
+ Establish context = () =>
+ {
+ UserInputHandler.Output = value => Messages.Add(value);
+ UserInputHandler.Input = () => Environment.NewLine;
+ };
+
+ Because of = () =>
+ result = UserInputHandler.GetUserValuesFor(DefaultValues, "The prompt");
+
+ It should_save_the_default_values = () =>
+ result.ShouldContainOnly(DefaultValues);
+ }
+}
@@ -56,6 +56,7 @@
<Link>Properties\ProjectVersion.cs</Link>
</Compile>
<Compile Include="Console\Options\CLOptionsSpecs.cs" />
+ <Compile Include="Console\UserInputHandlerSpecs.cs" />
<Compile Include="Core\Configuration\TestAssemblyFinderSpecs.cs" />
<Compile Include="Core\Runners\GilesTestListenerSpecs.cs" />
<Compile Include="Core\Utility\FakeFileSystemWatcher.cs">
View
@@ -87,6 +87,7 @@
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SlayerModule.cs" />
+ <Compile Include="UserInputHandler.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="CommandLine">
View
@@ -163,7 +163,7 @@ static void RequestQuit()
static void SetTestFilters()
{
- config.Filters = GetUserValues("Filters", config.Filters);
+ config.Filters = UserInputHandler.GetUserValuesFor(config.Filters, "Filters: Enter a namespace and type (MyNamespace.FooTests), one on each line. Use a blank line save.");
Console.WriteLine("Filters set to:");
config.Filters.Each(x => Console.WriteLine("\t{0}", x));
}
@@ -195,22 +195,6 @@ static void DisplayVerboseResults()
Console.WriteLine("Please run some tests first...");
}
- static List<T> GetUserValues<T>(string description, List<T> defaultValues)
- {
- Console.WriteLine("Filters: Enter a namespace and type (MyNamespace.FooTests), one on each line. Use a blank line save.");
- Console.WriteLine(" Current settings: {0}", defaultValues);
- var newValues = new List<T>();
-
- string newLine;
- do
- {
- newLine = Console.ReadLine();
- if (!string.IsNullOrWhiteSpace(newLine))
- newValues.Add((T)Convert.ChangeType(newLine, typeof(T)));
- }
- while (!string.IsNullOrWhiteSpace(newLine));
- return newValues;
- }
static T GetUserValue<T>(string description, T defaultValue)
{
@@ -0,0 +1,35 @@
+using System;
+using System.Collections.Generic;
+
+namespace Giles
+{
+ public static class UserInputHandler
+ {
+ public static Action<string> Output = value => Console.WriteLine(value);
+ public static Func<string> Input = Console.ReadLine;
+
+ public static List<T> GetUserValuesFor<T>(List<T> defaultValues, string description)
+ {
+ Output(description);
+ Output(string.Format(@" Current settings: {0}", GetLineSeparatedValueListFor(defaultValues)));
+ var newValues = new List<T>();
+
+ string newLine;
+ do
+ {
+ newLine = Input();
+ if (!string.IsNullOrWhiteSpace(newLine))
+ newValues.Add((T)Convert.ChangeType(newLine, typeof(T)));
+ }
+ while (!string.IsNullOrWhiteSpace(newLine));
+ return newValues.Count == 0 ? defaultValues : newValues;
+ }
+
+ static string GetLineSeparatedValueListFor<T>(List<T> defaultValues)
+ {
+ var result = "";
+ defaultValues.ForEach(x => result += Environment.NewLine + x.ToString());
+ return result;
+ }
+ }
+}

0 comments on commit 3171df5

Please sign in to comment.