Beispiel #1
0
        public void TestUselessStuffForBetterCoverage()
        {
            // Useless test to just make sure coverage numbers are better, written
            // in the first way I could think of that doesn't warn about doing something useless.
            var options = new SetPSReadlineOption();
            var getKeyHandlerCommand = new GetKeyHandlerCommand();
            var useless = ((object)options.AddToHistoryHandler ?? options).GetHashCode()
                          + options.EditMode.GetHashCode()
                          + ((object)options.ContinuationPrompt ?? options).GetHashCode()
                          + (options.ContinuationPromptColor ?? options).GetHashCode()
                          + options.HistoryNoDuplicates.GetHashCode()
                          + options.HistorySearchCursorMovesToEnd.GetHashCode()
                          + options.MaximumHistoryCount.GetHashCode()
                          + options.MaximumKillRingCount.GetHashCode()
                          + options.DingDuration.GetHashCode()
                          + options.DingTone.GetHashCode()
                          + options.BellStyle.GetHashCode()
                          + options.ExtraPromptLineCount.GetHashCode()
                          + options.ShowToolTips.GetHashCode()
                          + options.CompletionQueryItems.GetHashCode()
                          + (options.EmphasisColor ?? options).GetHashCode()
                          + options.HistorySearchCaseSensitive.GetHashCode()
                          + getKeyHandlerCommand.Bound.GetHashCode()
                          + getKeyHandlerCommand.Unbound.GetHashCode();

            // This assertion just avoids annoying warnings about unused variables.
            Assert.AreNotEqual(Math.PI, useless);
        }
Beispiel #2
0
        static private void SetPrompt(string prompt)
        {
            if (string.IsNullOrEmpty(prompt))
            {
                return;
            }

            var handle = NativeMethods.GetStdHandle((uint)StandardHandleId.Output);

            var lineCount = 1 + prompt.Count(c => c == '\n');

            if (lineCount > 1)
            {
                var options = new SetPSReadlineOption {
                    ExtraPromptLineCount = lineCount - 1
                };
                PSConsoleReadLine.SetOptions(options);
            }
            int bufferWidth   = Console.BufferWidth;
            var consoleBuffer = new CHAR_INFO[lineCount * bufferWidth];
            int j             = 0;

            for (int i = 0; i < prompt.Length; i++, j++)
            {
                if (prompt[i] == '\n')
                {
                    for (; j % Console.BufferWidth != 0; j++)
                    {
                        consoleBuffer[j] = new CHAR_INFO(' ', Console.ForegroundColor, Console.BackgroundColor);
                    }
                    Console.CursorTop += 1;
                    Console.CursorLeft = 0;
                    j -= 1;  // We don't actually write the newline
                }
                else
                {
                    consoleBuffer[j]    = new CHAR_INFO(prompt[i], Console.ForegroundColor, Console.BackgroundColor);
                    Console.CursorLeft += 1;
                }
            }

            var bufferSize = new COORD
            {
                X = (short)bufferWidth,
                Y = (short)lineCount
            };
            var bufferCoord = new COORD {
                X = 0, Y = 0
            };
            var writeRegion = new SMALL_RECT
            {
                Top    = 0,
                Left   = 0,
                Bottom = (short)(lineCount - 1),
                Right  = (short)bufferWidth
            };

            NativeMethods.WriteConsoleOutput(handle, consoleBuffer, bufferSize, bufferCoord, ref writeRegion);
        }
