mirror of
https://github.com/ppy/osu.git
synced 2024-11-15 03:47:26 +08:00
cb1f7e2dc7
Tests for the line-buffered reader added in 7b1ff38
were subtly
dependent on the execution environment due to differing end-of-line
markers on Windows and Unix-based systems.
Because StreamReader discards all newlines when reading line-by-line,
LineBufferedReader used a StringBuilder to patch the peeked lines
back together with the remaining contents of the file being read.
As StringBuilder.AppendLine uses the environment-specific newline
delimiter, the delimiters after the peeked-but-unconsumed lines can
therefore be substituted by the platform-specific variants, causing
the test failures due to the overly-simplified way they were written.
Reformulate the test to avoid such issues from resurfacing again
by splitting lines by \r or \n and then testing each line individually.
Additionally remove all raw literals in favour of explicitly mixing
various line delimiter character sequences for additional coverage.
126 lines
4.9 KiB
C#
126 lines
4.9 KiB
C#
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
using System;
|
|
using System.IO;
|
|
using System.Text;
|
|
using NUnit.Framework;
|
|
using osu.Game.IO;
|
|
|
|
namespace osu.Game.Tests.Beatmaps.IO
|
|
{
|
|
[TestFixture]
|
|
public class LineBufferedReaderTest
|
|
{
|
|
[Test]
|
|
public void TestReadLineByLine()
|
|
{
|
|
const string contents = "line 1\rline 2\nline 3";
|
|
|
|
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(contents)))
|
|
using (var bufferedReader = new LineBufferedReader(stream))
|
|
{
|
|
Assert.AreEqual("line 1", bufferedReader.ReadLine());
|
|
Assert.AreEqual("line 2", bufferedReader.ReadLine());
|
|
Assert.AreEqual("line 3", bufferedReader.ReadLine());
|
|
Assert.IsNull(bufferedReader.ReadLine());
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void TestPeekLineOnce()
|
|
{
|
|
const string contents = "line 1\r\npeek this\nline 3";
|
|
|
|
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(contents)))
|
|
using (var bufferedReader = new LineBufferedReader(stream))
|
|
{
|
|
Assert.AreEqual("line 1", bufferedReader.ReadLine());
|
|
Assert.AreEqual("peek this", bufferedReader.PeekLine());
|
|
Assert.AreEqual("peek this", bufferedReader.ReadLine());
|
|
Assert.AreEqual("line 3", bufferedReader.ReadLine());
|
|
Assert.IsNull(bufferedReader.ReadLine());
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void TestPeekLineMultipleTimes()
|
|
{
|
|
const string contents = "peek this once\nline 2\rpeek this a lot";
|
|
|
|
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(contents)))
|
|
using (var bufferedReader = new LineBufferedReader(stream))
|
|
{
|
|
Assert.AreEqual("peek this once", bufferedReader.PeekLine());
|
|
Assert.AreEqual("peek this once", bufferedReader.ReadLine());
|
|
Assert.AreEqual("line 2", bufferedReader.ReadLine());
|
|
Assert.AreEqual("peek this a lot", bufferedReader.PeekLine());
|
|
Assert.AreEqual("peek this a lot", bufferedReader.PeekLine());
|
|
Assert.AreEqual("peek this a lot", bufferedReader.PeekLine());
|
|
Assert.AreEqual("peek this a lot", bufferedReader.ReadLine());
|
|
Assert.IsNull(bufferedReader.ReadLine());
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void TestPeekLineAtEndOfStream()
|
|
{
|
|
const string contents = "first line\r\nsecond line";
|
|
|
|
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(contents)))
|
|
using (var bufferedReader = new LineBufferedReader(stream))
|
|
{
|
|
Assert.AreEqual("first line", bufferedReader.ReadLine());
|
|
Assert.AreEqual("second line", bufferedReader.ReadLine());
|
|
Assert.IsNull(bufferedReader.PeekLine());
|
|
Assert.IsNull(bufferedReader.ReadLine());
|
|
Assert.IsNull(bufferedReader.PeekLine());
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void TestPeekReadLineOnEmptyStream()
|
|
{
|
|
using (var stream = new MemoryStream())
|
|
using (var bufferedReader = new LineBufferedReader(stream))
|
|
{
|
|
Assert.IsNull(bufferedReader.PeekLine());
|
|
Assert.IsNull(bufferedReader.ReadLine());
|
|
Assert.IsNull(bufferedReader.ReadLine());
|
|
Assert.IsNull(bufferedReader.PeekLine());
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void TestReadToEndNoPeeks()
|
|
{
|
|
const string contents = "first line\r\nsecond line";
|
|
|
|
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(contents)))
|
|
using (var bufferedReader = new LineBufferedReader(stream))
|
|
{
|
|
Assert.AreEqual(contents, bufferedReader.ReadToEnd());
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void TestReadToEndAfterReadsAndPeeks()
|
|
{
|
|
const string contents = "this line is gone\rthis one shouldn't be\r\nthese ones\ndefinitely not";
|
|
|
|
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(contents)))
|
|
using (var bufferedReader = new LineBufferedReader(stream))
|
|
{
|
|
Assert.AreEqual("this line is gone", bufferedReader.ReadLine());
|
|
Assert.AreEqual("this one shouldn't be", bufferedReader.PeekLine());
|
|
|
|
var endingLines = bufferedReader.ReadToEnd().Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
|
|
Assert.AreEqual(3, endingLines.Length);
|
|
Assert.AreEqual("this one shouldn't be", endingLines[0]);
|
|
Assert.AreEqual("these ones", endingLines[1]);
|
|
Assert.AreEqual("definitely not", endingLines[2]);
|
|
}
|
|
}
|
|
}
|
|
}
|