|
| 1 | +// ---------------------------------------------------------------------------------- |
| 2 | +// |
| 3 | +// Copyright Microsoft Corporation |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | +// ---------------------------------------------------------------------------------- |
| 14 | + |
| 15 | +using Microsoft.Azure.PowerShell.Tools.AzPredictor.Test.Mocks; |
| 16 | +using System.Linq; |
| 17 | +using System.Management.Automation.Subsystem; |
| 18 | +using System.Threading; |
| 19 | +using Xunit; |
| 20 | + |
| 21 | +namespace Microsoft.Azure.PowerShell.Tools.AzPredictor.Test |
| 22 | +{ |
| 23 | + /// <summary> |
| 24 | + /// Tests for <see cref="AzPredictorService"/> |
| 25 | + /// </summary> |
| 26 | + [Collection("Model collection")] |
| 27 | + public class AzPredictorServiceTests |
| 28 | + { |
| 29 | + private readonly ModelFixture _fixture; |
| 30 | + private readonly AzPredictorService _service; |
| 31 | + private readonly Predictor _suggestionsPredictor; |
| 32 | + private readonly Predictor _commandsPredictor; |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Constructs a new instance of <see cref="AzPredictorServiceTests"/> |
| 36 | + /// </summary> |
| 37 | + /// <param name="fixture"></param> |
| 38 | + public AzPredictorServiceTests(ModelFixture fixture) |
| 39 | + { |
| 40 | + this._fixture = fixture; |
| 41 | + var startHistory = $"{AzPredictorConstants.CommandPlaceholder}{AzPredictorConstants.CommandConcatenator}{AzPredictorConstants.CommandPlaceholder}"; |
| 42 | + this._suggestionsPredictor = new Predictor(this._fixture.PredictionCollection[startHistory], null); |
| 43 | + this._commandsPredictor = new Predictor(this._fixture.CommandCollection, null); |
| 44 | + |
| 45 | + this._service = new MockAzPredictorService(startHistory, this._fixture.PredictionCollection[startHistory], this._fixture.CommandCollection); |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Verifies that the prediction comes from the suggestions list, not the command list. |
| 51 | + /// </summary> |
| 52 | + [Theory] |
| 53 | + [InlineData("CONNECT-AZACCOUNT")] |
| 54 | + [InlineData("set-azstorageaccount ")] |
| 55 | + [InlineData("Get-AzResourceG")] |
| 56 | + [InlineData("Get-AzStorageAcco")] |
| 57 | + [InlineData("Get-AzKeyVault -VaultName")] |
| 58 | + [InlineData("GET-AZSTORAGEACCOUNTKEY -NAME ")] |
| 59 | + [InlineData("new-azresourcegroup -name hello")] |
| 60 | + [InlineData("Get-AzContext -Name")] |
| 61 | + [InlineData("Get-AzContext -ErrorAction")] |
| 62 | + public void VerifyUsingSuggestion(string userInput) |
| 63 | + { |
| 64 | + var predictionContext = PredictionContext.Create(userInput); |
| 65 | + var expected = this._suggestionsPredictor.Query(predictionContext.InputAst, 1, CancellationToken.None); |
| 66 | + var actual = this._service.GetSuggestion(predictionContext.InputAst, 1, CancellationToken.None); |
| 67 | + Assert.NotEmpty(actual); |
| 68 | + Assert.NotNull(actual.First().Item1); |
| 69 | + Assert.Equal(expected.First().Key, actual.First().Item1); |
| 70 | + Assert.Equal(PredictionSource.CurrentCommand, actual.First().Item3); |
| 71 | + } |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// Verifies that when no prediction is in the suggestion list, we'll use the command list. |
| 75 | + /// </summary> |
| 76 | + [Theory] |
| 77 | + [InlineData("Get-AzResource -Name hello -Pre")] |
| 78 | + [InlineData("Get-AzADServicePrincipal -ApplicationObject")] |
| 79 | + public void VerifyUsingCommand(string userInput) |
| 80 | + { |
| 81 | + var predictionContext = PredictionContext.Create(userInput); |
| 82 | + var expected = this._commandsPredictor.Query(predictionContext.InputAst, 1, CancellationToken.None); |
| 83 | + var actual = this._service.GetSuggestion(predictionContext.InputAst, 1, CancellationToken.None); |
| 84 | + Assert.NotEmpty(actual); |
| 85 | + Assert.NotNull(actual.First().Item1); |
| 86 | + Assert.Equal(expected.First().Key, actual.First().Item1); |
| 87 | + Assert.Equal(PredictionSource.StaticCommands, actual.First().Item3); |
| 88 | + } |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// Verify that no prediction for the user input, meaning it's not in the prediction list or the command list. |
| 92 | + /// </summary> |
| 93 | + [Theory] |
| 94 | + [InlineData(AzPredictorConstants.CommandPlaceholder)] |
| 95 | + [InlineData("git status")] |
| 96 | + [InlineData("Get-ChildItem")] |
| 97 | + [InlineData("new-azresourcegroup -NoExistingParam")] |
| 98 | + [InlineData("get-azaccount ")] |
| 99 | + [InlineData("Get-AzContext Name")] |
| 100 | + [InlineData("NEW-AZCONTEXT")] |
| 101 | + public void VerifyNoPrediction(string userInput) |
| 102 | + { |
| 103 | + var predictionContext = PredictionContext.Create(userInput); |
| 104 | + var actual = this._service.GetSuggestion(predictionContext.InputAst, 1, CancellationToken.None); |
| 105 | + Assert.Empty(actual); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments