Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added support for + and - modifiers to add/remove filters from the cu…

…rrent setting
  • Loading branch information...
commit eaf530ea9a9d866f5d9296e36721702741761aa9 1 parent 3171df5
@codereflection authored
View
123 src/Giles.Specs/Console/UserInputHandlerSpecs.cs
@@ -10,16 +10,16 @@ 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 Queue<string> queue;
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);
+ queue = new Queue<string>(userValues);
UserInputHandler.Output = value => Messages.Add(value);
- UserInputHandler.Input = () => stack.Dequeue();
+ UserInputHandler.Input = () => queue.Dequeue();
};
Because of = () =>
@@ -48,4 +48,121 @@ public class when_getting_a_list_of_user_values_and_the_default_setting_is_accep
It should_save_the_default_values = () =>
result.ShouldContainOnly(DefaultValues);
}
+
+ [Subject(typeof(UserInputHandler))]
+ public class when_getting_a_list_of_user_values_and_the_user_adds_to_the_default_values
+ {
+ static List<string> result;
+ static List<string> userValues;
+ static Queue<string> queue;
+ 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 };
+ queue = new Queue<string>(userValues);
+ UserInputHandler.Output = value => Messages.Add(value);
+ UserInputHandler.Input = () => queue.Dequeue();
+ };
+
+ Because of = () =>
+ result = UserInputHandler.GetUserValuesFor(DefaultValues, "The Prompt");
+
+ It should_have_both_the_default_values = () =>
+ result.ShouldContain(DefaultValues.ToArray());
+
+ It should_add_the_new_items_and_remove_the_add_item_operator = () =>
+ result.FirstOrDefault(x => x.EndsWith("newValue")).StartsWith("+").ShouldBeFalse();
+ }
+
+ [Subject(typeof(UserInputHandler))]
+ public class when_getting_a_list_of_user_values_and_the_user_adds_to_the_default_values_with_only_one_value_having_a_modifier
+ {
+ static List<string> result;
+ static List<string> userValues;
+ static Queue<string> queue;
+ static readonly List<string> Messages = new List<string>();
+ static readonly List<string> DefaultValues = new List<string> { "value1" };
+
+ Establish context = () =>
+ {
+ userValues = new List<string> { "+newValue1", "newValue2", Environment.NewLine };
+ queue = new Queue<string>(userValues);
+ UserInputHandler.Output = value => Messages.Add(value);
+ UserInputHandler.Input = () => queue.Dequeue();
+ };
+
+ Because of = () =>
+ result = UserInputHandler.GetUserValuesFor(DefaultValues, "The Prompt");
+
+ It should_have_both_the_default_values = () =>
+ result.ShouldContain(DefaultValues.ToArray());
+
+ It should_add_the_new_items_with_and_without_a_modifier = () =>
+ {
+ result.ShouldContain("newValue1");
+ result.ShouldContain("newValue2");
+ };
+ }
+
+ [Subject(typeof(UserInputHandler))]
+ public class when_getting_a_list_of_user_values_and_the_user_removes_from_the_default_values
+ {
+ static List<string> result;
+ static List<string> userValues;
+ static Queue<string> queue;
+ static readonly List<string> Messages = new List<string>();
+ static readonly List<string> DefaultValues = new List<string> { "value1", "valueToRemove" };
+
+ Establish context = () =>
+ {
+ userValues = new List<string> { "-valueToRemove", Environment.NewLine };
+ queue = new Queue<string>(userValues);
+ UserInputHandler.Output = value => Messages.Add(value);
+ UserInputHandler.Input = () => queue.Dequeue();
+ };
+
+ Because of = () =>
+ result = UserInputHandler.GetUserValuesFor(DefaultValues, "The Prompt");
+
+ It should_remove_the_correct_value = () =>
+ result.ShouldNotContain("valueToRemove");
+
+ It should_maintain_the_other_values = () =>
+ result.ShouldContain("value1");
+ }
+
+ [Subject(typeof(UserInputHandler))]
+ public class when_getting_a_list_of_user_values_and_the_user_adds_and_removes_values_from_the_default_values
+ {
+ static List<string> result;
+ static List<string> userValues;
+ static Queue<string> queue;
+ static readonly List<string> Messages = new List<string>();
+ static readonly List<string> DefaultValues = new List<string> { "value1", "value2", "value3" };
+
+ Establish context = () =>
+ {
+ userValues = new List<string> { "-value2", "+value4", Environment.NewLine };
+ queue = new Queue<string>(userValues);
+ UserInputHandler.Output = value => Messages.Add(value);
+ UserInputHandler.Input = () => queue.Dequeue();
+ };
+
+ Because of = () =>
+ result = UserInputHandler.GetUserValuesFor(DefaultValues, "The Prompt");
+
+ It should_remove_the_correct_value = () =>
+ result.ShouldNotContain("value2");
+
+ It should_maintain_the_values_not_added_or_removed = () =>
+ {
+ result.ShouldContain("value1");
+ result.ShouldContain("value3");
+ };
+
+ It should_add_the_new_value = () =>
+ result.ShouldContain("value4");
+ }
}
View
2  src/Giles/Program.cs
@@ -163,7 +163,7 @@ static void RequestQuit()
static void SetTestFilters()
{
- config.Filters = UserInputHandler.GetUserValuesFor(config.Filters, "Filters: Enter a namespace and type (MyNamespace.FooTests), one on each line. Use a blank line save.");
+ config.Filters = UserInputHandler.GetUserValuesFor(config.Filters, "Filters: Enter a namespace and type (MyNamespace.FooTests).");
Console.WriteLine("Filters set to:");
config.Filters.Each(x => Console.WriteLine("\t{0}", x));
}
View
61 src/Giles/UserInputHandler.cs
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
+using System.Linq;
+using Giles.Core.Utility;
namespace Giles
{
@@ -7,28 +9,71 @@ public static class UserInputHandler
{
public static Action<string> Output = value => Console.WriteLine(value);
public static Func<string> Input = Console.ReadLine;
+ const string instructions = @"Enter one value on each line. Use a blank line save.
+Modifiers:
+ +[newValue] to add a value to the current settings
+ -[newValue] to remove a value from the current settings";
- public static List<T> GetUserValuesFor<T>(List<T> defaultValues, string description)
+ public static List<string> GetUserValuesFor(List<string> defaultValues, string description)
{
Output(description);
- Output(string.Format(@" Current settings: {0}", GetLineSeparatedValueListFor(defaultValues)));
- var newValues = new List<T>();
+ Output("");
+ Output(instructions);
+ Output(string.Format(" Current settings: {0}{1}", GetLineSeparatedValueListFor(defaultValues), Environment.NewLine));
+ var newValues = new List<string>();
+ var modifyingDefaultValues = false;
string newLine;
do
{
newLine = Input();
- if (!string.IsNullOrWhiteSpace(newLine))
- newValues.Add((T)Convert.ChangeType(newLine, typeof(T)));
+ if (string.IsNullOrWhiteSpace(newLine)) continue;
+
+ if (modifyingDefaultValues == false)
+ modifyingDefaultValues = ContainsAModifier(newLine);
+ newValues.Add(newLine);
}
while (!string.IsNullOrWhiteSpace(newLine));
- return newValues.Count == 0 ? defaultValues : newValues;
+
+ if (newValues.Count == 0) return defaultValues;
+
+ if (modifyingDefaultValues)
+ return GetModifiedList(defaultValues, newValues).ToList();
+
+ return newValues;
+ }
+
+ static IEnumerable<string> GetModifiedList(IEnumerable<string> defaultValues, IEnumerable<string> newValues)
+ {
+ var modifiedList = new List<string>(defaultValues);
+
+ modifiedList.AddRange(RemoveModifiersFrom(AddedValues(newValues)));
+
+ RemoveModifiersFrom(newValues.Where(x => x.StartsWith("-")))
+ .Each(x => modifiedList.Remove(x));
+
+ return modifiedList;
+ }
+
+ static IEnumerable<string> AddedValues(IEnumerable<string> newValues)
+ {
+ return newValues.Where(x => x.StartsWith("-") == false);
+ }
+
+ static IEnumerable<string> RemoveModifiersFrom(IEnumerable<string> newValues)
+ {
+ return newValues.Select(x => x.Replace("+", string.Empty).Replace("-", string.Empty));
+ }
+
+ static bool ContainsAModifier(string newLine)
+ {
+ return newLine.StartsWith("+") || newLine.StartsWith("-");
}
- static string GetLineSeparatedValueListFor<T>(List<T> defaultValues)
+ static string GetLineSeparatedValueListFor(List<string> defaultValues)
{
var result = "";
- defaultValues.ForEach(x => result += Environment.NewLine + x.ToString());
+ defaultValues.ForEach(x => result += Environment.NewLine + "\t" + x);
return result;
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.