示例#1
0
        public void ExecuteNext_ShouldFlipTwoElementsOnTheStack_WhenFlipThePancakesOnTopInstruction()
        {
            EmbeddedInterpreter embeddedInterpreter = new EmbeddedInterpreter(new string[] { "Put this X pancake on top!",
                                                                                             "Put this Xy pancake on top!",
                                                                                             "Flip the pancakes on top!",
                                                                                             "Eat all of the pancakes!" });

            embeddedInterpreter.ExecuteNext(new CancellationTokenSource().Token);
            embeddedInterpreter.ExecuteNext(new CancellationTokenSource().Token);
            var before = embeddedInterpreter.PancakeStack.ToArray();

            embeddedInterpreter.ExecuteNext(new CancellationTokenSource().Token);
            var result = embeddedInterpreter.PancakeStack.ToArray();

            Assert.AreNotEqual(before, result);
        }
示例#2
0
        public void ExecuteNext_ShouldDecrementEveryElementOnTheStack_WhenTakeOffTheSyrupInstructionAndStackIsNotEmpty()
        {
            EmbeddedInterpreter embeddedInterpreter = new EmbeddedInterpreter(new string[] { "Put this X pancake on top!",
                                                                                             "Put this Xy pancake on top!",
                                                                                             "Take off the syrup!",
                                                                                             "Eat all of the pancakes!" });

            embeddedInterpreter.ExecuteNext(new CancellationTokenSource().Token);
            embeddedInterpreter.ExecuteNext(new CancellationTokenSource().Token);
            var before   = embeddedInterpreter.PancakeStack;
            var expected = before.Select(x => x - 1).ToArray();

            embeddedInterpreter.ExecuteNext(new CancellationTokenSource().Token);
            var result = embeddedInterpreter.PancakeStack.ToArray();

            Assert.IsTrue(expected.SequenceEqual(result));
        }
示例#3
0
        /// <summary>
        /// Validates the code and runs it in the built-in interpreter with only one instruction per call in a new thread
        /// </summary>
        private void NextInstruction()
        {
            if (!IsDebuggingMode)
            {
                var validateCode = new ValidateSourceCode(Document.Text);
                _mapRealLineNumbersWithRaw = validateCode.MapRealLineNumbersWithRaw();
                if (!validateCode.CheckIfCodeEndsWithEatAllOfThePancakesInstruction())
                {
                    Console.ConsoleText = "The code must end with an \"Eat all of the pancakes!\" instruction";
                    return;
                }

                if (validateCode.ValidateInstructions().Item1)
                {
                    _embeddedInterpreter = new EmbeddedInterpreter(validateCode.ValidSourceCode);
                    DebugDocument.Lines  = new System.Collections.ObjectModel.ObservableCollection <TextLine>(Document.Text.Split(new string[] { System.Environment.NewLine }, StringSplitOptions.None).Select(x => new TextLine()
                    {
                        Text = x, BackgroundColor = Brushes.Transparent
                    }));

                    var startLine = 0;
                    _mapRealLineNumbersWithRaw.TryGetValue(0, out startLine);
                    DebugDocument.LinePointer = startLine;
                    _previousInstruction      = startLine;
                    CurrentView = _editorDebugView;

                    Console.ConsoleText = "";
                    AddHandlersToInterpreterThread(DebugMode.StepByStep);

                    IsDebuggingMode = true;
                    Debugger.Stack  = null;
                    Debugger.Label  = null;
                }
                else
                {
                    Console.ConsoleText = string.Format("Instruction on line {0} cannot be found{1}",
                                                        _mapRealLineNumbersWithRaw[validateCode.ValidateInstructions().Item2] + 1, Environment.NewLine);
                }
            }
            else if (!IsTaskWaitingForInput)
            {
                DebugDocument.Lines[_previousInstruction].BackgroundColor = Brushes.Transparent;
                DebugDocument.Lines[_previousInstruction].ForegroundColor = Brushes.White;

                _cancellationTokenSource = new CancellationTokenSource();
                CancellationToken cancellationToken = _cancellationTokenSource.Token;
                _interpreterTask = Task.Factory.StartNew(() =>
                                                         _embeddedInterpreter.ExecuteNext(cancellationToken), cancellationToken).ContinueWith((_) =>
                {
                    var line = 0;     // if error
                    _mapRealLineNumbersWithRaw.TryGetValue(_embeddedInterpreter.ProgramIterator, out line);
                    DebugDocument.LinePointer = line;
                    _previousInstruction      = line;
                });
            }
        }
示例#4
0
        public void ExecuteNext_ShouldIgnoreInstruction_WhenPutSyrupOnThePancakesInstructionAndStackIsEmpty()
        {
            EmbeddedInterpreter embeddedInterpreter = new EmbeddedInterpreter(new string[] { "Put syrup on the pancakes!",
                                                                                             "Eat all of the pancakes!" });

            embeddedInterpreter.ExecuteNext(new CancellationTokenSource().Token);
            var result = embeddedInterpreter.PancakeStack;

            Assert.IsTrue(!result.Any());
        }
