/// <summary>
        /// Sorts a bunch of given names from the supplied file and writes a sorted list of names out to the destination file.
        /// </summary>
        public async Task <List <FullName> > SortNames(string sourcePath, string destinationPath)
        {
            // Get the contents of the file
            var lines = (await _fileInteropService.ReadFromTextFile(sourcePath)).ToList();

            // Sanity check, even through the file interop service should also protect from this.
            if (lines?.Count < 1)
            {
                throw new Exception($"File ({sourcePath}) contains no elements.");
            }

            // Transform into the type we are working with
            List <FullName> unsortedNames = new List <FullName>();

            foreach (string line in lines)
            {
                var fullName = _fullNameFactory.CreateNewFullName(line);
                unsortedNames.Add(fullName);
            }

            // Copy the result into a separate list for sorting and sort by the FullName sorting setup.
            List <FullName> sortedNames = unsortedNames.ToList();

            sortedNames.Sort();

            // Write the results back out to the destination file
            await _fileInteropService.WriteTextToFile(destinationPath, sortedNames);

            return(sortedNames);
        }
Пример #2
0
        public void CreateNewFullName_WithSeparatorMissingFromInput_ThrowsInvalidNameException()
        {
            // We make a wrong assumption that the string is separated by ';'
            // Arrange
            string nameString = "Holey;Dooley";

            // Act && Assert
            Assert.Throws <InvalidNameException>(() => _fullNameFactory.CreateNewFullName(nameString));

            // Or we instantiate the factory with the wrong separator -> ','
            // Arrange
            _fullNameFactory = new FullNameFactory(new [] { ',' });
            nameString       = "Holey Dooley";

            // Act && Assert
            Assert.Throws <InvalidNameException>(() => _fullNameFactory.CreateNewFullName(nameString));
        }
Пример #3
0
        public void CreateNewFullName_WithValidNameString_CreatesNewFullNameInstance()
        {
            // Arrange
            string nameString = "James Smashing Bond";

            // Act
            var result = _fullNameFactory.CreateNewFullName(nameString);

            // Assert
            Assert.That(result.GivenNames, Is.EqualTo(new [] { "James", "Smashing" }));
            Assert.That(result.LastName, Is.EqualTo("Bond"));
            Assert.That(result.SeparatedBy, Is.EqualTo(_defaultSeparator));
            Assert.That(result.ToText(), Is.EqualTo(nameString));
        }
Пример #4
0
        public void CreateNewFullName_WithValidNameAndAlternateSeparator_CreatesNewFullNameInstance()
        {
            // Test instantiation with a different separator
            // Arrange
            string nameString   = "James,Smashing,Bond";
            var    altSeparator = new[] { ',' };

            _fullNameFactory = new FullNameFactory(altSeparator);

            // Act
            var result = _fullNameFactory.CreateNewFullName(nameString);

            // Assert
            Assert.That(result.GivenNames, Is.EqualTo(new[] { "James", "Smashing" }));
            Assert.That(result.LastName, Is.EqualTo("Bond"));
            Assert.That(result.SeparatedBy, Is.EqualTo(altSeparator));
            // Test this still represents a comma separated string.
            Assert.That(result.ToText(), Is.EqualTo("James Smashing Bond"));
            Assert.That(result.ToString(), Is.EqualTo(nameString));
        }