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; }
private static TurtleGraphicsCommand GetUncertainCommandHelper() { var turtleGraphicsCommand = new TurtleGraphicsCommand { Status = TurtleGraphicsCommandStatus.InvalidCommand, }; turtleGraphicsCommand.ErrorMessage = ReflectionHelper.GetEnumDescription( turtleGraphicsCommand.Status); return turtleGraphicsCommand; }
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(); }
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); }
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 + " "; } } }
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); }
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); }
public void CanValidateMissingAttributes() { // Arrange var turtleGraphicsCommand = new TurtleGraphicsCommand(); // Act turtleGraphicsCommand.ValidateCommand(); // Assert Assert.AreEqual(turtleGraphicsCommand.Status, TurtleGraphicsCommandStatus.InvalidCommand); Assert.AreEqual(turtleGraphicsCommand.ErrorMessage, NotAValidLogoCommand); }
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); }
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; }
public void ExecuteCommands(List<TurtleGraphicsCommand> commands) { var program = new TurtleGraphicsCommand { Commands = commands }; program.Execute(_canceller); }
public void CanFindCommandOfName() { // Arrange var turtleGraphicsCommand = new TurtleGraphicsCommand { CommandText = SimpleCommandText, }; // Act var hasCommand = turtleGraphicsCommand.HasCommandOfName(SimpleCommandText); // Assert Assert.AreEqual(hasCommand, true); }
public void CanSetProgramTextAndNormaliseLineBreaks() { // Arrange and Act var turtleGraphicsCommand = new TurtleGraphicsCommand { ProgramText = SimpleProgramText + Environment.NewLine + Environment.NewLine }; // Assert Assert.AreEqual(SimpleProgramText + Environment.NewLine, turtleGraphicsCommand.ProgramText); }
public void CanSetProgramText() { // Arrange and Act var turtleGraphicsCommand = new TurtleGraphicsCommand {ProgramText = SimpleProgramText}; // Assert Assert.AreEqual(SimpleProgramText, turtleGraphicsCommand.ProgramText); }
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; }
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); }
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); } } }
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); }
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); }
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); }
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); }