コード例 #1
0
        private Tuple <int, Exception> CountLines(string filename, bool withOutValues)
        {
            var       csvReader = new CSVReaderWriter();
            string    string1   = null;
            string    string2   = null;
            Exception ex        = null;

            csvReader.Open(filename, CSVReaderWriter.Mode.Read);
            int lineCount = 0;

            try
            {
                while (withOutValues ? csvReader.Read(out string1, out string2) : csvReader.Read(string1, string2))
                {
                    lineCount++;
                }
            }
            catch (Exception e)
            {
                ex = e;
            }

            csvReader.Close();

            return(new Tuple <int, Exception>(lineCount, ex));
        }
コード例 #2
0
        public void Should_read_columns_from_file()
        {
            CSVReaderWriter csvReaderWriter = new CSVReaderWriter();

            string fileName = GetTempFilePath();


            using (FileStream outputStream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                using (StreamWriter writer = new StreamWriter(outputStream))
                {
                    writer.WriteLine("ITEM 1\titeM 2");
                }
            }


            csvReaderWriter.Open(fileName, CSVReaderWriter.Mode.Read);

            string column1 = string.Empty;
            string column2 = string.Empty;


            Assert.True(csvReaderWriter.Read(out column1, out column2));

            Assert.AreEqual("ITEM 1", column1);

            Assert.AreEqual("iteM 2", column2);
        }
コード例 #3
0
        public void CSVReaderWriter_Read_NullWriterOpen()
        {
            var csvReaderWriter = new CSVReaderWriter(null, null, delimiter);

            Assert.Throws <ApplicationException>(() => csvReaderWriter.Open("somedata", Mode.Read));
            Assert.Throws <ApplicationException>(() => csvReaderWriter.OpenRead("somedata"));
        }
コード例 #4
0
 public void SetUp()
 {
     _csvStreamReader = new Mock <IReadStream>();
     _csvReader       = new CsvReader(_csvStreamReader.Object);
     _csvWriter       = new Mock <IWriteCsv>();
     _csvReaderWriter = new CSVReaderWriter(_csvReader, _csvWriter.Object);
 }
コード例 #5
0
        public void canOpenAndCloseFile()
        {
            CSVReaderWriter rw = new CSVReaderWriter();

            rw.Open(TestInputFile, Mode.Read);
            rw.Close();
        }
コード例 #6
0
        public void WriteCsvFile()
        {
            //Write test file
            using (var writer = new CSVReaderWriter())
            {
                writer.Open(_testWriteDataFile, CSVReaderWriter.Mode.Write);


                foreach (var testAddress in _mailAddressTests)
                {
                    writer.Write(testAddress.Name, testAddress.Address);
                }
                writer.Close();
            }

            //Read test file
            var contacts = new List <Contact>();

            using (var reader = new CSVReaderWriter())
            {
                reader.Open(_testWriteDataFile, CSVReaderWriter.Mode.Read);
                string column1, column2;
                while (reader.Read(out column1, out column2))
                {
                    contacts.Add(new Contact(column1, column2));
                }

                reader.Close();
            }

            CollectionAssert.AreEqual(_mailAddressTests, contacts);
        }
コード例 #7
0
        public void Process(string inputFile)
        {
            try
            {
                var reader = new CSVReaderWriter();
                reader.Open(inputFile, CSVReaderWriter.Mode.Read);

                string column1, column2;

                while (reader.Read(out column1, out column2))
                {
                    if (_mailShot is v1.IMailShot)
                    {
                        ((v1.IMailShot)(_mailShot)).SendMailShot(column1, column2);
                    }
                    else if (_mailShot is v2.IMailShot)
                    {
                        ((v2.IMailShot)(_mailShot)).SendEmailMailShot(column1, column2);
                    }

                    //_mailShot.SendMailShot(column1, column2);
                }

                //reader.Close();
            }
            catch (Exception ex)
            {
                //log error to identify and rectify
            }
        }
コード例 #8
0
 private void Harness(Action <CSVReaderWriter> callback)
 {
     using (var csv = new CSVReaderWriter())
     {
         callback(csv);
     }
 }
