Ejemplo n.º 1
0
    public async Task ReadLine_SelectAllAndType_ReplacesEverything()
    {
        var console = ConsoleStub.NewConsole();

        console.StubInput($"The quick brown fox jumped over the lazy dog{Control}{A}bonk{Enter}");
        var prompt = new Prompt(console: console);

        var result = await prompt.ReadLineAsync();

        Assert.True(result.IsSuccess);
        Assert.Equal("bonk", result.Text);
    }
Ejemplo n.º 2
0
    [InlineData(1)]    //triggered different assert failures
    public async Task FuzzedSelectionAndCompletionAndUndoRedo(int?seed)
    {
        seed ??= Guid.NewGuid().GetHashCode();
        var r = new Random(seed.Value);

        var directions = new[] { Home, End, LeftArrow, RightArrow, UpArrow, DownArrow };
        var keySet     =
            (
                from ctrl in new[] { false, true }
                from shift in new[] { false, true }
                from key in directions
                select key.ToKeyInfo('\0', shift: shift, control: ctrl)
            )
            .Concat(new[]
        {
            A.ToKeyInfo('a'),
            Enter.ToKeyInfo('\0', shift: true),
            Delete.ToKeyInfo('\0'),
            Spacebar.ToKeyInfo('\0', control: true), //completion trigger
            Z.ToKeyInfo('\0', control: true),        //undo
            Y.ToKeyInfo('\0', control: true),        //redo
        })
            .ToArray();

        var randomKeys = Enumerable.Range(1, 10_000)
                         .Select(_ => keySet[r.Next(keySet.Length)])
                         .Concat(Enumerable.Repeat(Enter.ToKeyInfo('\0'), 4)) // hit enter a few times to submit the prompt
                         .ToList();

        var console = ConsoleStub.NewConsole();

        console.StubInput(randomKeys);

        var prompt = new Prompt(
            persistentHistoryFilepath: Path.GetTempFileName(),
            callbacks: new TestPromptCallbacks
        {
            CompletionCallback = new CompletionTestData(null).CompletionHandlerAsync
        },
            console: console);

        try
        {
            var result = await prompt.ReadLineAsync();

            Assert.NotNull(result);
        }
        catch (Exception ex)
        {
            throw new InvalidOperationException("Fuzzing failed for seed: " + seed, ex);
        }
    }
Ejemplo n.º 3
0
        public void make_coffee()
        {
            ConsoleStub.Clear();
            var coffee = new Coffee();

            coffee.PrepareRecipe();
            var allConsole = ConsoleStub.GetAllConsole();

            allConsole.ShouldContain("Boiling water");
            allConsole.ShouldContain("Dripping Coffee through filter");
            allConsole.ShouldContain("Pouring into cup");
            allConsole.ShouldContain("Adding Sugar and Milk");
        }
Ejemplo n.º 4
0
    public async Task ReadLine_FullyTypeCompletion_CanOpenAgain()
    {
        var console = ConsoleStub.NewConsole();

        console.StubInput($"Aardvark {Control}{Spacebar}{Enter}{Enter}");

        var prompt = ConfigurePrompt(console);

        var result = await prompt.ReadLineAsync();

        Assert.True(result.IsSuccess);
        Assert.Equal($"Aardvark Aardvark", result.Text);
    }
Ejemplo n.º 5
0
    public async Task ReadLine_EmptyPrompt_AutoOpens()
    {
        var console = ConsoleStub.NewConsole();

        console.StubInput($"{Control}{Spacebar}{Enter}{Enter}");

        var prompt = ConfigurePrompt(console);

        var result = await prompt.ReadLineAsync();

        Assert.True(result.IsSuccess);
        Assert.Equal($"Aardvark", result.Text);
    }
