Renamed from DotnetStreams to DotnetStandardStreams.

This commit is contained in:
Phil Gilmore 2025-05-19 18:02:03 -06:00
parent 1e9844143c
commit 1d7cefb207
25 changed files with 1270 additions and 1270 deletions

View File

@ -1,37 +1,37 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.13.35919.96 d17.13
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotnetStreams", "DotnetStreams\DotnetStreams.csproj", "{42612FD1-4024-4091-B62F-3A897A4BBEF2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotnetStreamsTests", "DotnetStreamsTests\DotnetStreamsTests.csproj", "{1CE38FCB-E08F-8213-356A-F21626719E30}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotnetStreamsApp", "DotnetStreamsApp\DotnetStreamsApp.csproj", "{7EF73074-AAA6-13D4-5EF8-6D91B220C690}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{42612FD1-4024-4091-B62F-3A897A4BBEF2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{42612FD1-4024-4091-B62F-3A897A4BBEF2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{42612FD1-4024-4091-B62F-3A897A4BBEF2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{42612FD1-4024-4091-B62F-3A897A4BBEF2}.Release|Any CPU.Build.0 = Release|Any CPU
{1CE38FCB-E08F-8213-356A-F21626719E30}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1CE38FCB-E08F-8213-356A-F21626719E30}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1CE38FCB-E08F-8213-356A-F21626719E30}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1CE38FCB-E08F-8213-356A-F21626719E30}.Release|Any CPU.Build.0 = Release|Any CPU
{7EF73074-AAA6-13D4-5EF8-6D91B220C690}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7EF73074-AAA6-13D4-5EF8-6D91B220C690}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7EF73074-AAA6-13D4-5EF8-6D91B220C690}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7EF73074-AAA6-13D4-5EF8-6D91B220C690}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {56B0A117-876A-43B1-9E7E-3966279BC007}
EndGlobalSection
EndGlobal

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.13.35919.96
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotnetStandardStreams", "DotnetStandardStreams\DotnetStandardStreams.csproj", "{42612FD1-4024-4091-B62F-3A897A4BBEF2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotnetStandardStreamsTests", "DotnetStandardStreamsTests\DotnetStandardStreamsTests.csproj", "{1CE38FCB-E08F-8213-356A-F21626719E30}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotnetStandardStreamsApp", "DotnetStandardStreamsApp\DotnetStandardStreamsApp.csproj", "{7EF73074-AAA6-13D4-5EF8-6D91B220C690}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{42612FD1-4024-4091-B62F-3A897A4BBEF2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{42612FD1-4024-4091-B62F-3A897A4BBEF2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{42612FD1-4024-4091-B62F-3A897A4BBEF2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{42612FD1-4024-4091-B62F-3A897A4BBEF2}.Release|Any CPU.Build.0 = Release|Any CPU
{1CE38FCB-E08F-8213-356A-F21626719E30}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1CE38FCB-E08F-8213-356A-F21626719E30}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1CE38FCB-E08F-8213-356A-F21626719E30}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1CE38FCB-E08F-8213-356A-F21626719E30}.Release|Any CPU.Build.0 = Release|Any CPU
{7EF73074-AAA6-13D4-5EF8-6D91B220C690}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7EF73074-AAA6-13D4-5EF8-6D91B220C690}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7EF73074-AAA6-13D4-5EF8-6D91B220C690}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7EF73074-AAA6-13D4-5EF8-6D91B220C690}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {56B0A117-876A-43B1-9E7E-3966279BC007}
EndGlobalSection
EndGlobal

View File

