Exemple #1
0
        /// <summary>
        /// This method prompts the user for the starting coordinate and heading for the rover. If the user does not enter data, then this assumes they have no more rovers to track.
        /// If that is the case, it returns null. Otherwise it will loop infinitely until the user enters valid data or no data at all.
        /// </summary>
        /// <returns></returns>
        static NasaMarsRover GetUserEnteredRover()
        {
            while (true)
            {
                Console.WriteLine("".PadRight(75, '*'));
                var userInput = PromptForUserInput("Please provide the starting coordinate and heading for the rover.",
                                                   "Input must be two NON-NEGATIVE whole numbers and a cardinal direction, separated by a space,",
                                                   "horizontal (or x) coordinate first, then the vertical (or y) coordinate, ",
                                                   "then cardinal heading (i.e., 5 6 W)",
                                                   "Leave this empty and press ENTER if there are no more rovers to track.");

                if (string.IsNullOrWhiteSpace(userInput))
                {
                    return(null);
                }

                var parseResult = InputParseService.ParseUserInputToCreateRover(userInput);
                if (parseResult.IsResultParsedSuccessfully)
                {
                    return(parseResult.Results);
                }

                Console.WriteLine("Unable to parse input. Please try again.");
                Console.WriteLine(parseResult.ParsingException.Message);
            }
        }
Exemple #2
0
        public void InputParsing_SimpleCommand_TwoTokens()
        {
            const string      input  = "cd ../folder";
            InputParseService parser = new InputParseService();
            ParsedInput       parsed = parser.Parse(input);

            Assert.Equal(input, parsed.RawInput);
            Assert.Equal(2, parsed.Tokens.Count);
            Assert.Equal("cd", parsed.Tokens[0]);
            Assert.Equal("../folder", parsed.Tokens[1]);
        }
Exemple #3
0
        public void InputParsing_CommandWithTwoComplexParameters_ThreeTokens()
        {
            const string      input  = "_command42 \"complex param '## $ ! | text' \" 'second complex param with \"inner text\"'";
            InputParseService parser = new InputParseService();
            ParsedInput       parsed = parser.Parse(input);

            Assert.Equal(input, parsed.RawInput);
            Assert.Equal(3, parsed.Tokens.Count);
            Assert.Equal("_command42", parsed.Tokens[0]);
            Assert.Equal("\"complex param '## $ ! | text' \"", parsed.Tokens[1]);
            Assert.Equal("'second complex param with \"inner text\"'", parsed.Tokens[2]);
        }
Exemple #4
0
        public void InputParsing_CommandWithParametrisedOption_FourTokens()
        {
            const string      input  = "manager --ignore-folders ../folder ../second/sub";
            InputParseService parser = new InputParseService();
            ParsedInput       parsed = parser.Parse(input);

            Assert.Equal(input, parsed.RawInput);
            Assert.Equal(4, parsed.Tokens.Count);
            Assert.Equal("manager", parsed.Tokens[0]);
            Assert.Equal("--ignore-folders", parsed.Tokens[1]);
            Assert.Equal("../folder", parsed.Tokens[2]);
            Assert.Equal("../second/sub", parsed.Tokens[3]);
        }
Exemple #5
0
        /// <summary>
        /// This method prompts the user for the instruction string. It will loop indefinitely until it receives valid data or the program is terminated
        /// </summary>
        /// <returns></returns>
        static List <RoverInstructionEnum> GetInstructionsForRover()
        {
            while (true)
            {
                Console.WriteLine("".PadRight(75, '*'));
                var userInput = PromptForUserInput("Please provide the instructions for the rover.",
                                                   "Input must be a string of letters with no spaces. Valid instruction letters are 'L', 'R', and 'M' only.");
                var parseResult = InputParseService.ParseRoverInstructionsFromUserInput(userInput);
                if (parseResult.IsResultParsedSuccessfully)
                {
                    return(parseResult.Results);
                }

                Console.WriteLine("Unable to parse input. Please try again.");
                Console.WriteLine(parseResult.ParsingException.Message);
            }
        }
Exemple #6
0
        /// <summary>
        /// This method prompts the user for the coordinate that represents the northeast corner of the plateau. It loops indefinitely until the user enters valid information
        /// or the program in terminated.
        /// </summary>
        /// <returns></returns>
        static MarsCoordinate GetPlateauNorthEastCorner()
        {
            while (true)
            {
                var lineWidth = Console.WindowWidth;
                Console.WriteLine("".PadRight(75, '*'));
                var promptText = new List <string>();
                var userInput  = PromptForUserInput("Please provide the northeast coordinate for the plateau.",
                                                    "Input must be two two NON-NEGATIVE whole numbers, separated by a space,",
                                                    "horizontal (or x) coordinate first, then the vertical (or y) coordinate (i.e., 5 6)");

                var parseResult = InputParseService.ParsePlateauDimensions(userInput);
                if (parseResult.IsResultParsedSuccessfully)
                {
                    return(parseResult.Results);
                }

                Console.WriteLine("Unable to parse input. Please try again.");
                Console.WriteLine(parseResult.ParsingException.Message);
            }
        }
Exemple #7
0
        public void TreeEvaluation_ConfigurationCommand_EvaluateInput()
        {
            const string input = "cd ../folder '../second folder/sub'";

            var cdBuilder = Query.CreateBuilder();

            var cdParamBuilder = Parameter.CreateBuilder();

            cdParamBuilder.Key = "path";
            cdParamBuilder.ArgumentTemplate          = "[^\\<\\>\\:\\\"\\|\\?\\*[:cntrl:]]+";
            cdParamBuilder.IsOptional                = true;
            cdParamBuilder.IsRepeatable              = true;
            cdParamBuilder.Documentation.Title       = "Directory path";
            cdParamBuilder.Documentation.Description = "Specify full or relative path.";

            cdBuilder.Key = "current-directory";
            cdBuilder.Representations.AddRange(new[] { "current-directory", "cd", "chdir", "cdir" });
            cdBuilder.Parameters.Add(cdParamBuilder.ToImmutable());
            cdBuilder.Documentation.Title       = "Current directory";
            cdBuilder.Documentation.Description = "Gets or sets current working directory.";

            InputTreeBuilder builder = new InputTreeBuilder(new EmptyLogService());
            InputTree        tree    = builder.Build(new[] { cdBuilder.ToImmutable() }, null);

            IEnvironmentService         environment = new EnvironmentService(null);
            IInputParseService          parser      = new InputParseService();
            InputTreeEvaluationStrategy evaluation  = new InputTreeEvaluationStrategy(tree, environment);

            ParsedInput parsed    = parser.Parse(input);
            Input       evaluated = evaluation.Evaluate(parsed);

            Assert.Equal(parsed, evaluated.ParsedInput);
            Assert.Equal(3, evaluated.Tokens.Count);
            Assert.True(evaluated.Context.IsValid);
            Assert.Equal("current-directory", evaluated.Context.Key);
            Assert.Empty(evaluated.Context.Options);
            Assert.Single(evaluated.Context.Parameters);
            Assert.Equal("../folder", evaluated.Context.Parameters["path"].GetValues()[0]);
            Assert.Equal("../second folder/sub", evaluated.Context.Parameters["path"].GetValues()[1]);
        }