Пример #1
0
 public CommandLoop(Configuration configuration)
 {
     var adapter = new LowLevelToAbstractConsoleAdapter(configuration.Console);
     Console = adapter;
     _commandFactory = configuration.CommandFactory;
     _lockingService = configuration.LockingService;
     _history = configuration.ConsoleHistory;
     InitializeHistory(adapter);
 }
        public void WritesHelloWorld()
        {
            var inputSequence = "Hello World".ToInputSequence();
            var lowLevelConsole = new LowLevelTestConsole(inputSequence);
            var adapter = new LowLevelToAbstractConsoleAdapter(lowLevelConsole) { Prompt = "$ " };

            adapter.ReadUntilSequenceIsOver(inputSequence);

            Assert.AreEqual("$ Hello World", lowLevelConsole.ReadInLineFromTo(0,0,12));
        }
        public void WritesPrompt()
        {
            var inputSequence = " ".ToInputSequence();
            var lowLevelConsole = new LowLevelTestConsole(inputSequence);
            var adapter = new LowLevelToAbstractConsoleAdapter(lowLevelConsole) {Prompt = "$ "};

            adapter.ReadUntilSequenceIsOver(inputSequence);
            var written = lowLevelConsole.ReadCharacterAt(0, 0);
            Assert.AreEqual('$',written);
        }
        public void BreaksLineOnTheRightCharacter()
        {
            var inputSequence = "Hello this is shell.me. We are testing the new LowLevelConsole, which gives us great power!".ToInputSequence();
            var lowLevelConsole = new LowLevelTestConsole(inputSequence);
            var adapter = new LowLevelToAbstractConsoleAdapter(lowLevelConsole) { Prompt = "(S) " };

            adapter.ReadUntilSequenceIsOver(inputSequence);

            Assert.AreEqual("(S) Hello this is shell.me. We are testing the new LowLevelConsole, which gives ", lowLevelConsole.ReadInLineFromTo(0, 0, 79));
            Assert.AreEqual("us great power!", lowLevelConsole.ReadInLineFromTo(1, 0, 14));
        }
        public void CursorMovesLeftUntilItHitsThePrompt()
        {
            var inputSequence = "Hello"
                .ToInputSequence()
                .AddLeftArrowHit(5);

            var lowLevelConsole = new LowLevelTestConsole(inputSequence);
            var adapter = new LowLevelToAbstractConsoleAdapter(lowLevelConsole) { Prompt = "(S) " };

            adapter.ReadUntilSequenceIsOver(inputSequence);

            Assert.AreEqual("(S) Hello", lowLevelConsole.ReadInLineFromTo(0, 0, 8));
            Assert.AreEqual(4, lowLevelConsole.CursorLeft);
            Assert.AreEqual(0, lowLevelConsole.CursorTop);
        }
        public void BackspaceJumpsLineUpAndLetsUsWriteAgain()
        {
            var inputSequence = "Hello this is shell.me. We are testing the new LowLevelConsole, which gives us great power!"
                .ToInputSequence()
                .AddBackspaceHit(21)
                .AddInputSequence("rocks!");

            var lowLevelConsole = new LowLevelTestConsole(inputSequence);
            var adapter = new LowLevelToAbstractConsoleAdapter(lowLevelConsole) { Prompt = "(S) " };

            adapter.ReadUntilSequenceIsOver(inputSequence);

            Assert.AreEqual("(S) Hello this is shell.me. We are testing the new LowLevelConsole, which rocks!", lowLevelConsole.ReadInLineFromTo(0, 0, 79));
            //We are just making sure that there is nothing left on the second line
            Assert.AreEqual("                                                                                ", lowLevelConsole.ReadInLineFromTo(1, 0, 79));
        }
Пример #7
0
        private void InitializeHistory(LowLevelToAbstractConsoleAdapter adapter)
        {
            var keyMap = new Dictionary<ConsoleKey, Func<HistoryEntry>>
                             {
                                 { ConsoleKey.UpArrow, () => _history.GetNextEntry() },
                                 { ConsoleKey.DownArrow, () => _history.GetPreviousEntry() }
                             };

            adapter.KeyStrokes
                .Select(keyInfo => keyInfo.Key)
                .Where(key => key == ConsoleKey.UpArrow || key == ConsoleKey.DownArrow)
                .Select(key => keyMap[key])
                .Subscribe(func =>
                               {
                                   adapter.EraseCurrentLine();
                                   var historyEntry = func();

                                   if (historyEntry != null)
                                   {
                                       adapter.Write(historyEntry.Value);
                                   }
                               });
        }
        public void CursorCanNotMoveAheadTheWriting()
        {
            var inputSequence = "Hello"
                .ToInputSequence()
                .AddRightArrowHit(5);

            var lowLevelConsole = new LowLevelTestConsole(inputSequence);
            var adapter = new LowLevelToAbstractConsoleAdapter(lowLevelConsole) { Prompt = "(S) " };

            adapter.ReadUntilSequenceIsOver(inputSequence);

            Assert.AreEqual("(S) Hello", lowLevelConsole.ReadInLineFromTo(0, 0, 8));
            Assert.AreEqual(9, lowLevelConsole.CursorLeft);
            Assert.AreEqual(0, lowLevelConsole.CursorTop);
        }
        public void WriteLineWorks()
        {

            var lowLevelConsole = new LowLevelTestConsole();
            var adapter = new LowLevelToAbstractConsoleAdapter(lowLevelConsole) { Prompt = "(S) " };

            adapter.WriteLine("Hello World");
            adapter.WriteLine("Hello World");

            Assert.AreEqual("Hello World", lowLevelConsole.ReadInLineFromTo(0, 0, 10));
            Assert.AreEqual("Hello World", lowLevelConsole.ReadInLineFromTo(1, 0, 10));
            
            Assert.AreEqual(0, lowLevelConsole.CursorLeft);
        }
        public void ErasesCurrentLineAndResetsCursor()
        {
            var inputSequence = "Hello".ToInputSequence();

            var lowLevelConsole = new LowLevelTestConsole(inputSequence);
            var adapter = new LowLevelToAbstractConsoleAdapter(lowLevelConsole) { Prompt = "(S) " };

            adapter.ReadUntilSequenceIsOver(inputSequence);

            Assert.AreEqual("(S) Hello", lowLevelConsole.ReadInLineFromTo(0, 0, 8));
            Assert.AreEqual(9, lowLevelConsole.CursorLeft);

            adapter.EraseCurrentLine();

            Assert.AreEqual("(S)      ", lowLevelConsole.ReadInLineFromTo(0, 0, 8));
            Assert.AreEqual(4, lowLevelConsole.CursorLeft);
        }
        public void WritesMovesCursorBeforeWritingAndInsertsAnotherWord()
        {
            var inputSequence = "World"
                .ToInputSequence()
                .AddLeftArrowHit(5)
                .AddInputSequence("Hello ");

            var lowLevelConsole = new LowLevelTestConsole(inputSequence);
            var adapter = new LowLevelToAbstractConsoleAdapter(lowLevelConsole) { Prompt = "(S) " };

            adapter.ReadUntilSequenceIsOver(inputSequence);

            Assert.AreEqual("(S) Hello World", lowLevelConsole.ReadInLineFromTo(0, 0, 14));
            Assert.AreEqual(10, lowLevelConsole.CursorLeft);
        }