示例#1
0
 public void Reader_EmptyStream_EmptyList()
 {
     using (Dialect dialect = new Dialect())
     {
         ReadTest("", new List<IList<object>>(), dialect);
     }
 }
示例#2
0
 public void Next_EmptyStream_ReturnsEmptyList()
 {
     using (var dialect = new Dialect())
     {
         ReadTest("", new List<IList<object>>(), dialect);
     }
 }
示例#3
0
        public void Next_EndOfLine_LineIsRead()
        {
            using (var dialect = new Dialect())
            {
                ReadTest("a,b", new List<IList<object>> { new List<object> { "a", "b" } }, dialect);
                ReadTest("c,d\n", new List<IList<object>> { new List<object> { "c", "d" } }, dialect);
                ReadTest("e,f\r", new List<IList<object>> { new List<object> { "e", "f" } }, dialect);
                ReadTest("h,g\r\n", new List<IList<object>> { new List<object> { "h", "g" } }, dialect);

                ReadTest(
                    "a1,b1\nc1,d1",
                    new List<IList<object>>
                    {
                        new List<object> { "a1", "b1" },
                        new List<object> {"c1", "d1"}
                    },
                    dialect);

                ReadTest(
                    "a1,b1\rc1,d1",
                    new List<IList<object>>
                    {
                        new List<object> { "a1", "b1" },
                        new List<object> {"c1", "d1"}
                    },
                    dialect);

                ReadTest("\n\r", new List<IList<object>> { }, dialect);
            }
        }
        public byte[] ToCsvArray()
        {
            var dialect = new Dialect();
            byte[] result;

            using (var memoryStream = new MemoryStream())
            {
                using (var textWriter = new StreamWriter(memoryStream))
                using (var csvWriter = new CSVWriter(dialect, textWriter))
                {
                    csvWriter.WriteRow(new object[] { "Team", "Player", "Eligible?", "Draft Round", "Auction Cost" });

                    foreach (var keeper in _keepers)
                    {
                        csvWriter.WriteRow(new object[]
                        {
                            keeper.TeamName,
                            keeper.PlayerName,
                            keeper.IsEligible ? "Yes" : "No",
                            keeper.DraftRound,
                            keeper.Cost
                        });
                    }
                }

                result = memoryStream.ToArray();
            }

            return result;
        }
示例#5
0
        public CSVWriter(Dialect dialect, string filename, string encoding)
        {
            if (dialect == null)
            {
                throw new DialectIsNullException("Set dialect first!");
            }
            dialect.Check();
            _dialect = dialect;

            if (_writer == null)
            {
                if (string.IsNullOrEmpty(filename) || filename.Trim().Length < 1)
                {
                    throw new FileNameIsNullOrEmptyException();
                }

                if (!File.Exists(filename))
                {
                    throw new CannotWriteToFileException(string.Format("Can't write to file: '{0}', file not exists!", filename));
                }

                _ownsWriter = true;
                try
                {
                    _writer = new StreamWriter(filename, false, Encoding.GetEncoding(encoding));
                }
                catch(Exception exp)
                {
                    throw new CannotWriteToFileException(string.Format("Can't write to file: '{0}'!", filename), exp);
                }
            }
        }
示例#6
0
        public void Reader_Escape_Ok()
        {
            using (Dialect dialect = new Dialect(true, ',', '"', '\\', false, "\r\n", QuoteStyle.QUOTE_MINIMAL, false, false))
            {
                ReadTest("a,\\b,c", new List<IList<object>>() { new List<object>() { "a", "b", "c" } }, dialect);
            }

            using (Dialect dialect = new Dialect(true, ',', '"', '\\', false, "\r\n", QuoteStyle.QUOTE_MINIMAL, false, false))
            {
                ReadTest("a,b\\,c", new List<IList<object>>() { new List<object>() { "a", "b,c" } }, dialect);
            }

            using (Dialect dialect = new Dialect(true, ',', '"', '\\', false, "\r\n", QuoteStyle.QUOTE_MINIMAL, false, false))
            {
                ReadTest("a,\"b\\,c\"", new List<IList<object>>() { new List<object>() { "a", "b,c" } }, dialect);
            }

            using (Dialect dialect = new Dialect(true, ',', '"', '\\', false, "\r\n", QuoteStyle.QUOTE_MINIMAL, false, false))
            {
                ReadTest("a,\"b,\\c\"", new List<IList<object>>() { new List<object>() { "a", "b,c" } }, dialect);
            }

            using (Dialect dialect = new Dialect(true, ',', '"', '\\', false, "\r\n", QuoteStyle.QUOTE_MINIMAL, false, false))
            {
                ReadTest("a,\"b,c\\\"\"", new List<IList<object>>() { new List<object>() { "a", "b,c\"" } }, dialect);
            }

            using (Dialect dialect = new Dialect(true, ',', '"', '\\', false, "\r\n", QuoteStyle.QUOTE_MINIMAL, false, false))
            {
                ReadTest("a,\"b,c\"\\", new List<IList<object>>() { new List<object>() { "a", "b,c\\" } }, dialect);
            }
        }
