Example #1
0
        public void Should_throw_directorynotfoundexception_when_directory_not_exist()
        {
            string missingDirectory = @"unknown\notfound.csv";

            TestUtils.DeleteDirectory(missingDirectory);

            Assert.Throws <DirectoryNotFoundException>(delegate
            {
                var contactsReader = new ContactsReader(missingDirectory);
            });
        }
Example #2
0
        public void Should_throw_filenotfoundexception_when_file_not_exist()
        {
            string missingFile = @"notfound.csv";

            TestUtils.DeleteFile(missingFile);

            Assert.Throws <FileNotFoundException>(delegate
            {
                var contactsReader = new ContactsReader(missingFile);
            });
        }
Example #3
0
        public static async Task <IEnumerable <Contacts> > ReadFromFile(string file)
        {
            var listOfContacts = new List <Contacts>();

            using (var contactsReader = new ContactsReader(file))
            {
                while (!contactsReader.isEndOfStream())
                {
                    var foundContact = await contactsReader.ReadContacts();

                    if (foundContact != null)
                    {
                        listOfContacts.Add(foundContact);
                    }
                }
            }

            return(listOfContacts);
        }
Example #4
0
        public async Task Should_use_a_streamreader_to_read_file()
        {
            string contactName          = "robert simmons";
            string contactPostalAddress = "Holborn, London";

            string newContact = string.Format(CultureInfo.InvariantCulture, "{0}\t{1}", contactName, contactPostalAddress);

            using (var memoryStream = new MemoryStream())
                using (var streamWriter = new StreamWriter(memoryStream))
                    using (var streamReader = new StreamReader(memoryStream))
                        using (var contactsReader = new ContactsReader(streamReader, new char[] { '\t' }))
                        {
                            await streamWriter.WriteLineAsync(newContact);

                            await streamWriter.FlushAsync();

                            memoryStream.Position = 0; // Fix issue with NullReferenceException

                            var contact = await contactsReader.ReadContacts();

                            Assert.AreEqual(contactName, contact.Name);
                            Assert.AreEqual(contactPostalAddress, contact.PostalAddress);
                        }
        }