コード例 #1
0
        public void FinishedShouldBeTrueWhenNoDataRemains()
        {
            //Arrange
            var keeper = new StringKeeper("test");

            keeper.Take(); //t
            keeper.Take(); //e
            keeper.Take(); //s
            keeper.Take(); //t

            //Act/Assert
            keeper.Finished.Should().BeTrue();
        }
コード例 #2
0
        public void TakeReturnsNulWhenNoDataRemains()
        {
            //Arrange
            var keeper = new StringKeeper("test");

            keeper.Take(); //t
            keeper.Take(); //e
            keeper.Take(); //s
            keeper.Take(); //t

            //Act
            var result = keeper.Take();

            //Assert
            result.Should().Be("\0");
        }
コード例 #3
0
        public static IEnumerable <ApplyIfToken> Analyse(string source)
        {
            var stringKeeper = new StringKeeper(source);

            while (!stringKeeper.IsEmpty)
            {
                if (stringKeeper.IsWhitespace)
                {
                    stringKeeper = stringKeeper.Take();
                    continue;
                }

                StringKeeper newStringKeeper;
                ApplyIfToken token;
                if (TryTake(stringKeeper, Tokens, out newStringKeeper, out token) ||
                    TryTakeIdentifier(stringKeeper, out newStringKeeper, out token))
                {
                    stringKeeper = newStringKeeper;
                    yield return(token);
                }
                else
                {
                    var finish = new StringKeeper(stringKeeper);
                    while (!finish.IsEmpty)
                    {
                        finish = finish.Take();
                    }
                    yield return(new ApplyIfToken(ApplyIfTokenType.Error, finish.Difference(stringKeeper)));

                    stringKeeper = finish;
                }
            }
        }
コード例 #4
0
        public void TakeMovesThePositionAlong()
        {
            //Arrange
            var keeper = new StringKeeper("test");

            //Act
            keeper.Take();

            //Assert
            keeper.Next.Should().Be('e');
        }
コード例 #5
0
        public void TakeReturnsTheNextCharacter()
        {
            //Arrange
            var keeper = new StringKeeper("test");

            //Act
            var result = keeper.Take();

            //Assert
            result.Should().Be("t");
        }
コード例 #6
0
        public void CopiedInstancesShouldHaveTheSamePositionAsTheOriginal()
        {
            //Arrange
            var keeper = new StringKeeper("test");

            keeper.Take();

            //Act
            var keeper2 = new StringKeeper(keeper);

            //Assert
            keeper2.TakeAll().Should().Be(keeper.TakeAll());
        }
コード例 #7
0
        public void WhitespaceSkippedDoesNotHaveToBeAtTheStart()
        {
            //Arrange
            var keeper = new StringKeeper("X \t\n\r\ftest");

            keeper.Take();

            //Act
            keeper.SkipWhiteSpace();

            //Assert
            keeper.Next.Should().Be('t');
        }
コード例 #8
0
        public void TakeAllReturnsRemainingText()
        {
            //Arrange
            var keeper = new StringKeeper("test");

            keeper.Take();

            //Act
            var result = keeper.TakeAll();

            //Assert
            result.Should().Be("est");
        }
コード例 #9
0
        private static bool TryTake(StringKeeper stringKeeper, Dictionary <string, ApplyIfToken> tokens, out StringKeeper newStringKeeper, out ApplyIfToken token)
        {
            var thisToken = tokens.FirstOrDefault(x => stringKeeper.IsNext(x.Key, false));

            if (thisToken.Key != null)
            {
                var work = stringKeeper.Take(thisToken.Key.Length);
                newStringKeeper = work;
                token           = thisToken.Value;
                return(true);
            }

            newStringKeeper = stringKeeper;
            token           = null;
            return(false);
        }
コード例 #10
0
        private static bool TryTakeIdentifier(StringKeeper stringKeeper, out StringKeeper newStringKeeper, out ApplyIfToken token)
        {
            var work  = new StringKeeper(stringKeeper);
            var value = string.Empty;

            while (!IsTerminator(work))
            {
                value += work.Next;
                work   = work.Take();
            }

            if (!string.IsNullOrWhiteSpace(value))
            {
                newStringKeeper = work;
                token           = new ApplyIfToken(ApplyIfTokenType.Identifier, value);
                return(true);
            }

            newStringKeeper = stringKeeper;
            token           = null;
            return(false);
        }