Skip to content

Commit

Permalink
Merge pull request #51 from dotarj/feature/get-time-span
Browse files Browse the repository at this point in the history
GetTimeSpan method added to ProtoDataReader
  • Loading branch information
dotarj authored Jan 24, 2019
2 parents 6d14a24 + 34c8c07 commit 541c047
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/protobuf-net-data/ProtoDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,23 @@ public DateTime GetDateTime(int i)
return this.context.Buffers[i].DateTime;
}

/// <summary>
/// Gets the time span value of the specified field.
/// </summary>
/// <param name="i">The zero-based column ordinal.</param>
/// <returns>The time span value of the specified field.</returns>
/// <exception cref="InvalidOperationException">The <see cref="ProtoDataReader"/> is closed.</exception>
/// <exception cref="IndexOutOfRangeException">The index passed was outside the range of 0 through <see cref="IDataRecord.FieldCount"/>.</exception>
/// <exception cref="InvalidCastException">The specified cast is not valid.</exception>
public TimeSpan GetTimeSpan(int i)
{
this.ThrowIfClosed();
this.ThrowIfNoData();
this.ThrowIfIndexOutOfRange(i);

return this.context.Buffers[i].TimeSpan;
}

/// <summary>
/// Returns an <see cref="IDataReader"/> for the specified column ordinal.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright (c) Richard Dingwall, Arjen Post. See LICENSE in the project root for license information.

using System;
using Xunit;

namespace ProtoBuf.Data.Tests
{
public partial class ProtoDataReaderTests
{
public class TheGetTimeSpanMethod : ProtoDataReaderTests
{
[Fact]
public void ShouldThrowExceptionWhenDataReaderIsClosed()
{
// Arrange
var dataReader = this.CreateDataReader(value: TimeSpan.FromTicks(1));

dataReader.Close();

// Assert
Assert.Throws<InvalidOperationException>(() => dataReader.GetTimeSpan(0));
}

[Fact]
public void ShouldThrowExceptionWhenNoData()
{
// Arrange
var dataReader = this.CreateDataReader(value: TimeSpan.FromTicks(1));

// Assert
Assert.Throws<InvalidOperationException>(() => dataReader.GetTimeSpan(0));
}

[Fact]
public void ShouldThrowExceptionWhenIndexIsOutOfRange()
{
// Arrange
var dataReader = this.CreateDataReader(value: TimeSpan.FromTicks(1));

dataReader.Read();

// Assert
Assert.Throws<IndexOutOfRangeException>(() => dataReader.GetTimeSpan(dataReader.FieldCount));
}

[Fact]
public void ShouldThrowExceptionWhenIsNull()
{
// Arrange
var dataReader = this.CreateDataReader(value: (string)null);

dataReader.Read();

// Assert
Assert.Throws<InvalidOperationException>(() => dataReader.GetTimeSpan(0));
}

[Fact]
public void ShouldThrowExceptionIfInvalidType()
{
// Arrange
var dataReader = this.CreateDataReader(value: "foo");

dataReader.Read();

// Assert
Assert.Throws<InvalidOperationException>(() => dataReader.GetTimeSpan(0));
}

[Fact]
public void ShouldReturnCorrespondingValue()
{
// Arrange
var value = TimeSpan.FromTicks(1);
var dataReader = this.CreateDataReader(value: value);

dataReader.Read();

// Act
var result = dataReader.GetTimeSpan(0);

// Assert
Assert.Equal(value, result);
}
}
}
}

0 comments on commit 541c047

Please sign in to comment.