コード例 #1
0
        public static ITurtleGraphicsSyntaxAnalyser GetSyntaxAnalyserMock(TurtleGraphicsCommand command)
        {
            var commands = new List<TurtleGraphicsCommand> { command };

            TurtleGraphicsSyntaxAnalyser.Expect(m => m.ConvertTokensToCommands(null)).IgnoreArguments().
                Return(commands).Repeat.Once();

            return TurtleGraphicsSyntaxAnalyser;
        }
コード例 #2
0
        private static TurtleGraphicsCommand GetUncertainCommandHelper()
        {
            var turtleGraphicsCommand = new TurtleGraphicsCommand
            {
                Status = TurtleGraphicsCommandStatus.InvalidCommand,
            };

            turtleGraphicsCommand.ErrorMessage = ReflectionHelper.GetEnumDescription(
                turtleGraphicsCommand.Status);

            return turtleGraphicsCommand;
        }
コード例 #3
0
        public void CanCancelExecution()
        {
            // Arrange
            var turtleGraphicsCommand = new TurtleGraphicsCommand { ProgramText = null };
            var cancellerMock = MockRepository.GenerateMock<ICanceller>();
            cancellerMock.Expect(m => m.ShouldCancel()).Return(true).Repeat.Once();

            // Act
            var result = turtleGraphicsCommand.Execute(cancellerMock);

            // Assert
            Assert.AreEqual(false, result);
            cancellerMock.VerifyAllExpectations();
        }
コード例 #4
0
        public void CanCreateADefaultInstance()
        {
            // Arrange and Act
            var turtleGraphicsCommand = new TurtleGraphicsCommand();

            // Assert
            Assert.AreEqual(null, turtleGraphicsCommand.CommandText);
            Assert.AreEqual(string.Empty, turtleGraphicsCommand.ProgramText);
            Assert.AreEqual(0, turtleGraphicsCommand.Commands.Count);
            Assert.AreEqual(TurtleGraphicsCommandStatus.InvalidCommand, turtleGraphicsCommand.Status);
            Assert.AreEqual(null, turtleGraphicsCommand.Attribute);
            Assert.AreEqual(null, turtleGraphicsCommand.ImplementingFunctionName);
            Assert.AreEqual(null, turtleGraphicsCommand.ExecutionContext);
        }
コード例 #5
0
        private static void AddProgramText(TurtleGraphicsCommand turtleGraphicsCommand, IList<string> tokens, 
            int startTokenForCommand, int endTokenForCommand)
        {
            turtleGraphicsCommand.ProgramText = string.Empty;

            for (var i = startTokenForCommand; i < endTokenForCommand; i++)
            {
                var currentToken = tokens[i];

                if(IsBlockToken(currentToken))
                {
                    turtleGraphicsCommand.ProgramText += Environment.NewLine + currentToken + " " + Environment.NewLine;
                }
                else
                {
                    turtleGraphicsCommand.ProgramText += currentToken + " ";
                }
            }
        }
コード例 #6
0
        public void CanCreateASelectPenInstance()
        {
            // Arrange and Act
            var turtleGraphicsAttribute = new TurtleGraphicsAttribute();
            var turtleGraphicsArgumentAttribute = new List<TurtleGraphicsArgumentAttribute>();
            var argumentValues = new List<string>();

            var turtleGraphicsCommand = new TurtleGraphicsCommand
                                            {
                                                CommandText = GlobalConstants.SelectPenCommandText,
                                                Attribute = turtleGraphicsAttribute,
                                                ArgumentAttributes = turtleGraphicsArgumentAttribute,
                                                ArgumentValues = argumentValues,
                                                Status = TurtleGraphicsCommandStatus.Valid
                                            };

            // Assert
            Assert.AreEqual(GlobalConstants.SelectPenCommandText, turtleGraphicsCommand.CommandText);
            Assert.AreEqual(turtleGraphicsAttribute, turtleGraphicsCommand.Attribute);
            Assert.AreEqual(argumentValues, turtleGraphicsCommand.ArgumentValues);
            Assert.AreEqual(turtleGraphicsArgumentAttribute, turtleGraphicsCommand.ArgumentAttributes);
            Assert.AreEqual(TurtleGraphicsCommandStatus.Valid, turtleGraphicsCommand.Status);
        }