コード例 #9
0
        public void Read_With_Out_Parameters_When_ReaderStream_Not_Initialized_Throws_Exception()
        {
            string column1       = null;
            string column2       = null;
            var    csvReadWriter = new CSVReaderWriter();

            Assert.Throws <Exception>(() => csvReadWriter.Read(out column1, out column2));
        }
コード例 #10
0
 public void WhenInReadMode_WithNonExistingFileOrInvalidFilePath_Then_ThrowsFileNotFoundException()
 {
     using (var readerWriter = new CSVReaderWriter())
     {
         var mode = CSVReaderWriter.Mode.Read;
         Assert.Throws <FileNotFoundException>(() => readerWriter.Open(_currentDirectory + @"\sss", mode));
     }
 }
コード例 #11
0
 public void WhenInWriteMode_WithNonExistingFile_Then_NewFileIsCreated()
 {
     using (var readerWriter = new CSVReaderWriter())
     {
         var mode = CSVReaderWriter.Mode.Write;
         Assert.DoesNotThrow(() => readerWriter.Open(_currentDirectory + _nonExistingFile, mode));
     }
 }
コード例 #12
0
 public void ReadNotOpenedFile()
 {
     using (var reader = new CSVReaderWriter())
     {
         string column1, column2;
         Assert.Throws <CSVFileNotOpenException>(() => reader.Read(out column1, out column2));
     }
 }
コード例 #13
0
        public void Open_Method_to_write_Should_not_throw_Exception()
        {
            var csvReaderWriter = new CSVReaderWriter();

            Assert.DoesNotThrow(() => csvReaderWriter.Open(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, TestOutputFile), CSVReaderWriter.Mode.Write));

            csvReaderWriter.Close();
        }
コード例 #14
0
        public void CSVReaderWriter_Read_NullReader()
        {
            string column1         = null;
            string column2         = null;
            var    csvReaderWriter = new CSVReaderWriter(null, null, delimiter);

            Assert.Throws <ApplicationException>(() => csvReaderWriter.Read(out column1, out column2));
        }
コード例 #15
0
        public void Setup()
        {
            _fileOpener = A.Fake <IOpenFile>();
            _fileReader = A.Fake <IReadFile>();
            _fileWriter = A.Fake <IWriteFile>();

            _csvReaderWriter = new CSVReaderWriter(_fileOpener, _fileReader, _fileWriter);
        }
コード例 #16
0
        public void Write_Method_Should_not_throw_Exception_when_empty_string_array_is_passed()
        {
            var csvReaderWriter = new CSVReaderWriter();

            Assert.DoesNotThrow(() => csvReaderWriter.Write(new string[] {}));

            csvReaderWriter.Close();
        }
コード例 #17
0
        public void Should_throw_on_invalid_open_filemode()
        {
            CSVReaderWriter csvReaderWriter = new CSVReaderWriter();

            string fileName = GetTempFilePath();

            Assert.Throws <ArgumentException>(() => { csvReaderWriter.Open(fileName, CSVReaderWriter.Mode.Write | CSVReaderWriter.Mode.Read); });
        }
コード例 #18
0
 public AddressFileProcessor(IMailShot mailShot, CSVReaderWriter reader = null)
 {
     if (mailShot == null)
     {
         throw new ArgumentNullException("mailShot");
     }
     _mailShot = mailShot;
     _reader   = reader;
 }
コード例 #19
0
 public void WhenInReadMode_WritingToAFile_Then_ThrowsAnError()
 {
     using (var readerWriter = new CSVReaderWriter())
     {
         var mode = CSVReaderWriter.Mode.Read;
         readerWriter.Open(_currentDirectory + _csvWriteTestData, mode);
         Assert.Throws <ArgumentNullException>(() => readerWriter.Write());
     }
 }
コード例 #20
0
        public async Task ReadAsync_Empty_Returns_Empty_Enumeration()
        {
            using (var csv = new CSVReaderWriter())
            {
                csv.Open(EmptyFile, CSVReaderWriter.Mode.Read);

                Assert.That(await csv.ReadAsync(), Is.Empty);
            }
        }
