コード例 #1
0
        public void When_cells_are_empty_they_must_be_exposed_as_empty()
        {
            // Arrange
            string emptyCellValue = string.Empty;

            using DelimitedValuesReader reader = new DelimitedValuesReaderBuilder()
                                                 .WithColumnHeaders("A", "B", "C")
                                                 .WithoutRows()
                                                 .WithRow(new[]
            {
                emptyCellValue,
                emptyCellValue,
                emptyCellValue
            })
                                                 .Build();

            // Act
            IDelimitedValuesReaderRow row = reader.Single();
            string cell1 = row.GetCell("A");
            string cell2 = row.GetCell("B");
            string cell3 = row.GetCell("C");

            // Assert
            cell1.Should().Be(emptyCellValue);
            cell2.Should().Be(emptyCellValue);
            cell3.Should().Be(emptyCellValue);
        }
コード例 #2
0
        public void When_source_contains_uneven_number_of_quotes_it_should_not_read_entire_source()
        {
            // Arrange
            const string header     = "C1";
            string       cell       = DefaultTextQualifier + "12345";
            int          bufferSize = header.Length + Environment.NewLine.Length + cell.Length;

            using DelimitedValuesReader reader = new DelimitedValuesReaderBuilder()
                                                 .WithIntermediateReader(r => new ReaderThatFailsAfterReadingTooMuch(r, bufferSize))
                                                 .WithSettings(new DelimitedValuesReaderSettingsBuilder()
                                                               .WithMaximumLineLength(cell.Length))
                                                 .WithHeaderLine(header)
                                                 .WithoutRows()
                                                 .WithDataLine(cell)
                                                 .WithDataLine("A")
                                                 .WithDataLine("B")
                                                 .Build();

            // Act
            // ReSharper disable once AccessToDisposedClosure
            Action action = () => _ = reader.SkipWhile(_ => true).ToArray();

            // Assert
            action.Should().ThrowExactly <DelimitedValuesParseException>();
        }
コード例 #3
0
        public void When_reading_cell_value_as_nullable_boolean_it_should_succeed()
        {
            // Arrange
            using DelimitedValuesReader reader = new DelimitedValuesReaderBuilder()
                .WithColumnHeaders("A", "B", "C")
                .WithoutRows()
                .WithRow(new[]
                {
                    "True",
                    "False",
                    ""
                })
                .Build();

            // Act
            IDelimitedValuesReaderRow row = reader.First();
            bool? cell1 = row.GetCell<bool?>("A");
            bool? cell2 = row.GetCell<bool?>("B");
            bool? cell3 = row.GetCell<bool?>("C");

            // Assert
            cell1.Should().BeTrue();
            cell2.Should().BeFalse();
            cell3.Should().Be(null);
        }
コード例 #4
0
        public void When_header_and_first_line_have_been_read_it_should_be_positioned_at_second_line()
        {
            // Arrange
            using DelimitedValuesReader reader = new DelimitedValuesReaderBuilder().Build();

            // Act
            _ = reader.First();

            // Assert
            reader.LineNumber.Should().Be(2);
        }
コード例 #5
0
        public void When_header_and_first_line_have_been_read_it_should_be_positioned_at_second_line()
        {
            // Arrange
            DelimitedValuesReader reader = new DelimitedValuesReaderBuilder().Build();

            // Act
            // ReSharper disable once ReturnValueOfPureMethodIsNotUsed
            reader.First();

            // Assert
            reader.LineNumber.Should().Be(2);
        }
コード例 #6
0
        public void When_no_field_separator_is_specified_it_must_autodetect_tab_as_separator()
        {
            // Act
            using DelimitedValuesReader reader = new DelimitedValuesReaderBuilder()
                                                 .WithSettings(new DelimitedValuesReaderSettingsBuilder()
                                                               .WithFieldSeparator(null))
                                                 .WithHeaderLine("A\tB\tC")
                                                 .WithoutRows()
                                                 .Build();

            // Assert
            reader.ColumnNames.Should().HaveCount(3);
        }
コード例 #7
0
        public void When_accessing_line_after_disposal_it_should_fail()
        {
            // Arrange
            DelimitedValuesReader reader = new DelimitedValuesReaderBuilder().Build();

            reader.Dispose();

            // Act
            Action action = () => _ = reader.Line;

            // Assert
            action.Should().ThrowExactly <ObjectDisposedException>();
        }
