From d59b3220321476da774f3fc52911d859c2bfc35e Mon Sep 17 00:00:00 2001 From: "Jumping Yang[Wicresoft]" Date: Tue, 30 Aug 2016 01:01:03 +0800 Subject: [PATCH] Add BooleanParameterDCR and Eventing Pester Test (#2031) * Add BooleanParameterDCR and Eventing Pester Test * Correct describe for Eventing Pester Test * Add ProviderIntrinsics and Serialization Pester Test --- .../enginecore/BooleanParameterDCR.Tests.ps1 | 52 ++++++++++ test/powershell/enginecore/Eventing.Tests.ps1 | 36 +++++++ .../enginecore/ProviderIntrinsics.Tests.ps1 | 21 ++++ .../enginecore/Serialization.Tests.ps1 | 99 +++++++++++++++++++ 4 files changed, 208 insertions(+) create mode 100644 test/powershell/enginecore/BooleanParameterDCR.Tests.ps1 create mode 100644 test/powershell/enginecore/Eventing.Tests.ps1 create mode 100644 test/powershell/enginecore/ProviderIntrinsics.Tests.ps1 create mode 100644 test/powershell/enginecore/Serialization.Tests.ps1 diff --git a/test/powershell/enginecore/BooleanParameterDCR.Tests.ps1 b/test/powershell/enginecore/BooleanParameterDCR.Tests.ps1 new file mode 100644 index 000000000..b547241f6 --- /dev/null +++ b/test/powershell/enginecore/BooleanParameterDCR.Tests.ps1 @@ -0,0 +1,52 @@ +Describe "BooleanParameterDCR Tests" -tags "CI" { + BeforeAll { + Function ParserTestSwitchCmdlet + { + [CmdletBinding()] + param([switch]$switchParam) + return $switchParam + } + + Function ParserTestBoolCmdlet2 + { + [CmdletBinding()] + param([bool]$First=$false) + return $First + } + } + + $tests = @( + @{ inputTest = 0; expected = $false }, + @{ inputTest = 000; expected = $false }, + @{ inputTest = 0x00; expected = $false }, + @{ inputTest = 0.00; expected = $false } + ) + It "Test that passing zero works as the value for a Switch parameter, inputTest:,expect:" -TestCases $tests { + param ( $inputTest, $expected ) + [bool]$switchTestParam = $inputTest + $result = ParserTestSwitchCmdlet -switchParam:$switchTestParam + $result | should be $expected + } + + $tests = @( + @{ inputTest = $(1 -eq 1); expected = $true }, + @{ inputTest = $true; expected = $true }, + @{ inputTest = $TRUE; expected = $true } + ) + It "Test that $true is accepted as a true value for Switch parameters, inputTest:,expect:" -TestCases $tests { + param ( $inputTest, $expected ) + [bool]$switchTestParam = $inputTest + $result = ParserTestSwitchCmdlet -switchParam:$switchTestParam + $result | should be $expected + } + + It "Test that a nullable boolean is accepted for a boolean parameter." { + [System.Nullable[System.Int32]] $nullBoolVar = $false + $result = ParserTestBoolCmdlet2 $nullBoolVar + $result | should be $false + $result = ParserTestBoolCmdlet2 -First:$nullBoolVar + $result | should be $false + $result = ParserTestBoolCmdlet2 -First $nullBoolVar + $result | should be $false + } +} diff --git a/test/powershell/enginecore/Eventing.Tests.ps1 b/test/powershell/enginecore/Eventing.Tests.ps1 new file mode 100644 index 000000000..d16bb5ca5 --- /dev/null +++ b/test/powershell/enginecore/Eventing.Tests.ps1 @@ -0,0 +1,36 @@ +Describe "Event Subscriber Tests" -tags "CI" { + 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{ + Get-EventSubscriber | should BeNullOrEmpty + $messageData = new-object psobject + $job = Start-Job { Start-Sleep -Seconds 5; 1..5 } + $eventtest = Register-ObjectEvent $job -EventName StateChanged -SourceIdentifier EventSIDTest -Action {} -MessageData $messageData + new-event EventSIDTest + + wait-event EventSIDTest + $eventdata = get-event EventSIDTest + $eventdata.MessageData | should Be $messageData + remove-event EventSIDTest + Unregister-Event EventSIDTest + Get-EventSubscriber | should BeNullOrEmpty + } + + It "Access a global variable from an event action." { + Get-EventSubscriber | should BeNullOrEmpty + set-variable incomingGlobal -scope global -value globVarValue + $eventtest = 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 + $getvar.Value | should Be globVarValue + Unregister-Event foo + Get-EventSubscriber | should BeNullOrEmpty + } +} diff --git a/test/powershell/enginecore/ProviderIntrinsics.Tests.ps1 b/test/powershell/enginecore/ProviderIntrinsics.Tests.ps1 new file mode 100644 index 000000000..9af5e0b9b --- /dev/null +++ b/test/powershell/enginecore/ProviderIntrinsics.Tests.ps1 @@ -0,0 +1,21 @@ +Describe "ProviderIntrinsics Tests" -tags "CI" { + BeforeAll { + setup -d TestDir + } + It 'If a childitem exists, HasChild method returns $true' { + $ExecutionContext.InvokeProvider.ChildItem.HasChild("$TESTDRIVE") | Should be $true + } + It 'If a childitem does not exist, HasChild method returns $false' { + $ExecutionContext.InvokeProvider.ChildItem.HasChild("$TESTDRIVE/TestDir") | Should be $false + } + It 'If the path does not exist, HasChild throws an exception' { + try { + $ExecutionContext.InvokeProvider.ChildItem.HasChild("TESTDRIVE/ThisDirectoryDoesNotExist") + throw "Execution OK" + } + catch { + $_.fullyqualifiederrorid | should be "ItemNotFoundException" + } + } +} + diff --git a/test/powershell/enginecore/Serialization.Tests.ps1 b/test/powershell/enginecore/Serialization.Tests.ps1 new file mode 100644 index 000000000..bc869a702 --- /dev/null +++ b/test/powershell/enginecore/Serialization.Tests.ps1 @@ -0,0 +1,99 @@ +Describe "Serialization Tests" -tags "CI" { + BeforeAll { + $testfileName="SerializationTest.txt" + $testPath = Join-Path $TestDrive $testfileName + $testfile = New-Item $testPath -Force + function SerializeAndDeserialize([PSObject]$inputObject) + { + $xmlSerializer = New-Object System.Xml.Serialization.XmlSerializer($inputObject.GetType()) + $stream = [System.IO.StreamWriter]$testPath + $xmlWriter = [System.Xml.XmlWriter]::Create($stream) + $xmlSerializer.Serialize($xmlWriter,$inputObject) + $stream.Close() + + $stream = [System.IO.StreamReader]$testPath + $xmlReader = [System.Xml.XmlReader]::Create($stream) + $outputObject = $xmlSerializer.Deserialize($xmlReader) + $stream.Close() + $xmlReader.Dispose() + return $outputObject; + } + + enum MyColorFlag + { + RED + BLUE + } + } + AfterAll { + Remove-Item $testPath -Force -ErrorAction SilentlyContinue + } + + It 'Test DateTimeUtc serialize and deserialize work as expected.' { + $inputObject = [System.DateTime]::UtcNow; + SerializeAndDeserialize($inputObject) | Should be $inputObject + } + + It 'Test DateTime stamps serialize and deserialize work as expected.' { + $objs = [System.DateTime]::MaxValue, [System.DateTime]::MinValue, [System.DateTime]::Today, (new-object System.DateTime), (new-object System.DateTime 123456789) + foreach($inputObject in $objs) + { + SerializeAndDeserialize($inputObject) | Should be $inputObject + } + } + + #pending because of "System.Uri cannot be serialized because it does not have a parameterless constructor." + It 'Test system Uri objects serialize and deserialize work as expected.' -Pending:$true { + $uristrings = "http://www.microsoft.com","http://www.microsoft.com:8000","http://www.microsoft.com/index.html","http://www.microsoft.com/default.asp","http://www.microsoft.com/Hello%20World.htm" + foreach($uristring in $uristrings) + { + $inputObject = new-object System.Uri $uristring + SerializeAndDeserialize($inputObject) | Should be $inputObject + } + } + + It 'Test a byte array serialize and deserialize work as expected.' { + $objs1 = [byte]0, [byte]1, [byte]2, [byte]3, [byte]255 + $objs2 = @() + $objs3 = [byte]128 + $objs4 = @() + for($i=0;$i -lt 256;$i++) + { + $objs4 += [byte]$i + } + $objsArray = New-Object System.Collections.ArrayList + $objsArray.Add($objs1) + $objsArray.Add($objs2) + $objsArray.Add($objs3) + $objsArray.Add($objs4) + + foreach($inputObject in $objsArray ) + { + $outputs = SerializeAndDeserialize($inputObject); + for($i=0;$i -lt $inputObject.Length;$i++) + { + $outputs[$i] | Should be $inputObject[$i] + } + } + } + + It 'Test Enum serialize and deserialize work as expected.' { + $inputObject = [MyColorFlag]::RED + SerializeAndDeserialize($inputObject).ToString() | Should be $inputObject.ToString() + } + + It 'Test SecureString serialize and deserialize work as expected.' { + $inputObject = Convertto-Securestring -String "PowerShellRocks!" -AsPlainText -Force + SerializeAndDeserialize($inputObject).Length | Should be $inputObject.Length + + } + + It 'Test ScriptProperty object serialize and deserialize work as expected.' { + $versionObject = New-Object PSObject + $versionObject | Add-Member -MemberType NoteProperty -Name TestNote -Value "TestNote" + $versionObject | Add-Member -MemberType ScriptProperty -Name TestScriptProperty -Value { ($this.TestNote) } + + SerializeAndDeserialize($versionObject).TestScriptProperty | Should be $versionObject.TestScriptProperty + } +} +