public void write_csv_writes_all_public_properties_by_default()
        {
            using (var stringWriter = new StringWriter())
            using (var writer = new CsvWriter(stringWriter))
            {
                writer.NewLine = "<EOL>";

                var items = new TestType1[]
                {
                    new TestType1
                    {
                        Property1 = "1",
                        Property2 = "2",
                        Property3 = "3"
                    },
                    new TestType1
                    {
                        Property1 = "4",
                        Property2 = "5",
                        Property3 = "6"
                    }
                };

                items.WriteCsv(writer);
                var result = stringWriter.ToString();

                // can't assert exact contents because order of properties is undefined (and changes)
                Assert.Contains("Property1", result);
                Assert.Contains("Property2", result);
                Assert.Contains("1", result);
                Assert.Contains("2", result);
                Assert.Contains("4", result);
                Assert.Contains("5", result);
            }
        }
        public void write_csv_writes_header_record_by_default()
        {
            using (var stringWriter = new StringWriter())
            using (var writer = new CsvWriter(stringWriter))
            {
                writer.NewLine = "<EOL>";

                new TestType1[0].WriteCsv(writer);
                var result = stringWriter.ToString();

                // can't assert exact contents because order of properties is undefined (and changes)
                Assert.Contains("Property1", result);
                Assert.Contains("Property2", result);
            }
        }
        /// <summary>
        /// Copies all remaining records in <paramref name="this"/> to <paramref name="destination"/>.
        /// </summary>
        /// <param name="this">
        /// The data source.
        /// </param>
        /// <param name="destination">
        /// The data destination.
        /// </param>
        /// <returns>
        /// The number of records written to <paramref name="destination"/>.
        /// </returns>
        public static int CopyTo(this CsvReader @this, CsvWriter destination)
        {
            @this.AssertNotNull("@this");
            destination.AssertNotNull("destination");

            var num = 0;
            var buffer = new DataRecord[16];
            var read = 0;

            while ((read = @this.ReadDataRecords(buffer, 0, buffer.Length)) != 0)
            {
                destination.WriteRecords(buffer, 0, read);
                num += read;
            }

            return num;
        }
        /// <summary>
        /// Asynchronously copies all remaining records in <paramref name="this"/> to <paramref name="destination"/>.
        /// </summary>
        /// <param name="this">
        /// The data source.
        /// </param>
        /// <param name="destination">
        /// The data destination.
        /// </param>
        /// <returns>
        /// The number of records written to <paramref name="destination"/>.
        /// </returns>
        public async static Task<int> CopyToAsync(this CsvReader @this, CsvWriter destination)
        {
            @this.AssertNotNull("@this");
            destination.AssertNotNull("destination");

            var num = 0;
            var buffer = new DataRecord[16];
            var read = 0;

            while ((read = await @this.ReadDataRecordsAsync(buffer, 0, buffer.Length).ConfigureAwait(false)) != 0)
            {
                await destination.WriteRecordsAsync(buffer, 0, read).ConfigureAwait(false);
                num += read;
            }

            return num;
        }
        public async Task write_csv_async_returns_number_of_records_written()
        {
            using (var writer = new CsvWriter(new StringWriter()))
            {
                writer.WriteRecord("some", "record");

                var dataTable = new DataTable();
                dataTable.Columns.Add("First");
                dataTable.Columns.Add("Second");
                dataTable.Rows.Add("1", "2");
                dataTable.Rows.Add("3", "4");
                dataTable.Rows.Add("5", "6");

                Assert.Equal(3, await dataTable.WriteCsvAsync(writer, false));
                Assert.Equal(3, await dataTable.WriteCsvAsync(writer, true));
            }
        }
        public async Task write_csv_async_uses_object_to_string_converter_to_convert_objects_in_data_row_to_string()
        {
            using (var stringWriter = new StringWriter())
            using (var writer = new CsvWriter(stringWriter))
            {
                writer.NewLine = "<EOL>";

                var dataTable = new DataTable();
                dataTable.Columns.Add("First");
                dataTable.Columns.Add("Second");
                dataTable.Rows.Add("1", 2);
                dataTable.Rows.Add(3, 4d);
                dataTable.Rows.Add(5m, 6f);

                Assert.Equal(3, await dataTable.WriteCsvAsync(writer, true, null, o => o.ToString() + "_SUFFIX"));
                Assert.Equal("First,Second<EOL>1_SUFFIX,2_SUFFIX<EOL>3_SUFFIX,4_SUFFIX<EOL>5_SUFFIX,6_SUFFIX<EOL>", stringWriter.ToString());
            }
        }
        public async Task write_csv_async_writes_all_rows_as_data_records()
        {
            using (var stringWriter = new StringWriter())
            using (var writer = new CsvWriter(stringWriter))
            {
                writer.NewLine = "<EOL>";

                var dataTable = new DataTable();
                dataTable.Columns.Add("First");
                dataTable.Columns.Add("Second");
                dataTable.Rows.Add("1", "2");
                dataTable.Rows.Add("3", "4");
                dataTable.Rows.Add("5", "6");

                Assert.Equal(3, await dataTable.WriteCsvAsync(writer, true));
                Assert.Equal("First,Second<EOL>1,2<EOL>3,4<EOL>5,6<EOL>", stringWriter.ToString());
            }
        }
 public async Task write_csv_async_throws_if_csv_reader_is_disposed()
 {
     var writer = new CsvWriter(new StringWriter());
     writer.Dispose();
     await Assert.ThrowsAsync<ObjectDisposedException>(() => new DataTable().WriteCsvAsync(writer));
 }
 public async Task write_csv_async_throws_if_data_table_is_null()
 {
     using (var writer = new CsvWriter(new StringWriter()))
     {
         await Assert.ThrowsAsync<ArgumentNullException>(() => ((DataTable)null).WriteCsvAsync(writer));
     }
 }
 public void write_csv_throws_if_object_to_string_converter_is_null()
 {
     using (var writer = new CsvWriter(new StringWriter()))
     {
         Assert.Throws<ArgumentNullException>(() => new DataTable().WriteCsv(writer, false, null, null));
     }
 }
