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); }
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); }
private static RecordedTime?GetTimeElapsedSinceStart(IDelimitedValuesReaderRow row, string columnName, RecordedTime startTime) { string timeString = row.GetCell(columnName); TimeSpanWithAccuracy?timeWithAccuracy = TimeSpanWithAccuracy.FromString(timeString, Settings.Default.ImportExportCulture); return(timeWithAccuracy != null?startTime.Add(timeWithAccuracy.Value) : null); }
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); }
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"); }
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"); }
private static Competitor GetCompetitorFrom(IDelimitedValuesReaderRow row) { int competitorNumber = row.GetCell <int>(ImportExportColumns.CompetitorNumber); string?handlerName = row.GetCell <string>(ImportExportColumns.HandlerName); string?dogName = row.GetCell <string>(ImportExportColumns.DogName); string?countryCode = row.GetCell <string>(ImportExportColumns.CountryCode); if (string.IsNullOrWhiteSpace(handlerName)) { throw new InvalidDataException("Handler name is missing."); } if (string.IsNullOrWhiteSpace(dogName)) { throw new InvalidDataException("Dog name is missing."); } return(new Competitor(competitorNumber, handlerName, dogName).ChangeCountryCode(countryCode)); }
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 "); }
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); }
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); }
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); }
private static CompetitionRunResult GetRunResultFrom(IDelimitedValuesReaderRow row, bool hasOptionalColumns, DateTime startTimeUtc) { Competitor competitor = GetCompetitorFrom(row); var runResult = new CompetitionRunResult(competitor); if (hasOptionalColumns) { int? faultCount = row.GetCell <int?>(ImportExportColumns.FaultCount); int? refusalCount = row.GetCell <int?>(ImportExportColumns.RefusalCount); bool?isEliminated = row.GetCell <bool?>(ImportExportColumns.IsEliminated); // Note: We cannot reconstruct whether start time was high precision, because it does not roundtrip // through import/export. However, we do not need to know. A low-precision elapsed time is caused by // either one or both times to be low precision. So although we lost some information, the nett effect // when the precision of an elapsed time is recalculated will be the same as long as we assume that the start // time was high precision. var startTime = new RecordedTime(TimeSpan.Zero, startTimeUtc); RecordedTime?intermediateTime1 = GetTimeElapsedSinceStart(row, ImportExportColumns.IntermediateTime1, startTime); RecordedTime?intermediateTime2 = GetTimeElapsedSinceStart(row, ImportExportColumns.IntermediateTime2, startTime); RecordedTime?intermediateTime3 = GetTimeElapsedSinceStart(row, ImportExportColumns.IntermediateTime3, startTime); RecordedTime?finishTime = GetTimeElapsedSinceStart(row, ImportExportColumns.FinishTime, startTime); if (intermediateTime1 == null && intermediateTime2 == null && intermediateTime3 == null && finishTime == null) { startTime = null; } bool runCompleted = finishTime != null || (isEliminated != null && isEliminated.Value); if (runCompleted) { if (faultCount != null) { runResult = runResult.ChangeFaultCount(faultCount.Value); } if (refusalCount != null) { runResult = runResult.ChangeRefusalCount(refusalCount.Value); } if (isEliminated != null) { runResult = runResult.ChangeIsEliminated(isEliminated.Value); } if (startTime != null) { // @formatter:keep_existing_linebreaks true runResult = runResult .ChangeTimings(new CompetitionRunTimings(startTime) .ChangeIntermediateTime1(intermediateTime1) .ChangeIntermediateTime2(intermediateTime2) .ChangeIntermediateTime3(intermediateTime3) .ChangeFinishTime(finishTime)); // @formatter:keep_existing_linebreaks restore } } } return(runResult); }