Example #1
0
        public void Execute_FactoryThrowsException_FailureResultReturned()
        {
            // arrange
            var commandBuilder = new CommandInput.Builder(1, "command");
            var commandInput   = commandBuilder.Build();

            var parser = Substitute.For <IParser>();

            parser.Parse(Arg.Any <string>()).Returns(new[] { commandInput });

            var factory = Substitute.For <ICommandFactory>();

            factory.Create(Arg.Any <CommandInput>()).Returns(x => { throw new Exception(); });

            var binder = Substitute.For <ICommandParameterBinder>();

            var           sut             = new Session(parser, factory, binder);
            FailureResult executionResult = null;

            // act
            sut.Execute("command", result => executionResult = result as FailureResult);

            // assert
            Assert.Equal(1, executionResult.SourceLine);
        }
Example #2
0
        private CommandInput ParseCommandInput()
        {
            var parsedBlock = ParseBlock();

            if (parsedBlock.Block == null)
            {
                return(null);
            }

            if (parsedBlock.Block.GetType() != typeof(LiteralCommandParameter))
            {
                throw new ParseException(parsedBlock.LocationStart, "Command name must be a literal.");
            }

            var commandInputBuilder = new CommandInput.Builder(parsedBlock.LocationStart.LineNumber, (parsedBlock.Block as LiteralCommandParameter).Value);

            while (!parsedBlock.IsEndOfLine)
            {
                parsedBlock = ParseBlock();

                if (parsedBlock.Block != null)
                {
                    commandInputBuilder.AddParameter(parsedBlock.Block);
                }
            }

            return(commandInputBuilder.Build());
        }
Example #3
0
        private CommandInput CreateCommandInput(string commandName, params CommandParameter[] parameters)
        {
            var builder = new CommandInput.Builder(1, commandName);

            foreach (var parameter in parameters)
            {
                builder.AddParameter(parameter);
            }

            return(builder.Build());
        }
        public void AddParameter_ParameterIsNull_ThrowsException()
        {
            // arrange
            var    sut       = new CommandInput.Builder(1, "command");
            Action sutAction = () => sut.AddParameter(null);

            // act, assert
            var ex = Assert.Throws <ArgumentNullException>(sutAction);

            Assert.Equal("value", ex.ParamName);
        }
        public void Build_WhenCalled_ReturnsCommandInput()
        {
            // arrange
            var sut = new CommandInput.Builder(3, "command");

            // act
            var commandInput = sut.Build();

            // assert
            Assert.Equal(3, commandInput.SourceLine);
            Assert.Equal("command", commandInput.CommandName);
        }
        public void Build_EmptyOrWhiteSpaceParameter_ReturnsCommandInputWithParameter(string value)
        {
            // arrange
            var sut = new CommandInput.Builder(3, "command");

            sut.AddParameter(new LiteralCommandParameter(value));

            // act
            var commandInput = sut.Build();

            // assert
            Assert.Equal(value, (commandInput.Parameters[0] as LiteralCommandParameter).Value);
        }
Example #7
0
        public void Parse_NewlineInsideBracketedParameter_ParameterIncludesNewline()
        {
            // arrange
            var sut     = new Parser();
            var builder = new CommandInput.Builder(1, "command");

            builder.AddParameter(new LiteralCommandParameter("param\nparam"));
            var expectedCommand = builder.Build();

            // act
            var result = sut.Parse("command (param\nparam)");

            // assert
            Assert.Equal(expectedCommand, result[0], new CommandInputEqualityComparer());
        }
Example #8
0
        public void Parse_EscapedBrackets_BracketsIncludedInParameter()
        {
            // arrange
            var sut     = new Parser();
            var builder = new CommandInput.Builder(1, "command");

            builder.AddParameter(new LiteralCommandParameter("(param)"));
            var expectedCommand = builder.Build();

            // act
            var result = sut.Parse(@"command \(param\)");

            // assert
            Assert.Equal(expectedCommand, result[0], new CommandInputEqualityComparer());
        }
Example #9
0
        public void Parse_ParameterIsOnlyNewlines_NewlinesIncludedInParameter()
        {
            // arrange
            var sut     = new Parser();
            var builder = new CommandInput.Builder(1, "command");

            builder.AddParameter(new LiteralCommandParameter("\n\n"));
            var expectedCommand = builder.Build();

            // act
            var result = sut.Parse("command (\n\n)");

            // assert
            Assert.Equal(expectedCommand, result[0], new CommandInputEqualityComparer());
        }
        public void Build_AfterAddParameterCalled_ReturnsCommandInputWithNumberedParameter()
        {
            // arrange
            var sut = new CommandInput.Builder(3, "command");

            sut.AddParameter(new LiteralCommandParameter("value1"));
            sut.AddParameter(new LiteralCommandParameter("value2"));

            // act
            var commandInput = sut.Build();

            // assert
            Assert.Equal("value1", (commandInput.Parameters[0] as LiteralCommandParameter).Value);
            Assert.Equal("value2", (commandInput.Parameters[1] as LiteralCommandParameter).Value);
        }
