140 lines
3.0 KiB
C#
140 lines
3.0 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
namespace AMWD.Common.Tests.Extensions
|
|
{
|
|
[TestClass]
|
|
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
|
|
public class StreamExtensionsTests
|
|
{
|
|
[TestMethod]
|
|
public void ShouldReadLineFromStreamSynchronous()
|
|
{
|
|
// arrange
|
|
var sb = new StringBuilder();
|
|
sb.AppendLine("First Line");
|
|
sb.AppendLine("Second Line");
|
|
byte[] buffer = Encoding.UTF8.GetBytes(sb.ToString().Trim());
|
|
var stream = new MemoryStream(buffer);
|
|
|
|
// act
|
|
string line = stream.ReadLine();
|
|
|
|
// assert
|
|
Assert.AreEqual("First Line", line);
|
|
|
|
stream.Dispose();
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ShouldReadUntilEndAsLineSynchronous()
|
|
{
|
|
// arrange
|
|
byte[] buffer = Encoding.UTF8.GetBytes("Single Line");
|
|
var stream = new MemoryStream(buffer);
|
|
|
|
// act
|
|
string line = stream.ReadLine();
|
|
|
|
// assert
|
|
Assert.AreEqual("Single Line", line);
|
|
|
|
stream.Dispose();
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ShouldReturnNullWhenNotReadableSynchronous()
|
|
{
|
|
// arrange
|
|
var stream = new WriteOnlyStream();
|
|
|
|
// act
|
|
string line = stream.ReadLine();
|
|
|
|
// assert
|
|
Assert.IsNull(line);
|
|
|
|
stream.Dispose();
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task ShouldReadLineFromStreamAsynchronous()
|
|
{
|
|
// arrange
|
|
var sb = new StringBuilder();
|
|
sb.AppendLine("First Line");
|
|
sb.AppendLine("Second Line");
|
|
byte[] buffer = Encoding.UTF8.GetBytes(sb.ToString().Trim());
|
|
var stream = new MemoryStream(buffer);
|
|
|
|
// act
|
|
string line = await stream.ReadLineAsync();
|
|
|
|
// assert
|
|
Assert.AreEqual("First Line", line);
|
|
|
|
stream.Dispose();
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task ShouldReadUntilEndAsLineAsynchronous()
|
|
{
|
|
// arrange
|
|
byte[] buffer = Encoding.UTF8.GetBytes("Single Line");
|
|
var stream = new MemoryStream(buffer);
|
|
|
|
// act
|
|
string line = await stream.ReadLineAsync();
|
|
|
|
// assert
|
|
Assert.AreEqual("Single Line", line);
|
|
|
|
stream.Dispose();
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task ShouldReturnNullWhenNotReadableAsynchronous()
|
|
{
|
|
// arrange
|
|
var stream = new WriteOnlyStream();
|
|
|
|
// act
|
|
string line = await stream.ReadLineAsync();
|
|
|
|
// assert
|
|
Assert.IsNull(line);
|
|
|
|
stream.Dispose();
|
|
}
|
|
|
|
private class WriteOnlyStream : Stream
|
|
{
|
|
public override bool CanRead => false;
|
|
|
|
public override bool CanSeek => false;
|
|
|
|
public override bool CanWrite => true;
|
|
|
|
public override long Length => 0;
|
|
|
|
public override long Position { get => 0; set => throw new NotImplementedException(); }
|
|
|
|
public override void Flush()
|
|
{ }
|
|
|
|
public override int Read(byte[] buffer, int offset, int count) => 0;
|
|
|
|
public override long Seek(long offset, SeekOrigin origin) => 0;
|
|
|
|
public override void SetLength(long value)
|
|
{ }
|
|
|
|
public override void Write(byte[] buffer, int offset, int count)
|
|
{ }
|
|
}
|
|
}
|
|
}
|