public void When_disposing_row_multiple_times_it_should_write_the_row_only_once()
        {
            // Arrange
            var output = new StringWriter();

            using (DelimitedValuesWriter writer = new DelimitedValuesWriterBuilder()
                                                  .WritingTo(output)
                                                  .WithSettings(new DelimitedValuesWriterSettingsBuilder()
                                                                .WithoutColumnNamesOnFirstLine())
                                                  .WithSingleColumnHeader("A")
                                                  .Build())
            {
                IDelimitedValuesWriterRow row = writer.CreateRow();
                row.SetCell("A", "X");

                // Act
                row.Dispose();
                row.Dispose();
            }

            IEnumerable <string> lines = TextToLines(output.ToString());

            // Assert
            lines.Should().HaveCount(1);
        }
        public void When_cell_contains_field_separator_it_should_escape_cell()
        {
            // Arrange
            var output = new StringWriter();

            using (DelimitedValuesWriter writer = new DelimitedValuesWriterBuilder()
                                                  .WritingTo(output)
                                                  .WithSettings(new DelimitedValuesWriterSettingsBuilder()
                                                                .WithTextQualifier('\'')
                                                                .WithFieldSeparator('|')
                                                                .WithoutColumnNamesOnFirstLine())
                                                  .WithColumnHeaders("A", "B")
                                                  .Build())
            {
                using (IDelimitedValuesWriterRow row = writer.CreateRow())
                {
                    row.SetCell("A", "X|Y");
                    row.SetCell("B", "Z");
                }

                // Act
            }

            // Assert
            output.ToString().Should().Be("'X|Y'|Z" + Environment.NewLine);
        }
        public void When_writing_cell_value_it_should_respect_culture()
        {
            // Arrange
            var output  = new StringWriter();
            var culture = new CultureInfo("nl-NL");

            using (DelimitedValuesWriter writer = new DelimitedValuesWriterBuilder()
                                                  .WritingTo(output)
                                                  .WithSingleColumnHeader("A")
                                                  .WithSettings(new DelimitedValuesWriterSettingsBuilder()
                                                                .WithoutColumnNamesOnFirstLine()
                                                                .WithCulture(culture))
                                                  .Build())
            {
                // Act
                using (IDelimitedValuesWriterRow row = writer.CreateRow())
                {
                    string firstColumnName = writer.ColumnNames.First();
                    row.SetCell(firstColumnName, 3.5m);
                }
            }

            // Assert
            output.ToString().Should().Be("3,5" + Environment.NewLine);
        }
        public void When_creating_row_after_disposal_it_should_fail()
        {
            // Arrange
            DelimitedValuesWriter writer = new DelimitedValuesWriterBuilder().Build();

            writer.Dispose();

            // Act
            Action action = () => _ = writer.CreateRow();

            // Assert
            action.Should().ThrowExactly <ObjectDisposedException>();
        }
        public void When_completing_row_creation_after_disposal_it_should_fail()
        {
            // Arrange
            DelimitedValuesWriter writer = new DelimitedValuesWriterBuilder().Build();
            IDelimitedValuesWriterRow row = writer.CreateRow();
            writer.Dispose();

            // Act
            Action action = row.Dispose;

            // Assert
            action.ShouldThrow<ObjectDisposedException>();
        }
        public void When_creating_row_after_disposal_it_should_fail()
        {
            // Arrange
            DelimitedValuesWriter writer = new DelimitedValuesWriterBuilder().Build();
            writer.Dispose();

            // Act
            Action action = () =>
            {
                // ReSharper disable once UnusedVariable
                IDelimitedValuesWriterRow dummy = writer.CreateRow();
            };

            // Assert
            action.ShouldThrow<ObjectDisposedException>();
        }
        public void When_unspecified_column_names_are_assigned_a_cell_value_it_should_fail()
        {
            // Arrange
            using DelimitedValuesWriter writer = new DelimitedValuesWriterBuilder()
                                                 .WithSingleColumnHeader("A")
                                                 .Build();

            // Act
            Action action = () =>
            {
                using IDelimitedValuesWriterRow row = writer.CreateRow();
                row.SetCell("B", "dummy");
            };

            // Assert
            action.Should().ThrowExactly <ArgumentException>().WithMessage("Column with name 'B' does not exist.*");
        }
        public void When_not_closing_underlying_reader_it_should_flush_on_disposal()
        {
            // Arrange
            var outputStream = new MemoryStream();

            DelimitedValuesWriter writer = new DelimitedValuesWriterBuilder()
                                           .WritingTo(new StreamWriter(new BufferedStream(outputStream)))
                                           .WithSingleColumnHeader("A")
                                           .WithSettings(new DelimitedValuesWriterSettingsBuilder()
                                                         .KeepingReaderOpen())
                                           .Build();

            // Act
            writer.Dispose();

            // Assert
            outputStream.Length.Should().BeGreaterThan(0);
        }
        public void When_writing_cell_value_with_custom_converter_it_should_succeed()
        {
            // Arrange
            var output = new StringWriter();
            using (DelimitedValuesWriter writer = new DelimitedValuesWriterBuilder()
                .WritingTo(output)
                .WithSingleColumnHeader("A")
                .WithSettings(new DelimitedValuesWriterSettingsBuilder()
                    .WithoutColumnNamesOnFirstLine())
                .Build())
            {
                // Act
                using (IDelimitedValuesWriterRow row = writer.CreateRow())
                {
                    row.SetCell("A", 123, c => "Y");
                }
            }

            // Assert
            output.ToString().Should().Be("Y" + Environment.NewLine);
        }
        public void When_writing_cell_value_with_custom_converter_it_should_succeed()
        {
            // Arrange
            var output = new StringWriter();

            using (DelimitedValuesWriter writer = new DelimitedValuesWriterBuilder()
                                                  .WritingTo(output)
                                                  .WithSingleColumnHeader("A")
                                                  .WithSettings(new DelimitedValuesWriterSettingsBuilder()
                                                                .WithoutColumnNamesOnFirstLine())
                                                  .Build())
            {
                // Act
                using (IDelimitedValuesWriterRow row = writer.CreateRow())
                {
                    row.SetCell("A", 123, _ => "Y");
                }
            }

            // Assert
            output.ToString().Should().Be("Y" + Environment.NewLine);
        }
        public void When_disposing_row_multiple_times_it_should_write_the_row_only_once()
        {
            // Arrange
            var output = new StringWriter();
            using (DelimitedValuesWriter writer = new DelimitedValuesWriterBuilder()
                .WritingTo(output)
                .WithSettings(new DelimitedValuesWriterSettingsBuilder()
                    .WithoutColumnNamesOnFirstLine())
                .WithSingleColumnHeader("A")
                .Build())
            {
                IDelimitedValuesWriterRow row = writer.CreateRow();
                row.SetCell("A", "X");

                // Act
                row.Dispose();
                row.Dispose();
            }
            IEnumerable<string> lines = TextToLines(output.ToString());

            // Assert
            lines.Should().HaveCount(1);
        }
        public void When_cell_is_not_assigned_a_value_it_should_become_an_empty_cell()
        {
            // Arrange
            var output = new StringWriter();

            using (DelimitedValuesWriter writer = new DelimitedValuesWriterBuilder()
                                                  .WritingTo(output)
                                                  .WithSettings(new DelimitedValuesWriterSettingsBuilder()
                                                                .WithFieldSeparator('|')
                                                                .WithoutColumnNamesOnFirstLine())
                                                  .WithColumnHeaders("A", "B", "C")
                                                  .Build())
            {
                // Act
                using (IDelimitedValuesWriterRow row = writer.CreateRow())
                {
                    row.SetCell("A", "X");
                }
            }

            // Assert
            output.ToString().Should().Be("X||" + Environment.NewLine);
        }
        public void When_writing_cell_value_as_nullable_boolean_it_should_succeed()
        {
            // Arrange
            var output = new StringWriter();
            using (DelimitedValuesWriter writer = new DelimitedValuesWriterBuilder()
                .WritingTo(output)
                .WithColumnHeaders("A", "B", "C")
                .WithSettings(new DelimitedValuesWriterSettingsBuilder()
                    .WithoutColumnNamesOnFirstLine())
                .Build())
            {
                // Act
                using (IDelimitedValuesWriterRow row = writer.CreateRow())
                {
                    row.SetCell<bool?>("A", true);
                    row.SetCell<bool?>("B", null);
                    row.SetCell<bool?>("C", false);
                }
            }

            // Assert
            output.ToString().Should().Be("True,,False" + Environment.NewLine);
        }
        public void When_writing_cell_value_as_nullable_boolean_it_should_succeed()
        {
            // Arrange
            var output = new StringWriter();

            using (DelimitedValuesWriter writer = new DelimitedValuesWriterBuilder()
                                                  .WritingTo(output)
                                                  .WithColumnHeaders("A", "B", "C")
                                                  .WithSettings(new DelimitedValuesWriterSettingsBuilder()
                                                                .WithoutColumnNamesOnFirstLine())
                                                  .Build())
            {
                // Act
                using (IDelimitedValuesWriterRow row = writer.CreateRow())
                {
                    row.SetCell <bool?>("A", true);
                    row.SetCell <bool?>("B", null);
                    row.SetCell <bool?>("C", false);
                }
            }

            // Assert
            output.ToString().Should().Be("True,,False" + Environment.NewLine);
        }
        public void When_writing_cell_value_it_should_respect_culture()
        {
            // Arrange
            var output = new StringWriter();
            var culture = new CultureInfo("nl-NL");
            using (DelimitedValuesWriter writer = new DelimitedValuesWriterBuilder()
                .WritingTo(output)
                .WithSingleColumnHeader("A")
                .WithSettings(new DelimitedValuesWriterSettingsBuilder()
                    .WithoutColumnNamesOnFirstLine()
                    .WithCulture(culture))
                .Build())
            {
                // Act
                using (IDelimitedValuesWriterRow row = writer.CreateRow())
                {
                    string firstColumnName = writer.ColumnNames.First();
                    row.SetCell(firstColumnName, 3.5m);
                }
            }

            // Assert
            output.ToString().Should().Be("3,5" + Environment.NewLine);
        }
        public void When_cell_contains_text_qualifier_it_should_escape_cell()
        {
            // Arrange
            var output = new StringWriter();

            using (DelimitedValuesWriter writer = new DelimitedValuesWriterBuilder()
                                                  .WritingTo(output)
                                                  .WithSettings(new DelimitedValuesWriterSettingsBuilder()
                                                                .WithTextQualifier('\'')
                                                                .WithoutColumnNamesOnFirstLine())
                                                  .WithSingleColumnHeader("A")
                                                  .Build())
            {
                using (IDelimitedValuesWriterRow row = writer.CreateRow())
                {
                    row.SetCell("A", "Bed 'n Breakfast");
                }

                // Act
            }

            // Assert
            output.ToString().Should().Be("'Bed ''n Breakfast'" + Environment.NewLine);
        }
        public void When_cell_is_not_assigned_a_value_it_should_become_an_empty_cell()
        {
            // Arrange
            var output = new StringWriter();
            using (DelimitedValuesWriter writer = new DelimitedValuesWriterBuilder()
                .WritingTo(output)
                .WithSettings(new DelimitedValuesWriterSettingsBuilder()
                    .WithFieldSeparator('|')
                    .WithoutColumnNamesOnFirstLine())
                .WithColumnHeaders("A", "B", "C")
                .Build())
            {
                // Act
                using (IDelimitedValuesWriterRow row = writer.CreateRow())
                {
                    row.SetCell("A", "X");
                }
            }

            // Assert
            output.ToString().Should().Be("X||" + Environment.NewLine);
        }
        public void When_cell_contains_text_qualifier_it_should_escape_cell()
        {
            // Arrange
            var output = new StringWriter();
            using (DelimitedValuesWriter writer = new DelimitedValuesWriterBuilder()
                .WritingTo(output)
                .WithSettings(new DelimitedValuesWriterSettingsBuilder()
                    .WithTextQualifier('\'')
                    .WithoutColumnNamesOnFirstLine())
                .WithSingleColumnHeader("A")
                .Build())
            {
                using (IDelimitedValuesWriterRow row = writer.CreateRow())
                {
                    row.SetCell("A", "Bed 'n Breakfast");
                }

                // Act
            }

            // Assert
            output.ToString().Should().Be("'Bed ''n Breakfast'" + Environment.NewLine);
        }
        public void When_cell_contains_field_separator_it_should_escape_cell()
        {
            // Arrange
            var output = new StringWriter();
            using (DelimitedValuesWriter writer = new DelimitedValuesWriterBuilder()
                .WritingTo(output)
                .WithSettings(new DelimitedValuesWriterSettingsBuilder()
                    .WithTextQualifier('\'')
                    .WithFieldSeparator('|')
                    .WithoutColumnNamesOnFirstLine())
                .WithColumnHeaders("A", "B")
                .Build())
            {
                using (IDelimitedValuesWriterRow row = writer.CreateRow())
                {
                    row.SetCell("A", "X|Y");
                    row.SetCell("B", "Z");
                }

                // Act
            }

            // Assert
            output.ToString().Should().Be("'X|Y'|Z" + Environment.NewLine);
        }
        public void When_unspecified_column_names_are_assigned_a_cell_value_it_should_fail()
        {
            // Arrange
            DelimitedValuesWriter writer = new DelimitedValuesWriterBuilder()
                .WithSingleColumnHeader("A")
                .Build();

            // Act
            Action action = () =>
            {
                using (IDelimitedValuesWriterRow row = writer.CreateRow())
                {
                    row.SetCell("B", "dummy");
                }
            };

            // Assert
            action.ShouldThrow<ArgumentException>().WithMessage("Column with name 'B' does not exist.*");
        }
        public void When_not_closing_underlying_reader_it_should_flush_on_disposal()
        {
            // Arrange
            var outputStream = new MemoryStream();
            DelimitedValuesWriter writer = new DelimitedValuesWriterBuilder()
                .WritingTo(new StreamWriter(new BufferedStream(outputStream)))
                .WithSingleColumnHeader("A")
                .WithSettings(new DelimitedValuesWriterSettingsBuilder()
                    .KeepingReaderOpen())
                .Build();

            // Act
            writer.Dispose();

            // Assert
            outputStream.Length.Should().BeGreaterThan(0);
        }