public void ErrorsAreIgnoredWithNoErrorWriter()
        {
            var reader = Substitute.For<IConsoleReader>();
            var client = new ConsoleLoopClient(reader);

            client.OnError("Hello");
        }
        public void Constructor()
        {
            var reader = Substitute.For<IConsoleReader>();
            var client = new ConsoleLoopClient(reader);

            client.Reader.Should().BeSameAs(reader);
        }
        public void ErrorWriterReceivesError()
        {
            const string errorText = "Hello, errors!";

            var reader = Substitute.For<IConsoleReader>();

            var sb = new StringBuilder();
            using (var errorWriter = new StringWriter(sb))
            {
                var client = new ConsoleLoopClient(reader, errorWriter);
                client.OnError(errorText);
            }

            sb.ToString().Should().Be(errorText + Environment.NewLine);
        }
        public void Prompts()
        {
            const string prompt = "[Prompt!] ";

            var reader = Substitute.For<IConsoleReader>();
            var lineInput = Substitute.For<IConsoleLineInput>();

            lineInput.Prompt = prompt;
            reader.LineInput.Returns(lineInput);

            var client = new ConsoleLoopClient(reader);
            client.Prompt.Should().Be(prompt);

            const string newPrompt = "NewPrompt";
            client.Prompt = newPrompt;
            client.Prompt.Should().Be(newPrompt);
            lineInput.Prompt.ToString().Should().Be(newPrompt);

            client.DisplayPrompt();
            lineInput.Received(1).DisplayPrompt();
        }
Beispiel #5
0
        /// <summary>
        /// Utility for constructing loop clients.
        /// </summary>
        /// <param name="parameters">I/O parameters for loop.</param>
        /// <returns>A constructed loop client.</returns>
        public static ILoopClient CreateClient(LoopInputOutputParameters parameters)
        {
            var consoleInput  = parameters.ConsoleInput ?? BasicConsole.Default;
            var consoleOutput = parameters.ConsoleOutput ?? BasicConsole.Default;
            var keyBindingSet = parameters.KeyBindingSet ?? ConsoleKeyBindingSet.Default;

            var lineInput = parameters.LineInput ?? new ConsoleLineInput(
                consoleOutput,
                new ConsoleInputBuffer(),
                new ConsoleHistory());

            lineInput.Prompt = parameters.Prompt ?? Strings.DefaultPrompt;

            var consoleReader = new ConsoleReader(lineInput, consoleInput, consoleOutput, keyBindingSet);

            var consoleClient = new ConsoleLoopClient(consoleReader);

            consoleClient.Reader.CommentCharacter = parameters.EndOfLineCommentCharacter;

            return(consoleClient);
        }
Beispiel #6
0
        /// <summary>
        /// Constructor that creates a loop with a default client.
        /// </summary>
        /// <param name="parameters">Optionally provides parameters controlling
        /// the loop's input and output behaviors; if not provided, default
        /// parameters are used.</param>
        /// <param name="options">Options for loop.</param>
        public Loop(LoopInputOutputParameters parameters, LoopOptions options = null) : this(options)
        {
            var consoleInput  = parameters?.ConsoleInput ?? BasicConsoleInputAndOutput.Default;
            var consoleOutput = parameters?.ConsoleOutput ?? BasicConsoleInputAndOutput.Default;
            var keyBindingSet = parameters?.KeyBindingSet ?? ConsoleKeyBindingSet.Default;

            var lineInput = parameters?.LineInput ?? new ConsoleLineInput(
                consoleOutput,
                new ConsoleInputBuffer(),
                new ConsoleHistory(),
                GenerateCompletions);

            lineInput.Prompt = parameters?.Prompt ?? Strings.DefaultPrompt;

            ConsoleReader = new ConsoleReader(lineInput, consoleInput, consoleOutput, keyBindingSet);

            var consoleClient = new ConsoleLoopClient(
                ConsoleReader,
                parameters?.ErrorWriter ?? Console.Error);

            consoleClient.Reader.CommentCharacter = options?.EndOfLineCommentCharacter;

            Client = consoleClient;
        }
        public void ReadLine()
        {
            const string lineText = "The line that was read.";

            var reader = Substitute.For<IConsoleReader>();
            var lineInput = Substitute.For<IConsoleLineInput>();

            reader.LineInput.Returns(lineInput);
            reader.ReadLine().Returns(lineText);

            var client = new ConsoleLoopClient(reader);
            client.ReadLine().Should().Be(lineText);
            reader.Received(1).ReadLine();
        }