public IArguments Parse(string commandText, CommandDefinition commandDefinition) { var typeDefinition = commandDefinition.Type; if (CanParse(commandText, commandDefinition) == false) return null; var argumentsInput = StripCommandSignature(commandText, commandDefinition.Signature); IArguments commandArgumentsInstance = (IArguments) Activator.CreateInstance(typeDefinition); foreach (var commandParameter in commandDefinition.Parameters) { var propertyDefinition = commandParameter.PropertyInfo; var propertyValueBinder = _binderFactory.CreateForProperty(commandArgumentsInstance, propertyDefinition); var propertyValue = _parameterExtractor.ExtractValue(argumentsInput, commandParameter.Signature); if (propertyValueBinder != null) propertyValueBinder.BindValue(propertyValue); } return commandArgumentsInstance; }
public bool CanParse(string commandText, CommandDefinition commandDefinition) { if (string.IsNullOrWhiteSpace(commandText)) return false; if (commandDefinition == null) return false; return commandText.StartsWith($"{commandDefinition.Signature} ", StringComparison.InvariantCultureIgnoreCase) || commandText.Equals(commandDefinition.Signature); }
public void Parse_When_called_for_parserConfiguration_that_can_parse_input_Then_calls_commandArgumentParser_tryParse() { var input = "test -non-existing"; var definition = new CommandDefinition(typeof(string)); _commandParserConfigurationRegistryMock.Setup(x => x.GetAll()).Returns(new[] { definition }); _commandArgumentParserMock.Setup(x => x.CanParse(input, definition)).Returns(true); var command = _commandParser.ParseCommand(input); _commandArgumentParserMock.Verify(x => x.Parse(input, definition), Times.Once); }
Parse_When_called_for_parserConfiguration_that_cannot_parse_input_Then_returns_null_without_calling_commandArgumentParser_tryParse () { var input = "test -non-existing"; var definition = new CommandDefinition(typeof(string)); _commandParserConfigurationRegistryMock.Setup(x => x.GetAll()).Returns(new[] { definition }); _commandArgumentParserMock.Setup(x => x.CanParse(input, definition)).Returns(false); var command = _commandParser.ParseCommand(input); Assert.Null(command); _commandArgumentParserMock.Verify(x => x.Parse(input, It.IsAny<CommandDefinition>()), Times.Never); }
Parse_When_called_for_parserConfiguration_that_can_parse_input_and_parses_successfully_Then_calls_commandFactory_create_with_correct_arguments () { var input = "test -non-existing"; var definition = new CommandDefinition(typeof(string)); var argumentsMock = new Mock<IArguments>(); _commandParserConfigurationRegistryMock.Setup(x => x.GetAll()).Returns(new[] { definition }); _commandArgumentParserMock.Setup(x => x.CanParse(input, definition)).Returns(true); _commandArgumentParserMock.Setup(x => x.Parse(input, definition)).Returns(argumentsMock.Object); var command = _commandParser.ParseCommand(input); _commandFactoryMock.Verify(x => x.Create(argumentsMock.Object), Times.Once); }
Parse_When_called_for_parserConfiguration_that_can_parse_input_and_parses_successfully_Then_returns_command_returned_from_commandFactory () { var input = "test -non-existing"; var definition = new CommandDefinition(typeof(string)); var argumentsMock = new Mock<IArguments>(); var commandMock = new Mock<ICommand>(); _commandParserConfigurationRegistryMock.Setup(x => x.GetAll()).Returns(new[] { definition }); _commandArgumentParserMock.Setup(x => x.CanParse(input, definition)).Returns(true); _commandArgumentParserMock.Setup(x => x.Parse(input, definition)).Returns(argumentsMock.Object); _commandFactoryMock.Setup(x => x.Create(argumentsMock.Object)).Returns(commandMock.Object); var command = _commandParser.ParseCommand(input); Assert.Equal(commandMock.Object, command); }
private CommandDefinition GetStubCommandDefinition() { var argumentsType = typeof(StubArguments); var stringArgumentsPropertyInfo = argumentsType.GetProperty(nameof(StubArguments.String)); var boolArgumentsPropertyInfo = argumentsType.GetProperty(nameof(StubArguments.Flag)); var commandDefinition = new CommandDefinition(typeof(StubArguments)); commandDefinition.SetSignature("command"); commandDefinition.AddParameterDefinition(stringArgumentsPropertyInfo); commandDefinition.AddParameterDefinition(boolArgumentsPropertyInfo); return commandDefinition; }