@ -1,30 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DotnetStreams
{
public class AnonOutputTarget : IOutputTarget
{
private readonly Action<string> outputProc;
public AnonOutputTarget(Action<string> outputProc)
{
this.outputProc = outputProc;
}
public virtual void Close()
{
}
public virtual void Open()
{
}
public virtual void Output(string line)
{
this.outputProc?.Invoke(line);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DotnetStandardStreams
{
public class AnonOutputTarget : IOutputTarget
{
private readonly Action<string> outputProc;
public AnonOutputTarget(Action<string> outputProc)
{
this.outputProc = outputProc;
}
public virtual void Close()
{
}
public virtual void Open()
{
}
public virtual void Output(string line)
{
this.outputProc?.Invoke(line);
}
}
}

View File

@ -1,58 +1,58 @@
using System;
namespace DotnetStreams
{
public class BytesReadEventArgs
{
public byte[] Data { get; }
public int Size { get; }
public BytesReadEventArgs(byte[] data, int size)
{
this.Data = data;
this.Size = size;
}
}
public delegate void BytesReadEventHandler(object sender, BytesReadEventArgs e);
public class BinaryStdinReader
{
protected readonly Action<byte[], int> dataReceiverProc;
protected readonly Action<int> doneProc;
public event BytesReadEventHandler OnBytesRead;
public event EventHandler OnDone;
public BinaryStdinReader(Action<byte[], int> dataReceiverProc, Action<int> doneProc)
{
this.dataReceiverProc = dataReceiverProc;
this.doneProc = doneProc;
}
public BinaryStdinReader()
{
}
public void ReadBytes(Action<byte[], int> receiverProc, Action<int> doneProc)
{
using System.IO.Stream stdinStream = System.Console.OpenStandardInput();
int totalBytesRead = 0;
int bufferSize = 2048;
byte[] buffer = new byte[bufferSize];
int bytesRead = stdinStream.Read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
OnBytesRead?.Invoke(this, new BytesReadEventArgs(buffer, bytesRead));
receiverProc?.Invoke(buffer, bytesRead);
if (bytesRead < bufferSize)
break;
bytesRead = stdinStream.Read(buffer, 0, bufferSize);
totalBytesRead += bytesRead;
}
OnDone?.Invoke(this, new EventArgs());
doneProc?.Invoke(totalBytesRead);
}
}
}
using System;
namespace DotnetStandardStreams
{
public class BytesReadEventArgs
{
public byte[] Data { get; }
public int Size { get; }
public BytesReadEventArgs(byte[] data, int size)
{
this.Data = data;
this.Size = size;
}
}
public delegate void BytesReadEventHandler(object sender, BytesReadEventArgs e);
public class BinaryStdinReader
{
protected readonly Action<byte[], int>? dataReceiverProc;
protected readonly Action<int>? doneProc;
public event BytesReadEventHandler? OnBytesRead;
public event EventHandler? OnDone;
public BinaryStdinReader(Action<byte[], int> dataReceiverProc, Action<int> doneProc)
{
this.dataReceiverProc = dataReceiverProc;
this.doneProc = doneProc;
}
public BinaryStdinReader()
{
}
public void ReadBytes(Action<byte[], int> receiverProc, Action<int> doneProc)
{
using System.IO.Stream stdinStream = System.Console.OpenStandardInput();
int totalBytesRead = 0;
int bufferSize = 2048;
byte[] buffer = new byte[bufferSize];
int bytesRead = stdinStream.Read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
OnBytesRead?.Invoke(this, new BytesReadEventArgs(buffer, bytesRead));
receiverProc?.Invoke(buffer, bytesRead);
if (bytesRead < bufferSize)
break;
bytesRead = stdinStream.Read(buffer, 0, bufferSize);
totalBytesRead += bytesRead;
}
OnDone?.Invoke(this, new EventArgs());
doneProc?.Invoke(totalBytesRead);
}
}
}

View File

@ -1,22 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DotnetStreams
{
public class ConsoleOutputTarget : IOutputTarget
{
public virtual void Open()
{
}
public virtual void Output(string line)
{
Console.WriteLine(line);
}
public virtual void Close()
{
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DotnetStandardStreams
{
public class ConsoleOutputTarget : IOutputTarget
{
public virtual void Open()
{
}
public virtual void Output(string line)
{
Console.WriteLine(line);
}
public virtual void Close()
{
}
}
}

View File

@ -1,16 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Phil Gilmore</Authors>
<Description>Classes to seamlessly abstract StdIn, StdOut, coded lists or file streams in your console applications.</Description>
<PackageTags />
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Phil Gilmore</Authors>
<Description>Classes to seamlessly abstract StdIn, StdOut, coded lists or file streams in your console applications.</Description>
<PackageTags />
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>
</Project>

View File

@ -1,71 +1,71 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace DotnetStreams
{
public class FileTextSource : ITextSource
{
protected FileStream? file;
protected StreamReader? reader;
protected string filename;
public FileTextSource(string filename)
{
this.filename = filename;
file = null;
reader = null;
}
public virtual void Open()
{
file = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
reader = new StreamReader(file);
}
public virtual IEnumerable<string> ReadAll()
{
string? line = reader?.ReadLine();
while (line != null)
{
yield return line;
line = reader?.ReadLine();
}
}
public virtual string Read()
{
if (!Eof())
return reader?.ReadLine() ?? string.Empty;
else
return string.Empty;
}
public virtual bool Eof()
{
return reader?.EndOfStream ?? true;
}
public virtual void Close()
{
try
{
file?.Flush();
}
finally
{
try
{
file?.Close();
}
finally
{
file?.Dispose();
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace DotnetStandardStreams
{
public class FileTextSource : ITextSource
{
protected FileStream? file;
protected StreamReader? reader;
protected string filename;
public FileTextSource(string filename)
{
this.filename = filename;
file = null;
reader = null;
}
public virtual void Open()
{
file = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
reader = new StreamReader(file);
}
public virtual IEnumerable<string> ReadAll()
{
string? line = reader?.ReadLine();
while (line != null)
{
yield return line;
line = reader?.ReadLine();
}
}
public virtual string Read()
{
if (!Eof())
return reader?.ReadLine() ?? string.Empty;
else
return string.Empty;
}
public virtual bool Eof()
{
return reader?.EndOfStream ?? true;
}
public virtual void Close()
{
try
{
file?.Flush();
}
finally
{
try
{
file?.Close();
}
finally
{
file?.Dispose();
}
}
}
}
}

View File

@ -1,15 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DotnetStreams
{
public interface IOutputTarget
{
void Open();
void Output(string line);
void Close();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DotnetStandardStreams
{
public interface IOutputTarget
{
void Open();
void Output(string line);
void Close();
}
}

View File

@ -1,17 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DotnetStreams
{
public interface ITextSource
{
void Open();
IEnumerable<string> ReadAll();
string Read();
bool Eof();
void Close();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DotnetStandardStreams
{
public interface ITextSource
{
void Open();
IEnumerable<string> ReadAll();
string Read();
bool Eof();
void Close();
}
}

View File

@ -1,30 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DotnetStreams
{
public class ListOutputTarget : IOutputTarget
{
public IList<string> OutputList { get; }
public ListOutputTarget(IList<string> outputList)
{
this.OutputList = outputList;
}
public virtual void Open()
{
}
public virtual void Output(string text)
{
OutputList.Add(text);
}
public virtual void Close()
{
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DotnetStandardStreams
{
public class ListOutputTarget : IOutputTarget
{
public IList<string> OutputList { get; }
public ListOutputTarget(IList<string> outputList)
{
this.OutputList = outputList;
}
public virtual void Open()
{
}
public virtual void Output(string text)
{
OutputList.Add(text);
}
public virtual void Close()
{
}
}
}

View File

@ -1,71 +1,71 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DotnetStreams
{
public class ListTextSource : ITextSource
{
private bool isEof;
private readonly IEnumerable<string> source;
private IEnumerator<string>? enumerator;
//private string lastValue;
private bool firstIsRead;
private string nextLine;
public ListTextSource(IEnumerable<string> source)
{
this.source = source;
enumerator = null;
firstIsRead = false;
nextLine = string.Empty;
}
public virtual void Open()
{
}
public virtual IEnumerable<string> ReadAll()
{
return source.AsEnumerable();
}
public virtual string Read()
{
if (enumerator == null)
enumerator = source.GetEnumerator();
string thisLine;
if (!firstIsRead)
{
// Read the first, put it in the "last" buffer.
isEof = !enumerator.MoveNext();
nextLine = enumerator.Current;
firstIsRead = true;
}
thisLine = nextLine;
if (!isEof)
{
isEof = !enumerator.MoveNext();
if (!isEof)
nextLine = enumerator.Current;
else
nextLine = string.Empty;
}
return thisLine;
}
public virtual bool Eof() => isEof;
public virtual void Close()
{
enumerator = null;
isEof = false;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DotnetStandardStreams
{
public class ListTextSource : ITextSource
{
private bool isEof;
private readonly IEnumerable<string> source;
private IEnumerator<string>? enumerator;
//private string lastValue;
private bool firstIsRead;
private string nextLine;
public ListTextSource(IEnumerable<string> source)
{
this.source = source;
enumerator = null;
firstIsRead = false;
nextLine = string.Empty;
}
public virtual void Open()
{
}
public virtual IEnumerable<string> ReadAll()
{
return source.AsEnumerable();
}
public virtual string Read()
{
if (enumerator == null)
enumerator = source.GetEnumerator();
string thisLine;
if (!firstIsRead)
{
// Read the first, put it in the "last" buffer.
isEof = !enumerator.MoveNext();
nextLine = enumerator.Current;
firstIsRead = true;
}
thisLine = nextLine;
if (!isEof)
{
isEof = !enumerator.MoveNext();
if (!isEof)
nextLine = enumerator.Current;
else
nextLine = string.Empty;
}
return thisLine;
}
public virtual bool Eof() => isEof;
public virtual void Close()
{
enumerator = null;
isEof = false;
}
}
}

View File

@ -1,26 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DotnetStreams
{
public class ProcessedConsoleOutputTarget : ConsoleOutputTarget
{
protected Func<string, string> processorFunc;
public ProcessedConsoleOutputTarget(Func<string, string> processorFunc) : base()
{
if (processorFunc != null)
this.processorFunc = processorFunc;
else
this.processorFunc = line => line;
}
public override void Output(string line)
{
string output = processorFunc?.Invoke(line) ?? string.Empty;
Console.WriteLine(output);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DotnetStandardStreams
{
public class ProcessedConsoleOutputTarget : ConsoleOutputTarget
{
protected Func<string, string> processorFunc;
public ProcessedConsoleOutputTarget(Func<string, string> processorFunc) : base()
{
if (processorFunc != null)
this.processorFunc = processorFunc;
else
this.processorFunc = line => line;
}
public override void Output(string line)
{
string output = processorFunc?.Invoke(line) ?? string.Empty;
Console.WriteLine(output);
}
}
}

View File

@ -1,47 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DotnetStreams
{
public class StdInTextSource : ITextSource
{
protected bool isEof;
public virtual void Open()
{
isEof = false;
}
public virtual IEnumerable<string> ReadAll()
{
string? s = Console.ReadLine();
while (s != null)
{
yield return s;
s = Console.ReadLine();
}
isEof = true;
}
public virtual string Read()
{
string? s = Console.ReadLine();
if (s == null)
{
isEof = true;
return string.Empty;
}
else
return s;
}
public virtual bool Eof() => isEof;
public virtual void Close()
{
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DotnetStandardStreams
{
public class StdInTextSource : ITextSource
{
protected bool isEof;
public virtual void Open()
{
isEof = false;
}
public virtual IEnumerable<string> ReadAll()
{
string? s = Console.ReadLine();
while (s != null)
{
yield return s;
s = Console.ReadLine();
}
isEof = true;
}
public virtual string Read()
{
string? s = Console.ReadLine();
if (s == null)
{
isEof = true;
return string.Empty;
}
else
return s;
}
public virtual bool Eof() => isEof;
public virtual void Close()
{
}
}
}

View File

@ -1,14 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<Nullable>enable</Nullable>
<WarningsAsErrors>nullable</WarningsAsErrors>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\DotnetStreams\DotnetStreams.csproj" />
</ItemGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<WarningsAsErrors>nullable</WarningsAsErrors>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\DotnetStandardStreams\DotnetStandardStreams.csproj" />
</ItemGroup>
</Project>

View File

@ -1,297 +1,297 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DotnetStreams
{
//public interface ITextSource
//{
// void Open();
// IEnumerable<string> ReadAll();
// string Read();
// bool Eof();
// void Close();
//}
//public class StdInTextSource : ITextSource
//{
// protected bool isEof;
// public virtual void Open()
// {
// isEof = false;
// }
// public virtual IEnumerable<string> ReadAll()
// {
// string? s = Console.ReadLine();
// while (s != null)
// {
// yield return s;
// s = Console.ReadLine();
// }
// isEof = true;
// }
// public virtual string Read()
// {
// string? s = Console.ReadLine();
// if (s == null)
// {
// isEof = true;
// return string.Empty;
// }
// else
// return s;
// }
// public virtual bool Eof() => isEof;
// public virtual void Close()
// {
// }
//}
//public class FileTextSource : ITextSource
//{
// protected FileStream? file;
// protected StreamReader? reader;
// protected string filename;
// public FileTextSource(string filename)
// {
// this.filename = filename;
// file = null;
// reader = null;
// }
// public virtual void Open()
// {
// file = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
// reader = new StreamReader(file);
// }
// public virtual IEnumerable<string> ReadAll()
// {
// string? line = reader?.ReadLine();
// while (line != null)
// {
// yield return line;
// line = reader?.ReadLine();
// }
// }
// public virtual string Read()
// {
// if (!Eof())
// return reader?.ReadLine() ?? string.Empty;
// else
// return string.Empty;
// }
// public virtual bool Eof()
// {
// return reader?.EndOfStream ?? true;
// }
// public virtual void Close()
// {
// try
// {
// file?.Flush();
// }
// finally
// {
// try
// {
// file?.Close();
// }
// finally
// {
// file?.Dispose();
// }
// }
// }
//}
//public class ListTextSource : ITextSource
//{
// private bool isEof;
// private readonly IEnumerable<string> source;
// private IEnumerator<string>? enumerator;
// //private string lastValue;
// private bool firstIsRead;
// private string nextLine;
// public ListTextSource(IEnumerable<string> source)
// {
// this.source = source;
// enumerator = null;
// firstIsRead = false;
// nextLine = string.Empty;
// }
// public virtual void Open()
// {
// }
// public virtual IEnumerable<string> ReadAll()
// {
// return source.AsEnumerable();
// }
// public virtual string Read()
// {
// if (enumerator == null)
// enumerator = source.GetEnumerator();
// string thisLine;
// if (!firstIsRead)
// {
// // Read the first, put it in the "last" buffer.
// isEof = !enumerator.MoveNext();
// nextLine = enumerator.Current;
// firstIsRead = true;
// }
// thisLine = nextLine;
// if (!isEof)
// {
// isEof = !enumerator.MoveNext();
// if (!isEof)
// nextLine = enumerator.Current;
// else
// nextLine = string.Empty;
// }
// return thisLine;
// }
// public virtual bool Eof() => isEof;
// public virtual void Close()
// {
// enumerator = null;
// isEof = false;
// }
//}
//public interface IOutputTarget
//{
// void Open();
// void Output(string line);
// void Close();
//}
//public class ConsoleOutputTarget : IOutputTarget
//{
// public virtual void Open()
// {
// }
// public virtual void Output(string line)
// {
// Console.WriteLine(line);
// }
// public virtual void Close()
// {
// }
//}
//public class ProcessedConsoleOutputTarget : ConsoleOutputTarget
//{
// protected Func<string, string> processorFunc;
// public ProcessedConsoleOutputTarget(Func<string, string> processorFunc) : base()
// {
// if (processorFunc != null)
// this.processorFunc = processorFunc;
// else
// this.processorFunc = line => line;
// }
// public override void Output(string line)
// {
// string output = processorFunc?.Invoke(line) ?? string.Empty;
// Console.WriteLine(output);
// }
//}
//public class ListOutputTarget : IOutputTarget
//{
// public IList<string> OutputList { get; }
// public ListOutputTarget(IList<string> outputList)
// {
// this.OutputList = outputList;
// }
// public virtual void Open()
// {
// }
// public virtual void Output(string text)
// {
// OutputList.Add(text);
// }
// public virtual void Close()
// {
// }
//}
//public class BytesReadEventArgs
//{
// public byte[] Data { get; }
// public int Size { get; }
// public BytesReadEventArgs(byte[] data, int size)
// {
// this.Data = data;
// this.Size = size;
// }
//}
//public delegate void BytesReadEventHandler(object sender, BytesReadEventArgs e);
//public class BinaryStdinReader
//{
// protected readonly Action<byte[], int> dataReceiverProc;
// protected readonly Action<int> doneProc;
// public event BytesReadEventHandler OnBytesRead;
// public event EventHandler OnDone;
// public BinaryStdinReader(Action<byte[], int> dataReceiverProc, Action<int> doneProc)
// {
// this.dataReceiverProc = dataReceiverProc;
// this.doneProc = doneProc;
// }
// public BinaryStdinReader()
// {
// }
// public void ReadBytes(Action<byte[], int> receiverProc, Action<int> doneProc)
// {
// using System.IO.Stream stdinStream = System.Console.OpenStandardInput();
// int totalBytesRead = 0;
// int bufferSize = 2048;
// byte[] buffer = new byte[bufferSize];
// int bytesRead = stdinStream.Read(buffer, 0, bufferSize);
// while (bytesRead > 0)
// {
// OnBytesRead?.Invoke(this, new BytesReadEventArgs(buffer, bytesRead));
// receiverProc?.Invoke(buffer, bytesRead);
// if (bytesRead < bufferSize)
// break;
// bytesRead = stdinStream.Read(buffer, 0, bufferSize);
// totalBytesRead += bytesRead;
// }
// OnDone?.Invoke(this, new EventArgs());
// doneProc?.Invoke(totalBytesRead);
// }
//}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DotnetStandardStreams
{
//public interface ITextSource
//{
// void Open();
// IEnumerable<string> ReadAll();
// string Read();
// bool Eof();
// void Close();
//}
//public class StdInTextSource : ITextSource
//{
// protected bool isEof;
// public virtual void Open()
// {
// isEof = false;
// }
// public virtual IEnumerable<string> ReadAll()
// {
// string? s = Console.ReadLine();
// while (s != null)
// {
// yield return s;
// s = Console.ReadLine();
// }
// isEof = true;
// }
// public virtual string Read()
// {
// string? s = Console.ReadLine();
// if (s == null)
// {
// isEof = true;
// return string.Empty;
// }
// else
// return s;
// }
// public virtual bool Eof() => isEof;
// public virtual void Close()
// {
// }
//}
//public class FileTextSource : ITextSource
//{
// protected FileStream? file;
// protected StreamReader? reader;
// protected string filename;
// public FileTextSource(string filename)
// {
// this.filename = filename;
// file = null;
// reader = null;
// }
// public virtual void Open()
// {
// file = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
// reader = new StreamReader(file);
// }
// public virtual IEnumerable<string> ReadAll()
// {
// string? line = reader?.ReadLine();
// while (line != null)
// {
// yield return line;
// line = reader?.ReadLine();
// }
// }
// public virtual string Read()
// {
// if (!Eof())
// return reader?.ReadLine() ?? string.Empty;
// else
// return string.Empty;
// }
// public virtual bool Eof()
// {
// return reader?.EndOfStream ?? true;
// }
// public virtual void Close()
// {
// try
// {
// file?.Flush();
// }
// finally
// {
// try
// {
// file?.Close();
// }
// finally
// {
// file?.Dispose();
// }
// }
// }
//}
//public class ListTextSource : ITextSource
//{
// private bool isEof;
// private readonly IEnumerable<string> source;
// private IEnumerator<string>? enumerator;
// //private string lastValue;
// private bool firstIsRead;
// private string nextLine;
// public ListTextSource(IEnumerable<string> source)
// {
// this.source = source;
// enumerator = null;
// firstIsRead = false;
// nextLine = string.Empty;
// }
// public virtual void Open()
// {
// }
// public virtual IEnumerable<string> ReadAll()
// {
// return source.AsEnumerable();
// }
// public virtual string Read()
// {
// if (enumerator == null)
// enumerator = source.GetEnumerator();
// string thisLine;
// if (!firstIsRead)
// {
// // Read the first, put it in the "last" buffer.
// isEof = !enumerator.MoveNext();
// nextLine = enumerator.Current;
// firstIsRead = true;
// }
// thisLine = nextLine;
// if (!isEof)
// {
// isEof = !enumerator.MoveNext();
// if (!isEof)
// nextLine = enumerator.Current;
// else
// nextLine = string.Empty;
// }
// return thisLine;
// }
// public virtual bool Eof() => isEof;
// public virtual void Close()
// {
// enumerator = null;
// isEof = false;
// }
//}
//public interface IOutputTarget
//{
// void Open();
// void Output(string line);
// void Close();
//}
//public class ConsoleOutputTarget : IOutputTarget
//{
// public virtual void Open()
// {
// }
// public virtual void Output(string line)
// {
// Console.WriteLine(line);
// }
// public virtual void Close()
// {
// }
//}
//public class ProcessedConsoleOutputTarget : ConsoleOutputTarget
//{
// protected Func<string, string> processorFunc;
// public ProcessedConsoleOutputTarget(Func<string, string> processorFunc) : base()
// {
// if (processorFunc != null)
// this.processorFunc = processorFunc;
// else
// this.processorFunc = line => line;
// }
// public override void Output(string line)
// {
// string output = processorFunc?.Invoke(line) ?? string.Empty;
// Console.WriteLine(output);
// }
//}
//public class ListOutputTarget : IOutputTarget
//{
// public IList<string> OutputList { get; }
// public ListOutputTarget(IList<string> outputList)
// {
// this.OutputList = outputList;
// }
// public virtual void Open()
// {
// }
// public virtual void Output(string text)
// {
// OutputList.Add(text);
// }
// public virtual void Close()
// {
// }
//}
//public class BytesReadEventArgs
//{
// public byte[] Data { get; }
// public int Size { get; }
// public BytesReadEventArgs(byte[] data, int size)
// {
// this.Data = data;
// this.Size = size;
// }
//}
//public delegate void BytesReadEventHandler(object sender, BytesReadEventArgs e);
//public class BinaryStdinReader
//{
// protected readonly Action<byte[], int> dataReceiverProc;
// protected readonly Action<int> doneProc;
// public event BytesReadEventHandler OnBytesRead;
// public event EventHandler OnDone;
// public BinaryStdinReader(Action<byte[], int> dataReceiverProc, Action<int> doneProc)
// {
// this.dataReceiverProc = dataReceiverProc;
// this.doneProc = doneProc;
// }
// public BinaryStdinReader()
// {
// }
// public void ReadBytes(Action<byte[], int> receiverProc, Action<int> doneProc)
// {
// using System.IO.Stream stdinStream = System.Console.OpenStandardInput();
// int totalBytesRead = 0;
// int bufferSize = 2048;
// byte[] buffer = new byte[bufferSize];
// int bytesRead = stdinStream.Read(buffer, 0, bufferSize);
// while (bytesRead > 0)
// {
// OnBytesRead?.Invoke(this, new BytesReadEventArgs(buffer, bytesRead));
// receiverProc?.Invoke(buffer, bytesRead);
// if (bytesRead < bufferSize)
// break;
// bytesRead = stdinStream.Read(buffer, 0, bufferSize);
// totalBytesRead += bytesRead;
// }
// OnDone?.Invoke(this, new EventArgs());
// doneProc?.Invoke(totalBytesRead);
// }
//}
}

View File

@ -1,87 +1,87 @@
using System;
using System.IO;
using System.Linq;
using DotnetStreams;
namespace StreamsTest
{
class Program
{
static void Main(string[] args)
{
//Program p = new();
ITextSource textSource;
IOutputTarget textTarget = new ConsoleOutputTarget();
if (args.Length > 0)
textSource = new FileTextSource(args[0]);
else
textSource = new StdInTextSource();
Execute(
textSource,
textTarget);
}
// Works, but needs to be more compact.
//static void Main(string[] args)
//{
// Program p = new Program();
// ITextSource textSource = null;
// if (args.Length > 0)
// {
// var filename = args[0];
// if (!File.Exists(filename))
// {
// Console.WriteLine($"File not found ({filename}).");
// Environment.Exit(1);
// }
// else
// textSource = new FileTextSource(filename);
// }
// else
// textSource = new StdInTextSource();
// p.Execute(
// textSource,
// new ConsoleOutputTarget());
//}
//public void ExecuteReadLine(string[] args)
//{
// string s = Console.ReadLine();
// // This terminates on blank lines, no surprise
// //while (!string.IsNullOrEmpty(s))
// while (s != null)
// {
// s = s.Replace("\r", "{CR}")
// .Replace("\n", "{LF}")
// .Replace("\t", "{TAB}");
// if (s == string.Empty)
// s = "{EMPTYSTRING}";
// Console.WriteLine($"{s}");
// s = Console.ReadLine();
// }
// if (s == null)
// Console.WriteLine("/s/ is null");
//}
public static void Execute(ITextSource source, IOutputTarget target)
{
source.Open();
target.Open();
foreach (string line in source.ReadAll())
target.Output(line);
target.Close();
source.Close();
}
}
}
using System;
using System.IO;
using System.Linq;
using DotnetStandardStreams;
namespace StreamsTest
{
class Program
{
static void Main(string[] args)
{
//Program p = new();
ITextSource textSource;
IOutputTarget textTarget = new ConsoleOutputTarget();
if (args.Length > 0)
textSource = new FileTextSource(args[0]);
else
textSource = new StdInTextSource();
Execute(
textSource,
textTarget);
}
// Works, but needs to be more compact.
//static void Main(string[] args)
//{
// Program p = new Program();
// ITextSource textSource = null;
// if (args.Length > 0)
// {
// var filename = args[0];
// if (!File.Exists(filename))
// {
// Console.WriteLine($"File not found ({filename}).");
// Environment.Exit(1);
// }
// else
// textSource = new FileTextSource(filename);
// }
// else
// textSource = new StdInTextSource();
// p.Execute(
// textSource,
// new ConsoleOutputTarget());
//}
//public void ExecuteReadLine(string[] args)
//{
// string s = Console.ReadLine();
// // This terminates on blank lines, no surprise
// //while (!string.IsNullOrEmpty(s))
// while (s != null)
// {
// s = s.Replace("\r", "{CR}")
// .Replace("\n", "{LF}")
// .Replace("\t", "{TAB}");
// if (s == string.Empty)
// s = "{EMPTYSTRING}";
// Console.WriteLine($"{s}");
// s = Console.ReadLine();
// }
// if (s == null)
// Console.WriteLine("/s/ is null");
//}
public static void Execute(ITextSource source, IOutputTarget target)
{
source.Open();
target.Open();
foreach (string line in source.ReadAll())
target.Output(line);
target.Close();
source.Close();
}
}
}

View File

@ -1,9 +1,9 @@
{
"profiles": {
"StreamsTest": {
"commandName": "Project",
"commandLineArgs": "C:\\Users\\Phil\\source\\repos\\DotnetStreams\\DotnetStreamsApp\\delme.txt",
"sqlDebugging": true
}
}
{
"profiles": {
"StreamsTest": {
"commandName": "Project",
"commandLineArgs": "C:\\Users\\Phil\\source\\repos\\DotnetStandardStreams\\DotnetStandardStreamsApp\\delme.txt",
"sqlDebugging": true
}
}
}

View File

@ -1,4 +1,4 @@
Line 1
Line 2
Line 1
Line 2
Line 3

View File

@ -1,38 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
using Shouldly;
using DotnetStreams;
using System.IO;
using DotnetStreamsTests.Testables;
namespace DotnetStreamsTests
{
public class ConsoleOutputTargetTests
{
private IOutputTarget outputTarget;
[Fact]
public void WritesToOutputStream()
{
ListWriter writer = new();
outputTarget = new TestableConsoleOutputTarget(writer);
outputTarget.Open();
outputTarget.Output("1");
outputTarget.Output("2");
outputTarget.Output("");
outputTarget.Output("3");
outputTarget.Close();
writer.Lines.Count.ShouldBe(4);
writer.Lines[0].ShouldBe("1");
writer.Lines[1].ShouldBe("2");
writer.Lines[2].ShouldBe(string.Empty);
writer.Lines[3].ShouldBe("3");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
using Shouldly;
using DotnetStandardStreams;
using System.IO;
using DotnetStandardStreamsTests.Testables;
namespace DotnetStandardStreamsTests
{
public class ConsoleOutputTargetTests
{
private IOutputTarget outputTarget;
[Fact]
public void WritesToOutputStream()
{
ListWriter writer = new();
outputTarget = new TestableConsoleOutputTarget(writer);
outputTarget.Open();
outputTarget.Output("1");
outputTarget.Output("2");
outputTarget.Output("");
outputTarget.Output("3");
outputTarget.Close();
writer.Lines.Count.ShouldBe(4);
writer.Lines[0].ShouldBe("1");
writer.Lines[1].ShouldBe("2");
writer.Lines[2].ShouldBe(string.Empty);
writer.Lines[3].ShouldBe("3");
}
}
}

View File

@ -1,27 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="Shouldly" Version="4.0.3" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DotnetStreams\DotnetStreams.csproj" />
</ItemGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="Shouldly" Version="4.0.3" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DotnetStandardStreams\DotnetStandardStreams.csproj" />
</ItemGroup>
</Project>

View File

@ -1,27 +1,27 @@
using DotnetStreams;
using System;
using System.Collections.Generic;
using Xunit;
using Shouldly;
namespace DotnetStreamsTests
{
public class ListOutputTargetTests
{
[Fact]
public void WritesToList()
{
ListOutputTarget target = new(new List<string>());
target.Output("1");
target.Output("2");
target.Output("");
target.Output("3");
target.OutputList.Count.ShouldBe(4);
target.OutputList[0].ShouldBe("1");
target.OutputList[1].ShouldBe("2");
target.OutputList[2].ShouldBe(string.Empty);
target.OutputList[3].ShouldBe("3");
}
}
}
using DotnetStandardStreams;
using System;
using System.Collections.Generic;
using Xunit;
using Shouldly;
namespace DotnetStandardStreamsTests
{
public class ListOutputTargetTests
{
[Fact]
public void WritesToList()
{
ListOutputTarget target = new(new List<string>());
target.Output("1");
target.Output("2");
target.Output("");
target.Output("3");
target.OutputList.Count.ShouldBe(4);
target.OutputList[0].ShouldBe("1");
target.OutputList[1].ShouldBe("2");
target.OutputList[2].ShouldBe(string.Empty);
target.OutputList[3].ShouldBe("3");
}
}
}

View File

@ -1,39 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
using Shouldly;
using DotnetStreams;
namespace DotnetStreamsTests
{
public class ListTextSourceTests
{
[Fact]
public void ReadsFromList()
{
ITextSource reader = new ListTextSource(new[] { "1", "2", "", "3" });
string s0 = reader.Read();
s0.ShouldNotBeNull();
s0.ShouldBe("1");
reader.Eof().ShouldBe(false);
string s1 = reader.Read();
s1.ShouldNotBeNull();
s1.ShouldBe("2");
reader.Eof().ShouldBe(false);
string s2 = reader.Read();
s2.ShouldNotBeNull();
s2.ShouldBe(string.Empty);
reader.Eof().ShouldBe(false);
string s3 = reader.Read();
s3.ShouldNotBeNull();
s3.ShouldBe("3");
reader.Eof().ShouldBe(true);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
using Shouldly;
using DotnetStandardStreams;
namespace DotnetStandardStreamsTests
{
public class ListTextSourceTests
{
[Fact]
public void ReadsFromList()
{
ITextSource reader = new ListTextSource(new[] { "1", "2", "", "3" });
string s0 = reader.Read();
s0.ShouldNotBeNull();
s0.ShouldBe("1");
reader.Eof().ShouldBe(false);
string s1 = reader.Read();
s1.ShouldNotBeNull();
s1.ShouldBe("2");
reader.Eof().ShouldBe(false);
string s2 = reader.Read();
s2.ShouldNotBeNull();
s2.ShouldBe(string.Empty);
reader.Eof().ShouldBe(false);
string s3 = reader.Read();
s3.ShouldNotBeNull();
s3.ShouldBe("3");
reader.Eof().ShouldBe(true);
}
}
}

View File

@ -1,210 +1,210 @@
using DotnetStreamsTests.Testables;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
using Shouldly;
using DotnetStreams;
namespace DotnetStreamsTests
{
public class StdInTextSourceTests
{
private static TextReader CreateStdIn()
{
return new ListTextReader(new[] { "1", "2", "", "3" });
}
private static void WrapStdInTest(Action<ITextSource> testCode)
{
var oldInputReader = System.Console.In;
try
{
TextReader reader = CreateStdIn();
System.Console.SetIn(reader);
ITextSource stdin = new StdInTextSource();
stdin.Open();
try
{
testCode?.Invoke(stdin);
}
finally
{
stdin.Close();
}
}
finally
{
System.Console.SetIn(oldInputReader);
}
}
[Fact]
public void CanReadAllFromStandardIn()
{
WrapStdInTest(stdin =>
{
var actual = stdin.ReadAll().ToList();
actual.Count.ShouldBe(4);
actual[0].ShouldBe("1");
actual[1].ShouldBe("2");
actual[2].ShouldBe(string.Empty);
actual[3].ShouldBe("3");
});
}
//{
// // Fake the StdIn
// var oldInputReader = System.Console.In;
// try
// {
// TextReader reader = CreateStdIn();
// System.Console.SetIn(reader);
// ITextSource stdin = new StdInTextSource();
// stdin.Open();
// try
// {
// var actual = stdin.ReadAll().ToList();
// actual.Count.ShouldBe(4);
// actual[0].ShouldBe("1");
// actual[1].ShouldBe("2");
// actual[2].ShouldBe(string.Empty);
// actual[3].ShouldBe("3");
// }
// finally
// {
// stdin.Close();
// }
// }
// finally
// {
// System.Console.SetIn(oldInputReader);
// }
//}
[Fact]
public void CanReadIndividualLinesFromStandardIn()
{
WrapStdInTest(stdin =>
{
string s;
s = stdin.Read();
stdin.Eof().ShouldBe(false);
s.ShouldBe("1");
s = stdin.Read();
stdin.Eof().ShouldBe(false);
s.ShouldBe("2");
s = stdin.Read();
stdin.Eof().ShouldBe(false);
s.ShouldBe(string.Empty);
s = stdin.Read();
stdin.Eof().ShouldBe(false);
s.ShouldBe("3");
s = stdin.Read();
stdin.Eof().ShouldBe(true);
});
//// Fake the StdIn
//var oldInputReader = System.Console.In;
//try
//{
// TextReader reader = CreateStdIn();
// System.Console.SetIn(reader);
// ITextSource stdin = new StdInTextSource();
// stdin.Open();
// try
// {
// string s;
// s = stdin.Read();
// stdin.Eof().ShouldBe(false);
// s.ShouldBe("1");
// s = stdin.Read();
// stdin.Eof().ShouldBe(false);
// s.ShouldBe("2");
// s = stdin.Read();
// stdin.Eof().ShouldBe(false);
// s.ShouldBe(string.Empty);
// s = stdin.Read();
// stdin.Eof().ShouldBe(false);
// s.ShouldBe("3");
// s = stdin.Read();
// stdin.Eof().ShouldBe(true);
// }
// finally
// {
// stdin.Close();
// }
//}
//finally
//{
// System.Console.SetIn(oldInputReader);
//}
}
[Fact]
public void CanWhileLoopThroughStdInProperly()
{
WrapStdInTest(stdin =>
{
int lineCount = 0;
var s = stdin.Read();
while (!stdin.Eof())
{
lineCount++;
s = stdin.Read();
}
lineCount.ShouldBe(4);
});
}
//{
// // Fake the StdIn
// var oldInputReader = System.Console.In;
// try
// {
// TextReader reader = CreateStdIn();
// System.Console.SetIn(reader);
// ITextSource stdin = new StdInTextSource();
// stdin.Open();
// try
// {
// int lineCount = 0;
// var s = stdin.Read();
// while (!stdin.Eof())
// {
// // Do stuff with s here normally.
// lineCount++;
// s = stdin.Read();
// }
// lineCount.ShouldBe(4);
// }
// finally
// {
// stdin.Close();
// }
// }
// finally
// {
// System.Console.SetIn(oldInputReader);
// }
//}
}
}
using DotnetStandardStreamsTests.Testables;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
using Shouldly;
using DotnetStandardStreams;
namespace DotnetStandardStreamsTests
{
public class StdInTextSourceTests
{
private static TextReader CreateStdIn()
{
return new ListTextReader(new[] { "1", "2", "", "3" });
}
private static void WrapStdInTest(Action<ITextSource> testCode)
{
var oldInputReader = System.Console.In;
try
{
TextReader reader = CreateStdIn();
System.Console.SetIn(reader);
ITextSource stdin = new StdInTextSource();
stdin.Open();
try
{
testCode?.Invoke(stdin);
}
finally
{
stdin.Close();
}
}
finally
{
System.Console.SetIn(oldInputReader);
}
}
[Fact]
public void CanReadAllFromStandardIn()
{
WrapStdInTest(stdin =>
{
var actual = stdin.ReadAll().ToList();
actual.Count.ShouldBe(4);
actual[0].ShouldBe("1");
actual[1].ShouldBe("2");
actual[2].ShouldBe(string.Empty);
actual[3].ShouldBe("3");
});
}
//{
// // Fake the StdIn
// var oldInputReader = System.Console.In;
// try
// {
// TextReader reader = CreateStdIn();
// System.Console.SetIn(reader);
// ITextSource stdin = new StdInTextSource();
// stdin.Open();
// try
// {
// var actual = stdin.ReadAll().ToList();
// actual.Count.ShouldBe(4);
// actual[0].ShouldBe("1");
// actual[1].ShouldBe("2");
// actual[2].ShouldBe(string.Empty);
// actual[3].ShouldBe("3");
// }
// finally
// {
// stdin.Close();
// }
// }
// finally
// {
// System.Console.SetIn(oldInputReader);
// }
//}
[Fact]
public void CanReadIndividualLinesFromStandardIn()
{
WrapStdInTest(stdin =>
{
string s;
s = stdin.Read();
stdin.Eof().ShouldBe(false);
s.ShouldBe("1");
s = stdin.Read();
stdin.Eof().ShouldBe(false);
s.ShouldBe("2");
s = stdin.Read();
stdin.Eof().ShouldBe(false);
s.ShouldBe(string.Empty);
s = stdin.Read();
stdin.Eof().ShouldBe(false);
s.ShouldBe("3");
s = stdin.Read();
stdin.Eof().ShouldBe(true);
});
//// Fake the StdIn
//var oldInputReader = System.Console.In;
//try
//{
// TextReader reader = CreateStdIn();
// System.Console.SetIn(reader);
// ITextSource stdin = new StdInTextSource();
// stdin.Open();
// try
// {
// string s;
// s = stdin.Read();
// stdin.Eof().ShouldBe(false);
// s.ShouldBe("1");
// s = stdin.Read();
// stdin.Eof().ShouldBe(false);
// s.ShouldBe("2");
// s = stdin.Read();
// stdin.Eof().ShouldBe(false);
// s.ShouldBe(string.Empty);
// s = stdin.Read();
// stdin.Eof().ShouldBe(false);
// s.ShouldBe("3");
// s = stdin.Read();
// stdin.Eof().ShouldBe(true);
// }
// finally
// {
// stdin.Close();
// }
//}
//finally
//{
// System.Console.SetIn(oldInputReader);
//}
}
[Fact]
public void CanWhileLoopThroughStdInProperly()
{
WrapStdInTest(stdin =>
{
int lineCount = 0;
var s = stdin.Read();
while (!stdin.Eof())
{
lineCount++;
s = stdin.Read();
}
lineCount.ShouldBe(4);
});
}
//{
// // Fake the StdIn
// var oldInputReader = System.Console.In;
// try
// {
// TextReader reader = CreateStdIn();
// System.Console.SetIn(reader);
// ITextSource stdin = new StdInTextSource();
// stdin.Open();
// try
// {
// int lineCount = 0;
// var s = stdin.Read();
// while (!stdin.Eof())
// {
// // Do stuff with s here normally.
// lineCount++;
// s = stdin.Read();
// }
// lineCount.ShouldBe(4);
// }
// finally
// {
// stdin.Close();
// }
// }
// finally
// {
// System.Console.SetIn(oldInputReader);
// }
//}
}
}

View File

@ -1,26 +1,26 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DotnetStreamsTests.Testables
{
public class ListTextReader : TextReader
{
private readonly IEnumerator<string> enumerator;
public ListTextReader(IEnumerable<string> data)
{
enumerator = data.GetEnumerator();
}
public override string ReadLine()
{
if (enumerator.MoveNext())
return enumerator.Current;
else
return null;
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DotnetStandardStreamsTests.Testables
{
public class ListTextReader : TextReader
{
private readonly IEnumerator<string> enumerator;
public ListTextReader(IEnumerable<string> data)
{
enumerator = data.GetEnumerator();
}
public override string ReadLine()
{
if (enumerator.MoveNext())
return enumerator.Current;
else
return null;
}
}
}

View File

@ -1,30 +1,30 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DotnetStreamsTests.Testables
{
public class ListWriter : TextWriter
{
public List<string> Lines { get; }
public override Encoding Encoding => Encoding.UTF8;
public ListWriter()
{
Lines = new();
}
public override void Write(string value)
{
Lines.Add(value);
}
public override void WriteLine(string value)
{
Lines.Add(value);
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DotnetStandardStreamsTests.Testables
{
public class ListWriter : TextWriter
{
public List<string> Lines { get; }
public override Encoding Encoding => Encoding.UTF8;
public ListWriter()
{
Lines = new();
}
public override void Write(string value)
{
Lines.Add(value);
}
public override void WriteLine(string value)
{
Lines.Add(value);
}
}
}

View File

@ -1,24 +1,24 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using DotnetStreams;
namespace DotnetStreamsTests.Testables
{
public class TestableConsoleOutputTarget : ConsoleOutputTarget, IDisposable
{
private readonly TextWriter previousOutWriter;
public TestableConsoleOutputTarget(TextWriter outWriter) : base()
{
previousOutWriter = System.Console.Out;
System.Console.SetOut(outWriter);
}
public void Dispose()
{
System.Console.SetOut(previousOutWriter);
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using DotnetStandardStreams;
namespace DotnetStandardStreamsTests.Testables
{
public class TestableConsoleOutputTarget : ConsoleOutputTarget, IDisposable
{
private readonly TextWriter previousOutWriter;
public TestableConsoleOutputTarget(TextWriter outWriter) : base()
{
previousOutWriter = System.Console.Out;
System.Console.SetOut(outWriter);
}
public void Dispose()
{
System.Console.SetOut(previousOutWriter);
}
}
}