コード例 #8
0
        public void When_enumerating_after_disposal_it_should_fail()
        {
            // Arrange
            DelimitedValuesReader reader = new DelimitedValuesReaderBuilder().Build();

            reader.Dispose();

            // Act
            Action action = () => _ = reader.TakeWhile(_ => true).ToArray();

            // Assert
            action.Should().ThrowExactly <ObjectDisposedException>();
        }
コード例 #9
0
        public void When_enumerating_after_disposal_it_should_fail()
        {
            // Arrange
            DelimitedValuesReader reader = new DelimitedValuesReaderBuilder().Build();
            reader.Dispose();

            // Act
            // ReSharper disable once ReturnValueOfPureMethodIsNotUsed
            // ReSharper disable once ConvertToLambdaExpression
            Action action = () => { reader.TakeWhile(x => true).ToArray(); };

            // Assert
            action.ShouldThrow<ObjectDisposedException>();
        }
コード例 #10
0
        public void When_source_contains_multiple_column_names_they_must_be_exposed()
        {
            // Arrange
            DelimitedValuesReader reader = new DelimitedValuesReaderBuilder()
                .WithColumnHeaders("C1", "C2", "C3")
                .Build();

            // Act
            string[] columnNames = reader.ColumnNames.ToArray();

            // Assert
            columnNames.Should().HaveCount(3);
            columnNames[0].Should().Be("C1");
            columnNames[1].Should().Be("C2");
            columnNames[2].Should().Be("C3");
        }
コード例 #11
0
        public void When_source_contains_uneven_number_of_quotes_it_should_fail()
        {
            // Arrange
            using DelimitedValuesReader reader = new DelimitedValuesReaderBuilder()
                                                 .WithSingleColumnHeader()
                                                 .WithoutRows()
                                                 .WithDataLine(DefaultTextQualifier)
                                                 .Build();

            // Act
            // ReSharper disable once AccessToDisposedClosure
            Action action = () => _ = reader.SkipWhile(_ => true).ToArray();

            // Assert
            action.Should().ThrowExactly <DelimitedValuesParseException>().WithMessage("Missing closing text qualifier.");
        }
コード例 #12
0
        public void When_cell_contains_unquoted_text_before_quoted_text_it_should_fail()
        {
            // Arrange
            using DelimitedValuesReader reader = new DelimitedValuesReaderBuilder()
                                                 .WithSingleColumnHeader()
                                                 .WithoutRows()
                                                 .WithDataLine("a" + DefaultTextQualifier + "b" + DefaultTextQualifier)
                                                 .Build();

            // Act
            // ReSharper disable once AccessToDisposedClosure
            Action action = () => _ = reader.Take(1).ToArray();

            // Assert
            action.Should().ThrowExactly <DelimitedValuesParseException>().WithMessage("Text qualifier must be the first non-whitespace character of a cell.");
        }
コード例 #13
0
        public void When_reading_cell_value_with_custom_converter_it_should_succeed()
        {
            // Arrange
            DelimitedValuesReader reader = new DelimitedValuesReaderBuilder()
                .WithSingleColumnHeader("A")
                .WithoutRows()
                .WithRow(new[] { "X" })
                .Build();

            // Act
            IDelimitedValuesReaderRow row = reader.First();
            string cell = row.GetCell("A", c => "Y");

            // Assert
            cell.Should().Be("Y");
        }
コード例 #14
0
        public void When_source_contains_multiple_column_names_they_must_be_exposed()
        {
            // Arrange
            using DelimitedValuesReader reader = new DelimitedValuesReaderBuilder()
                                                 .WithColumnHeaders("C1", "C2", "C3")
                                                 .Build();

            // Act
            string[] columnNames = reader.ColumnNames.ToArray();

            // Assert
            columnNames.Should().HaveCount(3);
            columnNames[0].Should().Be("C1");
            columnNames[1].Should().Be("C2");
            columnNames[2].Should().Be("C3");
        }
