예제 #1
0
        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);
        }
예제 #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());
        }
예제 #3
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());
        }
예제 #4
0
        public void Parse_EscapedCommentSymbol_CommentSymbolIncludedInParameter()
        {
            // arrange
            var sut     = new Parser();
            var builder = new CommandInput.Builder(1, "command");

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

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

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

            builder.AddParameter(new ParameterNameCommandParameter("name"));
            builder.AddParameter(new LiteralCommandParameter("value"));
            var expectedCommand = builder.Build();

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

            // assert
            Assert.Equal(expectedCommand, result[0], new CommandInputEqualityComparer());
        }
예제 #6
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());
        }
예제 #7
0
        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);
        }
예제 #8
0
        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);
        }
예제 #9
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());
        }
예제 #10
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());
        }
예제 #11
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());
        }
예제 #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());
        }
예제 #13
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());
        }
예제 #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());
        }
예제 #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());
        }
예제 #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());
        }