public void ProgramLineReturnsThreeTokens()
        {
            ProgramLine programLine = new ProgramLine(30, _tokens);

            Assert.AreEqual("ONE", programLine.NextToken().Text);
            Assert.AreEqual("TWO", programLine.NextToken().Text);
            Assert.AreEqual("THREE", programLine.NextToken().Text);
        }
        public void ProgramLineEndOfLineIsTrueOnceTokensRead()
        {
            ProgramLine programLine = new ProgramLine(30, _tokens);

            programLine.NextToken();
            programLine.NextToken();
            programLine.NextToken();
            Assert.IsTrue(programLine.EndOfLine);
        }
        public void WrongTokenPushedBackThrowsException(bool throwsException)
        {
            ProgramLine programLine = new ProgramLine(30, _tokens);

            programLine.NextToken();
            var secondToken = programLine.NextToken();
            var thirdToken  = programLine.NextToken();

            var token = throwsException ? secondToken : thirdToken;

            Test.Throws <InvalidOperationException>(() => programLine.PushToken(token), throwsException);
        }
        public void ProgramLineEndOfLineIsFalseIfLastTokenPushedBack()
        {
            ProgramLine programLine = new ProgramLine(30, _tokens);

            programLine.NextToken();
            programLine.NextToken();
            var lastToken = programLine.NextToken();

            Assert.IsTrue(programLine.EndOfLine);
            programLine.PushToken(lastToken);
            Assert.IsFalse(programLine.EndOfLine);
        }
        public void ProgramLineEndOfLineIsTrueIfEolIsPushedBack()
        {
            ProgramLine programLine = new ProgramLine(30, _tokens);

            programLine.NextToken();
            programLine.NextToken();
            programLine.NextToken();
            var eolToken = programLine.NextToken();

            programLine.PushToken(eolToken);
            Assert.IsTrue(programLine.EndOfLine);
            var nextToken = programLine.NextToken();

            Assert.AreEqual(TokenType.EndOfLine, nextToken.Seperator);
        }
Beispiel #6
0
        public void ReturnUsesReturnAddressAndForNextLoops()
        {
            var runEnvironment = new RunEnvironment();
            var sut            = new Return(runEnvironment);
            var line10         = new ProgramLine(10, new List <IToken> {
                new Token("1000")
            });
            var line1000 = new ProgramLine(1000, new List <IToken> {
            });

            line10.NextToken();
            runEnvironment.CurrentLine = line1000;
            runEnvironment.ProgramStack.Push(new StackEntry {
                Line = line10, LineToken = line10.CurrentToken
            });
            runEnvironment.ProgramStack.Push(new StackEntry {
                Line = line10, LineToken = line10.CurrentToken
            });
            runEnvironment.ProgramStack.Push(new StackEntry {
                VariableName = "A", Line = line10, LineToken = line10.CurrentToken
            });
            runEnvironment.ProgramStack.Push(new StackEntry {
                VariableName = "B", Line = line10, LineToken = line10.CurrentToken
            });
            sut.Execute();
            Assert.AreEqual(1, runEnvironment.ProgramStack.Count);
            Assert.AreEqual(line10, runEnvironment.CurrentLine);
        }
Beispiel #7
0
        private string GetNextDataStatement()
        {
            if (_currentDataLine == null)
            {
                if (_faulted)
                {
                    throw new Exceptions.OutOfDataException();
                }

                RestoreToLineNumber(null);
            }

            while (_currentDataLine != null)
            {
                while (!_currentDataLine.EndOfLine)
                {
                    var token = _currentDataLine.NextToken();
                    if (token.TokenClass == TokenClass.Data)
                    {
                        return(token.Text);
                    }
                }

                _currentDataLine = _programRepository.GetNextLine(_currentDataLine.LineNumber.Value);
                _runEnvironment.DataErrorLine = _currentDataLine?.LineNumber;
            }

            _faulted = true;
            throw new Exceptions.OutOfDataException();
        }
        public void IfLastTokenPushedBackThenNextTokenReturnsIt()
        {
            ProgramLine programLine = new ProgramLine(30, _tokens);

            programLine.NextToken();
            programLine.NextToken();
            var lastToken = programLine.NextToken();

            Assert.IsTrue(programLine.EndOfLine);
            programLine.PushToken(lastToken);
            Assert.IsFalse(programLine.EndOfLine);

            var nextToken = programLine.NextToken();

            Assert.AreEqual("THREE", nextToken.Text);
        }
        public void EvaluatorGetLineThrowsExceptionOnBadLineNumber()
        {
            var tokens = new List <IToken>
            {
                new Token("20X00")
            };
            ProgramLine programLine = new ProgramLine(30, tokens);
            var         result      = programLine.GetLineNumber();

            Assert.IsNull(result);
            Assert.AreEqual("20X00", programLine.NextToken().Text);
        }
        public void EvaluatorGetLineReturnsNullAndTokenIsNotEaten()
        {
            var tokens = new List <IToken>
            {
                new Token("DATA", TokenClass.Statement, TokenType.Data)
            };
            ProgramLine programLine = new ProgramLine(30, tokens);
            var         result      = programLine.GetLineNumber();

            Assert.AreEqual(null, result);
            Assert.AreEqual("DATA", programLine.NextToken().Text);
        }
