private static SortCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions) { if (commandLineOptions == null) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must specify a option.")); } SortCommandLineOptions targetOptions = new SortCommandLineOptions(); if (commandLineOptions.Arguments.Count >= 0) { foreach (var arg in commandLineOptions.Arguments.Keys) { SortOptionType optionType = SortOptions.GetOptionType(arg); if (optionType == SortOptionType.None) { throw new CommandLineException( string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", string.Format(CultureInfo.CurrentCulture, "cannot parse the command line argument : [{0}].", arg))); } switch (optionType) { case SortOptionType.InputFile: targetOptions.InputFile = commandLineOptions.Arguments[arg]; break; case SortOptionType.OutputFile: targetOptions.IsSetOutputFile = true; targetOptions.OutputFile = commandLineOptions.Arguments[arg]; break; case SortOptionType.ReverseOrder: targetOptions.IsSetReverseOrder = true; break; case SortOptionType.Help: targetOptions.IsSetHelp = true; break; case SortOptionType.Version: targetOptions.IsSetVersion = true; break; } } } if (commandLineOptions.Parameters.Count > 0) { if (string.IsNullOrEmpty(targetOptions.InputFile)) { targetOptions.InputFile = commandLineOptions.Parameters.First(); } } return(targetOptions); }
public void Should_create_correct_ProductSorter_for_given_sortOption(SortOptionType sortOption, Type productSorterType, SortOrder sortOrder = SortOrder.Ascending) { // Arrange var mockedShopperHistoryService = Mock.Of <IShopperHistoryService>(); var productSorterFactory = new ProductSorterFactory(mockedShopperHistoryService); // Act var productSorter = productSorterFactory.Create(sortOption); // Assert Assert.Equal(productSorter.GetType(), productSorterType); Assert.Equal(productSorter.SortOrder, sortOrder); }
public static SortOptionType GetOptionType(string option) { SortOptionType optionType = SortOptionType.None; foreach (var pair in Options) { foreach (var item in pair.Value) { if (item == option) { optionType = pair.Key; break; } } } return(optionType); }
public IProductSorter Create(SortOptionType sortOption) { switch (sortOption) { case SortOptionType.Low: return(new ProductSorterByPrice()); case SortOptionType.High: return(new ProductSorterByPrice(SortOrder.Descending)); case SortOptionType.Ascending: return(new ProductSorterByName()); case SortOptionType.Descending: return(new ProductSorterByName(SortOrder.Descending)); case SortOptionType.Recommended: return(new ProductSorterByPopularity(_shopperHistoryService, sortOrder: SortOrder.Descending)); } throw new Exception("Invalid Sort Option"); }