Beispiel #11
0
 /// <summary>
 /// Writes all rows in <paramref name="this"/> to <paramref name="csvWriter"/>.
 /// </summary>
 /// <remarks>
 /// <para>
 /// All rows in <paramref name="this"/> will be written to <paramref name="csvWriter"/>.
 /// </para>
 /// </remarks>
 /// <param name="this">
 /// The <see cref="DataTable"/>.
 /// </param>
 /// <param name="csvWriter">
 /// The <see cref="CsvWriter"/>.
 /// </param>
 /// <param name="writeHeaderRecord">
 /// If <see langword="true"/>, a header record will also be written, which will be comprised of the column names defined for <paramref name="this"/>.
 /// </param>
 /// <returns>
 /// The actual number of rows from <paramref name="this"/> written to <paramref name="csvWriter"/>.
 /// </returns>
 public static int WriteCsv(this DataTable @this, CsvWriter csvWriter, bool writeHeaderRecord)
 {
     return(@this.WriteCsv(csvWriter, writeHeaderRecord, null));
 }
 /// <summary>
 /// Writes the items in <paramref name="this"/> to <paramref name="csvWriter"/>.
 /// </summary>
 /// <remarks>
 /// <para>
 /// All public properties of <typeparamref name="T"/> will be written to <paramref name="csvWriter"/> unless they are marked with <see cref="CsvIgnoreAttribute"/>.
 /// </para>
 /// </remarks>
 /// <typeparam name="T">
 /// The type of the items to be written to <paramref name="csvWriter"/>.
 /// </typeparam>
 /// <param name="this">
 /// The items to write.
 /// </param>
 /// <param name="csvWriter">
 /// The <see cref="CsvWriter"/>.
 /// </param>
 /// <param name="writeHeaderRecord">
 /// If <see langword="true"/>, a header record will first be written to <paramref name="csvWriter"/>, which is comprised of all property names.
 /// </param>
 /// <returns>
 /// The number of items written.
 /// </returns>
 public static int WriteCsv <T>(this IEnumerable <T> @this, CsvWriter csvWriter, bool writeHeaderRecord)
 {
     return(@this.WriteCsv(csvWriter, writeHeaderRecord, typeof(T).GetRuntimeProperties().Where(x => x.CanRead && x.GetMethod.IsPublic && x.GetCustomAttribute <CsvIgnoreAttribute>() == null).Select(x => x.Name).ToArray()));
 }
 public async Task write_csv_async_throws_if_any_property_name_is_null()
 {
     var writer = new CsvWriter(new StringWriter());
     var ex = await Assert.ThrowsAsync<ArgumentException>(() => new List<DateTime>().WriteCsvAsync(writer, true, new string[] { "Date", null }));
     Assert.Equal("A property name is null.", ex.Message);
 }
 public async Task write_csv_async_throws_if_property_names_is_null()
 {
     var writer = new CsvWriter(new StringWriter());
     await Assert.ThrowsAsync<ArgumentNullException>(() => new List<DateTime>().WriteCsvAsync(writer, true, null));
 }
 public async Task write_csv_async_throws_if_enumerable_is_null()
 {
     var writer = new CsvWriter(new StringWriter());
     await Assert.ThrowsAsync<ArgumentNullException>(() => ((IEnumerable<DateTime>)null).WriteCsvAsync(writer));
 }