示例#7
0
        public void ReadAll_InputWithHeader_Ok()
        {
            string input = "Header#1;Header#2;Header#3\r\n1;2;3\r\n4;5;6";

            IDataTransformer transformer = new NullTransformerForAdapterTesting(
            new string[]
            {
                "Header#1",
                "Header#2",
                "Header#3"
            },
            new ArrayList()
            {
                new string[] {"1", "2", "3"},
                new string[] {"4", "5", "6"},
            });

            using (Dialect dialect = new Dialect(true, ';', '"', '\\', true, "\r\n", QuoteStyle.QUOTE_NONE, true, true))
            {
                using (CSVAdapter adapter = new CSVAdapter(dialect, new StringReader(input)))
                {
                    adapter.ReadAll(transformer);
                }
            }
        }
示例#8
0
        public void Reader_EOL_Ok()
        {
            using (Dialect dialect = new Dialect())
            {
                ReadTest("a,b", new List<IList<object>>() { new List<object>() { "a", "b" } }, dialect);
                ReadTest("c,d\n", new List<IList<object>>() { new List<object>() { "c", "d" } }, dialect);
                ReadTest("e,f\r", new List<IList<object>>() { new List<object>() { "e", "f" } }, dialect);
                ReadTest("h,g\r\n", new List<IList<object>>() { new List<object>() { "h", "g" } }, dialect);

                ReadTest(
                    "a1,b1\nc1,d1",
                    new List<IList<object>>()
                    {
                        new List<object>() { "a1", "b1" },
                        new List<object>() {"c1", "d1"}
                    },
                    dialect);

                ReadTest(
                    "a1,b1\rc1,d1",
                    new List<IList<object>>()
                    {
                        new List<object>() { "a1", "b1" },
                        new List<object>() {"c1", "d1"}
                    },
                    dialect);
            }
        }
示例#9
0
        public CSVReader(Dialect dialect, string filename, string encoding)
        {
            if (dialect == null)
            {
                throw new DialectIsNullException();
            }
            _dialect = dialect;

            GrowBuffer();

            if (_reader == null)
            {
                if (string.IsNullOrEmpty(filename) || filename.Trim().Length < 1)
                {
                    throw new FileNameIsNullOrEmptyException();
                }

                if (!File.Exists(filename))
                {
                    throw new CannotReadFromFileException(string.Format("Can't read from file: '{0}', file not exists!", filename));
                }

                _ownsReader = true;
                try
                {
                    _reader = new StreamReader(filename, Encoding.GetEncoding(encoding));
                }
                catch(Exception exp)
                {
                    throw new CannotReadFromFileException(string.Format("Can't read from file: '{0}'!", filename), exp);
                }
            }

            InitializeHeaders();
        }
示例#10
0
        public CSVWriter(Dialect dialect, TextWriter writer)
        {
            if (dialect != null)
                _dialect = dialect;

            if (writer != null)
                _writer = writer;
        }
示例#11
0
        public CSVAdapter(Dialect dialect, TextWriter writer, string[] headers)
        {
            _dialect = dialect;
            _writer = writer;
            _headers = headers;

            CheckHeaders();
        }
示例#12
0
 public void Constructor_FileNameIsNullOrEmpty_ThrowsFileNameIsNullOrEmptyException(string fileName)
 {
     using (var dialect = new Dialect(true, ';', '\"', '\\', true, "\r\n", QuoteStyle.QuoteMinimal, false, false))
     {
         using (var writer = new CSVWriter(dialect, fileName, "UTF-8"))
         {
         }
     }
 }
示例#13
0
 public void Constructor_FileNotExists_ThrowsCannotWriteToFileException()
 {
     using (var dialect = new Dialect(true, ';', '\"', '\\', true, "\r\n", QuoteStyle.QuoteMinimal, false, false))
     {
         using (var writer = new CSVWriter(dialect, "VeryLongNonExistingFileNameForTesting", "UTF-8"))
         {
         }
     }
 }
