StreamExtensions mit UnitTests versehen
This commit is contained in:
138
AMWD.Common.Tests/Extensions/StreamExtensionsTests.cs
Normal file
138
AMWD.Common.Tests/Extensions/StreamExtensionsTests.cs
Normal file
@@ -0,0 +1,138 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
namespace AMWD.Common.Tests.Extensions
|
||||
{
|
||||
[TestClass]
|
||||
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)
|
||||
{ }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,27 +1,36 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AMWD.Common.Extensions
|
||||
namespace System.IO
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides extension methods for the <see cref="Stream"/>.
|
||||
/// </summary>
|
||||
public static class StreamExtensions
|
||||
{
|
||||
public static string ReadLine(this Stream stream, Encoding encoding = null, string eol = null)
|
||||
/// <summary>
|
||||
/// Reads a line from a <see cref="Stream"/>.
|
||||
/// </summary>
|
||||
/// <param name="stream">The stream to read from.</param>
|
||||
/// <param name="encoding">The encoding to use.</param>
|
||||
/// <param name="eol">The character determinating a line end.</param>
|
||||
/// <returns></returns>
|
||||
public static string ReadLine(this Stream stream, Encoding encoding = null, char? eol = null)
|
||||
{
|
||||
if (encoding == null)
|
||||
encoding = Encoding.Default;
|
||||
|
||||
if (eol == null)
|
||||
eol = Environment.NewLine;
|
||||
eol = Environment.NewLine.Last();
|
||||
|
||||
if (!stream.CanRead)
|
||||
return null;
|
||||
|
||||
var bytes = new List<byte>();
|
||||
string s;
|
||||
char ch;
|
||||
do
|
||||
{
|
||||
int result = stream.ReadByte();
|
||||
@@ -30,37 +39,45 @@ namespace AMWD.Common.Extensions
|
||||
|
||||
byte b = (byte)result;
|
||||
bytes.Add(b);
|
||||
s = encoding.GetString(new[] { b });
|
||||
ch = (char)result;
|
||||
}
|
||||
while (!eol.Contains(s));
|
||||
while (ch != eol);
|
||||
|
||||
return encoding.GetString(bytes.ToArray()).Trim();
|
||||
}
|
||||
|
||||
public static async Task<string> ReadLineAsync(this Stream stream, Encoding encoding = null, string eol = null, CancellationToken cancellationToken = default)
|
||||
/// <summary>
|
||||
/// Reads a line from a <see cref="Stream"/> asynchronous.
|
||||
/// </summary>
|
||||
/// <param name="stream">The stream to read from.</param>
|
||||
/// <param name="encoding">The encoding to use.</param>
|
||||
/// <param name="eol">The character determinating a line end.</param>
|
||||
/// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
|
||||
/// <returns></returns>
|
||||
public static async Task<string> ReadLineAsync(this Stream stream, Encoding encoding = null, char? eol = null, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (encoding == null)
|
||||
encoding = Encoding.Default;
|
||||
|
||||
if (eol == null)
|
||||
eol = Environment.NewLine;
|
||||
eol = Environment.NewLine.Last();
|
||||
|
||||
if (!stream.CanRead)
|
||||
return null;
|
||||
|
||||
var bytes = new List<byte>();
|
||||
string s;
|
||||
char ch;
|
||||
do
|
||||
{
|
||||
byte[] buffer = new byte[1];
|
||||
int count = await stream.ReadAsync(buffer, 0, buffer.Length);
|
||||
int count = await stream.ReadAsync(buffer, 0, buffer.Length, cancellationToken);
|
||||
if (count == 0)
|
||||
break;
|
||||
|
||||
bytes.Add(buffer[0]);
|
||||
s = encoding.GetString(buffer);
|
||||
ch = (char)buffer[0];
|
||||
}
|
||||
while (!eol.Contains(s));
|
||||
while (ch != eol);
|
||||
|
||||
return encoding.GetString(bytes.ToArray()).Trim();
|
||||
}
|
||||
|
||||
@@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
### Added
|
||||
- CHANGELOG
|
||||
- `HtmlHelper.IsDarkColor` to classify a color as dark or light one (by luminance)
|
||||
- `ReadLine` and `ReadLineAsync` as `StreamExtensions`
|
||||
|
||||
|
||||
### Changed
|
||||
- Unit-Tests enhanced
|
||||
|
||||
Reference in New Issue
Block a user