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)); }
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); }
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")); }
public void SetUp() { _csvStreamReader = new Mock <IReadStream>(); _csvReader = new CsvReader(_csvStreamReader.Object); _csvWriter = new Mock <IWriteCsv>(); _csvReaderWriter = new CSVReaderWriter(_csvReader, _csvWriter.Object); }
public void canOpenAndCloseFile() { CSVReaderWriter rw = new CSVReaderWriter(); rw.Open(TestInputFile, Mode.Read); rw.Close(); }
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); }
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 } }
private void Harness(Action <CSVReaderWriter> callback) { using (var csv = new CSVReaderWriter()) { callback(csv); } }
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)); }
public void WhenInReadMode_WithNonExistingFileOrInvalidFilePath_Then_ThrowsFileNotFoundException() { using (var readerWriter = new CSVReaderWriter()) { var mode = CSVReaderWriter.Mode.Read; Assert.Throws <FileNotFoundException>(() => readerWriter.Open(_currentDirectory + @"\sss", mode)); } }
public void WhenInWriteMode_WithNonExistingFile_Then_NewFileIsCreated() { using (var readerWriter = new CSVReaderWriter()) { var mode = CSVReaderWriter.Mode.Write; Assert.DoesNotThrow(() => readerWriter.Open(_currentDirectory + _nonExistingFile, mode)); } }
public void ReadNotOpenedFile() { using (var reader = new CSVReaderWriter()) { string column1, column2; Assert.Throws <CSVFileNotOpenException>(() => reader.Read(out column1, out column2)); } }
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(); }
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)); }
public void Setup() { _fileOpener = A.Fake <IOpenFile>(); _fileReader = A.Fake <IReadFile>(); _fileWriter = A.Fake <IWriteFile>(); _csvReaderWriter = new CSVReaderWriter(_fileOpener, _fileReader, _fileWriter); }
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(); }
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); }); }
public AddressFileProcessor(IMailShot mailShot, CSVReaderWriter reader = null) { if (mailShot == null) { throw new ArgumentNullException("mailShot"); } _mailShot = mailShot; _reader = reader; }
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()); } }
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); } }
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)); } }
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)); } }
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"); }); }
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); }); }
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); }
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); }
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); }
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); }
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)); }
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); }