Beispiel #16
0
 /// <summary>
 /// Writes all rows in <paramref name="this"/> to <paramref name="csvWriter"/>.
 /// </summary>
 /// <remarks>
 /// </remarks>
 /// <param name="this">
 /// The <see cref="DataTable"/>.
 /// </param>
 /// <param name="csvWriter">
 /// The <see cref="CsvWriter"/>.
 /// </param>
 /// <param name="writeHeaderRecord">
 /// If <see langword="true"/>, a header record will also be written, which will be comprised of the column names defined for <paramref name="this"/>.
 /// </param>
 /// <param name="maximumRows">
 /// The maximum number of rows from <paramref name="this"/> that should be written to <paramref name="csvWriter"/>.
 /// </param>
 /// <returns>
 /// The actual number of rows from <paramref name="this"/> written to <paramref name="csvWriter"/>.
 /// </returns>
 public static int WriteCsv(this DataTable @this, CsvWriter csvWriter, bool writeHeaderRecord, int?maximumRows)
 {
     return(@this.WriteCsv(csvWriter, writeHeaderRecord, maximumRows, o => o == null ? string.Empty : o.ToString()));
 }
 /// <summary>
 /// Writes the items in <paramref name="this"/> to <paramref name="csvWriter"/>.
 /// </summary>
 /// <remarks>
 /// <para>
 /// All public properties of <typeparamref name="T"/> will be written to <paramref name="csvWriter"/>. A header record will also be written, comprised of the property names.
 /// </para>
 /// </remarks>
 /// <typeparam name="T">
 /// The type of the items to be written to <paramref name="csvWriter"/>.
 /// </typeparam>
 /// <param name="this">
 /// The items to write.
 /// </param>
 /// <param name="csvWriter">
 /// The <see cref="CsvWriter"/>.
 /// </param>
 /// <returns>
 /// The number of items written.
 /// </returns>
 public static int WriteCsv <T>(this IEnumerable <T> @this, CsvWriter csvWriter)
 {
     return(@this.WriteCsv(csvWriter, true));
 }
        public async Task fill_data_table_async_works_with_large_csv_input()
        {
            var csv = string.Empty;

            using (var stringWriter = new StringWriter())
            using (var writer = new CsvWriter(stringWriter))
            {
                writer.WriteRecord("Header1", "Header2");

                for (var i = 0; i < 1000; ++i)
                {
                    writer.WriteRecord("value0_" + i, "value1_" + i);
                }

                writer.Flush();
                csv = stringWriter.ToString();
            }

            // read less than all available records
            using (var reader = CsvReader.FromCsvString(csv))
            {
                var table = new DataTable();
                reader.ReadHeaderRecord();

                Assert.Equal(913, await table.FillAsync(reader, 913));
                Assert.Equal(913, table.Rows.Count);
                Assert.True(reader.HasMoreRecords);
            }

            // read exactly available records
            using (var reader = CsvReader.FromCsvString(csv))
            {
                var table = new DataTable();
                reader.ReadHeaderRecord();

                Assert.Equal(1000, await table.FillAsync(reader, 1000));
                Assert.Equal(1000, table.Rows.Count);
                Assert.False(reader.HasMoreRecords);
            }

            // attempt to read more than available records
            using (var reader = CsvReader.FromCsvString(csv))
            {
                var table = new DataTable();
                reader.ReadHeaderRecord();

                Assert.Equal(1000, await table.FillAsync(reader, 1500));
                Assert.Equal(1000, table.Rows.Count);
                Assert.False(reader.HasMoreRecords);
            }
        }
 public async Task write_csv_async_throws_if_object_to_string_converter_is_null()
 {
     var writer = new CsvWriter(new StringWriter());
     await Assert.ThrowsAsync<ArgumentNullException>(() => new List<DateTime>().WriteCsvAsync(writer, true, new string[] { "Date" }, null));
 }