コード例 #15
0
        public void When_accessing_line_after_disposal_it_should_fail()
        {
            // Arrange
            DelimitedValuesReader reader = new DelimitedValuesReaderBuilder().Build();
            reader.Dispose();

            // Act
            Action action = () =>
            {
                // ReSharper disable once UnusedVariable
                string dummy = reader.Line;
            };

            // Assert
            action.ShouldThrow<ObjectDisposedException>();
        }
コード例 #16
0
        public void When_no_field_separator_is_specified_it_must_give_semicolon_precedence_over_comma_during_auto_detection()
        {
            // Arrange
            using DelimitedValuesReader reader = new DelimitedValuesReaderBuilder()
                                                 .WithSettings(new DelimitedValuesReaderSettingsBuilder()
                                                               .WithFieldSeparator(null))
                                                 .WithHeaderLine("A;B;C,D,E")
                                                 .WithoutRows()
                                                 .WithDataLine("1;2;345")
                                                 .Build();

            // Act
            IDelimitedValuesReaderRow row = reader.Single();

            // Assert
            reader.ColumnNames.Should().HaveCount(3);
            row.GetCell("A").Should().Be("1");
        }
コード例 #17
0
        public void When_quoted_cell_contains_like_breaks_they_must_be_preserved()
        {
            // Arrange
            const string cellValue = "a\rb\nc\r\nd";

            using DelimitedValuesReader reader = new DelimitedValuesReaderBuilder()
                                                 .WithSingleColumnHeader("C")
                                                 .WithoutRows()
                                                 .WithDataLine(DefaultTextQualifier + cellValue + DefaultTextQualifier)
                                                 .Build();

            // Act
            IDelimitedValuesReaderRow row = reader.Single();
            string cell = row.GetCell("C");

            // Assert
            cell.Should().Be(cellValue);
        }
コード例 #18
0
        public void When_reading_cell_value_with_custom_converter_it_should_succeed()
        {
            // Arrange
            using DelimitedValuesReader reader = new DelimitedValuesReaderBuilder()
                .WithSingleColumnHeader("A")
                .WithoutRows()
                .WithRow(new[]
                {
                    "X"
                })
                .Build();

            // Act
            IDelimitedValuesReaderRow row = reader.First();
            string? cell = row.GetCell("A", _ => "Y");

            // Assert
            cell.Should().Be("Y");
        }
コード例 #19
0
        public void When_reading_cell_value_it_should_respect_culture()
        {
            // Arrange
            var culture = new CultureInfo("nl-NL");
            DelimitedValuesReader reader = new DelimitedValuesReaderBuilder()
                .WithSettings(new DelimitedValuesReaderSettingsBuilder()
                    .WithCulture(culture))
                .WithSingleColumnHeader("A")
                .WithoutRows()
                .WithRow(new[] { "3,5" })
                .Build();

            // Act
            IDelimitedValuesReaderRow row = reader.First();
            decimal cell = row.GetCell<decimal>("A");

            // Assert
            cell.Should().Be(3.5m);
        }
コード例 #20
0
        public void When_reading_cell_value_as_nullable_boolean_it_should_succeed()
        {
            // Arrange
            DelimitedValuesReader reader = new DelimitedValuesReaderBuilder()
                .WithColumnHeaders("A", "B", "C")
                .WithoutRows()
                .WithRow(new[] { "True", "False", "" })
                .Build();

            // Act
            IDelimitedValuesReaderRow row = reader.First();
            var cell1 = row.GetCell<bool?>("A");
            var cell2 = row.GetCell<bool?>("B");
            var cell3 = row.GetCell<bool?>("C");

            // Assert
            cell1.Should().BeTrue();
            cell2.Should().BeFalse();
            cell3.Should().Be(null);
        }
コード例 #21
0
        public void When_lines_are_broken_using_carriage_returns_followed_by_with_line_feeds_it_should_report_the_correct_starting_line_number()
        {
            // Arrange
            const string lineBreaker            = "\r\n";
            const string cellValueWithLineBreak = "Cell with" + lineBreaker + "line break";
            string       rowWithLineBreak       = DefaultTextQualifier + cellValueWithLineBreak + DefaultTextQualifier;

            using DelimitedValuesReader reader = new DelimitedValuesReaderBuilder()
                                                 .WithSingleColumnHeader()
                                                 .WithoutRows()
                                                 .WithDataLine(rowWithLineBreak)
                                                 .WithDataLine(rowWithLineBreak)
                                                 .Build();

            // Act
            _ = reader.Take(2).ToArray();

            // Assert
            reader.LineNumber.Should().Be(4);
        }
