示例#1
0
        public void CanGetCurrentPosition()
        {
            var empty = new TextTestFixture("");

            empty.Advance(0).Position.ShouldBe(new Position(1, 1));
            empty.Advance(1).Position.ShouldBe(new Position(1, 1));

            var newLine = "\n";

            var lines =
                "Line 1" + newLine    //Index 0-5, \n
                + "Line 2" + newLine  //Index 7-12, \n
                + "Line 3" + newLine; //Index 14-19, \n

            var list = new TextTestFixture(lines, newLine);

            list.Advance(0).Position.ShouldBe(new Position(1, 1));
            list.Advance(5).Position.ShouldBe(new Position(1, 6));
            list.Advance(6).Position.ShouldBe(new Position(1, 7));

            list.Advance(7).Position.ShouldBe(new Position(2, 1));
            list.Advance(12).Position.ShouldBe(new Position(2, 6));
            list.Advance(13).Position.ShouldBe(new Position(2, 7));

            list.Advance(14).Position.ShouldBe(new Position(3, 1));
            list.Advance(19).Position.ShouldBe(new Position(3, 6));
            list.Advance(20).Position.ShouldBe(new Position(3, 7));

            list.Advance(21).Position.ShouldBe(new Position(4, 1));
            list.Advance(1000).Position.ShouldBe(new Position(4, 1));
        }
示例#2
0
        public void CanAdvanceAheadNCharacters()
        {
            var empty = new TextTestFixture("");

            empty.Advance(0).ToString().ShouldBe("");
            empty.Advance(1).ToString().ShouldBe("");

            var abc = new TextTestFixture("abc");

            abc.Advance(0).ToString().ShouldBe("abc");
            abc.Advance(1).ToString().ShouldBe("bc");
            abc.Advance(2).ToString().ShouldBe("c");
            abc.Advance(3).ToString().ShouldBe("");
            abc.Advance(4).ToString().ShouldBe("");
            abc.Advance(100).ToString().ShouldBe("");
        }
示例#3
0
        public void ProducesTokenUponSuccessfulMatch()
        {
            lower.TryMatch((InputText)abcDEF, out Token token).ShouldBeTrue();
            token.ShouldBe(lower, "abc", 1, 1);

            upper.TryMatch(abcDEF.Advance(3), out token).ShouldBeTrue();
            token.ShouldBe(upper, "DEF", 1, 4);

            caseInsensitive.TryMatch((InputText)abcDEF, out token).ShouldBeTrue();
            token.ShouldBe(caseInsensitive, "abcDEF", 1, 1);
        }
示例#4
0
        public void CanMatchLeadingCharactersByPredicate()
        {
            Predicate <char> letters       = char.IsLetter;
            Predicate <char> digits        = char.IsDigit;
            Predicate <char> alphanumerics = char.IsLetterOrDigit;

            var empty = new InputText("");

            empty.Match(letters).ShouldFail();

            var abc123 = new TextTestFixture("abc123");

            abc123.Advance(0).Match(digits).ShouldFail();
            abc123.Advance(0).Match(letters).ShouldSucceed("abc");
            abc123.Advance(0).Match(alphanumerics).ShouldSucceed("abc123");

            abc123.Advance(2).Match(digits).ShouldFail();
            abc123.Advance(2).Match(letters).ShouldSucceed("c");
            abc123.Advance(2).Match(alphanumerics).ShouldSucceed("c123");

            abc123.Advance(3).Match(digits).ShouldSucceed("123");
            abc123.Advance(3).Match(letters).ShouldFail();
            abc123.Advance(3).Match(alphanumerics).ShouldSucceed("123");

            abc123.Advance(6).Match(digits).ShouldFail();
            abc123.Advance(6).Match(letters).ShouldFail();
            abc123.Advance(6).Match(alphanumerics).ShouldFail();
        }
示例#5
0
        public void CanMatchLeadingCharactersByTokenRegex()
        {
            var end           = new TokenRegex(@"$");
            var letters       = new TokenRegex(@"[a-z]+");
            var digits        = new TokenRegex(@"[0-9]+");
            var alphanumerics = new TokenRegex(@"[a-z0-9]+");

            var empty = new InputText("");

            empty.Match(letters).ShouldFail();
            empty.Match(end).ShouldSucceed("");

            var abc123 = new TextTestFixture("abc123");

            abc123.Advance(0).Match(digits).ShouldFail();
            abc123.Advance(0).Match(letters).ShouldSucceed("abc");
            abc123.Advance(0).Match(alphanumerics).ShouldSucceed("abc123");

            abc123.Advance(2).Match(digits).ShouldFail();
            abc123.Advance(2).Match(letters).ShouldSucceed("c");
            abc123.Advance(2).Match(alphanumerics).ShouldSucceed("c123");

            abc123.Advance(3).Match(digits).ShouldSucceed("123");
            abc123.Advance(3).Match(letters).ShouldFail();
            abc123.Advance(3).Match(alphanumerics).ShouldSucceed("123");

            abc123.Advance(6).Match(digits).ShouldFail();
            abc123.Advance(6).Match(letters).ShouldFail();
            abc123.Advance(6).Match(alphanumerics).ShouldFail();
        }