Ejemplo n.º 6
0
    public async Task ReadLine_UpArrow_DoesNotCycleThroughHistory_WhenInMultilineStatement()
    {
        var console = ConsoleStub.NewConsole();
        var prompt  = new Prompt(console: console);

        console.StubInput($"a{Enter}");
        await prompt.ReadLineAsync();

        console.StubInput($"{Shift}{Enter}{UpArrow}{UpArrow}{UpArrow}{UpArrow}{UpArrow}b{Enter}");
        var result = await prompt.ReadLineAsync();

        Assert.Equal($"b{Environment.NewLine}", result.Text);
    }
Ejemplo n.º 7
0
    public async Task ReadLine_OpenWindowAtBeginningOfPrompt_Opens()
    {
        var console = ConsoleStub.NewConsole();

        console.StubInput($"a{LeftArrow} {LeftArrow}a{Enter}{Enter}");

        var prompt = ConfigurePrompt(console);

        var result = await prompt.ReadLineAsync();

        Assert.True(result.IsSuccess);
        Assert.Equal($"Ant a", result.Text);
    }
Ejemplo n.º 8
0
    public async Task ReadLine_UnsubmittedText_IsNotLostWhenChangingHistory()
    {
        var console = ConsoleStub.NewConsole();
        var prompt  = new Prompt(console: console);

        console.StubInput($"Hello World{Enter}");
        await prompt.ReadLineAsync();

        console.StubInput($"this prompt is my persistent storage{UpArrow}{DownArrow}{Enter}");
        var result = await prompt.ReadLineAsync();

        Assert.Equal("this prompt is my persistent storage", result.Text);
    }
Ejemplo n.º 9
0
    public async Task ReadLine_CompletionWithNoMatches_DoesNotAutoComplete()
    {
        var console = ConsoleStub.NewConsole();

        console.StubInput($"A{Enter} Q{Enter}"); // first {Enter} selects an autocompletion, second {Enter} submits because there are no completions.

        var prompt = ConfigurePrompt(console);

        var result = await prompt.ReadLineAsync();

        Assert.True(result.IsSuccess);
        Assert.Equal($"Ant Q", result.Text);
    }
Ejemplo n.º 10
0
    public async Task ReadLine_SingleCompletion()
    {
        var console = ConsoleStub.NewConsole();

        console.StubInput($"Aa{Enter}{Enter}");

        var prompt = ConfigurePrompt(console);

        var result = await prompt.ReadLineAsync();

        Assert.True(result.IsSuccess);
        Assert.Equal("Aardvark", result.Text);
    }
Ejemplo n.º 11
0
    public async Task ReadLine_MultilineCompletion()
    {
        var console = ConsoleStub.NewConsole();

        console.StubInput($"Aa{Enter}{Shift}{Enter}Z{Control}{Spacebar}{Enter}{Enter}");

        var prompt = ConfigurePrompt(console);

        var result = await prompt.ReadLineAsync();

        Assert.True(result.IsSuccess);
        Assert.Equal($"Aardvark{NewLine}Zebra", result.Text);
    }
Ejemplo n.º 12
0
    public async Task ReadLine_CompletionMenu_AutoOpens()
    {
        var console = ConsoleStub.NewConsole();

        console.StubInput($"A{Enter}{Shift}{Enter}Z{Enter}{Enter}");

        Prompt prompt = ConfigurePrompt(console);

        var result = await prompt.ReadLineAsync();

        Assert.True(result.IsSuccess);
        Assert.Equal($"Ant{NewLine}Zebra", result.Text);
    }
Ejemplo n.º 13
0
        public void make_Tea()
        {
            ConsoleStub.Clear();
            var coffee = new Tea();

            coffee.PrepareRecipe();
            var allConsole = ConsoleStub.GetAllConsole();

            allConsole.ShouldContain("Boiling water");
            allConsole.ShouldContain("Steeping the tea");
            allConsole.ShouldContain("Pouring into cup");
            allConsole.ShouldContain("Adding Lemon");
        }
Ejemplo n.º 14
0
        public void RunAsync_Exception()
        {
            // arrange
            ConsoleStub.Create("1");
            // act
            var result = RunAsync <Startup>(x =>
            {
                throw new Exception();
            });

            // assert
            result.Should().Be(1);
        }