Beispiel #11
0
        public void EndTest()
        {
            var mockRunEnvironment = new Mock <IRunEnvironment>();
            var programLine        = new ProgramLine(30, new List <IToken> {
                new Token("Name"), new Token("1")
            });

            programLine.NextToken();
            mockRunEnvironment.Setup(mre => mre.CurrentLine).Returns(programLine);
            var sut = new End(mockRunEnvironment.Object);

            Test.Throws <EndException>(sut.Execute);
            mockRunEnvironment.VerifySet(mre => mre.ContinueToken = 1);
        }
Beispiel #12
0
        public void PopDeletesReturnAddress()
        {
            var runEnvironment = new RunEnvironment();
            var sut            = new Pop(runEnvironment);
            var line10         = new ProgramLine(10, new List <IToken> {
                new Token("1000")
            });
            var line1000 = new ProgramLine(1000, new List <IToken> {
            });

            line10.NextToken();
            runEnvironment.CurrentLine = line1000;
            runEnvironment.ProgramStack.Push(new StackEntry {
                Line = line10, LineToken = line10.CurrentToken
            });
            sut.Execute();
            Assert.AreEqual(0, runEnvironment.ProgramStack.Count);
            Assert.AreEqual(line1000, runEnvironment.CurrentLine);
        }
Beispiel #13
0
        public void ReturnThrowsWhenNoReturnAddress()
        {
            var runEnvironment = new RunEnvironment();
            var sut            = new Return(runEnvironment);
            var line10         = new ProgramLine(10, new List <IToken> {
                new Token("1000")
            });
            var line1000 = new ProgramLine(1000, new List <IToken> {
            });

            line10.NextToken();
            runEnvironment.CurrentLine = line1000;
            runEnvironment.ProgramStack.Push(new StackEntry {
                VariableName = "A", Line = line10, LineToken = line10.CurrentToken
            });
            runEnvironment.ProgramStack.Push(new StackEntry {
                VariableName = "B", Line = line10, LineToken = line10.CurrentToken
            });
            Test.Throws <ReturnWithoutGosubException>(sut.Execute);
        }
        public void ProgramLineEndOfLineIsFalseIfEolPlusTokenIsPushedBack()
        {
            ProgramLine programLine = new ProgramLine(30, _tokens);

            programLine.NextToken();
            var token1   = programLine.NextToken();
            var token2   = programLine.NextToken();
            var eolToken = programLine.NextToken();

            programLine.PushToken(eolToken);
            Assert.IsTrue(programLine.EndOfLine);
            programLine.PushToken(token2);
            programLine.PushToken(token1);
            Assert.IsFalse(programLine.EndOfLine);
            var nextToken = programLine.NextToken();

            Assert.AreEqual(token1, nextToken);
            nextToken = programLine.NextToken();
            Assert.AreEqual(token2, nextToken);
        }
        public void TokenPushedBackWhenAtStartOfLineThrowsException(bool throwsException)
        {
            ProgramLine programLine = new ProgramLine(30, _tokens);
            var         token       = throwsException ? new Token(")", TokenClass.Seperator, TokenType.CloseBracket) : programLine.NextToken();

            Test.Throws <InvalidOperationException>(() => programLine.PushToken(token), throwsException);
        }