Add eventing tests

Ensure we check both instance and static members to ensure we add or
remove the event correctly for both cases.

Also added a tab completion test for event members, which we don't seem
to have explicit tests for that I could find. As this completion is
related to this PR, seemed appropriate to add tests for that behaviour
here as well.

Co-authored-by: Ilya <darpa@yandex.ru>
This commit is contained in:
Joel Sallow 2020-06-09 23:44:57 -04:00 committed by Rain Sallow (/u/ta11ow)
parent d96ec170b3
commit d08595fc64
2 changed files with 69 additions and 8 deletions

View File

@ -2054,10 +2054,17 @@ dir -Recurse `
It "Test member completion of a static method invocation" {
$inputStr = '[powershell]::Create().'
$res = TabExpansion2 -inputScript $inputStr -cursorColumn $inputStr.Length
$res.CompletionMatches | Should -HaveCount 33
$res.CompletionMatches | Should -HaveCount 34
$res.CompletionMatches[0].CompletionText | Should -BeExactly "Commands"
}
It "Completes event member names" {
$inputStr = '$timer = [System.Timers.Timer]::new(); $timer.El'
$results = TabExpansion2 -inputScript $inputStr -cursorColumn $inputStr.Length
$results.CompletionMatches | Should -HaveCount 1
$results.CompletionMatches[0].CompletionText | Should -BeExactly 'Elapsed'
}
It "Test completion with common parameters" {
$inputStr = 'invoke-webrequest -out'
$res = TabExpansion2 -inputScript $inputStr -cursorColumn $inputStr.Length

View File

@ -2,15 +2,17 @@
# Licensed under the MIT License.
Describe "Event Subscriber Tests" -Tags "Feature" {
BeforeEach {
Get-EventSubscriber | Unregister-Event
}
AfterEach {
Get-EventSubscriber | Unregister-Event
}
# can't let this case to work
It "Register an event with no action, trigger it and wait for it to be raised." -Pending:$true{
It "Register an event with no action, trigger it and wait for it to be raised." -Pending:$true {
Get-EventSubscriber | Should -BeNullOrEmpty
$messageData = New-Object psobject
$job = Start-Job { Start-Sleep -Seconds 5; 1..5 }
@ -28,7 +30,7 @@ Describe "Event Subscriber Tests" -Tags "Feature" {
It "Access a global variable from an event action." {
Get-EventSubscriber | Should -BeNullOrEmpty
Set-Variable incomingGlobal -Scope global -Value globVarValue
$null = Register-EngineEvent -SourceIdentifier foo -Action {Set-Variable -Scope global -Name aglobalvariable -Value $incomingGlobal}
$null = Register-EngineEvent -SourceIdentifier foo -Action { Set-Variable -Scope global -Name aglobalvariable -Value $incomingGlobal }
New-Event foo
$getvar = Get-Variable aglobalvariable -Scope global
$getvar.Name | Should -Be aglobalvariable
@ -40,14 +42,66 @@ Describe "Event Subscriber Tests" -Tags "Feature" {
It 'Should not throw when having finally block in Powershell.Exiting Action scriptblock' {
$pwsh = "$PSHOME/pwsh"
$output = & $pwsh {
Register-EngineEvent -SourceIdentifier Powershell.Exiting -Action {
try{
try{} finally{}
Register-EngineEvent -SourceIdentifier Powershell.Exiting -Action {
try {
try {} finally {}
} catch { Write-Host "Exception" -NoNewline }
}
catch{ Write-Host "Exception" -NoNewline }
}
} | Out-String
$output | Should -Not -BeLike "*Exception*"
}
Context 'Event (De)Registration with += / -=' {
BeforeAll {
$EventScript = { $global:EventTestResult = 120 }
Add-Type -TypeDefinition @'
using System;
public static class _A99_EventTestHelper_91C_ {
public static event EventHandler TestEvent;
public static void RaiseEvent() => TestEvent?.Invoke(null, null);
}
'@
}
BeforeEach {
$global:EventTestResult = 0
}
AfterAll {
Get-Variable -Name EventTestResult -Scope Global -ErrorAction Ignore | Remove-Variable
}
It 'Registers events with += for an event member' {
$ps = [powershell]::Create()
$ps.InvocationStateChanged += $EventScript
$ps.AddScript('2 + 2').Invoke() > $null
$global:EventTestResult | Should -Be 120
}
It 'Deregisters events with -= for an event member' {
$ps = [powershell]::Create()
$ps.InvocationStateChanged += $EventScript
$ps.InvocationStateChanged -= $EventScript
$ps.AddScript('2 + 2').Invoke() > $null
$global:EventTestResult | Should -Be 0
}
It 'Correctly registers a static event with +=' {
[_A99_EventTestHelper_91C_]::TestEvent += $EventScript
[_A99_EventTestHelper_91C_]::RaiseEvent()
$global:EventTestResult | Should -Be 120
}
It 'Correctly unregisters a static event with -=' {
# Original event should still be registered from above
[_A99_EventTestHelper_91C_]::TestEvent -= $EventScript
[_A99_EventTestHelper_91C_]::RaiseEvent()
$global:EventTestResult | Should -Be 0
}
}
}