Ejemplo n.º 15
0
    public async Task ReadLine_Delete_SmartHomeSelection()
    {
        var console = ConsoleStub.NewConsole();
        var prompt  = new Prompt(console: console);

        console.StubInput(
            $"    abcd{Home}{Home}",        //get caret at location 0
            $"{Shift}{Home}{Delete}{Enter}" //select first 4 spaces and delete them
            );
        var result = await prompt.ReadLineAsync();

        Assert.True(result.IsSuccess);
        Assert.Equal("abcd", result.Text);
    }
Ejemplo n.º 16
0
    public async Task ReadLine_HorizontalSelectWordAndType_ReplacesWord()
    {
        var console = ConsoleStub.NewConsole();

        console.StubInput(
            $"The quick brown fox jumped over the lazy dog{Control | Shift}{LeftArrow}giraffe",
            $"{Control}{LeftArrow}{Control}{LeftArrow}{Control | Shift}{RightArrow}enigmatic {Enter}");
        var prompt = new Prompt(console: console);

        var result = await prompt.ReadLineAsync();

        Assert.True(result.IsSuccess);
        Assert.Equal("The quick brown fox jumped over the enigmatic giraffe", result.Text);
    }
Ejemplo n.º 17
0
    public async Task ReadLineAsync_CompletesCJKText_Completes()
    {
        var console = ConsoleStub.NewConsole();

        console.StubInput($"书{Enter}{Enter}");

        var prompt = new Prompt(console: console, callbacks: new TestPromptCallbacks
        {
            CompletionCallback = new CompletionTestData("书桌上有").CompletionHandlerAsync
        });
        var result = await prompt.ReadLineAsync();

        Assert.Equal("书桌上有", result.Text);
    }
Ejemplo n.º 18
0
    public async Task ReadLine_MultipleCompletion()
    {
        var console = ConsoleStub.NewConsole();

        // complete 3 animals. For the third animal, start completing Alligator, but then backspace, navigate the completion menu and complete as Albatross instead.
        console.StubInput($"Aa{Enter} Z{Tab} Alli{Backspace}{Backspace}{DownArrow}{UpArrow}{DownArrow}{Enter}{Enter}");

        var prompt = ConfigurePrompt(console);

        var result = await prompt.ReadLineAsync();

        Assert.True(result.IsSuccess);
        Assert.Equal("Aardvark Zebra Albatross", result.Text);
    }
        public void test_light_on()
        {
            ConsoleStub.Clear();
            var controller     = new SimpleRemoteControl();
            var light          = new Light();
            var lightOnCommand = new LightOnCommand(light);

            controller.SetCommand(lightOnCommand);
            controller.ButtonWasPressed();

            var allConsole = ConsoleStub.GetAllConsole();

            allConsole.ShouldContain("light is on");
        }
Ejemplo n.º 20
0
    public async Task ReadLine_Delete_DownSelection()
    {
        var console = ConsoleStub.NewConsole();
        var prompt  = new Prompt(console: console);

        console.StubInput(
            $"abcd{Shift}{Enter}",
            $"efgh",
            $"{Control}{Home}{RightArrow}{RightArrow}{Shift}{DownArrow}{Delete}{Enter}"
            );
        var result = await prompt.ReadLineAsync();

        Assert.True(result.IsSuccess);
        Assert.Equal("abgh", result.Text);
    }