コード例 #7
0
        public void CanValidateNoIntegerArgument()
        {
            // Arrange
            var turtleGraphicsCommand = new TurtleGraphicsCommand
                                            {
                                                Attribute = new TurtleGraphicsAttribute(),
                                                ArgumentAttributes =
                                                    new List<TurtleGraphicsArgumentAttribute>
                                                        {
                                                            new TurtleGraphicsArgumentAttribute
                                                                {ArgumentType = DataTypes.Integer},
                                                            new TurtleGraphicsArgumentAttribute
                                                                {ArgumentType = DataTypes.Integer}
                                                        },
                                                        ArgumentValues = new List<string>{"1", "NAN"}
                                            };

            // Act
            turtleGraphicsCommand.ValidateCommand();

            // Assert
            Assert.AreEqual(turtleGraphicsCommand.Status, TurtleGraphicsCommandStatus.NotAnInteger);
            Assert.AreEqual(turtleGraphicsCommand.ErrorMessage, ArgumentNotAnInteger);
        }
コード例 #8
0
        public void CanValidateMissingAttributes()
        {
            // Arrange
            var turtleGraphicsCommand = new TurtleGraphicsCommand();

            // Act
            turtleGraphicsCommand.ValidateCommand();

            // Assert
            Assert.AreEqual(turtleGraphicsCommand.Status, TurtleGraphicsCommandStatus.InvalidCommand);
            Assert.AreEqual(turtleGraphicsCommand.ErrorMessage, NotAValidLogoCommand);
        }
コード例 #9
0
        public void CanSetNullProgramText()
        {
            // Arrange and Act
            var turtleGraphicsCommand = new TurtleGraphicsCommand { ProgramText = null };

            // Assert
            Assert.AreEqual(string.Empty, turtleGraphicsCommand.ProgramText);
        }
        private static void CanExecuteCommand(List<string> args, ITurtleGraphicsCommands
            turtleGraphicsSystemMock, TurtleGraphicsAttribute turtleGraphicsAttribute,
            List<TurtleGraphicsArgumentAttribute> argumentAttributes, string implementingFunctionName)
        {
            // Arrange
            var tokens = new List<string> { turtleGraphicsAttribute.CommandText};

            if (args != null)
            {
                tokens.AddRange(args);
            }

            var textParserMock = GetTextParserMock(tokens);

            var turtleGraphicsCommand = new TurtleGraphicsCommand
            {
                Attribute = turtleGraphicsAttribute,
                ArgumentAttributes = argumentAttributes,
                CommandText = turtleGraphicsAttribute.CommandText,
                ArgumentValues = args,
                Status = TurtleGraphicsCommandStatus.Valid,
                ExecutionContext = turtleGraphicsSystemMock,
                ImplementingFunctionName = implementingFunctionName,
            };

            var turtleGraphicsSyntaxAnalyser = GetSyntaxAnalyserMock(turtleGraphicsCommand);

            var commandProcessor = GetExecutionEngineInstance(textParserMock, turtleGraphicsSyntaxAnalyser);

            var commandLine = turtleGraphicsAttribute.CommandText + " ";

            if (args != null)
            {
                foreach (var arg in args)
                {
                    commandLine += arg;
                    commandLine += " ";
                }
            }

            // Act
            var turtleGraphicsCommandReturned = commandProcessor.ExecuteCommandLine(commandLine, false, true);

            // Assert
            textParserMock.VerifyAllExpectations();
            turtleGraphicsSystemMock.VerifyAllExpectations();
            turtleGraphicsSyntaxAnalyser.VerifyAllExpectations();
            Assert.AreEqual(TurtleGraphicsCommandStatus.Valid, turtleGraphicsCommandReturned[0].Status);
        }
コード例 #11
0
        private void GetArgumentsFromTokens(IList<string> tokens, TurtleGraphicsCommand turtleGraphicsCommand)
        {
            var argValues = new List<string>();

            for (var nextAttrIdx = 0; nextAttrIdx < turtleGraphicsCommand.ArgumentAttributes.Count;
                 nextAttrIdx++)
            {
                if (_currentTokenIndex >= tokens.Count)
                {
                    break;
                }

                argValues.Add(tokens[_currentTokenIndex]);

                _currentTokenIndex++;
            }

            turtleGraphicsCommand.ArgumentValues = argValues;
        }
コード例 #12
0
        public void ExecuteCommands(List<TurtleGraphicsCommand> commands)
        {
            var program = new TurtleGraphicsCommand { Commands = commands };

            program.Execute(_canceller);
        }
コード例 #13
0
        public void CanFindCommandOfName()
        {
            // Arrange
            var turtleGraphicsCommand = new TurtleGraphicsCommand
                                            {
                                                CommandText = SimpleCommandText,
                                            };

            // Act
            var hasCommand = turtleGraphicsCommand.HasCommandOfName(SimpleCommandText);

            // Assert
            Assert.AreEqual(hasCommand, true);
        }
