|
| 1 | +// |
| 2 | +// Copyright (c) Microsoft. All rights reserved. |
| 3 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 4 | +// |
| 5 | + |
| 6 | +using Microsoft.PowerShell.EditorServices.Console; |
| 7 | +using System.Threading; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Xunit; |
| 10 | +using Microsoft.PowerShell.EditorServices.Utility; |
| 11 | + |
| 12 | +namespace Microsoft.PowerShell.EditorServices.Test.Console |
| 13 | +{ |
| 14 | + public class ChoicePromptHandlerTests |
| 15 | + { |
| 16 | + private readonly ChoiceDetails[] Choices = |
| 17 | + new ChoiceDetails[] |
| 18 | + { |
| 19 | + new ChoiceDetails("&Apple", ""), |
| 20 | + new ChoiceDetails("Ba&nana", ""), |
| 21 | + new ChoiceDetails("&Orange", "") |
| 22 | + }; |
| 23 | + |
| 24 | + private const int DefaultChoice = 1; |
| 25 | + |
| 26 | + [Fact] |
| 27 | + public void ChoicePromptReturnsCorrectIdForChoice() |
| 28 | + { |
| 29 | + TestChoicePromptHandler choicePromptHandler = new TestChoicePromptHandler(); |
| 30 | + Task<int> promptTask = |
| 31 | + choicePromptHandler.PromptForChoice( |
| 32 | + "Test prompt", |
| 33 | + "Message is irrelevant", |
| 34 | + Choices, |
| 35 | + DefaultChoice, |
| 36 | + CancellationToken.None); |
| 37 | + |
| 38 | + choicePromptHandler.ReturnInputString("apple"); |
| 39 | + |
| 40 | + // Wait briefly for the prompt task to complete |
| 41 | + promptTask.Wait(1000); |
| 42 | + |
| 43 | + Assert.Equal(TaskStatus.RanToCompletion, promptTask.Status); |
| 44 | + Assert.Equal(0, promptTask.Result); |
| 45 | + Assert.Equal(1, choicePromptHandler.TimesPrompted); |
| 46 | + } |
| 47 | + |
| 48 | + [Fact] |
| 49 | + public void ChoicePromptReturnsCorrectIdForHotKey() |
| 50 | + { |
| 51 | + TestChoicePromptHandler choicePromptHandler = new TestChoicePromptHandler(); |
| 52 | + Task<int> promptTask = |
| 53 | + choicePromptHandler.PromptForChoice( |
| 54 | + "Test prompt", |
| 55 | + "Message is irrelevant", |
| 56 | + Choices, |
| 57 | + DefaultChoice, |
| 58 | + CancellationToken.None); |
| 59 | + |
| 60 | + // Try adding whitespace to ensure it works |
| 61 | + choicePromptHandler.ReturnInputString(" N "); |
| 62 | + |
| 63 | + // Wait briefly for the prompt task to complete |
| 64 | + promptTask.Wait(1000); |
| 65 | + |
| 66 | + Assert.Equal(TaskStatus.RanToCompletion, promptTask.Status); |
| 67 | + Assert.Equal(1, promptTask.Result); |
| 68 | + Assert.Equal(1, choicePromptHandler.TimesPrompted); |
| 69 | + } |
| 70 | + |
| 71 | + [Fact] |
| 72 | + public void ChoicePromptRepromptsOnInvalidInput() |
| 73 | + { |
| 74 | + TestChoicePromptHandler choicePromptHandler = |
| 75 | + new TestChoicePromptHandler(); |
| 76 | + |
| 77 | + Task<int> promptTask = |
| 78 | + choicePromptHandler.PromptForChoice( |
| 79 | + "Test prompt", |
| 80 | + "Message is irrelevant", |
| 81 | + Choices, |
| 82 | + DefaultChoice, |
| 83 | + CancellationToken.None); |
| 84 | + |
| 85 | + // Choice is invalid, should reprompt |
| 86 | + choicePromptHandler.ReturnInputString("INVALID"); |
| 87 | + |
| 88 | + Assert.Equal(TaskStatus.WaitingForActivation, promptTask.Status); |
| 89 | + Assert.Equal(2, choicePromptHandler.TimesPrompted); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + internal class TestChoicePromptHandler : ChoicePromptHandler |
| 94 | + { |
| 95 | + private TaskCompletionSource<string> linePromptTask; |
| 96 | + |
| 97 | + public int TimesPrompted { get; private set; } |
| 98 | + |
| 99 | + public TestChoicePromptHandler() : base(Logging.NullLogger) |
| 100 | + { |
| 101 | + } |
| 102 | + |
| 103 | + public void ReturnInputString(string inputString) |
| 104 | + { |
| 105 | + this.linePromptTask.SetResult(inputString); |
| 106 | + } |
| 107 | + |
| 108 | + protected override Task<string> ReadInputString(CancellationToken cancellationToken) |
| 109 | + { |
| 110 | + this.linePromptTask = new TaskCompletionSource<string>(); |
| 111 | + return this.linePromptTask.Task; |
| 112 | + } |
| 113 | + |
| 114 | + protected override void ShowPrompt(PromptStyle promptStyle) |
| 115 | + { |
| 116 | + // No action needed, just count the prompts. |
| 117 | + this.TimesPrompted++; |
| 118 | + } |
| 119 | + } |
| 120 | +} |
| 121 | + |
0 commit comments