コード例 #1
0
ファイル: ReaderTests.cs プロジェクト: ssharunas/LibCSV4Net
 public void Reader_EmptyStream_EmptyList()
 {
     using (Dialect dialect = new Dialect())
     {
         ReadTest("", new List<IList<object>>(), dialect);
     }
 }
コード例 #2
0
ファイル: ReaderTests.cs プロジェクト: emmorts/LibCSV4Net
 public void Next_EmptyStream_ReturnsEmptyList()
 {
     using (var dialect = new Dialect())
     {
         ReadTest("", new List<IList<object>>(), dialect);
     }
 }
コード例 #3
0
ファイル: ReaderTests.cs プロジェクト: emmorts/LibCSV4Net
        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);
            }
        }
コード例 #4
0
        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
ファイル: CSVWriter.cs プロジェクト: emmorts/LibCSV4Net
        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
ファイル: ReaderTests.cs プロジェクト: ssharunas/LibCSV4Net
        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
ファイル: AdapterTests.cs プロジェクト: ssharunas/LibCSV4Net
        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
ファイル: ReaderTests.cs プロジェクト: ssharunas/LibCSV4Net
        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
ファイル: CSVReader.cs プロジェクト: dkucinskas/LibCSV4Net
        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
ファイル: CSVWriter.cs プロジェクト: ssharunas/LibCSV4Net
        public CSVWriter(Dialect dialect, TextWriter writer)
        {
            if (dialect != null)
                _dialect = dialect;

            if (writer != null)
                _writer = writer;
        }
コード例 #11
0
ファイル: CSVAdapter.cs プロジェクト: emmorts/LibCSV4Net
        public CSVAdapter(Dialect dialect, TextWriter writer, string[] headers)
        {
            _dialect = dialect;
            _writer = writer;
            _headers = headers;

            CheckHeaders();
        }
コード例 #12
0
ファイル: WriterTests.cs プロジェクト: dkucinskas/LibCSV4Net
 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
ファイル: WriterTests.cs プロジェクト: dkucinskas/LibCSV4Net
 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
ファイル: CSVReader.cs プロジェクト: ssharunas/LibCSV4Net
        public CSVReader(Dialect dialect, TextReader reader)
        {
            if (dialect != null)
                _dialect = dialect;

            GrowBuffer();

            if (reader != null)
                _reader = reader;
        }
コード例 #15
0
ファイル: AdapterTests.cs プロジェクト: emmorts/LibCSV4Net
        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
ファイル: ReaderTests.cs プロジェクト: emmorts/LibCSV4Net
 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
ファイル: ReaderTests.cs プロジェクト: emmorts/LibCSV4Net
        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
ファイル: WriterTests.cs プロジェクト: dkucinskas/LibCSV4Net
 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
ファイル: CSVWriter.cs プロジェクト: emmorts/LibCSV4Net
        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
ファイル: CSVWriter.cs プロジェクト: ssharunas/LibCSV4Net
        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
ファイル: CSVReader.cs プロジェクト: ssharunas/LibCSV4Net
        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
ファイル: CSVReader.cs プロジェクト: dkucinskas/LibCSV4Net
        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
ファイル: ReaderTests.cs プロジェクト: ssharunas/LibCSV4Net
        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
ファイル: AdapterTests.cs プロジェクト: emmorts/LibCSV4Net
        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
ファイル: AdapterTests.cs プロジェクト: emmorts/LibCSV4Net
        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
ファイル: AdapterTests.cs プロジェクト: emmorts/LibCSV4Net
        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
ファイル: ReaderBaseTests.cs プロジェクト: emmorts/LibCSV4Net
        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
ファイル: AdapterTests.cs プロジェクト: ssharunas/LibCSV4Net
        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
ファイル: AdapterTests.cs プロジェクト: emmorts/LibCSV4Net
        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
ファイル: AdapterTests.cs プロジェクト: emmorts/LibCSV4Net
        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);
                }
            }
        }