示例#14
0
        public CSVReader(Dialect dialect, TextReader reader)
        {
            if (dialect != null)
                _dialect = dialect;

            GrowBuffer();

            if (reader != null)
                _reader = reader;
        }
示例#15
0
        public void Adapter_HeaderIsNull_ThrowsException()
        {
            IDataTransformer transformer = new NullTransformerForAdapterTesting(new string[] {}, new string[] {});

            using (var dialect = new Dialect(true, ';', '"', '\\', true, "\r\n", QuoteStyle.QuoteNone, true, true))
            {
                using (var adapter = new CSVAdapter(dialect, new StringWriter(), null))
                {
                }
            }
        }
示例#16
0
 public void Next_EmptyLine_NextReturnsFalse()
 {
     string input = "Header#1;Header#2;Header#3\r\n1;2;3\r\n \r\n";
     using (var dialect = new Dialect(true, ';', '"', '\0', false, "\r\n", QuoteStyle.QuoteMinimal, false, true))
     {
         using (var reader = new CSVReader(dialect, new StringReader(input)))
         {
             Assert.IsTrue(reader.Next());
             Assert.IsFalse(reader.Next());
         }
     }
 }
示例#17
0
        public void Next_EscapedSequecesRead_SequencesRead()
        {
            using (var dialect = new Dialect(true, ',', '"', '\0', false, "\r\n", QuoteStyle.QuoteMinimal, false, false))
            {
                ReadTest(" a, b, c", new List<IList<object>> { new List<object> { " a", " b", " c" } }, dialect);
            }

            using (var dialect = new Dialect(true, ',', '"', '\\', false, "\r\n", QuoteStyle.QuoteMinimal, false, false))
            {
                ReadTest("a,\"b,c\\n\\t\\r\"\\", new List<IList<object>> { new List<object> { "a", "b,c\n\t\r\\" } }, dialect);
            }
        }
示例#18
0
 public void Constructor_FileIsLocked_ThrowsCannotWriteToFileException()
 {
     using (var writer = new StreamWriter("test_write_file_locked.csv", false, Encoding.GetEncoding("utf-8")))
     {
         using (var dialect = new Dialect(true, ';', '\"', '\\', true, "\r\n", QuoteStyle.QuoteMinimal, false, false))
         {
             using (var csvWriter = new CSVWriter(dialect, "test_write_file_locked.csv", "UTF-8"))
             {
             }
         }
     }
 }
示例#19
0
        public CSVWriter(Dialect dialect, TextWriter writer)
        {
            if (dialect == null)
            {
                throw new DialectIsNullException("Set dialect first!");
            }
            dialect.Check();
            _dialect = dialect;

            if (writer != null)
            {
                _writer = writer;
            }
        }
示例#20
0
        public CSVWriter(Dialect dialect, string filename, string encoding)
        {
            if (dialect != null)
                _dialect = dialect;

            if (_writer == null)
            {
                if (filename.Trim().Length > 0)
                    if (!File.Exists(filename))
                        throw new WriterException(string.Format("Can't write to file: '{0}', file not exists!", filename));

                _writer = new StreamWriter(filename, false, Encoding.GetEncoding(encoding));
            }
        }
示例#21
0
        public CSVReader(Dialect dialect, string filename, string encoding)
        {
            if (dialect != null)
                _dialect = dialect;

            GrowBuffer();

            if (_reader == null)
            {
                if (!File.Exists(filename))
                    throw new ReaderException(string.Format("Can't read from file: '{0}', file not exists!", filename));

                _reader = new StreamReader(filename, Encoding.GetEncoding(encoding));
            }
        }
示例#22
0
        public CSVReader(Dialect dialect, TextReader reader)
        {
            if (dialect == null)
            {
                throw new DialectIsNullException();
            }
            _dialect = dialect;

            GrowBuffer();

            if (reader == null)
            {
                throw new TextReaderIsNullException();
            }
            _reader = reader;

            InitializeHeaders();
        }
示例#23
0
        public void Reader_DialectInternalError_ThrowException()
        {
            IList<IList<object>> results = new List<IList<object>>();

            using (Dialect dialect = new Dialect(true, '\0', '"', '\\', true, "\r\n", QuoteStyle.QUOTE_MINIMAL, true, false))
            {
                using (CSVReader reader = new CSVReader(dialect, new StringReader("1,2,3")))
                {
                    while (reader.NextRecord())
                    {
                        string[] record = reader.GetCurrentRecord();
                        if (record != null && record.Length > 0)
                            results.Add(record);
                        record = null;
                    }
                }
            }
        }