Ejemplo n.º 21
0
    public async Task ReadLine_Delete_RightSelection()
    {
        //right select all + delete
        var console = ConsoleStub.NewConsole();
        var prompt  = new Prompt(console: console);

        console.StubInput(
            $"abcd",
            $"{Home}{Shift}{End}{Delete}{Enter}"
            );
        var result = await prompt.ReadLineAsync();

        Assert.True(result.IsSuccess);
        Assert.Equal("", result.Text);


        //right select 'b' + delete
        console.StubInput(
            $"abcd",
            $"{Home}{RightArrow}{Shift}{RightArrow}{Delete}{Enter}"
            );
        result = await prompt.ReadLineAsync();

        Assert.True(result.IsSuccess);
        Assert.Equal("acd", result.Text);


        //right select 'bc' + delete
        console.StubInput(
            $"abcd",
            $"{Home}{RightArrow}{Shift}{RightArrow}{Shift}{RightArrow}{Delete}{Enter}"
            );
        result = await prompt.ReadLineAsync();

        Assert.True(result.IsSuccess);
        Assert.Equal("ad", result.Text);


        //right select 'bcd' + delete
        console.StubInput(
            $"abcd",
            $"{Home}{RightArrow}{Shift}{End}{Delete}{Enter}"
            );
        result = await prompt.ReadLineAsync();

        Assert.True(result.IsSuccess);
        Assert.Equal("a", result.Text);
    }