Beispiel #3
0
        public void TestUselessStuffForBetterCoverage()
        {
            // Useless test to just make sure coverage numbers are better, written
            // in the first way I could think of that doesn't warn about doing something useless.
            var options = new SetPSReadlineOption();
            var getKeyHandlerCommand = new GetKeyHandlerCommand();
            var useless = ((object)options.AddToHistoryHandler ?? options).GetHashCode()
                          + options.EditMode.GetHashCode()
                          + ((object)options.ContinuationPrompt ?? options).GetHashCode()
                          + options.ContinuationPromptBackgroundColor.GetHashCode()
                          + options.ContinuationPromptForegroundColor.GetHashCode()
                          + options.HistoryNoDuplicates.GetHashCode()
                          + options.HistorySearchCursorMovesToEnd.GetHashCode()
                          + options.MaximumHistoryCount.GetHashCode()
                          + options.MaximumKillRingCount.GetHashCode()
                          + options.DingDuration.GetHashCode()
                          + options.DingTone.GetHashCode()
                          + options.BellStyle.GetHashCode()
                          + options.ExtraPromptLineCount.GetHashCode()
                          + options.ShowToolTips.GetHashCode()
                          + options.CompletionQueryItems.GetHashCode()
                          + options.EmphasisBackgroundColor.GetHashCode()
                          + options.EmphasisForegroundColor.GetHashCode()
                          + options.HistorySearchCaseSensitive.GetHashCode()
                          + getKeyHandlerCommand.Bound.GetHashCode()
                          + getKeyHandlerCommand.Unbound.GetHashCode();

            // This assertion just avoids annoying warnings about unused variables.
            Assert.AreNotEqual(Math.PI, useless);

            bool exception = false;

            try
            {
                CreateCharInfoBuffer(0, new object());
            }
            catch (ArgumentException)
            {
                exception = true;
            }
            Assert.IsTrue(exception, "CreateCharBuffer invalid arugment raised an exception");
        }
Beispiel #4
0
        private void SetPrompt(string prompt)
        {
            if (string.IsNullOrEmpty(prompt))
            {
                var options = new SetPSReadlineOption {
                    ExtraPromptLineCount = 0
                };
                PSConsoleReadLine.SetOptions(options);
                return;
            }

            var lineCount = 1 + prompt.Count(c => c == '\n');

            if (lineCount > 1)
            {
                var options = new SetPSReadlineOption {
                    ExtraPromptLineCount = lineCount - 1
                };
                PSConsoleReadLine.SetOptions(options);
            }
            _console.Write(prompt);
        }
Beispiel #5
0
        private void TestSetup(KeyMode keyMode, params KeyHandler[] keyHandlers)
        {
            _console       = new TestConsole();
            _mockedMethods = new MockedMethods();
            var instance = (PSConsoleReadLine)typeof(PSConsoleReadLine)
                           .GetField("_singleton", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null);

            typeof(PSConsoleReadLine)
            .GetField("_mockableMethods", BindingFlags.Instance | BindingFlags.NonPublic)
            .SetValue(instance, _mockedMethods);
            typeof(PSConsoleReadLine)
            .GetField("_console", BindingFlags.Instance | BindingFlags.NonPublic)
            .SetValue(instance, _console);

            PSConsoleReadLine.ClearHistory();
            PSConsoleReadLine.ClearKillRing();

            var options = new SetPSReadlineOption
            {
                AddToHistoryHandler  = null,
                BellStyle            = PSConsoleReadlineOptions.DefaultBellStyle,
                CompletionQueryItems = PSConsoleReadlineOptions.DefaultCompletionQueryItems,
                ContinuationPrompt   = PSConsoleReadlineOptions.DefaultContinuationPrompt,
                ContinuationPromptBackgroundColor = _console.BackgroundColor,
                ContinuationPromptForegroundColor = _console.ForegroundColor,
                DingDuration                  = 1,      // Make tests virtually silent when they ding
                DingTone                      = 37,     // Make tests virtually silent when they ding
                EmphasisBackgroundColor       = _console.BackgroundColor,
                EmphasisForegroundColor       = PSConsoleReadlineOptions.DefaultEmphasisForegroundColor,
                ErrorBackgroundColor          = ConsoleColor.DarkRed,
                ErrorForegroundColor          = ConsoleColor.Red,
                ExtraPromptLineCount          = PSConsoleReadlineOptions.DefaultExtraPromptLineCount,
                HistoryNoDuplicates           = PSConsoleReadlineOptions.DefaultHistoryNoDuplicates,
                HistorySaveStyle              = HistorySaveStyle.SaveNothing,
                HistorySearchCaseSensitive    = PSConsoleReadlineOptions.DefaultHistorySearchCaseSensitive,
                HistorySearchCursorMovesToEnd = PSConsoleReadlineOptions.DefaultHistorySearchCursorMovesToEnd,
                MaximumHistoryCount           = PSConsoleReadlineOptions.DefaultMaximumHistoryCount,
                MaximumKillRingCount          = PSConsoleReadlineOptions.DefaultMaximumKillRingCount,
                ResetTokenColors              = true,
                ShowToolTips                  = PSConsoleReadlineOptions.DefaultShowToolTips,
                WordDelimiters                = PSConsoleReadlineOptions.DefaultWordDelimiters,
            };

            switch (keyMode)
            {
            case KeyMode.Cmd:
                options.EditMode = EditMode.Windows;
                break;

            case KeyMode.Emacs:
                options.EditMode = EditMode.Emacs;
                break;

            case KeyMode.Vi:
                options.EditMode = EditMode.Vi;
                break;
            }

            PSConsoleReadLine.SetOptions(options);

            foreach (var keyHandler in keyHandlers)
            {
                PSConsoleReadLine.SetKeyHandler(new [] { keyHandler.Chord }, keyHandler.Handler, "", "");
            }

            var colorOptions = new SetPSReadlineOption();

            foreach (var val in typeof(TokenClassification).GetEnumValues())
            {
                colorOptions.TokenKind       = (TokenClassification)val;
                colorOptions.ForegroundColor = ForegroundColors[(int)val];
                colorOptions.BackgroundColor = BackgroundColors[(int)val];
                PSConsoleReadLine.SetOptions(colorOptions);
            }
        }