コード例 #22
0
        public void When_quoted_cell_contains_leading_and_trailing_whitespace_it_must_be_preserved()
        {
            // Arrange
            const string columnHeaderName = "C";

            using DelimitedValuesReader reader = new DelimitedValuesReaderBuilder()
                                                 .WithSingleColumnHeader(columnHeaderName)
                                                 .WithoutRows()
                                                 .WithRow(new[]
            {
                " \"  A \"  "
            })
                                                 .Build();

            // Act
            IDelimitedValuesReaderRow row = reader.Single();

            // Assert
            row.GetCell(columnHeaderName).Should().Be("  A ");
        }
コード例 #23
0
        public void When_quoted_cell_surrounds_text_qualifiers_they_must_be_unescaped()
        {
            // Arrange
            const string cellValue = "\"A nice day...\"";

            string escaped = DefaultTextQualifier +
                             cellValue.Replace(DefaultTextQualifier, DefaultTextQualifier + DefaultTextQualifier, StringComparison.Ordinal) + DefaultTextQualifier;

            using DelimitedValuesReader reader = new DelimitedValuesReaderBuilder()
                                                 .WithSingleColumnHeader("C")
                                                 .WithoutRows()
                                                 .WithDataLine(escaped)
                                                 .Build();

            // Act
            IDelimitedValuesReaderRow row = reader.Single();
            string cell = row.GetCell("C");

            // Assert
            cell.Should().Be(cellValue);
        }
コード例 #24
0
        public void When_quoted_cell_contains_field_separators_they_must_be_preserved()
        {
            // Arrange
            const char   separator = ':';
            const string cellValue = "x:y:z";

            using DelimitedValuesReader reader = new DelimitedValuesReaderBuilder()
                                                 .WithSettings(new DelimitedValuesReaderSettingsBuilder()
                                                               .WithFieldSeparator(separator))
                                                 .WithHeaderLine("A" + separator + "B")
                                                 .WithoutRows()
                                                 .WithDataLine(DefaultTextQualifier + cellValue + DefaultTextQualifier + separator)
                                                 .Build();

            // Act
            IDelimitedValuesReaderRow row = reader.Single();
            string cell = row.GetCell("A");

            // Assert
            cell.Should().Be(cellValue);
        }
コード例 #25
0
        public void When_lines_are_broken_using_line_feeds_it_should_report_the_correct_starting_line_number()
        {
            // Arrange
            const string lineBreaker = "\n";
            const string cellValueWithLineBreak = "Cell with" + lineBreaker + "line break";
            string rowWithLineBreak = DefaultTextQualifier + cellValueWithLineBreak + DefaultTextQualifier;

            DelimitedValuesReader reader = new DelimitedValuesReaderBuilder()
                .WithSingleColumnHeader()
                .WithoutRows()
                .WithDataLine(rowWithLineBreak)
                .WithDataLine(rowWithLineBreak)
                .Build();

            // Act
            // ReSharper disable once ReturnValueOfPureMethodIsNotUsed
            reader.Take(2).ToArray();

            // Assert
            reader.LineNumber.Should().Be(4);
        }
コード例 #26
0
        public void When_source_contains_uneven_number_of_quotes_it_should_report_the_correct_starting_line_number()
        {
            // Arrange
            using DelimitedValuesReader reader = new DelimitedValuesReaderBuilder()
                                                 .WithSingleColumnHeader()
                                                 .WithoutRows()
                                                 .WithDataLine(DefaultTextQualifier + "12345")
                                                 .WithDataLine("A")
                                                 .WithDataLine("B")
                                                 .Build();

            try
            {
                // Act
                _ = reader.SkipWhile(_ => true).ToArray();
            }
            catch (DelimitedValuesParseException)
            {
                // Assert
                reader.LineNumber.Should().Be(2);
            }
        }
