From 34c8c07a35ea788424b77a9401baf18d4d75d7dd Mon Sep 17 00:00:00 2001 From: Arjen Post Date: Wed, 25 Apr 2018 11:34:43 +0200 Subject: [PATCH] GetTimeSpan method added to ProtoDataReader --- src/protobuf-net-data/ProtoDataReader.cs | 17 ++++ ...otoDataReaderTests.TheGetTimeSpanMethod.cs | 87 +++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 test/protobuf-net-data.Tests/ProtoDataReaderTests.TheGetTimeSpanMethod.cs diff --git a/src/protobuf-net-data/ProtoDataReader.cs b/src/protobuf-net-data/ProtoDataReader.cs index 9d7e074..bce3a5b 100644 --- a/src/protobuf-net-data/ProtoDataReader.cs +++ b/src/protobuf-net-data/ProtoDataReader.cs @@ -475,6 +475,23 @@ public DateTime GetDateTime(int i) return this.context.Buffers[i].DateTime; } + /// + /// Gets the time span value of the specified field. + /// + /// The zero-based column ordinal. + /// The time span value of the specified field. + /// The is closed. + /// The index passed was outside the range of 0 through . + /// The specified cast is not valid. + public TimeSpan GetTimeSpan(int i) + { + this.ThrowIfClosed(); + this.ThrowIfNoData(); + this.ThrowIfIndexOutOfRange(i); + + return this.context.Buffers[i].TimeSpan; + } + /// /// Returns an for the specified column ordinal. /// diff --git a/test/protobuf-net-data.Tests/ProtoDataReaderTests.TheGetTimeSpanMethod.cs b/test/protobuf-net-data.Tests/ProtoDataReaderTests.TheGetTimeSpanMethod.cs new file mode 100644 index 0000000..380ba9f --- /dev/null +++ b/test/protobuf-net-data.Tests/ProtoDataReaderTests.TheGetTimeSpanMethod.cs @@ -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(() => dataReader.GetTimeSpan(0)); + } + + [Fact] + public void ShouldThrowExceptionWhenNoData() + { + // Arrange + var dataReader = this.CreateDataReader(value: TimeSpan.FromTicks(1)); + + // Assert + Assert.Throws(() => dataReader.GetTimeSpan(0)); + } + + [Fact] + public void ShouldThrowExceptionWhenIndexIsOutOfRange() + { + // Arrange + var dataReader = this.CreateDataReader(value: TimeSpan.FromTicks(1)); + + dataReader.Read(); + + // Assert + Assert.Throws(() => dataReader.GetTimeSpan(dataReader.FieldCount)); + } + + [Fact] + public void ShouldThrowExceptionWhenIsNull() + { + // Arrange + var dataReader = this.CreateDataReader(value: (string)null); + + dataReader.Read(); + + // Assert + Assert.Throws(() => dataReader.GetTimeSpan(0)); + } + + [Fact] + public void ShouldThrowExceptionIfInvalidType() + { + // Arrange + var dataReader = this.CreateDataReader(value: "foo"); + + dataReader.Read(); + + // Assert + Assert.Throws(() => 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); + } + } + } +}