protected void RunReadUntilTest(Func <LookaheadTextReader, string> readMethod, int expectedRaw, int expectedChar, int expectedLine)
        {
            // Arrange
            LookaheadTextReader reader = CreateReader("a\r\nbcd\r\nefg");

            reader.Read(); // Reader: "\r\nbcd\r\nefg"
            reader.Read(); // Reader: "\nbcd\r\nefg"
            reader.Read(); // Reader: "bcd\r\nefg"

            // Act
            string         read = null;
            SourceLocation actualLocation;

            using (reader.BeginLookahead())
            {
                read           = readMethod(reader);
                actualLocation = reader.CurrentLocation;
            }

            // Assert
            Assert.Equal(3, reader.CurrentLocation.AbsoluteIndex);
            Assert.Equal(0, reader.CurrentLocation.CharacterIndex);
            Assert.Equal(1, reader.CurrentLocation.LineIndex);
            Assert.Equal(expectedRaw, actualLocation.AbsoluteIndex);
            Assert.Equal(expectedChar, actualLocation.CharacterIndex);
            Assert.Equal(expectedLine, actualLocation.LineIndex);
            Assert.Equal('b', reader.Peek());
            Assert.Equal(read, readMethod(reader));
        }
Example #2
0
        protected void RunReadUntilTest(Func <LookaheadTextReader, string> readMethod, int expectedRaw, int expectedChar, int expectedLine)
        {
            // Arrange
            LookaheadTextReader reader = CreateReader("a\r\nbcd\r\nefg");

            reader.Read(); // Reader: "\r\nbcd\r\nefg"
            reader.Read(); // Reader: "\nbcd\r\nefg"
            reader.Read(); // Reader: "bcd\r\nefg"

            // Act
            string         read = null;
            SourceLocation actualLocation;

            using (reader.BeginLookahead()) {
                read           = readMethod(reader);
                actualLocation = reader.CurrentLocation;
            }

            // Assert
            Assert.AreEqual(3, reader.CurrentLocation.AbsoluteIndex, "The reader did not correctly restore the raw index when backtracking");
            Assert.AreEqual(0, reader.CurrentLocation.CharacterIndex, "The reader did not correctly restore the character index when backtracking");
            Assert.AreEqual(1, reader.CurrentLocation.LineIndex, "The reader did not correctly restore the line index when backtracking");
            Assert.AreEqual(expectedRaw, actualLocation.AbsoluteIndex, "The reader did not correctly advance the raw index");
            Assert.AreEqual(expectedChar, actualLocation.CharacterIndex, "The reader did not correctly advance the character index");
            Assert.AreEqual(expectedLine, actualLocation.LineIndex, "The reader did not correctly advance the line index");
            Assert.AreEqual('b', reader.Peek(), "The reader did not correctly backtrack to the appropriate character");
            Assert.AreEqual(read, readMethod(reader));
        }
 private static void AdvanceReader(int offset, LookaheadTextReader reader)
 {
     for (int i = 0; i < offset; i++)
     {
         reader.Read();
     }
 }
        private void RunPeekOrReadTest(string input, int offset, bool isRead)
        {
            using (LookaheadTextReader reader = CreateReader(input))
            {
                AdvanceReader(offset, reader);

                // Act
                int?actual = null;
                if (isRead)
                {
                    actual = reader.Read();
                }
                else
                {
                    actual = reader.Peek();
                }

                Assert.NotNull(actual);

                // Asserts
                AssertReaderValueCorrect(actual.Value, input, offset, "Peek");

                if (isRead)
                {
                    AssertReaderValueCorrect(reader.Peek(), input, offset + 1, "Read");
                }
                else
                {
                    Assert.Equal(actual, reader.Peek());
                }
            }
        }
        protected void RunBufferReadTest(Func <LookaheadTextReader, char[], int, int, int> readMethod)
        {
            // Arrange
            LookaheadTextReader reader = CreateReader("abcdefg");

            reader.Read(); // Reader: "bcdefg"

            // Act
            char[]         buffer = new char[4];
            int            read   = -1;
            SourceLocation actualLocation;

            using (reader.BeginLookahead())
            {
                read           = readMethod(reader, buffer, 0, 4);
                actualLocation = reader.CurrentLocation;
            }

            // Assert
            Assert.Equal("bcde", new String(buffer));
            Assert.Equal(4, read);
            Assert.Equal(5, actualLocation.AbsoluteIndex);
            Assert.Equal(5, actualLocation.CharacterIndex);
            Assert.Equal(0, actualLocation.LineIndex);
            Assert.Equal(1, reader.CurrentLocation.CharacterIndex);
            Assert.Equal(0, reader.CurrentLocation.LineIndex);
            Assert.Equal('b', reader.Peek());
        }
Example #6
0
        private void RunPeekOrReadTest(string input, int offset, bool isRead)
        {
            using (LookaheadTextReader reader = CreateReader(input)) {
                AdvanceReader(offset, reader);

                // Act
                int?actual = null;
                if (isRead)
                {
                    actual = reader.Read();
                }
                else
                {
                    actual = reader.Peek();
                }

                if (actual == null)
                {
                    Assert.Inconclusive("Actual value was never set?!");
                }

                // Asserts
                AssertReaderValueCorrect(actual.Value, input, offset, "Peek");

                if (isRead)
                {
                    AssertReaderValueCorrect(reader.Peek(), input, offset + 1, "Read");
                }
                else
                {
                    Assert.AreEqual(actual, reader.Peek(), "Peek moved the reader to the next character!");
                }
            }
        }
Example #7
0
        protected void RunBufferReadTest(Func <LookaheadTextReader, char[], int, int, int> readMethod)
        {
            // Arrange
            LookaheadTextReader reader = CreateReader("abcdefg");

            reader.Read(); // Reader: "bcdefg"

            // Act
            char[]         buffer = new char[4];
            int            read   = -1;
            SourceLocation actualLocation;

            using (reader.BeginLookahead()) {
                read           = readMethod(reader, buffer, 0, 4);
                actualLocation = reader.CurrentLocation;
            }

            // Assert
            Assert.AreEqual("bcde", new String(buffer), "The reader did not fill the buffer with the correct characters");
            Assert.AreEqual(4, read, "The reader did not report the correct number of read characters");
            Assert.AreEqual(5, actualLocation.AbsoluteIndex, "The reader did not correctly advance the raw index");
            Assert.AreEqual(5, actualLocation.CharacterIndex, "The reader did not correctly advance the character index");
            Assert.AreEqual(0, actualLocation.LineIndex, "The reader did not correctly advance the line index");
            Assert.AreEqual(1, reader.CurrentLocation.CharacterIndex, "The reader did not correctly restore the character index when backtracking");
            Assert.AreEqual(0, reader.CurrentLocation.LineIndex, "The reader did not correctly restore the line index when backtracking");
            Assert.AreEqual('b', reader.Peek(), "The reader did not correctly backtrack to the appropriate character");
        }
 protected void Read(StringBuilder builder, LookaheadTextReader reader)
 {
     builder.Append((char)reader.Read());
 }