コード例 #27
0
        public void When_reading_cell_value_it_should_respect_culture()
        {
            // Arrange
            var culture = new CultureInfo("nl-NL");

            using DelimitedValuesReader reader = new DelimitedValuesReaderBuilder()
                .WithSettings(new DelimitedValuesReaderSettingsBuilder()
                    .WithCulture(culture))
                .WithSingleColumnHeader("A")
                .WithoutRows()
                .WithRow(new[]
                {
                    "3,5"
                })
                .Build();

            // Act
            IDelimitedValuesReaderRow row = reader.First();
            decimal cell = row.GetCell<decimal>("A");

            // Assert
            cell.Should().Be(3.5m);
        }
コード例 #28
0
        public void When_cell_contains_unquoted_text_after_quoted_text_it_should_fail()
        {
            // Arrange
            DelimitedValuesReader reader = new DelimitedValuesReaderBuilder()
                .WithSingleColumnHeader()
                .WithoutRows()
                .WithDataLine(DefaultTextQualifier + "a" + DefaultTextQualifier + "b")
                .Build();

            // Act
            // ReSharper disable once ReturnValueOfPureMethodIsNotUsed
            Action action = () => reader.Take(1).ToArray();

            // Assert
            action.ShouldThrow<DelimitedValuesParseException>()
                .WithMessage("Text-qualified cell cannot contain non-whitespace after the closing text qualifier.");
        }
コード例 #29
0
        public void When_quoted_cell_contains_like_breaks_they_must_be_preserved()
        {
            // Arrange
            const string cellValue = "a\rb\nc\r\nd";
            DelimitedValuesReader reader = new DelimitedValuesReaderBuilder()
                .WithSingleColumnHeader("C")
                .WithoutRows()
                .WithDataLine(DefaultTextQualifier + cellValue + DefaultTextQualifier)
                .Build();

            // Act
            IDelimitedValuesReaderRow row = reader.Single();
            string cell = row.GetCell("C");

            // Assert
            cell.Should().Be(cellValue);
        }
コード例 #30
0
        public void When_no_field_separator_is_specified_it_must_autodetect_tab_as_separator()
        {
            // Act
            DelimitedValuesReader reader = new DelimitedValuesReaderBuilder()
                .WithSettings(new DelimitedValuesReaderSettingsBuilder()
                    .WithFieldSeparator(null))
                .WithHeaderLine("A\tB\tC")
                .WithoutRows()
                .Build();

            // Assert
            reader.ColumnNames.Should().HaveCount(3);
        }
コード例 #31
0
        public void When_quoted_cell_surrounds_text_qualifiers_they_must_be_unescaped()
        {
            // Arrange
            const string cellValue = "\"A nice day...\"";
            string escaped = DefaultTextQualifier +
                cellValue.Replace(DefaultTextQualifier, DefaultTextQualifier + DefaultTextQualifier) +
                DefaultTextQualifier;
            DelimitedValuesReader reader = new DelimitedValuesReaderBuilder()
                .WithSingleColumnHeader("C")
                .WithoutRows()
                .WithDataLine(escaped)
                .Build();

            // Act
            IDelimitedValuesReaderRow row = reader.Single();
            string cell = row.GetCell("C");

            // Assert
            cell.Should().Be(cellValue);
        }
コード例 #32
0
        public void When_cells_are_empty_they_must_be_exposed_as_empty()
        {
            // Arrange
            string emptyCellValue = string.Empty;
            DelimitedValuesReader reader = new DelimitedValuesReaderBuilder()
                .WithColumnHeaders("A", "B", "C")
                .WithoutRows()
                .WithRow(new[] { emptyCellValue, emptyCellValue, emptyCellValue })
                .Build();

            // Act
            IDelimitedValuesReaderRow row = reader.Single();
            string cell1 = row.GetCell("A");
            string cell2 = row.GetCell("B");
            string cell3 = row.GetCell("C");

            // Assert
            cell1.Should().Be(emptyCellValue);
            cell2.Should().Be(emptyCellValue);
            cell3.Should().Be(emptyCellValue);
        }
