예제 #1
0
        static void Main(string[] args)
        {
            var builder = new ConfigurationBuilder()
                          .SetBasePath(Directory.GetCurrentDirectory())
                          .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

            IConfigurationRoot configuration = builder.Build();
            var _NameSorterSettingsConfig    = new NameSorterSettingsConfig();

            configuration.GetSection("NameSorterSettings").Bind(_NameSorterSettingsConfig);

            //reads Output location from command-line arg
            string inputFileName = args[0].Trim();

            //reads Output location from config
            string outputFileName = _NameSorterSettingsConfig.Output;

            //reads Input file and stores data in memory-list for processing
            IReadDataService _dataReader = new FileReadDataService();
            List <Name>      ListOfNames = _dataReader.ReadData(inputFileName);

            //reads Sorting method from config
            ISortingService _nameSorter = new NameSortingService(_NameSorterSettingsConfig.SortingSwitch);

            ListOfNames = _nameSorter.SortNameList(ListOfNames);

            //stores sorted names to the Output file
            IWriteDataService _dataWriter = new FileWriteDataService();

            _dataWriter.WriteData(outputFileName, ListOfNames);

            //print sorted names on screen
            new PrintNameList().PrintFullList(ListOfNames);
            Console.ReadKey();
        }
예제 #2
0
        /// <summary>
        /// Starts the application
        /// </summary>
        /// <param name="args">The argument (source file name) to be passed in.</param>
        static async Task Main(string[] args)
        {
            var             fileIoService = new FileInteropService();
            FullNameFactory nameFactory   = new FullNameFactory();
            var             service       = new NameSortingService(fileIoService, nameFactory);

            try
            {
                // Get the file path to read from
                var filePath = args.FirstOrDefault();

                if (Debugger.IsAttached) // Override file path when executing in VS
                {
                    filePath = "./unsorted-names-list.txt";
                }

                if (filePath == null)
                {
                    throw new ArgumentNullException(nameof(filePath), "Must specify the source file path.");
                }

                Console.WriteLine($"Beginning name sorting, retrieving names from: {filePath}.{_newline}");

                // Infer destination file name as per spec, this allows for change of directory,
                // -- However can be brittle for long file paths (ie. directory that contains unsorted).
                var destinationPath = filePath.Replace("unsorted", "sorted");
                var start           = DateTimeOffset.Now;

                var sortedNames = await service.SortNames(filePath, destinationPath);

                // Display the result.
                sortedNames.ForEach(
                    name =>
                {
                    var index = sortedNames.IndexOf(name);
                    Console.WriteLine($"{index}. {name.ToText()}");
                });

                var elapsedTime = DateTimeOffset.Now.Subtract(start);
                Console.WriteLine($"{_newline}Sorting complete, results available at {destinationPath}. Took {elapsedTime.TotalSeconds} seconds.");
            }
            catch (ArgumentNullException argNullEx)
            {
                Console.WriteLine("Please make sure you've supplied all the arguments and try again.");
                Console.WriteLine(argNullEx);
            }
            catch (InvalidNameException badNameEx)
            {
                Console.WriteLine("It appears you've provided an invalid name. Lets try that again with proper input shall we?");
                Console.WriteLine($"Detail: {badNameEx.Message}");
            }
            catch (Exception e)
            {
                Console.WriteLine("An unexpected error occurred, check below for details.");
                Console.WriteLine(e);
            }
        }
        public void SortName_WithInvalidFileContents_ThrowsException()
        {
            // Arrange
            SetupInvalidInputFileMocks();
            _nameSortingService = new NameSortingService(_fileServiceMock.Object, _factoryMock.Object);

            string sourcePath      = "./unsorted-names-list.txt";
            string destinationPath = "./sorted-names-list.txt";

            // Act
            // Assert
            Assert.ThrowsAsync <Exception>(async() => await _nameSortingService.SortNames(sourcePath, destinationPath));
        }
        public void SortName_WithValidListOfNames_SortsNamesInOrder()
        {
            // Arrange
            SetupValidFileMocks();
            _nameSortingService = new NameSortingService(_fileServiceMock.Object, _factoryMock.Object);

            string sourcePath      = "./unsorted-names-list.txt";
            string destinationPath = "./sorted-names-list.txt";

            // Act
            Task.FromResult(_nameSortingService.SortNames(sourcePath, destinationPath));

            // Assert
            Assert.That(_fileOutput, Is.Not.Null.Or.Empty);
            Assert.That(_fileOutput, Is.EqualTo(TestData.ValidSortedListOfNamesAsStrings));
        }