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()); } }
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()); } }
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()); } }
protected override void ParseStreamInto(LineBufferedReader stream, T output) { Section section = Section.None; string line; while ((line = stream.ReadLine()) != null) { if (ShouldSkipLine(line)) { continue; } if (line.StartsWith(@"[", StringComparison.Ordinal) && line.EndsWith(@"]", StringComparison.Ordinal)) { if (!Enum.TryParse(line.Substring(1, line.Length - 2), out section)) { Logger.Log($"Unknown section \"{line}\" in \"{output}\""); section = Section.None; } continue; } try { ParseLine(output, section, line); } catch (Exception e) { Logger.Log($"Failed to process line \"{line}\" into \"{output}\": {e.Message}", LoggingTarget.Runtime, LogLevel.Important); } } }
public void TestPeekLineOnce() { const string contents = @"line 1 peek this line 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()); } }
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()); } }
protected override void ParseStreamInto(LineBufferedReader stream, T output) { Section section = Section.None; string line; while ((line = stream.ReadLine()) != null) { if (ShouldSkipLine(line)) { continue; } if (line.StartsWith('[') && line.EndsWith(']')) { if (!Enum.TryParse(line[1..^ 1], out section))
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()); string[] 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]); } }
public void TestReadToEndAfterReadsAndPeeks() { const string contents = @"this line is gone this one shouldn't be these ones definitely 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()); const string ending = @"this one shouldn't be these ones definitely not"; Assert.AreEqual(ending, bufferedReader.ReadToEnd()); } }
protected override void ParseStreamInto(LineBufferedReader stream, T output) { Section section = Section.None; string line; while ((line = stream.ReadLine()) != null) { if (ShouldSkipLine(line)) continue; if (section != Section.Metadata) { // comments should not be stripped from metadata lines, as the song metadata may contain "//" as valid data. line = StripComments(line); } line = line.TrimEnd(); if (line.StartsWith('[') && line.EndsWith(']')) { if (!Enum.TryParse(line[1..^1], out section))