Example #11
0
        public void Parse_NestedBracketsInParameters_ParameterIncludesBrackets()
        {
            // arrange
            var sut     = new Parser();
            var builder = new CommandInput.Builder(1, "command");

            builder.AddParameter(new LiteralCommandParameter("\nic  (pa ram)\nic (pa ram)"));
            var expectedCommand = builder.Build();

            // act
            var result = sut.Parse("command (\nic  \\(pa ram\\)\nic \\(pa ram\\))");

            // assert
            Assert.Equal(expectedCommand, result[0], new CommandInputEqualityComparer());
        }
Example #12
0
        public void Parse_ParameterIncludesSlash_ParameterParsed()
        {
            // arrange
            var sut     = new Parser();
            var builder = new CommandInput.Builder(1, "command");

            builder.AddParameter(new LiteralCommandParameter(@"\"));
            var expectedCommand = builder.Build();

            // act
            var result = sut.Parse(@"command \\");

            // assert
            Assert.Equal(expectedCommand, result[0], new CommandInputEqualityComparer());
        }
Example #13
0
        public void Parse_SingleParameter_ParameterParsed(string input)
        {
            // arrange
            var sut     = new Parser();
            var builder = new CommandInput.Builder(1, "command");

            builder.AddParameter(new LiteralCommandParameter("param"));
            var expectedCommand = builder.Build();

            // act
            var result = sut.Parse(input);

            // assert
            Assert.Equal(expectedCommand, result[0], new CommandInputEqualityComparer());
        }
Example #14
0
        public void Parse_InputContainsParameterNameSymbolInsideBlock_ParsesAsLiteral()
        {
            // arrange
            var builder = new CommandInput.Builder(1, "cmd");

            builder.AddParameter(new LiteralCommandParameter("p -ar"));
            var expectedCommand = builder.Build();

            var sut = new Parser();

            // act
            var result = sut.Parse("cmd (p -ar)");

            // assert
            Assert.Equal(expectedCommand, result[0], new CommandInputEqualityComparer());
        }
Example #15
0
        public void Parse_InputContainsParameterName_ParsesParameterName()
        {
            // arrange
            var builder = new CommandInput.Builder(1, "cmd");

            builder.AddParameter(new ParameterNameCommandParameter("par"));
            var expectedCommand = builder.Build();

            var sut = new Parser();

            // act
            var result = sut.Parse("cmd -par");

            // assert
            Assert.Equal(expectedCommand, result[0], new CommandInputEqualityComparer());
        }
Example #16
0
        public void Parse_InputContainsEscapedVariableSymbol_ParseSymbolAsLiteral()
        {
            // arrange
            var builder = new CommandInput.Builder(1, "cmd");

            builder.AddParameter(new LiteralCommandParameter("$var$"));
            var expectedCommand = builder.Build();

            var sut = new Parser();

            // act
            var result = sut.Parse(@"cmd \$var\$");

            // assert
            Assert.Equal(expectedCommand, result[0], new CommandInputEqualityComparer());
        }
Example #17
0
        public void Parse_MultipleBracketedParameters_ParametersParsedProperly()
        {
            // arrange
            var sut     = new Parser();
            var builder = new CommandInput.Builder(1, "command");

            builder.AddParameter(new LiteralCommandParameter("pa ram"));
            builder.AddParameter(new LiteralCommandParameter("pa ram"));
            var expectedCommand = builder.Build();

            // act
            var result = sut.Parse("command (pa ram) (pa ram)");

            // assert
            Assert.Equal(expectedCommand, result[0], new CommandInputEqualityComparer());
        }
Example #18
0
        public void Parse_TwoCommandsTwoParameters_ParsedCommandsWithParameters(string input)
        {
            // arrange
            var sut = new Parser();

            var builder1 = new CommandInput.Builder(1, "command");

            builder1.AddParameter(new LiteralCommandParameter("param1"));
            builder1.AddParameter(new LiteralCommandParameter("param2"));
            var expectedCommand1 = builder1.Build();

            var builder2 = new CommandInput.Builder(2, "command");

            builder2.AddParameter(new LiteralCommandParameter("param1"));
            builder2.AddParameter(new LiteralCommandParameter("param2"));
            var expectedCommand2 = builder2.Build();

            // act
            var result = sut.Parse(input);

            // assert
            Assert.Equal(new[] { expectedCommand1, expectedCommand2 }, result, new CommandInputEqualityComparer());
        }
Example #19
0
        private CommandInput CreateCommandInput(int sourceLine, string commandName)
        {
            var builder = new CommandInput.Builder(sourceLine, commandName);

            return(builder.Build());
        }