|
| 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 System.Collections.ObjectModel; |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +namespace Microsoft.PowerShell.EditorServices.Test.Language |
| 10 | +{ |
| 11 | + using System.Management.Automation; |
| 12 | + |
| 13 | + public class RunspaceSynchronizerTests |
| 14 | + { |
| 15 | + [Trait("Category", "RunspaceSynchronizer")] |
| 16 | + [Theory] |
| 17 | + // variable test |
| 18 | + [InlineData("$foo = 'foo'", "$foo", "foo")] |
| 19 | + // module functions test |
| 20 | + [InlineData("Import-Module ../../../../PowerShellEditorServices.Test.Shared/RunspaceSynchronizer/testModule.psm1", "Search-Foo", "success")] |
| 21 | + // module aliases test |
| 22 | + [InlineData("Import-Module ../../../../PowerShellEditorServices.Test.Shared/RunspaceSynchronizer/testModule.psm1", "(Get-Alias sfoo).Definition", "Search-Foo")] |
| 23 | + public void TestRunspaceSynchronizerSyncsData(string sourceScript, string targetScript, object expected) |
| 24 | + { |
| 25 | + using (PowerShell pwshSource = PowerShell.Create()) |
| 26 | + using (PowerShell pwshTarget = PowerShell.Create()) |
| 27 | + { |
| 28 | + RunspaceSynchronizer.InitializeRunspaces(pwshSource.Runspace, pwshTarget.Runspace); |
| 29 | + AssertExpectedIsSynced(pwshSource, pwshTarget, sourceScript, targetScript, expected); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + [Fact] |
| 34 | + public void TestRunspaceSynchronizerOverwritesTypes() |
| 35 | + { |
| 36 | + using (PowerShell pwshSource = PowerShell.Create()) |
| 37 | + using (PowerShell pwshTarget = PowerShell.Create()) |
| 38 | + { |
| 39 | + RunspaceSynchronizer.InitializeRunspaces(pwshSource.Runspace, pwshTarget.Runspace); |
| 40 | + AssertExpectedIsSynced(pwshSource, pwshTarget, "$foo = 444", "$foo.GetType().Name", "Int32"); |
| 41 | + AssertExpectedIsSynced(pwshSource, pwshTarget, "$foo = 'change to string'", "$foo.GetType().Name", "String"); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + private static void AssertExpectedIsSynced( |
| 46 | + PowerShell pwshSource, |
| 47 | + PowerShell pwshTarget, |
| 48 | + string sourceScript, |
| 49 | + string targetScript, |
| 50 | + object expected) |
| 51 | + { |
| 52 | + pwshSource.AddScript(sourceScript).Invoke(); |
| 53 | + RunspaceSynchronizer.Activate(); |
| 54 | + |
| 55 | + // We need to allow the event some time to fire. |
| 56 | + System.Threading.Thread.Sleep(1000); |
| 57 | + |
| 58 | + var results = pwshTarget.AddScript(targetScript).Invoke<PSObject>(); |
| 59 | + |
| 60 | + Assert.Single(results); |
| 61 | + Assert.NotNull(results[0].BaseObject); |
| 62 | + Assert.Equal(expected, results[0].BaseObject); |
| 63 | + } |
| 64 | + } |
| 65 | +} |
0 commit comments