示例#24
0
        public void ReadAll_ExistingFileName_ReturnsRecords()
        {
            IDataTransformer transformer = new NullTransformerForAdapterTesting(
                new[] { "Header#1", "Header#2", "Header#3" },
                new[]
                {
                    new[] {"1", "2", "3"},
                    new[] {"4", "5", "6"}
                });

            using (var dialect = new Dialect(true, ';', '"', '\\', true, "\r\n", QuoteStyle.QuoteNone, true, true))
            {
                using (var adapter = new CSVAdapter(dialect, "test.csv", "utf-8"))
                {
                    adapter.ReadAll(transformer);
                }
            }
        }
示例#25
0
        public void ReadAll_WithoutHeaders_ReturnRecords()
        {
            const string input = "1;2;3\r\n4;5;6";
            IDataTransformer transformer = new NullTransformerForAdapterTesting(
                null,
                new[]
                {
                    new[] {"1", "2", "3"},
                    new[] {"4", "5", "6"}
                });

            using (var dialect = new Dialect(true, ';', '"', '\\', true, "\r\n", QuoteStyle.QuoteNone, true, false))
            {
                using (var adapter = new CSVAdapter(dialect, new StringReader(input)))
                {
                    adapter.ReadAll(transformer);
                }
            }
        }
示例#26
0
        public void ReadAll_ExistingStream_ReturnsRecords()
        {
            const string input = "Header#1;Header#2;Header#3\r\n1;2;3\r\n4;5;6";

            IDataTransformer transformer = new NullTransformerForAdapterTesting(
                new[] { "Header#1", "Header#2", "Header#3" },
                new[]
                {
                    new[] {"1", "2", "3"},
                    new[] {"4", "5", "6"}
                });

            using (var dialect = new Dialect(true, ';', '"', '\\', true, "\r\n", QuoteStyle.QuoteNone, true, true))
            {
                using (var adapter = new CSVAdapter(dialect, new StringReader(input)))
                {
                    adapter.ReadAll(transformer);
                }
            }
        }
示例#27
0
        public static void ReadTest(string input, IList<IList<object>> expect, Dialect dialect)
        {
            IList<IList<object>> results = new List<IList<object>>();
            using (var reader = new CSVReader(dialect, new StringReader(input)))
            {
                while (reader.Next())
                {
                    var record = reader.Current;
                    if (record != null && record.Length > 0)
                    {
                        results.Add (record);
                    }
                    record = null;
                }
            }

            Assert.AreEqual(expect, results);

            DisposeIListOfIListOfObjects(results);
            results = null;

            DisposeIListOfIListOfObjects(expect);
            expect = null;
        }
示例#28
0
        public void WriteAll_OutputWithHeader_Ok()
        {
            string[] header = new string[]
            {
                "Header#1",
                "Header#2",
                "Header#3"
            };

            ArrayList data = new ArrayList()
            {
                header,
                new string[] {"1", "2", "3"},
                new string[] {"4", "5", "6"},
            };

            IDataTransformer transformer = new NullTransformerForAdapterTesting(
            header,
            data);

            using (Dialect dialect = new Dialect(true, ';', '"', '\\', true, "\r\n", QuoteStyle.QUOTE_NONE, true, true))
            {
                using (CSVAdapter adapter = new CSVAdapter(dialect, new StringWriter()))
                {
                    adapter.WriteAll(data, transformer);
                }
            }
        }
示例#29
0
        public void WriteAll_DataTransformerIsNull_ThrowsException()
        {
            var headers = new[]
            {
                "Header#1",
                "Header#2",
                "Header#3"
            };

            var data = new[]
            {
                new[] {"1", "2", "3"},
                new[] {"4", "5", "6"}
            };

            using (var dialect = new Dialect(true, ';', '"', '\\', true, "\r\n", QuoteStyle.QuoteNone, true, false))
            {
                using (var adapter = new CSVAdapter(dialect, new StringWriter(), headers))
                {
                    adapter.WriteAll(data, null);
                }
            }
        }
示例#30
0
        public void WriteAll_WithoutHeaders_WroteRecords()
        {
            var data = new[]
            {
                new[] {"1", "2", "3"},
                new[] {"4", "5", "6"}
            };
            IDataTransformer transformer = new NullTransformerForAdapterTesting(
                null, data);

            using (var dialect = new Dialect(true, ';', '"', '\\', true, "\r\n", QuoteStyle.QuoteNone, true, false))
            {
                using (var adapter = new CSVAdapter(dialect, new StringWriter()))
                {
                    adapter.WriteAll(data, transformer);
                }
            }
        }