コード例 #21
0
        public void WhenInValidMode_Then_ThrowsException()
        {
            //declare both read and write mode
            var mode = CSVReaderWriter.Mode.Read | CSVReaderWriter.Mode.Write;

            using (var readerWriter = new CSVReaderWriter())
            {
                Assert.Throws <Exception>(() => readerWriter.Open(_currentDirectory + _csvReadTestData, mode));
            }
        }
コード例 #22
0
 public void WhenInWriteMode_ReadingAFile_Then_ThrowsAnError()
 {
     using (var readerWriter = new CSVReaderWriter())
     {
         var    mode = CSVReaderWriter.Mode.Write;
         string col1, col2;
         readerWriter.Open(_currentDirectory + _csvReadTestData, mode);
         Assert.Throws <ArgumentNullException>(() => readerWriter.Read(out col1, out col2));
     }
 }
コード例 #23
0
        public void Should_throw_on_write_columns_with_empty()
        {
            CSVReaderWriter csvReaderWriter = new CSVReaderWriter();

            string fileName = GetTempFilePath();

            csvReaderWriter.Open(fileName, CSVReaderWriter.Mode.Write);

            Assert.Throws <ArgumentException>(() => { csvReaderWriter.Write("COL 1", "", "Test"); });
        }
コード例 #24
0
        public void Should_throw_on_write_null_columns()
        {
            CSVReaderWriter csvReaderWriter = new CSVReaderWriter();

            string fileName = GetTempFilePath();

            csvReaderWriter.Open(fileName, CSVReaderWriter.Mode.Write);

            Assert.Throws <ArgumentNullException>(() => { csvReaderWriter.Write(null); });
        }
コード例 #25
0
        public void Read_Method_Should_return_false_with_null_ref_input_string_parameters()
        {
            var    csvReaderWriter = new CSVReaderWriter();
            string column1, column2;
            var    result = csvReaderWriter.Read(out column1, out column2);

            Assert.False(result);
            Assert.Null(column1);
            Assert.Null(column2);
        }
コード例 #26
0
        public void DisposeCleansWriter()
        {
            var writerMock = new Mock <ICsvFileWriter>();

            writerMock.Setup(x => x.Close());

            using (var sut = new CSVReaderWriter(null, writerMock.Object)) { }

            writerMock.Verify(x => x.Close(), Times.Once);
        }
コード例 #27
0
        public void Write_WriterIsDisposed_ThrowException()
        {
            var csv = new CSVReaderWriter(It.IsAny <ICSVReader>(), null);

            csv.Close();

            var ex = Assert.Throws <NullReferenceException>(() => csv.Write(It.IsAny <string[]>()));

            Assert.AreEqual("The file writer is closed. Please reopen it.", ex.Message);
        }
コード例 #28
0
        public void DisposeCleansReader()
        {
            var readerMock = new Mock <ICsvFileReader>();

            readerMock.Setup(x => x.Close());

            using (var sut = new CSVReaderWriter(readerMock.Object, null)) { }

            readerMock.Verify(x => x.Close(), Times.Once);
        }
コード例 #29
0
        public void Should_send_mail_using_mailshot_service()
        {
            var stream          = new MemoryStream(Encoding.UTF8.GetBytes(LineValue));
            var cSVReaderWriter = new CSVReaderWriter(stream);

            var processor = new AddressFileProcessor(_fakeMailShotService, cSVReaderWriter);

            processor.Process(TestInputFile);

            Assert.That(_fakeMailShotService.Counter, Is.EqualTo(1));
        }
コード例 #30
0
        public void Read_With_Out_Parameters_When_ReaderStream_Initialized_Reads_Data()
        {
            string column1       = null;
            string column2       = null;
            var    csvReadWriter = new CSVReaderWriter();

            csvReadWriter.Open(@"test_data\contacts.csv", CSVReaderWriter.Mode.Read);
            csvReadWriter.Read(out column1, out column2);
            Assert.NotNull(column1);
            Assert.NotNull(column2);
        }