Beispiel #6
0
        private void TestSetup(KeyMode keyMode, params KeyHandler[] keyHandlers)
        {
            Console.Clear();
            PSConsoleReadLine.ClearHistory();
            PSConsoleReadLine.ClearKillRing();

            // We don't want stdout redirected, we want it sent to the screen.
            var standardOutput = new StreamWriter(Console.OpenStandardOutput())
            {
                AutoFlush = true
            };

            Console.SetOut(standardOutput);

            var options = new SetPSReadlineOption
            {
                AddToHistoryHandler  = null,
                BellStyle            = PSConsoleReadlineOptions.DefaultBellStyle,
                CompletionQueryItems = PSConsoleReadlineOptions.DefaultCompletionQueryItems,
                ContinuationPrompt   = PSConsoleReadlineOptions.DefaultContinuationPrompt,
                ContinuationPromptBackgroundColor = Console.BackgroundColor,
                ContinuationPromptForegroundColor = Console.ForegroundColor,
                DingDuration                  = 1,      // Make tests virtually silent when they ding
                DingTone                      = 37,     // Make tests virtually silent when they ding
                EmphasisBackgroundColor       = Console.BackgroundColor,
                EmphasisForegroundColor       = PSConsoleReadlineOptions.DefaultEmphasisForegroundColor,
                ExtraPromptLineCount          = PSConsoleReadlineOptions.DefaultExtraPromptLineCount,
                HistoryNoDuplicates           = PSConsoleReadlineOptions.DefaultHistoryNoDuplicates,
                HistorySearchCaseSensitive    = PSConsoleReadlineOptions.DefaultHistorySearchCaseSensitive,
                HistorySearchCursorMovesToEnd = PSConsoleReadlineOptions.DefaultHistorySearchCursorMovesToEnd,
                MaximumHistoryCount           = PSConsoleReadlineOptions.DefaultMaximumHistoryCount,
                MaximumKillRingCount          = PSConsoleReadlineOptions.DefaultMaximumKillRingCount,
                ResetTokenColors              = true,
                ShowToolTips                  = PSConsoleReadlineOptions.DefaultShowToolTips,
                WordDelimiters                = PSConsoleReadlineOptions.DefaultWordDelimiters,
            };

            switch (keyMode)
            {
            case KeyMode.Cmd:
                options.EditMode = EditMode.Windows;
                break;

            case KeyMode.Emacs:
                options.EditMode = EditMode.Emacs;
                break;

#if FALSE
            case KeyMode.Vi:
                options.EditMode = EditMode.Vi;
                break;
#endif
            }

            PSConsoleReadLine.SetOptions(options);

            foreach (var keyHandler in keyHandlers)
            {
                PSConsoleReadLine.SetKeyHandler(new [] { keyHandler.Chord }, keyHandler.Handler, "", "");
            }

            var colorOptions = new SetPSReadlineOption();
            foreach (var val in typeof(TokenClassification).GetEnumValues())
            {
                colorOptions.TokenKind       = (TokenClassification)val;
                colorOptions.ForegroundColor = ForegroundColors[(int)val];
                colorOptions.BackgroundColor = BackgroundColors[(int)val];
                PSConsoleReadLine.SetOptions(colorOptions);
            }
        }