コード例 #1
0
        public async Task FindsFunctionDefinitionForAlias()
        {
            // TODO: Eventually we should get the alises through the AST instead of relying on them
            // being defined in the runspace.
            await psesHost.ExecutePSCommandAsync(
                new PSCommand().AddScript("Set-Alias -Name My-Alias -Value My-Function"),
                CancellationToken.None).ConfigureAwait(true);

            SymbolReference definitionResult = await GetDefinition(FindsFunctionDefinitionOfAliasData.SourceDetails).ConfigureAwait(true);

            Assert.Equal(1, definitionResult.ScriptRegion.StartLineNumber);
            Assert.Equal(10, definitionResult.ScriptRegion.StartColumnNumber);
            Assert.Equal("My-Function", definitionResult.SymbolName);
        }
コード例 #2
0
        public async Task CanRegisterAndInvokeCommandWithCmdletName()
        {
            string        filePath      = TestUtilities.NormalizePath("C:\\Temp\\Test.ps1");
            ScriptFile    currentFile   = new(new Uri(filePath), "This is a test file", new Version("7.0"));
            EditorContext editorContext = new(
                editorOperations : null,
                currentFile,
                new BufferPosition(line : 1, column : 1),
                BufferRange.None);

            EditorCommand commandAdded = null;

            extensionCommandService.CommandAdded += (object _, EditorCommand command) => commandAdded = command;

            const string commandName        = "test.function";
            const string commandDisplayName = "Function extension";

            await psesHost.ExecutePSCommandAsync(
                new PSCommand().AddScript(
                    "function Invoke-Extension { $global:extensionValue = 5 }; " +
                    $"Register-EditorCommand -Name {commandName} -DisplayName \"{commandDisplayName}\" -Function Invoke-Extension"),
                CancellationToken.None).ConfigureAwait(true);

            Assert.NotNull(commandAdded);
            Assert.Equal(commandAdded.Name, commandName);
            Assert.Equal(commandAdded.DisplayName, commandDisplayName);

            // Invoke the command
            await extensionCommandService.InvokeCommandAsync(commandName, editorContext).ConfigureAwait(true);

            // Assert the expected value
            PSCommand         psCommand = new PSCommand().AddScript("$global:extensionValue");
            IEnumerable <int> results   = await psesHost.ExecutePSCommandAsync <int>(psCommand, CancellationToken.None).ConfigureAwait(true);

            Assert.Equal(5, results.FirstOrDefault());
        }