コード例 #14
0
        public void CanSetProgramTextAndNormaliseLineBreaks()
        {
            // Arrange and Act
            var turtleGraphicsCommand = new TurtleGraphicsCommand
                                            {
                                                ProgramText = SimpleProgramText + Environment.NewLine + Environment.NewLine
                                            };

            // Assert
            Assert.AreEqual(SimpleProgramText + Environment.NewLine, turtleGraphicsCommand.ProgramText);
        }
コード例 #15
0
        public void CanSetProgramText()
        {
            // Arrange and Act
            var turtleGraphicsCommand = new TurtleGraphicsCommand {ProgramText = SimpleProgramText};

            // Assert
            Assert.AreEqual(SimpleProgramText, turtleGraphicsCommand.ProgramText);
        }
コード例 #16
0
        public void CanCountInvalidCommand()
        {
            // Arrange
            var turtleGraphicsCommand = new TurtleGraphicsCommand
            {
                ProgramText = null,
                Commands = new List<TurtleGraphicsCommand>()
            };

            turtleGraphicsCommand.Commands.Add(new TurtleGraphicsCommand());

            // Act
            var invalidCommandCount = turtleGraphicsCommand.CountInValidCommands();

            // Assert
            Assert.AreEqual(invalidCommandCount, 2);
        }
        private static List<TurtleGraphicsCommand> CanExecuteCommand(List<string> args, ITurtleGraphicsControlStructures
            turtleGraphicsControlStructuresMock, TurtleGraphicsAttribute turtleGraphicsAttribute,
            List<TurtleGraphicsArgumentAttribute> argumentAttributes, TurtleGraphicsCommand turtleGraphicsCommand,
            string implementingFunctionName, ExecutionEngineStatusChangedEventHandler stateChangedHandler = null,
            bool hasCompletedWithoutErrors = true)
        {
            // Arrange
            var tokens = new List<string> { turtleGraphicsAttribute.CommandText};

            if (args != null)
            {
                tokens.AddRange(args);
            }

            var textParserMock = GetTextParserMock(tokens);

            turtleGraphicsCommand.Attribute = turtleGraphicsAttribute;
            turtleGraphicsCommand.ArgumentAttributes = argumentAttributes;
            turtleGraphicsCommand.CommandText = turtleGraphicsAttribute.CommandText;
            turtleGraphicsCommand.ArgumentValues = args;
            turtleGraphicsCommand.Status = TurtleGraphicsCommandStatus.Valid;
            turtleGraphicsCommand.ExecutionContext = turtleGraphicsControlStructuresMock;
            turtleGraphicsCommand.ImplementingFunctionName = implementingFunctionName;

            var turtleGraphicsSyntaxAnalyser = GetSyntaxAnalyserMock(turtleGraphicsCommand);
            var executionEngine = GetExecutionEngineInstance(textParserMock, turtleGraphicsSyntaxAnalyser);

            if (stateChangedHandler != null)
            {
                executionEngine.StatusChanged += stateChangedHandler;
            }

            var commandLine = turtleGraphicsAttribute.CommandText + " ";

            if (args != null)
            {
                foreach (var arg in args)
                {
                    commandLine += arg;
                    commandLine += " ";
                }
            }

            // Act
            var turtleGraphicsCommandReturned = executionEngine.ExecuteCommandLine(commandLine, false, true);

            // Assert
            textParserMock.VerifyAllExpectations();
            turtleGraphicsControlStructuresMock.VerifyAllExpectations();
            Assert.AreEqual(hasCompletedWithoutErrors, executionEngine.HasExecutedWithoutErrors());

            return turtleGraphicsCommandReturned;
        }
コード例 #18
0
        public void CanGetErrorMessages()
        {
            // Arrange
            var turtleGraphicsCommand = new TurtleGraphicsCommand
            {
                ProgramText = null,
                Commands = new List<TurtleGraphicsCommand>()
            };

            turtleGraphicsCommand.Commands.Add(new TurtleGraphicsCommand());

            // Act
            var errorMessages = turtleGraphicsCommand.GetErrorMessages().Trim();

            // Assert
            Assert.AreEqual(errorMessages, string.Empty);
        }
コード例 #19
0
        private void GetFunctionArgumentsFromTokens(IList<string> tokens, TurtleGraphicsCommand 
            turtleGraphicsCommand)
        {
            var argValues = new List<string>();

            while (_currentTokenIndex < tokens.Count && !IsStartBlockToken(tokens[_currentTokenIndex]))
            {
                argValues.Add(tokens[_currentTokenIndex]);
                _currentTokenIndex++;
            }

            turtleGraphicsCommand.ArgumentValues = argValues;

            // Function takes a variable number of parameters that can only be determined at this point
            var countDifference = turtleGraphicsCommand.ArgumentValues.Count -
                                  turtleGraphicsCommand.ArgumentAttributes.Count;

            if(countDifference >= 1)
            {
                var commandAttribute = turtleGraphicsCommand.ArgumentAttributes[0];

                for (var i = 0; i < countDifference; i++)
                {
                    turtleGraphicsCommand.ArgumentAttributes.Add(commandAttribute);
                }
            }
        }
