Skip to content

Commit

Permalink
increase code coverage and some code changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dotarj committed Mar 24, 2018
1 parent f1631cf commit c469638
Show file tree
Hide file tree
Showing 27 changed files with 2,232 additions and 44 deletions.
249 changes: 205 additions & 44 deletions src/protobuf-net-data/ProtoDataReader.cs

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions src/protobuf-net-data/Throw.cs
Original file line number Diff line number Diff line change
@@ -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>(T value, string parameterName)
{
if (value == null)
{
throw new ArgumentNullException(parameterName);
}
}
}
}
32 changes: 32 additions & 0 deletions test/protobuf-net-data.Tests/DataReaderHelper.cs
Original file line number Diff line number Diff line change
@@ -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>(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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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<ObjectDisposedException>(() => memoryStream.Position = 0);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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<InvalidOperationException>(() => dataReader.GetBoolean(0));
}

[Fact]
public void ShouldThrowExceptionWhenNoData()
{
// Arrange
var dataReader = DataReaderHelper.CreateDataReader(value: true);

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

[Fact]
public void ShouldThrowExceptionWhenIndexIsOutOfRange()
{
// Arrange
var dataReader = DataReaderHelper.CreateDataReader(value: true);

dataReader.Read();

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

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

dataReader.Read();

// Assert
Assert.Throws<InvalidOperationException>(() => 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);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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<InvalidOperationException>(() => dataReader.GetByte(1));
}

[Fact]
public void ShouldThrowExceptionWhenNoData()
{
// Arrange
var dataReader = DataReaderHelper.CreateDataReader(value: (byte)0b0010_1010);

// Assert
Assert.Throws<InvalidOperationException>(() => dataReader.GetByte(1));
}

[Fact]
public void ShouldThrowExceptionWhenIndexIsOutOfRange()
{
// Arrange
var dataReader = DataReaderHelper.CreateDataReader(value: (byte)0b0010_1010);

dataReader.Read();

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

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

dataReader.Read();

// Assert
Assert.Throws<InvalidOperationException>(() => 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);
}
}
}
}
Loading

0 comments on commit c469638

Please sign in to comment.