Ejemplo n.º 22
0
    public async Task ReadLine_UndoRedoAndCaretPosition()
    {
        for (int i = 1; i <= 4; i++)
        {
            var console = ConsoleStub.NewConsole();
            var inputs  = new List <FormattableString>();
            inputs.Add($"abcd");
            inputs.AddRange(Enumerable.Repeat <FormattableString>($"{Control}{Z}", i));
            inputs.Add($"|");
            inputs.Add($"{Enter}");
            console.StubInput(inputs.ToArray());
            var prompt = new Prompt(console: console);
            var result = await prompt.ReadLineAsync();

            Assert.True(result.IsSuccess);
            Assert.Equal("abcd"[..(4 - i)] + "|", result.Text);
Ejemplo n.º 23
0
    public async Task ReadLine_WriteWordNotInCompletionList_TriggerCompletionList_ShouldNotWriteSpace()
    {
        var console = ConsoleStub.NewConsole();

        console.StubInput(
            $"abc",
            $"{Escape}",            //close completion list
            $"{Control}{Spacebar}", //trigger new one
            $"{Escape}",            //close completion list
            $"{Enter}");
        var prompt = ConfigurePrompt(console, completions: new[] { "aaa" });
        var result = await prompt.ReadLineAsync();

        Assert.True(result.IsSuccess);
        Assert.Equal("abc", result.Text);
    }
Ejemplo n.º 24
0
    public async Task ReadLine_TextOperationsWithUndo_AreUndone()
    {
        var console = ConsoleStub.NewConsole();

        console.StubInput(
            $"It's a small world, after all",
            $"{Control}{LeftArrow}{Control}{LeftArrow}{Control}{LeftArrow}{Control}{LeftArrow}",
            $"{Control | Shift}{RightArrow}{Delete}",
            $"{Control}{Z}{Enter}"
            );
        var prompt = new Prompt(console: console);
        var result = await prompt.ReadLineAsync();

        Assert.True(result.IsSuccess);
        Assert.Equal("It's a small world, after all", result.Text);
    }
Ejemplo n.º 25
0
    public async Task ReadLine_HistoryWithTextOnPrompt_FiltersHistory()
    {
        var console = ConsoleStub.NewConsole();
        var prompt  = new Prompt(console: console);

        console.StubInput($"one{Enter}");
        await prompt.ReadLineAsync();

        console.StubInput($"two{Enter}");
        await prompt.ReadLineAsync();

        console.StubInput($"o{UpArrow}{Enter}");
        var result = await prompt.ReadLineAsync();

        Assert.Equal("one", result.Text);
    }
Ejemplo n.º 26
0
    public async Task WeakerFilteringMatch()
    {
        var console = ConsoleStub.NewConsole();
        var prompt  = new Prompt(console: console);

        console.StubInput($"Console.WriteLine(){Enter}");
        await prompt.ReadLineAsync();

        console.StubInput($"Console.ReadLine(){Enter}");
        await prompt.ReadLineAsync();

        console.StubInput($"write{UpArrow}{Enter}");
        var result = await prompt.ReadLineAsync();

        Assert.Equal($"Console.WriteLine()", result.Text);
    }
Ejemplo n.º 27
0
    public async Task SkipExactMatches()
    {
        var console = ConsoleStub.NewConsole();
        var prompt  = new Prompt(console: console);

        console.StubInput($"a{Enter}");
        await prompt.ReadLineAsync();

        console.StubInput($"b{Enter}");
        await prompt.ReadLineAsync();

        console.StubInput($"b{UpArrow}{Enter}");
        var result = await prompt.ReadLineAsync();

        Assert.Equal($"a", result.Text);
    }
Ejemplo n.º 28
0
    public async Task ReadLine_CJKCharacters_SyntaxHighlight()
    {
        var format1 = new ConsoleFormat(Foreground: Red);
        var format2 = new ConsoleFormat(Foreground: Blue);
        var format3 = new ConsoleFormat(Foreground: Green);

        var console = ConsoleStub.NewConsole(width: 20);

        console.StubInput($"苹果 o 蓝莓 o avocado o{Enter}");

        var prompt = new Prompt(
            callbacks: new TestPromptCallbacks
        {
            HighlightCallback = new SyntaxHighlighterTestData(new Dictionary <string, AnsiColor>
            {
                { "苹果", format1.Foreground !.Value },
Ejemplo n.º 29
0
    public async Task ReadLine_WriteWord_WriteDot_RightSelectDotAndLastLetter_TriggerSelection_ShouldNotOpenCompletionList()
    {
        var console = ConsoleStub.NewConsole();

        console.StubInput(
            $"aaa.",
            $"{LeftArrow}{LeftArrow}{Shift}{RightArrow}{Shift}{RightArrow}", //right select 'a.'
            $"{Control}{Spacebar}",                                          //try to show completion list (should not open)
            $"{Enter}");                                                     //submit prompt
        var prompt = ConfigurePrompt(
            console,
            completions: new[] { "aaa", "bbb" });
        var result = await prompt.ReadLineAsync();

        Assert.True(result.IsSuccess);
        Assert.Equal($"aaa.", result.Text);
    }
Ejemplo n.º 30
0
    public async Task ReadLine_CommitCompletionItemByCharacter_ShouldInsertCompletion_And_InsertPressedCharacter()
    {
        foreach (var commitChar in new[] { ' ', '.', '(' })
        {
            var console = ConsoleStub.NewConsole();
            console.StubInput(
                $"ab",
                $"{commitChar}", //should insert completion
                $"{Escape}",     //to be sure that following Enter won't insert completion
                $"{Enter}");     //submit prompt
            var prompt = ConfigurePrompt(
                console,
                completions: new[] { "abcd" },
                configuration: new PromptConfiguration(
                    keyBindings: new KeyBindings(
                        commitCompletion: new KeyPressPatterns(
                            new(Enter), new(Tab), new(' '), new('.'), new('(')))
                    ));
            var result = await prompt.ReadLineAsync();

            Assert.True(result.IsSuccess);
            Assert.Equal($"abcd{commitChar}", result.Text);
        }

        foreach (var commitKey in new[] { Tab, Enter })
        {
            var console = ConsoleStub.NewConsole();
            console.StubInput(
                $"ab",
                $"{commitKey}", //should insert completion
                $"{Escape}",    //to be sure that following Enter won't insert completion
                $"{Enter}");    //submit prompt
            var prompt = ConfigurePrompt(
                console,
                completions: new[] { "abcd" },
                configuration: new PromptConfiguration(
                    keyBindings: new KeyBindings(
                        commitCompletion: new KeyPressPatterns(
                            new(Enter), new(Tab), new(' '), new('.'), new('(')))
                    ));
            var result = await prompt.ReadLineAsync();

            Assert.True(result.IsSuccess);
            Assert.Equal($"abcd", result.Text);
        }
    }