Beispiel #20
0
 /// <summary>
 /// Writes all rows in <paramref name="this"/> to <paramref name="csvWriter"/>.
 /// </summary>
 /// <remarks>
 /// <para>
 /// All rows in <paramref name="this"/> will be written to <paramref name="csvWriter"/>. A header record will also be written, which will be comprised of the column names defined for
 /// <paramref name="this"/>.
 /// </para>
 /// </remarks>
 /// <param name="this">
 /// The <see cref="DataTable"/>.
 /// </param>
 /// <param name="csvWriter">
 /// The <see cref="CsvWriter"/>.
 /// </param>
 /// <returns>
 /// The actual number of rows from <paramref name="this"/> written to <paramref name="csvWriter"/>.
 /// </returns>
 public static int WriteCsv(this DataTable @this, CsvWriter csvWriter)
 {
     return(@this.WriteCsv(csvWriter, true));
 }
 public void write_csv_throws_if_any_property_cannot_be_resolved()
 {
     var writer = new CsvWriter(new StringWriter());
     var ex = Assert.Throws<InvalidOperationException>(() => new List<DateTime>().WriteCsv(writer, true, new string[] { "Date", "Foo" }));
     Assert.Equal("Property 'Foo' was not found on type 'System.DateTime'.", ex.Message);
 }
        public async Task write_csv_async_writes_only_requested_properties_if_specified()
        {
            using (var stringWriter = new StringWriter())
            using (var writer = new CsvWriter(stringWriter))
            {
                writer.NewLine = "<EOL>";

                var items = new TestType1[]
                {
                    new TestType1
                    {
                        Property1 = "1",
                        Property2 = "2",
                        Property3 = "3"
                    },
                    new TestType1
                    {
                        Property1 = "4",
                        Property2 = "5",
                        Property3 = "6"
                    }
                };

                await items.WriteCsvAsync(writer, true, new string[] { "Property2" });
                Assert.Equal("Property2<EOL>2<EOL>5<EOL>", stringWriter.ToString());
            }
        }
 public void write_csv_throws_if_data_table_is_null()
 {
     using (var writer = new CsvWriter(new StringWriter()))
     {
         Assert.Throws<ArgumentNullException>(() => ((DataTable)null).WriteCsv(writer));
     }
 }
 public void write_csv_throws_if_object_to_string_converter_is_null()
 {
     var writer = new CsvWriter(new StringWriter());
     Assert.Throws<ArgumentNullException>(() => new List<DateTime>().WriteCsv(writer, true, new string[] { "Date" }, null));
 }
 public void write_csv_throws_if_csv_reader_is_disposed()
 {
     var writer = new CsvWriter(new StringWriter());
     writer.Dispose();
     Assert.Throws<ObjectDisposedException>(() => new DataTable().WriteCsv(writer));
 }
        public async Task write_csv_async_writes_converts_null_property_values_to_empty_strings()
        {
            using (var stringWriter = new StringWriter())
            using (var writer = new CsvWriter(stringWriter))
            {
                writer.NewLine = "<EOL>";

                var items = new TestType2[]
                {
                    new TestType2
                    {
                        Property1 = null,
                        Property2 = 2d,
                        Property3 = 3,
                        Property4 = 4m
                    },
                    new TestType2
                    {
                        Property1 = null,
                        Property2 = 6d,
                        Property3 = 7,
                        Property4 = null
                    }
                };

                await items.WriteCsvAsync(writer);
                Assert.Equal("Property1,Property2,Property3,Property4<EOL>,2,3,4<EOL>,6,7,<EOL>", stringWriter.ToString());
            }
        }
 public async Task write_csv_async_throws_if_object_to_string_converter_is_null()
 {
     using (var writer = new CsvWriter(new StringWriter()))
     {
         await Assert.ThrowsAsync<ArgumentNullException>(() => new DataTable().WriteCsvAsync(writer, false, null, null));
     }
 }
        public async Task write_csv_async_uses_object_to_string_converter_if_specified()
        {
            using (var stringWriter = new StringWriter())
            using (var writer = new CsvWriter(stringWriter))
            {
                writer.NewLine = "<EOL>";

                var items = new TestType2[]
                {
                    new TestType2
                    {
                        Property1 = "1",
                        Property2 = 2d,
                        Property3 = 3,
                        Property4 = 4m
                    },
                    new TestType2
                    {
                        Property1 = "5",
                        Property2 = 6d,
                        Property3 = 7,
                        Property4 = 8m
                    }
                };

                await items.WriteCsvAsync(writer, true, new string[] { "Property1", "Property2" }, o => o.ToString() + "_SUFFIX");
                Assert.Equal("Property1,Property2<EOL>1_SUFFIX,2_SUFFIX<EOL>5_SUFFIX,6_SUFFIX<EOL>", stringWriter.ToString());
            }
        }
        public async Task write_csv_async_writes_a_header_record_based_on_data_table_column_names_if_requested()
        {
            using (var stringWriter = new StringWriter())
            using (var writer = new CsvWriter(stringWriter))
            {
                writer.NewLine = "<EOL>";

                var dataTable = new DataTable();
                dataTable.Columns.Add("First");
                dataTable.Columns.Add("Second");

                Assert.Equal(0, await dataTable.WriteCsvAsync(writer, true));
                Assert.Equal("First,Second<EOL>", stringWriter.ToString());
            }
        }
        public async Task write_csv_async_returns_number_of_items_written()
        {
            using (var writer = new CsvWriter(new StringWriter()))
            {
                writer.WriteRecord("some", "record");

                var items = new TestType1[]
                {
                    new TestType1
                    {
                        Property1 = "1",
                        Property2 = "2",
                        Property3 = "3"
                    },
                    new TestType1
                    {
                        Property1 = "4",
                        Property2 = "5",
                        Property3 = "6"
                    }
                };

                Assert.Equal(2, await items.WriteCsvAsync(writer));
                Assert.Equal(2, await items.WriteCsvAsync(writer, false));
            }
        }
        public async Task write_csv_async_stops_if_it_reaches_maximum_rows()
        {
            using (var stringWriter = new StringWriter())
            using (var writer = new CsvWriter(stringWriter))
            {
                writer.NewLine = "<EOL>";

                var dataTable = new DataTable();
                dataTable.Columns.Add("First");
                dataTable.Columns.Add("Second");
                dataTable.Rows.Add("1", "2");
                dataTable.Rows.Add("3", "4");
                dataTable.Rows.Add("5", "6");

                Assert.Equal(2, await dataTable.WriteCsvAsync(writer, true, 2));
                Assert.Equal("First,Second<EOL>1,2<EOL>3,4<EOL>", stringWriter.ToString());
            }
        }
 public async Task write_csv_async_non_reflection_throws_if_object_to_record_converter_is_null()
 {
     using (var writer = new CsvWriter(new StringWriter()))
     {
         var header = new string[] { "The Year", "Even Year?" };
         await Assert.ThrowsAsync<ArgumentNullException>(() => new DateTime[0].WriteCsvAsync(writer, header, null));
     }
 }
        public async Task write_csv_async_object_to_string_converter_converts_nulls_to_empty_string()
        {
            using (var stringWriter = new StringWriter())
            using (var writer = new CsvWriter(stringWriter))
            {
                writer.NewLine = "<EOL>";

                var dataTable = new DataTable();
                dataTable.Columns.Add("First");
                dataTable.Columns.Add("Second");
                dataTable.Rows.Add("1", 2);
                dataTable.Rows.Add(null, 4d);
                dataTable.Rows.Add(5m, null);

                Assert.Equal(3, await dataTable.WriteCsvAsync(writer));
                Assert.Equal("First,Second<EOL>1,2<EOL>,4<EOL>5,<EOL>", stringWriter.ToString());
            }
        }
        public async Task write_csv_async_non_reflection_overload_allows_arbitrary_conversion_of_objects_to_csv()
        {
            using (var stringWriter = new StringWriter())
            using (var writer = new CsvWriter(stringWriter))
            {
                writer.NewLine = "<EOL>";

                var items = new DateTime[]
                {
                    new DateTime(2004, 12, 31),
                    new DateTime(1978, 12, 4),
                    new DateTime(1979, 10, 26)
                };

                var header = new string[] { "The Year", "Even Year?" };
                await items.WriteCsvAsync(writer, header, dt => new string[] { dt.Year.ToString(CultureInfo.InvariantCulture), (dt.Year % 2 == 0).ToString(CultureInfo.InvariantCulture) });
                Assert.Equal("The Year,Even Year?<EOL>2004,True<EOL>1978,True<EOL>1979,False<EOL>", stringWriter.ToString());
            }
        }
        public async Task write_csv_async_works_with_large_data_table_input()
        {
            var dataTable = new DataTable();
            dataTable.Columns.Add("First");
            dataTable.Columns.Add("Second");

            for (var i = 0; i < 1000; ++i)
            {
                dataTable.Rows.Add("value0_" + i, "value1_" + i);
            }

            // write less than all available records
            using (var writer = new CsvWriter(new StringWriter()))
            {
                Assert.Equal(913, await dataTable.WriteCsvAsync(writer, true, 913));
            }

            // write exactly available records
            using (var writer = new CsvWriter(new StringWriter()))
            {
                Assert.Equal(1000, await dataTable.WriteCsvAsync(writer, true, 1000));
            }

            // attempt to write more than available records
            using (var writer = new CsvWriter(new StringWriter()))
            {
                Assert.Equal(1000, await dataTable.WriteCsvAsync(writer, true, 1500));
            }
        }
 /// <summary>
 /// Writes the items in <paramref name="this"/> to <paramref name="csvWriter"/>.
 /// </summary>
 /// <remarks>
 /// <para>
 /// Property values are obtained via reflection.
 /// </para>
 /// </remarks>
 /// <typeparam name="T">
 /// The type of the items to be written to <paramref name="csvWriter"/>.
 /// </typeparam>
 /// <param name="this">
 /// The items to write.
 /// </param>
 /// <param name="csvWriter">
 /// The <see cref="CsvWriter"/>.
 /// </param>
 /// <param name="writeHeaderRecord">
 /// If <see langword="true"/>, a header record will first be written to <paramref name="csvWriter"/>, which is comprised of all specified property names.
 /// </param>
 /// <param name="propertyNames">
 /// The names of public properties in <typeparamref name="T"/> that should be written to <paramref name="csvWriter"/>.
 /// </param>
 /// <returns>
 /// The number of items written.
 /// </returns>
 public static int WriteCsv <T>(this IEnumerable <T> @this, CsvWriter csvWriter, bool writeHeaderRecord, string[] propertyNames)
 {
     return(@this.WriteCsv(csvWriter, writeHeaderRecord, propertyNames, o => o == null ? string.Empty : o.ToString()));
 }