182 lines
3.5 KiB
C#
182 lines
3.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using DotnetStandardStreams;
|
|
using DotnetStandardStreamsTests.Testables;
|
|
|
|
namespace DotnetStandardStreamsTests;
|
|
|
|
public class FileTextSourceTests
|
|
{
|
|
private string tempFilename { get; set; }
|
|
|
|
private void CleanupTempFile()
|
|
{
|
|
if (File.Exists(tempFilename))
|
|
File.Delete(tempFilename);
|
|
}
|
|
|
|
private void CreateInputFile()
|
|
{
|
|
tempFilename = System.IO.Path.GetTempFileName();
|
|
const string content = "1\r\n2\r\n\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n";
|
|
CleanupTempFile();
|
|
File.WriteAllText(tempFilename, content);
|
|
}
|
|
|
|
private void CreateEmptyInputFile()
|
|
{
|
|
tempFilename = System.IO.Path.GetTempFileName();
|
|
CleanupTempFile();
|
|
File.WriteAllText(tempFilename, string.Empty);
|
|
}
|
|
|
|
public void WrapFileTest(Action<ITextSource> testCode, Action? explicitSourceCreation = null)
|
|
{
|
|
explicitSourceCreation = explicitSourceCreation ?? CreateInputFile;
|
|
explicitSourceCreation();
|
|
try
|
|
{
|
|
ITextSource source = new FileTextSource(tempFilename);
|
|
source.Open();
|
|
try
|
|
{
|
|
testCode(source);
|
|
}
|
|
finally
|
|
{
|
|
source.Close();
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
CleanupTempFile();
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void CanReadAllFromFile()
|
|
{
|
|
WrapFileTest(static textSource =>
|
|
{
|
|
List<string> actual = textSource.ReadAll().ToList();
|
|
actual.Count.ShouldBe(10);
|
|
actual.ShouldBe(["1", "2", "", "4", "5", "6", "7", "8", "9", "10"]);
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public void CanReadIndividualLinesFromFile()
|
|
{
|
|
WrapFileTest(static textSource =>
|
|
{
|
|
string s;
|
|
s = textSource.Read();
|
|
textSource.Eof().ShouldBe(false);
|
|
s.ShouldBe("1");
|
|
|
|
s = textSource.Read();
|
|
textSource.Eof().ShouldBe(false);
|
|
s.ShouldBe("2");
|
|
|
|
s = textSource.Read();
|
|
textSource.Eof().ShouldBe(false);
|
|
s.ShouldBe("");
|
|
|
|
s = textSource.Read();
|
|
textSource.Eof().ShouldBe(false);
|
|
s.ShouldBe("4");
|
|
|
|
s = textSource.Read();
|
|
textSource.Eof().ShouldBe(false);
|
|
s.ShouldBe("5");
|
|
|
|
s = textSource.Read();
|
|
textSource.Eof().ShouldBe(false);
|
|
s.ShouldBe("6");
|
|
|
|
s = textSource.Read();
|
|
textSource.Eof().ShouldBe(false);
|
|
s.ShouldBe("7");
|
|
|
|
s = textSource.Read();
|
|
textSource.Eof().ShouldBe(false);
|
|
s.ShouldBe("8");
|
|
|
|
s = textSource.Read();
|
|
textSource.Eof().ShouldBe(false);
|
|
s.ShouldBe("9");
|
|
|
|
s = textSource.Read();
|
|
textSource.Eof().ShouldBe(false);
|
|
s.ShouldBe("10");
|
|
|
|
s = textSource.Read();
|
|
textSource.Eof().ShouldBe(true);
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public void CanWhileLoopThroughFileNormally()
|
|
{
|
|
WrapFileTest(static textSource =>
|
|
{
|
|
int lineCount = 0;
|
|
_ = textSource.Read();
|
|
|
|
while (!textSource.Eof())
|
|
{
|
|
lineCount++;
|
|
_ = textSource.Read();
|
|
}
|
|
|
|
lineCount.ShouldBe(10);
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public void CanReadAllThroughFileNormally()
|
|
{
|
|
WrapFileTest(static textSource =>
|
|
{
|
|
List<string> lines = textSource.ReadAll().ToList();
|
|
int lineCount = lines.Count;
|
|
lineCount.ShouldBe(10);
|
|
lines.ShouldBe(["1", "2", "", "4", "5", "6", "7", "8", "9", "10"]);
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public void CanWhileLoopThroughEmptyFile()
|
|
{
|
|
WrapFileTest(
|
|
static textSource =>
|
|
{
|
|
int lineCount = 0;
|
|
_ = textSource.Read();
|
|
|
|
while (!textSource.Eof())
|
|
{
|
|
lineCount++;
|
|
_ = textSource.Read();
|
|
}
|
|
|
|
lineCount.ShouldBe(0);
|
|
},
|
|
CreateEmptyInputFile);
|
|
}
|
|
|
|
[Fact]
|
|
public void CanReadAllThroughEmptyFile()
|
|
{
|
|
WrapFileTest(static textSource =>
|
|
{
|
|
List<string> lines = textSource.ReadAll().ToList();
|
|
int lineCount = lines.Count;
|
|
lineCount.ShouldBe(0);
|
|
},
|
|
CreateEmptyInputFile);
|
|
}
|
|
}
|