Esempio n. 1
0
        public void TestPosition()
        {
            var testDocument = string.Join("",
                                           tokenLists
                                           .SelectMany(x => x.Value)
                                           .Select(v => string.Format("\t\t\t\t{0}\n", v.Item2)));

            var  SUT         = new Scanner(testDocument, "TestDocument.hcl");
            var  expectedPos = Pos.CreateForFile("TestDocument.hcl");
            char ch;

            foreach (var tokenList in tokenLists)
            {
                foreach (var entry in tokenList.Value)
                {
                    // First advance the expectedPos by 4 tabs
                    for (var i = 0; i < 4; i++)
                    {
                        expectedPos = expectedPos.NextInString(testDocument, out ch);
                    }
                    // Make the scanner scan another token
                    var actual = SUT.Scan();
                    Assert.That(actual.Pos.Offset, Is.EqualTo(expectedPos.Offset), "Wrong offset for {0}:{1}", tokenList.Key, entry.Item2);
                    Assert.That(actual.Pos.Line, Is.EqualTo(expectedPos.Line), "Wrong line for {0}:{1}", tokenList.Key, entry.Item2);
                    Assert.That(actual.Pos.Column, Is.EqualTo(expectedPos.Column), "Wrong column for {0}:{1}", tokenList.Key, entry.Item2);
                    Assert.That(actual.Pos.Filename, Is.EqualTo(expectedPos.Filename), "Wrong filename for {0}:{1}", tokenList.Key, entry.Item2);
                    Assert.That(SUT.ErrorCount, Is.EqualTo(0), "Should have no errors");

                    // Advance the ExpectedPos for the read in string
                    foreach (var _ in actual.Text)
                    {
                        expectedPos = expectedPos.NextInString(testDocument, out ch);
                    }
                    // Then advance the expectedPos by 1 more for the newline
                    expectedPos = expectedPos.NextInString(testDocument, out ch);
                }
            }
        }