DotnetStandardStreams/source/DotnetStandardStreamsTests/StdInTextSourceTests.cs

211 lines
3.9 KiB
C#

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);
// }
//}
}
}