Exemplo n.º 1
0
 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());
         }
 }
Exemplo n.º 2
0
        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());
                }
        }
Exemplo n.º 3
0
        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);
                }
            }
        }
Exemplo n.º 5
0
        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());
                }
        }
Exemplo n.º 6
0
        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());
                }
        }
Exemplo n.º 7
0
        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))
Exemplo n.º 8
0
        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]);
                }
        }
Exemplo n.º 9
0
        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());
                }
        }
Exemplo n.º 10
0
        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))