예제 #1
0
        public void Read_ExistsEmptyString_ReadEmptyString()
        {
            // Arrange
            const string INPUT_STR = "123\n\n456";

            using (var stream = INPUT_STR.ToStream())
            {
                // Act
                using (var reader = new CsvReader(stream, DELIMETER_AS_SPACE))
                {
                    // Assert
                    string[] values = null;
                    values = reader.Read();
                    Assert.IsNotNull(values);
                    Assert.AreEqual(1, values.Count());
                    Assert.AreEqual("123", values[0]);

                    values = reader.Read();
                    Assert.IsNotNull(values);
                    Assert.AreEqual(1, values.Count());
                    Assert.AreEqual(string.Empty, values[0]);

                    values = reader.Read();
                    Assert.IsNotNull(values);
                    Assert.AreEqual(1, values.Count());
                    Assert.AreEqual("456", values[0]);

                    reader.Close();
                }
            }
        }
예제 #2
0
        public void Read_ReaderDisposed_ThrowException()
        {
            // Arrange
            var str = "123 4567\n89 10";

            using (var inputStream = str.ToStream())
            {
                // Act
                var inputReader = new CsvReader(inputStream);

                inputReader.Read();
                inputReader.Close();
                inputReader.Read();
            }
        }
예제 #3
0
        protected void Open_Internal(string fileName, Mode mode, char delimiter, bool append, Encoding encoding)
        {
            fileName.ThrowIfNullOrEmpty("fileName");

            switch (mode)
            {
                case Mode.Read:
                {
                    if (ReaderStream != null)
                        ReaderStream.Close();

                    ReaderStream = new CsvReader(fileName, delimiter, encoding);
                    break;
                }
                case Mode.Write:
                {
                    if (WriterStream != null)
                        WriterStream.Close();

                    WriterStream = new CsvWriter(fileName, delimiter, append, encoding);
                    break;
                }
                default:
                    throw new Exception(String.Format("Unknown mode {0}", mode));
            }
        }
예제 #4
0
        public void Read_RealFileUTF8_Success()
        {
            // Arrange
            var filePath = Path.Combine(_pathToTestDir, "quotesUTF8.txt");

            if (File.Exists(filePath))
                File.Delete(filePath);

            using (var sw = new StreamWriter(File.Open(filePath, FileMode.Create), Encoding.UTF8))
            {
                sw.WriteLine("è €\nè2 €2");
            }

            using (var reader = new CsvReader(filePath, DELIMETER_AS_SPACE))
            {
                // Act
                var values = reader.Read();

                // Assert
                Assert.IsNotNull(values);
                Assert.AreEqual(2, values.Count());
                Assert.AreEqual("è", values[0]);
                Assert.AreEqual("€", values[1]);

                // Act
                values = reader.Read();

                // Assert
                Assert.IsNotNull(values);
                Assert.AreEqual(2, values.Count());
                Assert.AreEqual("è2", values[0]);
                Assert.AreEqual("€2", values[1]);
            }

            if (File.Exists(filePath))
                File.Delete(filePath);
        }
예제 #5
0
        public void Read_TwoStringsWithSemicolonDelimeter_Success()
        {
            // Arrange
            const char DELIMETER_AS_SEMICOLON = ';';
            var str = "123;4567\nabc;grs";

            using (var stream = str.ToStream())
            {
                // Act
                using (var reader = new CsvReader(stream, DELIMETER_AS_SEMICOLON))
                {
                    // Assert
                    string[] values = null;
                    values = reader.Read();
                    Assert.IsNotNull(values);
                    Assert.AreEqual(2, values.Count());
                    Assert.AreEqual("123", values[0]);
                    Assert.AreEqual("4567", values[1]);

                    values = reader.Read();
                    Assert.IsNotNull(values);
                    Assert.AreEqual(2, values.Count());
                    Assert.AreEqual("abc", values[0]);
                    Assert.AreEqual("grs", values[1]);

                    values = reader.Read();
                    Assert.IsNull(values);

                    reader.Close();
                }
            }
        }
예제 #6
0
        public void Read_TwoStringsAndTwoFields_Success()
        {
            // Arrange
            var str = "123 4567\nabc grs";

            using (var stream = str.ToStream())
            {
                // Act
                using (var reader = new CsvReader(stream, ' '))
                {
                    // Assert
                    string[] values = null;
                    values = reader.Read();
                    Assert.IsNotNull(values);
                    Assert.AreEqual(2, values.Count());
                    Assert.AreEqual("123", values[0]);
                    Assert.AreEqual("4567", values[1]);

                    values = reader.Read();
                    Assert.IsNotNull(values);
                    Assert.AreEqual(2, values.Count());
                    Assert.AreEqual("abc", values[0]);
                    Assert.AreEqual("grs", values[1]);

                    values = reader.Read();
                    Assert.IsNull(values);

                    reader.Close();
                }
            }
        }
예제 #7
0
        public void Read_StringWithTwoFilledFieldsAndOneEmptyField_Success()
        {
            // Arrange
            const string INPUT_STR = "123  45";

            using (var inputStream = INPUT_STR.ToStream())
            {
                // Act
                using (var inputReader = new CsvReader(inputStream, DELIMETER_AS_SPACE))
                {
                    // Assert
                    string[] values = null;
                    values = inputReader.Read();
                    Assert.IsNotNull(values);
                    Assert.AreEqual(3, values.Count());
                    Assert.AreEqual("123", values[0]);
                    Assert.AreEqual("", values[1]);
                    Assert.AreEqual("45", values[2]);
                }
            }
        }
예제 #8
0
        public void Read_RealFile_Success()
        {
            // Arrange
            var filePath = Path.Combine(_pathToTestDir, "quotes.txt");
            using (var reader = new CsvReader(filePath))
            {
                var lineCounter = 0;

                // Act
                while (reader.Read() != null)
                    lineCounter++;

                // Assert
                var LINES_IN_SAMPLE_FILE = 1000;
                Assert.AreEqual(LINES_IN_SAMPLE_FILE, lineCounter);
            }
        }