From c469638907d07e286d4d0f74804df6da1d945c0c Mon Sep 17 00:00:00 2001 From: Arjen Post Date: Sat, 24 Mar 2018 08:05:30 +0100 Subject: [PATCH] increase code coverage and some code changes --- src/protobuf-net-data/ProtoDataReader.cs | 249 ++++++++++++++---- src/protobuf-net-data/Throw.cs | 17 ++ .../DataReaderHelper.cs | 32 +++ ...rotoBufDataReaderTests.TheDisposeMethod.cs | 38 +++ ...oBufDataReaderTests.TheGetBooleanMethod.cs | 75 ++++++ ...rotoBufDataReaderTests.TheGetByteMethod.cs | 75 ++++++ ...otoBufDataReaderTests.TheGetBytesMethod.cs | 195 ++++++++++++++ ...rotoBufDataReaderTests.TheGetCharMethod.cs | 75 ++++++ ...otoBufDataReaderTests.TheGetCharsMethod.cs | 195 ++++++++++++++ ...toBufDataReaderTests.TheGetDataTypeName.cs | 39 +++ ...BufDataReaderTests.TheGetDateTimeMethod.cs | 75 ++++++ ...oBufDataReaderTests.TheGetDecimalMethod.cs | 75 ++++++ ...toBufDataReaderTests.TheGetDoubleMethod.cs | 75 ++++++ ...ufDataReaderTests.TheGetFieldTypeMethod.cs | 53 ++++ ...otoBufDataReaderTests.TheGetFloatMethod.cs | 75 ++++++ ...rotoBufDataReaderTests.TheGetGuidMethod.cs | 75 ++++++ ...otoBufDataReaderTests.TheGetInt16Method.cs | 75 ++++++ ...otoBufDataReaderTests.TheGetInt32Method.cs | 75 ++++++ ...otoBufDataReaderTests.TheGetInt64Method.cs | 70 +++++ ...rotoBufDataReaderTests.TheGetNameMethod.cs | 55 ++++ ...oBufDataReaderTests.TheGetOrdinalMethod.cs | 57 ++++ ...DataReaderTests.TheGetSchemaTableMethod.cs | 111 ++++++++ ...toBufDataReaderTests.TheGetStringMethod.cs | 73 +++++ ...otoBufDataReaderTests.TheGetValueMethod.cs | 63 +++++ ...toBufDataReaderTests.TheGetValuesMethod.cs | 90 +++++++ ...otoBufDataReaderTests.TheIsDBNullMethod.cs | 71 +++++ ...oBufDataReaderTests.TheNextResultMethod.cs | 118 +++++++++ 27 files changed, 2232 insertions(+), 44 deletions(-) create mode 100644 src/protobuf-net-data/Throw.cs create mode 100644 test/protobuf-net-data.Tests/DataReaderHelper.cs create mode 100644 test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheDisposeMethod.cs create mode 100644 test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetBooleanMethod.cs create mode 100644 test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetByteMethod.cs create mode 100644 test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetBytesMethod.cs create mode 100644 test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetCharMethod.cs create mode 100644 test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetCharsMethod.cs create mode 100644 test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetDataTypeName.cs create mode 100644 test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetDateTimeMethod.cs create mode 100644 test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetDecimalMethod.cs create mode 100644 test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetDoubleMethod.cs create mode 100644 test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetFieldTypeMethod.cs create mode 100644 test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetFloatMethod.cs create mode 100644 test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetGuidMethod.cs create mode 100644 test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetInt16Method.cs create mode 100644 test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetInt32Method.cs create mode 100644 test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetInt64Method.cs create mode 100644 test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetNameMethod.cs create mode 100644 test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetOrdinalMethod.cs create mode 100644 test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetSchemaTableMethod.cs create mode 100644 test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetStringMethod.cs create mode 100644 test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetValueMethod.cs create mode 100644 test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetValuesMethod.cs create mode 100644 test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheIsDBNullMethod.cs create mode 100644 test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheNextResultMethod.cs diff --git a/src/protobuf-net-data/ProtoDataReader.cs b/src/protobuf-net-data/ProtoDataReader.cs index e5c3439..4d98f4b 100644 --- a/src/protobuf-net-data/ProtoDataReader.cs +++ b/src/protobuf-net-data/ProtoDataReader.cs @@ -20,6 +20,7 @@ namespace ProtoBuf.Data using System.Data; using System.IO; using ProtoBuf.Data.Internal; + using System.Runtime.CompilerServices; /// /// A custom for de-serializing a protocol-buffer binary stream back @@ -75,7 +76,8 @@ public int FieldCount { get { - ErrorIfClosed(); + ThrowIfClosed(); + return dataTable.Columns.Count; } } @@ -84,7 +86,8 @@ public int Depth { get { - ErrorIfClosed(); + ThrowIfClosed(); + return 1; } } @@ -104,7 +107,6 @@ object IDataRecord.this[int i] { get { - ErrorIfClosed(); return GetValue(i); } } @@ -113,38 +115,49 @@ object IDataRecord.this[string name] { get { - ErrorIfClosed(); return GetValue(GetOrdinal(name)); } } public string GetName(int i) { - ErrorIfClosed(); + this.ThrowIfClosed(); + this.ThrowIfIndexOutOfRange(i); + return dataTable.Columns[i].ColumnName; } public string GetDataTypeName(int i) { - ErrorIfClosed(); + this.ThrowIfClosed(); + this.ThrowIfIndexOutOfRange(i); + return dataTable.Columns[i].DataType.Name; } public Type GetFieldType(int i) { - ErrorIfClosed(); + this.ThrowIfClosed(); + this.ThrowIfIndexOutOfRange(i); + return dataTable.Columns[i].DataType; } public object GetValue(int i) { - ErrorIfClosed(); + this.ThrowIfClosed(); + this.ThrowIfNoData(); + this.ThrowIfIndexOutOfRange(i); + return currentRow[i]; } public int GetValues(object[] values) { - ErrorIfClosed(); + Throw.IfNull(values, nameof(values)); + + this.ThrowIfClosed(); + this.ThrowIfNoData(); int length = Math.Min(values.Length, dataTable.Columns.Count); @@ -155,109 +168,174 @@ public int GetValues(object[] values) public int GetOrdinal(string name) { - ErrorIfClosed(); - return dataTable.Columns[name].Ordinal; + ThrowIfClosed(); + + var column = dataTable.Columns[name]; + + if (column == null) + { + throw new IndexOutOfRangeException(name); + } + + return column.Ordinal; } public bool GetBoolean(int i) { - ErrorIfClosed(); + this.ThrowIfClosed(); + this.ThrowIfNoData(); + this.ThrowIfIndexOutOfRange(i); + this.ThrowIfNoValueIsNull(i); + return (bool)currentRow[i]; } public byte GetByte(int i) { - ErrorIfClosed(); + this.ThrowIfClosed(); + this.ThrowIfNoData(); + this.ThrowIfIndexOutOfRange(i); + this.ThrowIfNoValueIsNull(i); + return (byte)currentRow[i]; } - public long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length) + public long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferOffset, int length) { - ErrorIfClosed(); - var sourceBuffer = (byte[])currentRow[i]; - length = Math.Min(length, currentRow.Length - (int)fieldOffset); - Array.Copy(sourceBuffer, fieldOffset, buffer, bufferoffset, length); - return length; + this.ThrowIfClosed(); + this.ThrowIfNoData(); + this.ThrowIfIndexOutOfRange(i); + this.ThrowIfNoValueIsNull(i); + + return this.CopyArray((byte[])currentRow[i], fieldOffset, buffer, bufferOffset, length); } public char GetChar(int i) { - ErrorIfClosed(); + this.ThrowIfClosed(); + this.ThrowIfNoData(); + this.ThrowIfIndexOutOfRange(i); + this.ThrowIfNoValueIsNull(i); + return (char)currentRow[i]; } - public long GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length) + public long GetChars(int i, long fieldOffset, char[] buffer, int bufferOffset, int length) { - ErrorIfClosed(); - var sourceBuffer = (char[])currentRow[i]; - length = Math.Min(length, currentRow.Length - (int)fieldoffset); - Array.Copy(sourceBuffer, fieldoffset, buffer, bufferoffset, length); - return length; + this.ThrowIfClosed(); + this.ThrowIfNoData(); + this.ThrowIfIndexOutOfRange(i); + this.ThrowIfNoValueIsNull(i); + + return this.CopyArray((char[])currentRow[i], fieldOffset, buffer, bufferOffset, length); } public Guid GetGuid(int i) { - ErrorIfClosed(); + this.ThrowIfClosed(); + this.ThrowIfNoData(); + this.ThrowIfIndexOutOfRange(i); + this.ThrowIfNoValueIsNull(i); + return (Guid)currentRow[i]; } public short GetInt16(int i) { - ErrorIfClosed(); + this.ThrowIfClosed(); + this.ThrowIfNoData(); + this.ThrowIfIndexOutOfRange(i); + this.ThrowIfNoValueIsNull(i); + return (short)currentRow[i]; } public int GetInt32(int i) { - ErrorIfClosed(); + this.ThrowIfClosed(); + this.ThrowIfNoData(); + this.ThrowIfIndexOutOfRange(i); + this.ThrowIfNoValueIsNull(i); + return (int)currentRow[i]; } public long GetInt64(int i) { - ErrorIfClosed(); + this.ThrowIfClosed(); + this.ThrowIfNoData(); + this.ThrowIfIndexOutOfRange(i); + this.ThrowIfNoValueIsNull(i); + return (long)currentRow[i]; } public float GetFloat(int i) { - ErrorIfClosed(); + this.ThrowIfClosed(); + this.ThrowIfNoData(); + this.ThrowIfIndexOutOfRange(i); + this.ThrowIfNoValueIsNull(i); + return (float)currentRow[i]; } public double GetDouble(int i) { - ErrorIfClosed(); + this.ThrowIfClosed(); + this.ThrowIfNoData(); + this.ThrowIfIndexOutOfRange(i); + this.ThrowIfNoValueIsNull(i); + return (double)currentRow[i]; } public string GetString(int i) { - ErrorIfClosed(); + this.ThrowIfClosed(); + this.ThrowIfNoData(); + this.ThrowIfIndexOutOfRange(i); + this.ThrowIfNoValueIsNull(i); + return (string)currentRow[i]; } public decimal GetDecimal(int i) { - ErrorIfClosed(); + this.ThrowIfClosed(); + this.ThrowIfNoData(); + this.ThrowIfIndexOutOfRange(i); + this.ThrowIfNoValueIsNull(i); + return (decimal)currentRow[i]; } public DateTime GetDateTime(int i) { - ErrorIfClosed(); + this.ThrowIfClosed(); + this.ThrowIfNoData(); + this.ThrowIfIndexOutOfRange(i); + this.ThrowIfNoValueIsNull(i); + return (DateTime)currentRow[i]; } public IDataReader GetData(int i) { - ErrorIfClosed(); + this.ThrowIfClosed(); + this.ThrowIfNoData(); + this.ThrowIfIndexOutOfRange(i); + this.ThrowIfNoValueIsNull(i); + return ((DataTable)currentRow[i]).CreateDataReader(); } public bool IsDBNull(int i) { - ErrorIfClosed(); + this.ThrowIfClosed(); + this.ThrowIfNoData(); + this.ThrowIfIndexOutOfRange(i); + return currentRow[i] == null || currentRow[i] is DBNull; } @@ -269,7 +347,7 @@ public void Close() public bool NextResult() { - ErrorIfClosed(); + ThrowIfClosed(); ConsumeAnyRemainingRows(); @@ -277,7 +355,6 @@ public bool NextResult() if (currentField == 0) { - IsClosed = true; return false; } @@ -290,7 +367,8 @@ public bool NextResult() public DataTable GetSchemaTable() { - ErrorIfClosed(); + ThrowIfClosed(); + using (var schemaReader = dataTable.CreateDataReader()) { return schemaReader.GetSchemaTable(); @@ -299,7 +377,7 @@ public DataTable GetSchemaTable() public bool Read() { - ErrorIfClosed(); + ThrowIfClosed(); if (reachedEndOfCurrentTable) { @@ -524,11 +602,94 @@ private void ReadCurrentRow() ProtoReader.EndSubItem(token, reader); } - private void ErrorIfClosed() + private long CopyArray(Array source, long fieldOffset, Array buffer, int bufferOffset, int length) + { + // Partial implementation of SqlDataReader.GetBytes. + if (fieldOffset < 0) + { + throw new InvalidOperationException("Invalid value for argument 'fieldOffset'. The value must be greater than or equal to 0."); + } + + if (length < 0) + { + throw new IndexOutOfRangeException($"Data length '{length}' is less than 0."); + } + + var copyLength = source.LongLength; + + if (buffer == null) + { + return copyLength; + } + + if (bufferOffset < 0 || bufferOffset >= buffer.Length) + { + throw new ArgumentOutOfRangeException("bufferOffset", $"Invalid destination buffer (size of {buffer.Length}) offset: {bufferOffset}."); + } + + if (copyLength + bufferOffset > buffer.Length) + { + throw new IndexOutOfRangeException($"Buffer offset '{bufferOffset}' plus the elements available '{copyLength}' is greater than the length of the passed in buffer."); + } + + if (fieldOffset >= copyLength) + { + return 0; + } + + if (fieldOffset + length > copyLength) + { + copyLength = copyLength - fieldOffset; + } + else + { + copyLength = length; + } + + Array.Copy(source, fieldOffset, buffer, bufferOffset, copyLength); + + return copyLength; + } + +#if NET45 || NETSTANDARD20 + private void ThrowIfClosed([CallerMemberName]string memberName = "") + { + if (this.IsClosed) + { + throw new InvalidOperationException($"Invalid attempt to call {memberName} when reader is closed."); + } + } +#else + private void ThrowIfClosed() + { + if (this.IsClosed) + { + throw new InvalidOperationException("Invalid attempt to call method when reader is closed."); + } + } +#endif + + private void ThrowIfIndexOutOfRange(int i) + { + if (i < 0 || i >= this.dataTable.Columns.Count) + { + throw new IndexOutOfRangeException(); + } + } + + private void ThrowIfNoData() + { + if (reachedEndOfCurrentTable || this.currentRow == null) + { + throw new InvalidOperationException("Invalid attempt to read when no data is present."); + } + } + + private void ThrowIfNoValueIsNull(int i) { - if (IsClosed) + if (this.currentRow[i] == null) { - throw new InvalidOperationException("Attempt to access ProtoDataReader which was already closed."); + throw new InvalidOperationException("Invalid attempt to read when no data is present."); } } } diff --git a/src/protobuf-net-data/Throw.cs b/src/protobuf-net-data/Throw.cs new file mode 100644 index 0000000..8169758 --- /dev/null +++ b/src/protobuf-net-data/Throw.cs @@ -0,0 +1,17 @@ +// Copyright (c) Richard Dingwall, Arjen Post. See LICENSE in the project root for license information. + +using System; + +namespace ProtoBuf.Data +{ + internal static class Throw + { + public static void IfNull(T value, string parameterName) + { + if (value == null) + { + throw new ArgumentNullException(parameterName); + } + } + } +} diff --git a/test/protobuf-net-data.Tests/DataReaderHelper.cs b/test/protobuf-net-data.Tests/DataReaderHelper.cs new file mode 100644 index 0000000..f7c9535 --- /dev/null +++ b/test/protobuf-net-data.Tests/DataReaderHelper.cs @@ -0,0 +1,32 @@ +// Copyright (c) Richard Dingwall, Arjen Post. See LICENSE in the project root for license information. + +using System.Data; +using System.IO; + +namespace ProtoBuf.Data.Tests +{ + internal static class DataReaderHelper + { + public static ProtoDataReader CreateDataReader(TDataType value) + { + var dataTable = new DataTable(); + + dataTable.Columns.Add(typeof(TDataType).Name, typeof(TDataType)); + + dataTable.Rows.Add(value); + + return ToProtoDataReader(dataTable.CreateDataReader()); + } + + public static ProtoDataReader ToProtoDataReader(IDataReader dataReader) + { + var memoryStream = new MemoryStream(); + + DataSerializer.Serialize(memoryStream, dataReader); + + memoryStream.Position = 0; + + return (ProtoDataReader)DataSerializer.Deserialize(memoryStream); + } + } +} diff --git a/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheDisposeMethod.cs b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheDisposeMethod.cs new file mode 100644 index 0000000..b138de2 --- /dev/null +++ b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheDisposeMethod.cs @@ -0,0 +1,38 @@ +// Copyright (c) Richard Dingwall, Arjen Post. See LICENSE in the project root for license information. + +using System; +using System.Data; +using System.IO; +using Xunit; + +namespace ProtoBuf.Data.Tests +{ + public partial class ProtoDataReaderTests + { + public class TheDisposeMethod : ProtoDataReaderTests + { + [Fact] + public void ShouldDisposeStream() + { + // Arrange + var dataTable = new DataTable(); + + dataTable.Columns.Add("foo", typeof(int)); + + var memoryStream = new MemoryStream(); + + DataSerializer.Serialize(memoryStream, dataTable.CreateDataReader()); + + memoryStream.Position = 0; + + var dataReader = (ProtoDataReader)DataSerializer.Deserialize(memoryStream); + + // Act + dataReader.Dispose(); + + // Assert + Assert.Throws(() => memoryStream.Position = 0); + } + } + } +} diff --git a/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetBooleanMethod.cs b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetBooleanMethod.cs new file mode 100644 index 0000000..13126fc --- /dev/null +++ b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetBooleanMethod.cs @@ -0,0 +1,75 @@ +// Copyright (c) Richard Dingwall, Arjen Post. See LICENSE in the project root for license information. + +using System; +using System.Data; +using Xunit; + +namespace ProtoBuf.Data.Tests +{ + public partial class ProtoDataReaderTests + { + public class TheGetBooleanMethod : ProtoDataReaderTests + { + [Fact] + public void ShouldThrowExceptionWhenDataReaderIsClosed() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: true); + + dataReader.Close(); + + // Assert + Assert.Throws(() => dataReader.GetBoolean(0)); + } + + [Fact] + public void ShouldThrowExceptionWhenNoData() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: true); + + // Assert + Assert.Throws(() => dataReader.GetBoolean(0)); + } + + [Fact] + public void ShouldThrowExceptionWhenIndexIsOutOfRange() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: true); + + dataReader.Read(); + + // Assert + Assert.Throws(() => dataReader.GetBoolean(dataReader.FieldCount)); + } + + [Fact] + public void ShouldThrowExceptionWhenIsNull() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: (string)null); + + dataReader.Read(); + + // Assert + Assert.Throws(() => dataReader.GetBoolean(0)); + } + + [Fact] + public void ShouldReturnCorrespondingValue() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: true); + + dataReader.Read(); + + // Act + var result = dataReader.GetBoolean(0); + + // Assert + Assert.True(result); + } + } + } +} diff --git a/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetByteMethod.cs b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetByteMethod.cs new file mode 100644 index 0000000..fad8bbb --- /dev/null +++ b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetByteMethod.cs @@ -0,0 +1,75 @@ +// 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 TheGetByteMethod : ProtoDataReaderTests + { + [Fact] + public void ShouldThrowExceptionWhenDataReaderIsClosed() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: (byte)0b0010_1010); + + dataReader.Close(); + + // Assert + Assert.Throws(() => dataReader.GetByte(1)); + } + + [Fact] + public void ShouldThrowExceptionWhenNoData() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: (byte)0b0010_1010); + + // Assert + Assert.Throws(() => dataReader.GetByte(1)); + } + + [Fact] + public void ShouldThrowExceptionWhenIndexIsOutOfRange() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: (byte)0b0010_1010); + + dataReader.Read(); + + // Assert + Assert.Throws(() => dataReader.GetByte(dataReader.FieldCount)); + } + + [Fact] + public void ShouldThrowExceptionWhenIsNull() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: (string)null); + + dataReader.Read(); + + // Assert + Assert.Throws(() => dataReader.GetByte(0)); + } + + [Fact] + public void ShouldReturnCorrespondingValue() + { + // Arrange + var value = (byte)0b0010_1010; + var dataReader = DataReaderHelper.CreateDataReader(value: value); + + dataReader.Read(); + + // Act + var result = dataReader.GetByte(0); + + // Assert + Assert.Equal(value, result); + } + } + } +} diff --git a/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetBytesMethod.cs b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetBytesMethod.cs new file mode 100644 index 0000000..1d3aea9 --- /dev/null +++ b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetBytesMethod.cs @@ -0,0 +1,195 @@ +// 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 TheGetBytesMethod : ProtoDataReaderTests + { + [Fact] + public void ShouldThrowExceptionWhenDataReaderIsClosed() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: new[] { (byte)0b0010_1010 }); + + dataReader.Close(); + + // Assert + Assert.Throws(() => dataReader.GetBytes(0, 0, new byte[0], 0, 1)); + } + + [Fact] + public void ShouldThrowExceptionWhenNoData() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: new[] { (byte)0b0010_1010 }); + + // Assert + Assert.Throws(() => dataReader.GetBytes(0, 0, new byte[1], 0, 1)); + } + + [Fact] + public void ShouldThrowExceptionWhenIndexIsOutOfRange() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: new[] { (byte)0b0010_1010 }); + + dataReader.Read(); + + // Assert + Assert.Throws(() => dataReader.GetBytes(dataReader.FieldCount, 0, new byte[0], 0, 1)); + } + + [Fact] + public void ShouldThrowExceptionWhenIsNull() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: (string)null); + + dataReader.Read(); + + // Assert + Assert.Throws(() => dataReader.GetBytes(0, 0, new byte[1], 0, 1)); + } + + [Fact] + public void ShouldThrowExceptionWhenFieldOffsetIsLessThanZero() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: new[] { (byte)0b0010_1010 }); + + dataReader.Read(); + + // Assert + Assert.Throws(() => dataReader.GetBytes(0, -1, new byte[1], 0, 1)); + } + + [Fact] + public void ShouldThrowExceptionWhenLengthIsLessThanZero() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: new[] { (byte)0b0010_1010 }); + + dataReader.Read(); + + // Assert + Assert.Throws(() => dataReader.GetBytes(0, 0, new byte[1], 0, -1)); + } + + [Fact] + public void ShouldThrowExceptionWhenBufferOffsetIsLessThanZero() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: new[] { (byte)0b0010_1010 }); + + dataReader.Read(); + + // Assert + Assert.Throws("bufferOffset", () => dataReader.GetBytes(0, 0, new byte[1], -1, 1)); + } + + [Fact] + public void ShouldThrowExceptionWhenBufferOffsetIsGreaterThanBufferSize() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: new[] { (byte)0b0010_1010 }); + + dataReader.Read(); + + // Assert + Assert.Throws("bufferOffset", () => dataReader.GetBytes(0, 0, new byte[1], 10, 1)); + } + + [Fact] + public void ShouldThrowExceptionWhenBufferOffsetIsEqualToBufferSize() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: new[] { (byte)0b0010_1010 }); + + dataReader.Read(); + + // Assert + Assert.Throws("bufferOffset", () => dataReader.GetBytes(0, 0, new byte[1], 1, 1)); + } + + [Fact] + public void ShouldReturnByteArrayLengthWhenBufferIsNull() + { + // Arrange + var value = new[] { (byte)0b0010_1010 }; + var dataReader = DataReaderHelper.CreateDataReader(value: value); + + dataReader.Read(); + + // Act + var result = dataReader.GetBytes(0, 0, null, 0, 1); + + // Assert + Assert.Equal(value.Length, result); + } + + [Fact] + public void ShouldThrowExceptionWhenByteArrayLengthAndBufferOffsetIsGreaterThanBufferLength() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: new[] { (byte)0b0010_1000, (byte)0b1010 }); + + dataReader.Read(); + + // Assert + Assert.Throws(() => dataReader.GetBytes(0, 0, new byte[2], 1, 0)); + } + + [Fact] + public void ShouldReturnZeroWhenFieldOffsetIsGreaterThanByteArrayLength() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: new[] { (byte)0b0010_1010 }); + + dataReader.Read(); + + // Act + var copyLength = dataReader.GetBytes(0, 1, new byte[1], 0, 1); + + // Assert + Assert.Equal(0, copyLength); + } + + [Fact] + public void ShouldAdjustCopyLengthWhenFieldOffsetAndLengthExceedsByteArrayLength() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: new[] { (byte)0b0010_1010 }); + + dataReader.Read(); + + // Act + var copyLength = dataReader.GetBytes(0, 0, new byte[1], 0, 2); + + // Assert + Assert.Equal(1, copyLength); + } + + [Fact] + public void ShouldReturnCorrespondingValue() + { + // Arrange + var value = new[] { (byte)0b0010_1010 }; + var dataReader = DataReaderHelper.CreateDataReader(value: value); + + dataReader.Read(); + + var buffer = new byte[1]; + + // Act + var copyLength = dataReader.GetBytes(0, 0, buffer, 0, 1); + + // Assert + Assert.Equal(value, buffer); + } + } + } +} diff --git a/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetCharMethod.cs b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetCharMethod.cs new file mode 100644 index 0000000..07e2de8 --- /dev/null +++ b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetCharMethod.cs @@ -0,0 +1,75 @@ +// 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 TheGetCharMethod : ProtoDataReaderTests + { + [Fact] + public void ShouldThrowExceptionWhenDataReaderIsClosed() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: 'a'); + + dataReader.Close(); + + // Assert + Assert.Throws(() => dataReader.GetChar(0)); + } + + [Fact] + public void ShouldThrowExceptionWhenNoData() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: 'a'); + + // Assert + Assert.Throws(() => dataReader.GetChar(0)); + } + + [Fact] + public void ShouldThrowExceptionWhenIndexIsOutOfRange() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: 'a'); + + dataReader.Read(); + + // Assert + Assert.Throws(() => dataReader.GetChar(dataReader.FieldCount)); + } + + [Fact] + public void ShouldThrowExceptionWhenIsNull() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: (string)null); + + dataReader.Read(); + + // Assert + Assert.Throws(() => dataReader.GetChar(0)); + } + + [Fact] + public void ShouldReturnCorrespondingValue() + { + // Arrange + var value = 'z'; + var dataReader = DataReaderHelper.CreateDataReader(value: value); + + dataReader.Read(); + + // Act + var result = dataReader.GetChar(0); + + // Assert + Assert.Equal(value, result); + } + } + } +} diff --git a/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetCharsMethod.cs b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetCharsMethod.cs new file mode 100644 index 0000000..5912498 --- /dev/null +++ b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetCharsMethod.cs @@ -0,0 +1,195 @@ +// 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 TheGetCharsMethod : ProtoDataReaderTests + { + [Fact] + public void ShouldThrowExceptionWhenDataReaderIsClosed() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: new[] { 'a' }); + + dataReader.Close(); + + // Assert + Assert.Throws(() => dataReader.GetChars(0, 0, new char[0], 0, 1)); + } + + [Fact] + public void ShouldThrowExceptionWhenNoData() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: new[] { 'a' }); + + // Assert + Assert.Throws(() => dataReader.GetChars(0, 0, new char[1], 0, 1)); + } + + [Fact] + public void ShouldThrowExceptionWhenIndexIsOutOfRange() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: new[] { 'a' }); + + dataReader.Read(); + + // Assert + Assert.Throws(() => dataReader.GetChars(dataReader.FieldCount, 0, new char[0], 0, 1)); + } + + [Fact] + public void ShouldThrowExceptionWhenIsNull() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: (string)null); + + dataReader.Read(); + + // Assert + Assert.Throws(() => dataReader.GetChars(0, 0, new char[1], 0, 1)); + } + + [Fact] + public void ShouldThrowExceptionWhenFieldOffsetIsLessThanZero() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: new[] { 'a' }); + + dataReader.Read(); + + // Assert + Assert.Throws(() => dataReader.GetChars(0, -1, new char[1], 0, 1)); + } + + [Fact] + public void ShouldThrowExceptionWhenLengthIsLessThanZero() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: new[] { 'a' }); + + dataReader.Read(); + + // Assert + Assert.Throws(() => dataReader.GetChars(0, 0, new char[1], 0, -1)); + } + + [Fact] + public void ShouldThrowExceptionWhenBufferOffsetIsLessThanZero() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: new[] { 'a' }); + + dataReader.Read(); + + // Assert + Assert.Throws("bufferOffset", () => dataReader.GetChars(0, 0, new char[1], -1, 1)); + } + + [Fact] + public void ShouldThrowExceptionWhenBufferOffsetIsGreaterThanBufferSize() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: new[] { 'a' }); + + dataReader.Read(); + + // Assert + Assert.Throws("bufferOffset", () => dataReader.GetChars(0, 0, new char[1], 10, 1)); + } + + [Fact] + public void ShouldThrowExceptionWhenBufferOffsetIsEqualToBufferSize() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: new[] { 'a' }); + + dataReader.Read(); + + // Assert + Assert.Throws("bufferOffset", () => dataReader.GetChars(0, 0, new char[1], 1, 1)); + } + + [Fact] + public void ShouldReturnByteArrayLengthWhenBufferIsNull() + { + // Arrange + var value = new[] { 'a' }; + var dataReader = DataReaderHelper.CreateDataReader(value: value); + + dataReader.Read(); + + // Act + var result = dataReader.GetChars(0, 0, null, 0, 1); + + // Assert + Assert.Equal(value.Length, result); + } + + [Fact] + public void ShouldThrowExceptionWhenByteArrayLengthAndBufferOffsetIsGreaterThanBufferLength() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: new[] { 'a', 'b' }); + + dataReader.Read(); + + // Assert + Assert.Throws(() => dataReader.GetChars(0, 0, new char[2], 1, 0)); + } + + [Fact] + public void ShouldReturnZeroWhenFieldOffsetIsGreaterThanByteArrayLength() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: new[] { 'a' }); + + dataReader.Read(); + + // Act + var copyLength = dataReader.GetChars(0, 1, new char[1], 0, 1); + + // Assert + Assert.Equal(0, copyLength); + } + + [Fact] + public void ShouldAdjustCopyLengthWhenFieldOffsetAndLengthExceedsByteArrayLength() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: new[] { 'a' }); + + dataReader.Read(); + + // Act + var copyLength = dataReader.GetChars(0, 0, new char[1], 0, 2); + + // Assert + Assert.Equal(1, copyLength); + } + + [Fact] + public void ShouldReturnCorrespondingValue() + { + // Arrange + var value = new[] { 'a' }; + var dataReader = DataReaderHelper.CreateDataReader(value: value); + + dataReader.Read(); + + var buffer = new char[1]; + + // Act + var copyLength = dataReader.GetChars(0, 0, buffer, 0, 1); + + // Assert + Assert.Equal(value, buffer); + } + } + } +} diff --git a/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetDataTypeName.cs b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetDataTypeName.cs new file mode 100644 index 0000000..c18ba49 --- /dev/null +++ b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetDataTypeName.cs @@ -0,0 +1,39 @@ +// 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 TheGetDataTypeNameMethod : ProtoDataReaderTests + { + [Fact] + public void ShouldThrowExceptionWhenDataReaderIsClosed() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: "foo"); + + dataReader.Close(); + + // Assert + Assert.Throws(() => dataReader.GetDataTypeName(0)); + } + + [Fact] + public void ShouldReturnCorrespondingDataTypeName() + { + // Arrange + var value = "foo"; + var dataReader = DataReaderHelper.CreateDataReader(value: value); + + // Act + var result = dataReader.GetDataTypeName(0); + + // Assert + Assert.Equal(value.GetType().Name, result); + } + } + } +} diff --git a/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetDateTimeMethod.cs b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetDateTimeMethod.cs new file mode 100644 index 0000000..5873543 --- /dev/null +++ b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetDateTimeMethod.cs @@ -0,0 +1,75 @@ +// 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 TheGetDateTimeMethod : ProtoDataReaderTests + { + [Fact] + public void ShouldThrowExceptionWhenDataReaderIsClosed() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: DateTime.Now); + + dataReader.Close(); + + // Assert + Assert.Throws(() => dataReader.GetDateTime(0)); + } + + [Fact] + public void ShouldThrowExceptionWhenNoData() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: DateTime.Now); + + // Assert + Assert.Throws(() => dataReader.GetDateTime(0)); + } + + [Fact] + public void ShouldThrowExceptionWhenIndexIsOutOfRange() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: DateTime.Now); + + dataReader.Read(); + + // Assert + Assert.Throws(() => dataReader.GetDateTime(dataReader.FieldCount)); + } + + [Fact] + public void ShouldThrowExceptionWhenIsNull() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: (string)null); + + dataReader.Read(); + + // Assert + Assert.Throws(() => dataReader.GetDateTime(0)); + } + + [Fact] + public void ShouldReturnCorrespondingValue() + { + // Arrange + var value = DateTime.Now; + var dataReader = DataReaderHelper.CreateDataReader(value: value); + + dataReader.Read(); + + // Act + var result = dataReader.GetDateTime(0); + + // Assert + Assert.Equal(value, result); + } + } + } +} diff --git a/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetDecimalMethod.cs b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetDecimalMethod.cs new file mode 100644 index 0000000..97b4de3 --- /dev/null +++ b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetDecimalMethod.cs @@ -0,0 +1,75 @@ +// 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 TheGetDecimalMethod : ProtoDataReaderTests + { + [Fact] + public void ShouldThrowExceptionWhenDataReaderIsClosed() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: decimal.MinValue); + + dataReader.Close(); + + // Assert + Assert.Throws(() => dataReader.GetDecimal(0)); + } + + [Fact] + public void ShouldThrowExceptionWhenNoData() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: decimal.MinValue); + + // Assert + Assert.Throws(() => dataReader.GetDecimal(0)); + } + + [Fact] + public void ShouldThrowExceptionWhenIndexIsOutOfRange() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: decimal.MinValue); + + dataReader.Read(); + + // Assert + Assert.Throws(() => dataReader.GetDecimal(dataReader.FieldCount)); + } + + [Fact] + public void ShouldThrowExceptionWhenIsNull() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: (string)null); + + dataReader.Read(); + + // Assert + Assert.Throws(() => dataReader.GetDecimal(0)); + } + + [Fact] + public void ShouldReturnCorrespondingValue() + { + // Arrange + var value = decimal.MinValue; + var dataReader = DataReaderHelper.CreateDataReader(value: value); + + dataReader.Read(); + + // Act + var result = dataReader.GetDecimal(0); + + // Assert + Assert.Equal(value, result); + } + } + } +} diff --git a/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetDoubleMethod.cs b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetDoubleMethod.cs new file mode 100644 index 0000000..77fde37 --- /dev/null +++ b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetDoubleMethod.cs @@ -0,0 +1,75 @@ +// 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 TheGetDoubleMethod : ProtoDataReaderTests + { + [Fact] + public void ShouldThrowExceptionWhenDataReaderIsClosed() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: double.MinValue); + + dataReader.Close(); + + // Assert + Assert.Throws(() => dataReader.GetDouble(0)); + } + + [Fact] + public void ShouldThrowExceptionWhenNoData() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: double.MinValue); + + // Assert + Assert.Throws(() => dataReader.GetDouble(0)); + } + + [Fact] + public void ShouldThrowExceptionWhenIndexIsOutOfRange() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: double.MinValue); + + dataReader.Read(); + + // Assert + Assert.Throws(() => dataReader.GetDouble(dataReader.FieldCount)); + } + + [Fact] + public void ShouldThrowExceptionWhenIsNull() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: (string)null); + + dataReader.Read(); + + // Assert + Assert.Throws(() => dataReader.GetDouble(0)); + } + + [Fact] + public void ShouldReturnCorrespondingValue() + { + // Arrange + var value = double.MinValue; + var dataReader = DataReaderHelper.CreateDataReader(value: value); + + dataReader.Read(); + + // Act + var result = dataReader.GetDouble(0); + + // Assert + Assert.Equal(value, result); + } + } + } +} diff --git a/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetFieldTypeMethod.cs b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetFieldTypeMethod.cs new file mode 100644 index 0000000..ed2e801 --- /dev/null +++ b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetFieldTypeMethod.cs @@ -0,0 +1,53 @@ +// 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 TheGetFieldTypeMethod : ProtoDataReaderTests + { + [Fact] + public void ShouldThrowExceptionWhenDataReaderIsClosed() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: "foo"); + + dataReader.Close(); + + // Assert + Assert.Throws(() => dataReader.GetFieldType(0)); + } + + [Fact] + public void ShouldThrowExceptionWhenIndexIsOutOfRange() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: "foo"); + + dataReader.Read(); + + // Assert + Assert.Throws(() => dataReader.GetFieldType(dataReader.FieldCount)); + } + + [Fact] + public void ShouldReturnCorrespondingFieldType() + { + // Arrange + var value = "foo"; + var dataReader = DataReaderHelper.CreateDataReader(value: value); + + dataReader.Read(); + + // Act + var result = dataReader.GetFieldType(0); + + // Assert + Assert.Equal(value.GetType(), result); + } + } + } +} diff --git a/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetFloatMethod.cs b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetFloatMethod.cs new file mode 100644 index 0000000..46fd336 --- /dev/null +++ b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetFloatMethod.cs @@ -0,0 +1,75 @@ +// 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 TheGetFloatMethod : ProtoDataReaderTests + { + [Fact] + public void ShouldThrowExceptionWhenDataReaderIsClosed() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: float.MinValue); + + dataReader.Close(); + + // Assert + Assert.Throws(() => dataReader.GetFloat(0)); + } + + [Fact] + public void ShouldThrowExceptionWhenNoData() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: float.MinValue); + + // Assert + Assert.Throws(() => dataReader.GetFloat(0)); + } + + [Fact] + public void ShouldThrowExceptionWhenIndexIsOutOfRange() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: float.MinValue); + + dataReader.Read(); + + // Assert + Assert.Throws(() => dataReader.GetFloat(dataReader.FieldCount)); + } + + [Fact] + public void ShouldThrowExceptionWhenIsNull() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: (string)null); + + dataReader.Read(); + + // Assert + Assert.Throws(() => dataReader.GetFloat(0)); + } + + [Fact] + public void ShouldReturnCorrespondingValue() + { + // Arrange + var value = float.MinValue; + var dataReader = DataReaderHelper.CreateDataReader(value: value); + + dataReader.Read(); + + // Act + var result = dataReader.GetFloat(0); + + // Assert + Assert.Equal(value, result); + } + } + } +} diff --git a/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetGuidMethod.cs b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetGuidMethod.cs new file mode 100644 index 0000000..d230250 --- /dev/null +++ b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetGuidMethod.cs @@ -0,0 +1,75 @@ +// 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 TheGetGuidMethod : ProtoDataReaderTests + { + [Fact] + public void ShouldThrowExceptionWhenDataReaderIsClosed() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: Guid.NewGuid()); + + dataReader.Close(); + + // Assert + Assert.Throws(() => dataReader.GetGuid(0)); + } + + [Fact] + public void ShouldThrowExceptionWhenNoData() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: Guid.NewGuid()); + + // Assert + Assert.Throws(() => dataReader.GetGuid(0)); + } + + [Fact] + public void ShouldThrowExceptionWhenIndexIsOutOfRange() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: Guid.NewGuid()); + + dataReader.Read(); + + // Assert + Assert.Throws(() => dataReader.GetGuid(dataReader.FieldCount)); + } + + [Fact] + public void ShouldThrowExceptionWhenIsNull() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: (string)null); + + dataReader.Read(); + + // Assert + Assert.Throws(() => dataReader.GetGuid(0)); + } + + [Fact] + public void ShouldReturnCorrespondingValue() + { + // Arrange + var value = Guid.NewGuid(); + var dataReader = DataReaderHelper.CreateDataReader(value: value); + + dataReader.Read(); + + // Act + var result = dataReader.GetGuid(0); + + // Assert + Assert.Equal(value, result); + } + } + } +} diff --git a/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetInt16Method.cs b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetInt16Method.cs new file mode 100644 index 0000000..cc93d57 --- /dev/null +++ b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetInt16Method.cs @@ -0,0 +1,75 @@ +// 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 TheGetInt16Method : ProtoDataReaderTests + { + [Fact] + public void ShouldThrowExceptionWhenDataReaderIsClosed() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: short.MinValue); + + dataReader.Close(); + + // Assert + Assert.Throws(() => dataReader.GetInt16(0)); + } + + [Fact] + public void ShouldThrowExceptionWhenNoData() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: short.MinValue); + + // Assert + Assert.Throws(() => dataReader.GetInt16(0)); + } + + [Fact] + public void ShouldThrowExceptionWhenIndexIsOutOfRange() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: short.MinValue); + + dataReader.Read(); + + // Assert + Assert.Throws(() => dataReader.GetInt16(dataReader.FieldCount)); + } + + [Fact] + public void ShouldThrowExceptionWhenIsNull() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: (string)null); + + dataReader.Read(); + + // Assert + Assert.Throws(() => dataReader.GetInt16(0)); + } + + [Fact] + public void ShouldReturnCorrespondingValue() + { + // Arrange + var value = short.MinValue; + var dataReader = DataReaderHelper.CreateDataReader(value: value); + + dataReader.Read(); + + // Act + var result = dataReader.GetInt16(0); + + // Assert + Assert.Equal(value, result); + } + } + } +} diff --git a/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetInt32Method.cs b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetInt32Method.cs new file mode 100644 index 0000000..c56b172 --- /dev/null +++ b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetInt32Method.cs @@ -0,0 +1,75 @@ +// 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 TheGetInt32Method : ProtoDataReaderTests + { + [Fact] + public void ShouldThrowExceptionWhenDataReaderIsClosed() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: int.MinValue); + + dataReader.Close(); + + // Assert + Assert.Throws(() => dataReader.GetInt32(0)); + } + + [Fact] + public void ShouldThrowExceptionWhenNoData() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: int.MinValue); + + // Assert + Assert.Throws(() => dataReader.GetInt32(0)); + } + + [Fact] + public void ShouldThrowExceptionWhenIndexIsOutOfRange() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: int.MinValue); + + dataReader.Read(); + + // Assert + Assert.Throws(() => dataReader.GetInt32(dataReader.FieldCount)); + } + + [Fact] + public void ShouldThrowExceptionWhenIsNull() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: (string)null); + + dataReader.Read(); + + // Assert + Assert.Throws(() => dataReader.GetInt32(0)); + } + + [Fact] + public void ShouldReturnCorrespondingValue() + { + // Arrange + var value = int.MinValue; + var dataReader = DataReaderHelper.CreateDataReader(value: value); + + dataReader.Read(); + + // Act + var result = dataReader.GetInt32(0); + + // Assert + Assert.Equal(value, result); + } + } + } +} diff --git a/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetInt64Method.cs b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetInt64Method.cs new file mode 100644 index 0000000..5faa5fa --- /dev/null +++ b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetInt64Method.cs @@ -0,0 +1,70 @@ +// 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 TheGetInt64Method : ProtoDataReaderTests + { + [Fact] + public void ShouldThrowExceptionWhenDataReaderIsClosed() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: long.MinValue); + + // Assert + Assert.Throws(() => dataReader.GetInt64(0)); + } + + [Fact] + public void ShouldThrowExceptionWhenNoData() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: long.MinValue); + + // Assert + Assert.Throws(() => dataReader.GetInt64(0)); + } + + [Fact] + public void ShouldThrowExceptionWhenIndexIsOutOfRange() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: long.MinValue); + + dataReader.Read(); + + // Assert + Assert.Throws(() => dataReader.GetInt64(dataReader.FieldCount)); + } + + [Fact] + public void ShouldThrowExceptionWhenIsNull() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: (string)null); + + dataReader.Read(); + + // Assert + Assert.Throws(() => dataReader.GetInt64(0)); + } + + [Fact] + public void ShouldReturnCorrespondingValue() + { + // Arrange + var value = long.MinValue; + var dataReader = DataReaderHelper.CreateDataReader(value: value); + + dataReader.Read(); + + // Assert + Assert.Equal(value, dataReader.GetInt64(0)); + } + } + } +} diff --git a/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetNameMethod.cs b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetNameMethod.cs new file mode 100644 index 0000000..3250c14 --- /dev/null +++ b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetNameMethod.cs @@ -0,0 +1,55 @@ +// Copyright (c) Richard Dingwall, Arjen Post. See LICENSE in the project root for license information. + +using System; +using System.Data; +using Xunit; + +namespace ProtoBuf.Data.Tests +{ + public partial class ProtoDataReaderTests + { + public class TheGetNameMethod : ProtoDataReaderTests + { + [Fact] + public void ShouldThrowExceptionWhenDataReaderIsClosed() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: "foo"); + + dataReader.Close(); + + // Assert + Assert.Throws(() => dataReader.GetName(0)); + } + + [Fact] + public void ShouldThrowExceptionWhenIndexIsOutOfRange() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: "foo"); + + dataReader.Read(); + + // Assert + Assert.Throws(() => dataReader.GetName(dataReader.FieldCount)); + } + + [Fact] + public void ShouldReturnCorrespondingName() + { + // Arrange + var dataTable = new DataTable(); + + dataTable.Columns.Add("foo", typeof(int)); + + var dataReader = DataReaderHelper.ToProtoDataReader(dataTable.CreateDataReader()); + + // Act + var result = dataReader.GetName(0); + + // Assert + Assert.Equal(dataTable.Columns[0].ColumnName, result); + } + } + } +} diff --git a/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetOrdinalMethod.cs b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetOrdinalMethod.cs new file mode 100644 index 0000000..f9c6fbd --- /dev/null +++ b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetOrdinalMethod.cs @@ -0,0 +1,57 @@ +// Copyright (c) Richard Dingwall, Arjen Post. See LICENSE in the project root for license information. + +using System; +using System.Data; +using Xunit; + +namespace ProtoBuf.Data.Tests +{ + public partial class ProtoDataReaderTests + { + public class TheGetOrdinalMethod : ProtoDataReaderTests + { + [Fact] + public void ShouldThrowExceptionWhenDataReaderIsClosed() + { + // Arrange + var dataTable = new DataTable(); + + dataTable.Columns.Add("foo", typeof(int)); + + var dataReader = DataReaderHelper.ToProtoDataReader(dataTable.CreateDataReader()); + + dataReader.Close(); + + // Assert + Assert.Throws(() => dataReader.GetOrdinal(dataTable.Columns[0].ColumnName)); + } + + [Fact] + public void ShouldThrowExceptionWhenIndexIsOutOfRange() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: "foo"); + + dataReader.Read(); + + // Assert + Assert.Throws(() => dataReader.GetOrdinal("bar")); + } + + [Fact] + public void ShouldReturnCorrespondingOrdinal() + { + // Arrange + var dataTable = new DataTable(); + + dataTable.Columns.Add("foo", typeof(int)); + dataTable.Columns.Add("bar", typeof(int)); + + var dataReader = DataReaderHelper.ToProtoDataReader(dataTable.CreateDataReader()); + + // Assert + Assert.Equal(1, dataReader.GetOrdinal(dataTable.Columns[1].ColumnName)); + } + } + } +} diff --git a/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetSchemaTableMethod.cs b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetSchemaTableMethod.cs new file mode 100644 index 0000000..b071b8f --- /dev/null +++ b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetSchemaTableMethod.cs @@ -0,0 +1,111 @@ +// Copyright (c) Richard Dingwall, Arjen Post. See LICENSE in the project root for license information. + +using System; +using System.Data; +using Xunit; + +namespace ProtoBuf.Data.Tests +{ + public partial class ProtoDataReaderTests + { + public class TheGetSchemaTableMethod : ProtoDataReaderTests + { + [Fact] + public void ShouldThrowExceptionWhenDataReaderIsClosed() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: "foo"); + + dataReader.Close(); + + // Assert + Assert.Throws(() => dataReader.GetSchemaTable()); + } + + [Fact] + public void ShouldSetTableName() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: "foo"); + + // Act + var schemaTable = dataReader.GetSchemaTable(); + + // Assert + Assert.Equal("SchemaTable", schemaTable.TableName); + } + + [Fact] + public void ShouldSetColumnName() + { + // Arrange + var dataTable = new DataTable(); + + dataTable.Columns.Add("foo", typeof(int)); + dataTable.Columns.Add("bar", typeof(string)); + + var dataReader = DataReaderHelper.ToProtoDataReader(dataTable.CreateDataReader()); + + // Act + var schemaTable = dataReader.GetSchemaTable(); + + // Assert + Assert.Equal(dataTable.Columns[1].ColumnName, schemaTable.Rows[1]["ColumnName"]); + } + + [Fact] + public void ShouldSetColumnOrdinal() + { + // Arrange + var dataTable = new DataTable(); + + dataTable.Columns.Add("foo", typeof(int)); + dataTable.Columns.Add("bar", typeof(string)); + + var dataReader = DataReaderHelper.ToProtoDataReader(dataTable.CreateDataReader()); + + // Act + var schemaTable = dataReader.GetSchemaTable(); + + // Assert + Assert.Equal(1, schemaTable.Rows[1]["ColumnOrdinal"]); + } + + [Fact] + public void ShouldSetDefaultColumnSize() + { + // Arrange + var dataTable = new DataTable(); + + dataTable.Columns.Add("foo", typeof(int)); + dataTable.Columns.Add("bar", typeof(string)); + + var dataReader = DataReaderHelper.ToProtoDataReader(dataTable.CreateDataReader()); + + // Act + var schemaTable = dataReader.GetSchemaTable(); + + // Assert + Assert.Equal(-1, schemaTable.Rows[1]["ColumnSize"]); + } + + [Fact] + public void ShouldSetDataType() + { + // Arrange + var dataTable = new DataTable(); + + dataTable.Columns.Add("foo", typeof(int)); + dataTable.Columns.Add("bar", typeof(string)); + + var dataReader = DataReaderHelper.ToProtoDataReader(dataTable.CreateDataReader()); + + // Act + var schemaTable = dataReader.GetSchemaTable(); + + // Assert + Assert.Equal(dataTable.Columns[1].DataType, schemaTable.Rows[1]["DataType"]); + } + } + } +} diff --git a/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetStringMethod.cs b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetStringMethod.cs new file mode 100644 index 0000000..b4a3077 --- /dev/null +++ b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetStringMethod.cs @@ -0,0 +1,73 @@ +// 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 TheGetStringMethod : ProtoDataReaderTests + { + [Fact] + public void ShouldThrowExceptionWhenDataReaderIsClosed() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: "foo"); + + // Assert + Assert.Throws(() => dataReader.GetString(0)); + } + + [Fact] + public void ShouldThrowExceptionWhenNoData() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: "foo"); + + // Assert + Assert.Throws(() => dataReader.GetString(0)); + } + + [Fact] + public void ShouldThrowExceptionWhenIndexIsOutOfRange() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: "foo"); + + dataReader.Read(); + + // Assert + Assert.Throws(() => dataReader.GetString(dataReader.FieldCount)); + } + + [Fact] + public void ShouldThrowExceptionWhenIsNull() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: (string)null); + + dataReader.Read(); + + // Assert + Assert.Throws(() => dataReader.GetString(0)); + } + + [Fact] + public void ShouldReturnCorrespondingValue() + { + // Arrange + var value = "foo"; + var dataReader = DataReaderHelper.CreateDataReader(value: value); + + dataReader.Read(); + + // Act + var result = dataReader.GetString(0); + + // Assert + Assert.Equal(value, result); + } + } + } +} diff --git a/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetValueMethod.cs b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetValueMethod.cs new file mode 100644 index 0000000..83f547b --- /dev/null +++ b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetValueMethod.cs @@ -0,0 +1,63 @@ +// 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 TheGetValueMethod : ProtoDataReaderTests + { + [Fact] + public void ShouldThrowExceptionWhenDataReaderIsClosed() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: "foo"); + + dataReader.Close(); + + // Assert + Assert.Throws(() => dataReader.GetValue(0)); + } + + [Fact] + public void ShouldThrowExceptionWhenNoData() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: "foo"); + + // Assert + Assert.Throws(() => dataReader.GetValue(0)); + } + + [Fact] + public void ShouldThrowExceptionWhenIndexIsOutOfRange() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: "foo"); + + dataReader.Read(); + + // Assert + Assert.Throws(() => dataReader.GetValue(dataReader.FieldCount)); + } + + [Fact] + public void ShouldReturnCorrespondingValue() + { + // Arrange + var value = "foo"; + var dataReader = DataReaderHelper.CreateDataReader(value: value); + + dataReader.Read(); + + // Act + var result = dataReader.GetValue(0); + + // Assert + Assert.Equal(value, Convert.ToString(result)); + } + } + } +} diff --git a/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetValuesMethod.cs b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetValuesMethod.cs new file mode 100644 index 0000000..da06a81 --- /dev/null +++ b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheGetValuesMethod.cs @@ -0,0 +1,90 @@ +// Copyright (c) Richard Dingwall, Arjen Post. See LICENSE in the project root for license information. + +using System; +using System.Data; +using Xunit; + +namespace ProtoBuf.Data.Tests +{ + public partial class ProtoDataReaderTests + { + public class TheGetValuesMethod : ProtoDataReaderTests + { + [Fact] + public void ShouldThrowExceptionWhenValuesIsNull() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: "foo"); + + dataReader.Close(); + + // Assert + Assert.Throws("values", () => dataReader.GetValues(null)); + } + + [Fact] + public void ShouldThrowExceptionWhenNoData() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: "foo"); + + // Assert + Assert.Throws(() => dataReader.GetValues(new object[1])); + } + + [Fact] + public void ShouldThrowExceptionWhenDataReaderIsClosed() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: "foo"); + + dataReader.Close(); + + // Assert + Assert.Throws(() => dataReader.GetValues(new object[1])); + } + + [Fact] + public void ShouldReturnCorrespondingValues() + { + // Arrange + var value = "foo"; + var dataReader = DataReaderHelper.CreateDataReader(value: value); + + dataReader.Read(); + + var result = new object[1]; + + // Act + dataReader.GetValues(result); + + // Assert + Assert.Equal(new[] { value }, result); + } + + [Fact] + public void ShouldReturnCorrespondingValuesWithSmallerArray() + { + // Arrange + var dataTable = new DataTable(); + + dataTable.Columns.Add("foo", typeof(int)); + dataTable.Columns.Add("bar", typeof(int)); + + dataTable.Rows.Add(1, 2); + + var dataReader = DataReaderHelper.ToProtoDataReader(dataTable.CreateDataReader()); + + dataReader.Read(); + + var result = new object[1]; + + // Act + dataReader.GetValues(result); + + // Assert + Assert.Equal(new[] { dataTable.Rows[0][0] }, result); + } + } + } +} diff --git a/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheIsDBNullMethod.cs b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheIsDBNullMethod.cs new file mode 100644 index 0000000..43c4c82 --- /dev/null +++ b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheIsDBNullMethod.cs @@ -0,0 +1,71 @@ +// 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 TheIsDBNullMethod : ProtoDataReaderTests + { + [Fact] + public void ShouldThrowExceptionWhenDataReaderIsClosed() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: "foo"); + + dataReader.Close(); + + // Assert + Assert.Throws(() => dataReader.IsDBNull(0)); + } + + [Fact] + public void ShouldThrowExceptionWhenNoData() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: "foo"); + + // Assert + Assert.Throws(() => dataReader.IsDBNull(0)); + } + + [Fact] + public void ShouldThrowExceptionWhenIndexIsOutOfRange() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: "foo"); + + dataReader.Read(); + + // Assert + Assert.Throws(() => dataReader.IsDBNull(dataReader.FieldCount)); + } + + [Fact] + public void ShouldReturnTrueIfNull() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: (string)null); + + dataReader.Read(); + + // Assert + Assert.True(dataReader.IsDBNull(0)); + } + + [Fact] + public void ShouldReturnFalseIfNotNull() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: "foo"); + + dataReader.Read(); + + // Assert + Assert.False(dataReader.IsDBNull(0)); + } + } + } +} diff --git a/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheNextResultMethod.cs b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheNextResultMethod.cs new file mode 100644 index 0000000..0e929b2 --- /dev/null +++ b/test/protobuf-net-data.Tests/ProtoBufDataReaderTests.TheNextResultMethod.cs @@ -0,0 +1,118 @@ +// Copyright (c) Richard Dingwall, Arjen Post. See LICENSE in the project root for license information. + +using System; +using System.Data; +using Xunit; + +namespace ProtoBuf.Data.Tests +{ + public partial class ProtoDataReaderTests + { + public class TheNextResultMethod : ProtoDataReaderTests + { + [Fact] + public void ShouldThrowExceptionWhenDataReaderIsClosed() + { + // Arrange + var dataReader = DataReaderHelper.CreateDataReader(value: "foo"); + + dataReader.Close(); + + // Assert + Assert.Throws(() => dataReader.NextResult()); + } + + [Fact] + public void ShouldNotCloseWhenNoMoreResults() + { + // Act + var dataReader = DataReaderHelper.CreateDataReader(value: (string)null); + + dataReader.NextResult(); + + // Assert + Assert.False(dataReader.IsClosed); + } + + [Fact] + public void ShouldReturnFalseWhenNoMoreResults() + { + // Act + var dataReader = DataReaderHelper.CreateDataReader(value: (string)null); + + // Act + var result = dataReader.NextResult(); + + // Assert + Assert.False(result); + } + + [Fact] + public void ShouldReturnFalseWhenMoreResults() + { + // Act + var dataSet = new DataSet(); + + dataSet.Tables.Add(new DataTable()); + dataSet.Tables.Add(new DataTable()); + + var dataReader = DataReaderHelper.ToProtoDataReader(dataSet.CreateDataReader()); + + // Act + var result = dataReader.NextResult(); + + // Assert + Assert.True(result); + } + + [Fact] + public void ShouldReadRemainingRecords() + { + // Act + var dataSet = new DataSet(); + + dataSet.Tables.Add(new DataTable()); + dataSet.Tables.Add(new DataTable()); + + dataSet.Tables[0].Columns.Add("foo", typeof(string)); + dataSet.Tables[1].Columns.Add("bar", typeof(string)); + + dataSet.Tables[0].Rows.Add("baz"); + dataSet.Tables[1].Rows.Add("qux"); + + var dataReader = DataReaderHelper.ToProtoDataReader(dataSet.CreateDataReader()); + + // Act + dataReader.NextResult(); + + // Assert + dataReader.Read(); + + Assert.Equal(dataSet.Tables[1].Rows[0][0], dataReader.GetValue(0)); + } + + [Fact] + public void ShouldChangeColumns() + { + // Act + var dataSet = new DataSet(); + + dataSet.Tables.Add(new DataTable()); + dataSet.Tables.Add(new DataTable()); + + dataSet.Tables[0].Columns.Add("foo", typeof(string)); + dataSet.Tables[1].Columns.Add("bar", typeof(string)); + + var dataReader = DataReaderHelper.ToProtoDataReader(dataSet.CreateDataReader()); + + // Act + dataReader.NextResult(); + + // Assert + dataReader.Read(); + + Assert.Equal(dataSet.Tables[1].Columns[0].ColumnName, dataReader.GetName(0)); + } + } + } +}