示例#5
0
        public void ExecuteNext_ShouldReceivePancakeStackChangedEvent_WhenEveryIteration()
        {
            embeddedInterpreterCorrectCode = new EmbeddedInterpreter(sampleCorrectCode);
            Stack <int> result = null;

            embeddedInterpreterCorrectCode.PancakeStackChangedEvent += new EventHandler <Stack <int> >((_, e) => result = e);

            embeddedInterpreterCorrectCode.ExecuteNext(new CancellationTokenSource().Token);

            Assert.IsNotNull(result);
        }
示例#6
0
        public void ExecuteNext_ShouldReceiveNewOutputEvent_WhenOutputOccurs()
        {
            embeddedInterpreterCorrectCode = new EmbeddedInterpreter(sampleCorrectCode);
            string result = string.Empty;

            embeddedInterpreterCorrectCode.NewOutputEvent += new EventHandler <OutputEventArgs>((_, e) => result = e.CharacterOutput.ToString());

            for (int i = 0; i < sampleCorrectCode.Length; i++)
            {
                embeddedInterpreterCorrectCode.ExecuteNext(new CancellationTokenSource().Token);
            }

            Assert.AreNotEqual(string.Empty, result);
        }
示例#7
0
        public void ExecuteNext_ShouldReceiveLabelDictionaryChangedEvent_WhenNewLabelOccurs()
        {
            embeddedInterpreterCorrectCodeWithLabel = new EmbeddedInterpreter(sampleCorrectCodeWithLabel);
            Dictionary <string, int> result = null;

            embeddedInterpreterCorrectCodeWithLabel.LabelDictionaryChangedEvent += new EventHandler <Dictionary <string, int> >((_, e) => result = e);

            for (int i = 0; i < sampleCorrectCode.Length; i++)
            {
                embeddedInterpreterCorrectCodeWithLabel.ExecuteNext(new CancellationTokenSource().Token);
            }

            Assert.IsNotNull(result);
        }
示例#8
0
        public void ExecuteNext_ShouldReturnErrorMessage_WhenJumpToNonexistentLabel()
        {
            embeddedInterpreterIncorrectCodeJumpToNonexistentLabel = new EmbeddedInterpreter(sampleIncorrectCodeJumpToNonexistentLabel);
            bool wasErrorMessageReceived = false;

            embeddedInterpreterIncorrectCodeJumpToNonexistentLabel.NewOutputEvent += new EventHandler <OutputEventArgs>((_, e) => wasErrorMessageReceived = e.RuntimeError);

            for (int i = 0; i < sampleCorrectCode.Length; i++)
            {
                embeddedInterpreterIncorrectCodeJumpToNonexistentLabel.ExecuteNext(new CancellationTokenSource().Token);
            }

            Assert.IsTrue(wasErrorMessageReceived);
        }
示例#9
0
        public void ExecuteNext_ShouldReceiveWaitingForInputEvent_WhenASCIIInputOccurs()
        {
            embeddedInterpreterCorrectCodeWithASCIIInput = new EmbeddedInterpreter(sampleCorrectCodeWithASCIIInput);
            bool wasEventRaised = false;

            embeddedInterpreterCorrectCodeWithASCIIInput.WaitingForInputEvent += new EventHandler <WaitingForInputEventArgs>((_, e) =>
                                                                                                                             { wasEventRaised = e.Type == InputType.Alphanumeric;
                                                                                                                               embeddedInterpreterCorrectCodeWithASCIIInput.Input = "test";
                                                                                                                               embeddedInterpreterCorrectCodeWithASCIIInput.WaitHandle.Set(); });

            embeddedInterpreterCorrectCodeWithASCIIInput.ExecuteNext(new CancellationTokenSource().Token);

            Assert.IsTrue(wasEventRaised);
        }
示例#10
0
        public void ExecuteNext_ShouldReceiveEndOfExecutionEvent_WhenExecutingEndsInStepByStep()
        {
            embeddedInterpreterCorrectCode = new EmbeddedInterpreter(sampleCorrectCode);
            bool wasEventRaised = false;
            CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
            CancellationToken       cancellationToken        = _cancellationTokenSource.Token;

            embeddedInterpreterCorrectCode.EndOfExecutionEvent += new EventHandler((o, e) => wasEventRaised = true);

            for (int i = 0; i < sampleCorrectCode.Length; i++)
            {
                embeddedInterpreterCorrectCode.ExecuteNext(cancellationToken);
            }
            Assert.IsTrue(wasEventRaised);
        }
示例#11
0
        public void ExecuteNext_ShouldReturnErrorMessage_WhenNonNumberPassedToNumericInput()
        {
            embeddedInterpreterCorrectCodeWithNumericInput = new EmbeddedInterpreter(sampleCorrectCodeWithNumericInput);
            bool wasErrorMessageReceived = false;

            embeddedInterpreterCorrectCodeWithNumericInput.WaitingForInputEvent += new EventHandler <WaitingForInputEventArgs>((_, e) =>
            {
                embeddedInterpreterCorrectCodeWithNumericInput.Input = "text";
                embeddedInterpreterCorrectCodeWithNumericInput.WaitHandle.Set();
            });
            embeddedInterpreterCorrectCodeWithNumericInput.NewOutputEvent += new EventHandler <OutputEventArgs>((_, e) => wasErrorMessageReceived = e.RuntimeError);

            embeddedInterpreterCorrectCodeWithNumericInput.ExecuteNext(new CancellationTokenSource().Token);

            Assert.IsTrue(wasErrorMessageReceived);
        }