Esempio n. 1
0
        private static StandardStreamReader WrapInput(IConsole console, Stream?stream, bool isRedirected)
        {
            if (stream is null)
            {
                return(StandardStreamReader.CreateNull(console));
            }

            return(new StandardStreamReader(Stream.Synchronized(stream), Console.InputEncoding, false, isRedirected, console));
        }
Esempio n. 2
0
        public TestConsole(
            Func <TestConsole, string> onReadLine        = null,
            IEnumerable <string> pipedInput              = null,
            Func <TestConsole, ConsoleKeyInfo> onReadKey = null)
        {
            _onReadKey        = onReadKey;
            IsInputRedirected = pipedInput != null;

            if (pipedInput != null)
            {
                if (onReadLine != null)
                {
                    throw new Exception($"{nameof(onReadLine)} and {nameof(pipedInput)} cannot both be specified. " +
                                        "Windows will throw 'System.IO.IOException: The handle is invalid' on an attempt to ");
                }

                if (pipedInput is ICollection <string> inputs)
                {
                    var queue = new Queue <string>(inputs);
                    onReadLine = console => queue.Count == 0 ? null : queue.Dequeue();
                }
                else
                {
                    onReadLine = console => pipedInput.Take(1).FirstOrDefault();
                }
            }

            var joined = new StandardStreamWriter();

            Joined = joined;
            Out    = new StandardStreamWriter(joined);
            Error  = new StandardStreamWriter(joined);
            In     = new StandardStreamReader(
                () =>
            {
                var input = onReadLine?.Invoke(this);
                // write to joined output so it can be logged for debugging
                joined.WriteLine();
                joined.WriteLine($"IConsole.ReadLine > {input}");
                joined.WriteLine();
                return(input);
            });
        }
Esempio n. 3
0
 public SystemConsole()
 {
     Error = StandardStreamWriter.Create(Console.Error);
     Out   = StandardStreamWriter.Create(Console.Out);
     In    = StandardStreamReader.Create(Console.In);
 }