コード例 #33
0
        public void When_source_contains_uneven_number_of_quotes_it_should_fail()
        {
            // Arrange
            DelimitedValuesReader reader = new DelimitedValuesReaderBuilder()
                .WithSingleColumnHeader()
                .WithoutRows()
                .WithDataLine(DefaultTextQualifier)
                .Build();

            // Act
            // ReSharper disable once ReturnValueOfPureMethodIsNotUsed
            Action action = () => reader.SkipWhile(x => true).ToArray();

            // Assert
            action.ShouldThrow<DelimitedValuesParseException>().WithMessage("Missing closing text qualifier.");
        }
コード例 #34
0
        public void When_quoted_cell_contains_leading_and_trailing_whitespace_it_must_be_preserved()
        {
            // Arrange
            const string columnHeaderName = "C";
            DelimitedValuesReader reader = new DelimitedValuesReaderBuilder()
                .WithSingleColumnHeader(columnHeaderName)
                .WithoutRows()
                .WithRow(new[] { " \"  A \"  " })
                .Build();

            // Act
            IDelimitedValuesReaderRow row = reader.Single();

            // Assert
            row.GetCell(columnHeaderName).Should().Be("  A ");
        }
コード例 #35
0
        public void When_source_contains_uneven_number_of_quotes_it_should_not_read_entire_source()
        {
            // Arrange
            const string header = "C1";
            string cell = DefaultTextQualifier + "12345";
            int bufferSize = header.Length + Environment.NewLine.Length + cell.Length;

            DelimitedValuesReader reader = new DelimitedValuesReaderBuilder()
                .WithIntermediateReader(r => new ReaderThatFailsAfterReadingTooMuch(r, bufferSize))
                .WithSettings(new DelimitedValuesReaderSettingsBuilder()
                    .WithMaximumLineLength(cell.Length))
                .WithHeaderLine(header)
                .WithoutRows()
                .WithDataLine(cell)
                .WithDataLine("A")
                .WithDataLine("B")
                .Build();

            // Act
            // ReSharper disable once ReturnValueOfPureMethodIsNotUsed
            Action action = () => reader.SkipWhile(x => true).ToArray();

            // Assert
            action.ShouldThrow<DelimitedValuesParseException>();
        }
コード例 #36
0
            When_no_field_separator_is_specified_it_must_give_semicolon_precedence_over_comma_during_autodetection()
        {
            // Arrange
            DelimitedValuesReader reader = new DelimitedValuesReaderBuilder()
                .WithSettings(new DelimitedValuesReaderSettingsBuilder()
                    .WithFieldSeparator(null))
                .WithHeaderLine("A;B;C,D,E")
                .WithoutRows()
                .WithDataLine("1;2;345")
                .Build();

            // Act
            IDelimitedValuesReaderRow row = reader.Single();

            // Assert
            reader.ColumnNames.Should().HaveCount(3);
            row.GetCell("A").Should().Be("1");
        }
コード例 #37
0
        public void When_source_contains_uneven_number_of_quotes_it_should_report_the_correct_starting_line_number()
        {
            // Arrange
            DelimitedValuesReader reader = new DelimitedValuesReaderBuilder()
                .WithSingleColumnHeader()
                .WithoutRows()
                .WithDataLine(DefaultTextQualifier + "12345")
                .WithDataLine("A")
                .WithDataLine("B")
                .Build();

            try
            {
                // Act
                // ReSharper disable once ReturnValueOfPureMethodIsNotUsed
                reader.SkipWhile(r => true).ToArray();
            }
            catch (DelimitedValuesParseException)
            {
                // Assert
                reader.LineNumber.Should().Be(2);
            }
        }
コード例 #38
0
        public void When_quoted_cell_contains_field_separators_they_must_be_preserved()
        {
            // Arrange
            const char separator = ':';
            const string cellValue = "x:y:z";
            DelimitedValuesReader reader = new DelimitedValuesReaderBuilder()
                .WithSettings(new DelimitedValuesReaderSettingsBuilder()
                    .WithFieldSeparator(separator))
                .WithHeaderLine("A" + separator + "B")
                .WithoutRows()
                .WithDataLine(DefaultTextQualifier + cellValue + DefaultTextQualifier + separator)
                .Build();

            // Act
            IDelimitedValuesReaderRow row = reader.Single();
            string cell = row.GetCell("A");

            // Assert
            cell.Should().Be(cellValue);
        }