コード例 #20
0
        public void CanGetTypedValueOfVariableInParameter()
        {
            // Arrange
            var turtleGraphicsCommand = new TurtleGraphicsCommand
            {
                Attribute = new TurtleGraphicsAttribute(),
                ArgumentAttributes =
                    new List<TurtleGraphicsArgumentAttribute>
                                                        {
                                                            new TurtleGraphicsArgumentAttribute
                                                                {
                                                                    ArgumentType = DataTypes.String,
                                                                    AllowVariableSubstitution = true,
                                                                    RegEx = GlobalConstants.PatternThatMatchVariable
                                                                }
                                                        },
                ArgumentValues = new List<string> { ValidVariablePattern }
            };

            // Act
            var value = turtleGraphicsCommand.GetTypedArgumentValue(0);

            // Assert
            Assert.AreEqual("0", value);
        }
        public void CanHandleInvalidCommand()
        {
            // Arrange
            var tokens = new List<string> { InvalidCommand };

            var textParserMock = GetTextParserMock(tokens);

            var turtleGraphicsCommand = new TurtleGraphicsCommand
            {
                CommandText = InvalidCommand,
                Status = TurtleGraphicsCommandStatus.InvalidCommand
            };

            var turtleGraphicsSyntaxAnalyser = GetSyntaxAnalyserMock(turtleGraphicsCommand);
            var commandProcessor = GetExecutionEngineInstance(textParserMock, turtleGraphicsSyntaxAnalyser);

            // Act
            var turtleGraphicsCommandReturned = commandProcessor.ExecuteCommandLine(InvalidCommand,
                false);

            // Assert
            textParserMock.VerifyAllExpectations();

            Assert.AreEqual(TurtleGraphicsCommandStatus.InvalidCommand, turtleGraphicsCommandReturned[0].Status);
        }
コード例 #22
0
        public void CanValidateArgumentPattern()
        {
            // Arrange
            var turtleGraphicsCommand = new TurtleGraphicsCommand
                                            {
                                                Attribute = new TurtleGraphicsAttribute(),
                                                ArgumentAttributes =
                                                    new List<TurtleGraphicsArgumentAttribute>
                                                        {
                                                            new TurtleGraphicsArgumentAttribute
                                                                {
                                                                    ArgumentType = DataTypes.String,
                                                                    RegEx = GlobalConstants.PatternThatMatchVariable
                                                                }
                                                        },
                                                ArgumentValues = new List<string> {InvalidVariablePattern}
                                            };

            // Act
            turtleGraphicsCommand.ValidateCommand();

            // Assert
            Assert.AreEqual(turtleGraphicsCommand.Status, TurtleGraphicsCommandStatus.InvalidArgumentPattern);
            Assert.AreEqual(turtleGraphicsCommand.ErrorMessage, ArgumentInvalidPattern);
        }
コード例 #23
0
        public void CanValidateMissingArgument()
        {
            // Arrange
            var turtleGraphicsCommand = new TurtleGraphicsCommand
                                            {
                                                Attribute = new TurtleGraphicsAttribute(),
                                                ArgumentAttributes =
                                                    new List<TurtleGraphicsArgumentAttribute>
                                                        {
                                                            new TurtleGraphicsArgumentAttribute()
                                                        }
                                            };

            // Act
            turtleGraphicsCommand.ValidateCommand();

            // Assert
            Assert.AreEqual(turtleGraphicsCommand.Status, TurtleGraphicsCommandStatus.MissingArguments);
            Assert.AreEqual(turtleGraphicsCommand.ErrorMessage, ArgumentMissing);
        }
コード例 #24
0
        public void CanFindCommandOfNameInInnerCommand()
        {
            // Arrange
            var turtleGraphicsCommand = new TurtleGraphicsCommand
            {
               Commands = new List<TurtleGraphicsCommand>(),
            };

            turtleGraphicsCommand.Commands.Add(new TurtleGraphicsCommand {CommandText = SimpleCommandText,});
            turtleGraphicsCommand.Commands.Add(new TurtleGraphicsCommand());

            // Act
            var hasCommand = turtleGraphicsCommand.HasCommandOfName(SimpleCommandText);

            // Assert
            Assert.AreEqual(hasCommand, true);
        }