Add Set-Variable Pester Unit test

This commit is contained in:
JumpingYang001 committed 2016-04-13 20:37:27 -07:00
1 parent 244b41a974
commit 46135239ba
1 file changed
+163
+163
View File
@@ -1,3 +1,166 @@
Describe "Set-Variable DRT Unit Tests" -Tags DRT{
It "Set-Variable normal variable Name should works"{
Set-Variable foo bar
$var1=Get-Variable -Name foo
$var1.Name|Should Be "foo"
$var1.Value|Should Be "bar"
$var1.Options|Should Be "None"
$var1.Description|Should Be ""
}
It "Set-Variable normal variable Name with position should works"{
Set-Variable -Name foo bar
$var1=Get-Variable -Name foo
$var1.Name|Should Be "foo"
$var1.Value|Should Be "bar"
$var1.Options|Should Be "None"
$var1.Description|Should Be ""
}
It "Set-Variable normal variable Name with scope should works"{
Set-Variable -Name foo -Value bar0
Set-Variable -Name foo -Value bar -Scope "1"
$var1=Get-Variable -Name foo -scope "1"
$var1.Name|Should Be "foo"
$var1.Value|Should Be "bar"
$var1.Options|Should Be "None"
$var1.Description|Should Be ""
Set-Variable -Name foo -Value newValue -Scope "local"
$var1=Get-Variable -Name foo -scope "local"
$var1.Name|Should Be "foo"
$var1.Value|Should Be "newValue"
$var1.Options|Should Be "None"
$var1.Description|Should Be ""
Set-Variable -Name foo -Value newValue2 -Scope "script"
$var1=Get-Variable -Name foo -scope "script"
$var1.Name|Should Be "foo"
$var1.Value|Should Be "newValue2"
$var1.Options|Should Be "None"
$var1.Description|Should Be ""
}
It "Set-Variable normal variable Name with position should works"{
Set-Variable abcaVar bar
Set-Variable bcdaVar anotherVal
Set-Variable aVarfoo bogusval
Set-Variable -Name "*aV*" -Value "overwrite" -Include "*Var*" -Exclude "bcd*"
$var1=Get-Variable -Name "*aVar*" -Scope "local"
$var1[0].Name|Should Be "abcaVar"
$var1[0].Value|Should Be "overwrite"
$var1[0].Options|Should Be "None"
$var1[0].Description|Should Be ""
$var1[1].Name|Should Be "aVarfoo"
$var1[1].Value|Should Be "overwrite"
$var1[1].Options|Should Be "None"
$var1[1].Description|Should Be ""
$var1[2].Name|Should Be "bcdaVar"
$var1[2].Value|Should Be "anotherVal"
$var1[2].Options|Should Be "None"
$var1[2].Description|Should Be ""
}
It "Set-Variable normal variable Name with Description and Value should works"{
Set-Variable foo bar
Set-Variable -Name foo $null -Description "new description" -PassThru:$true -Scope "local"
$var1=Get-Variable -Name foo -Scope "local"
$var1.Name|Should Be "foo"
$var1.Value|Should Be $null
$var1.Options|Should Be "None"
$var1.Description|Should Be "new description"
}
It "Set-Variable normal variable Name with just Description should works"{
Set-Variable foo bar
Set-Variable -Name foo -Description "new description" -PassThru:$true -Scope "local"
$var1=Get-Variable -Name foo -Scope "local"
$var1.Name|Should Be "foo"
$var1.Value|Should Be "bar"
$var1.Options|Should Be "None"
$var1.Description|Should Be "new description"
}
It "Set-Variable overwrite Constant Option should throw SessionStateUnauthorizedAccessException"{
Set-Variable -Name abcaVar bar -Option Constant -Scope "local"
try {
Set-Variable -Name abcaVar new -Scope "local" -EA Stop
Throw "Execution OK"
}
catch {
$_.FullyQualifiedErrorId | Should be "VariableNotWritable,Microsoft.PowerShell.Commands.SetVariableCommand"
}
}
It "Set-Variable of existing Private variable without force should throw Exception"{
Set-Variable abcaVar bar -Description "new description" -Option Private
$var1=Get-Variable -Name abcaVar
$var1.Name|Should Be "abcaVar"
$var1.Value|Should Be "bar"
$var1.Options|Should Be "Private"
$var1.Description|Should Be "new description"
Set-Variable abcaVar other -Description "new description"
$var1=Get-Variable -Name abcaVar
$var1.Name|Should Be "abcaVar"
$var1.Value|Should Be "other"
$var1.Options|Should Be "Private"
$var1.Description|Should Be "new description"
}
It "Set-Variable with Exclude, then Get-Variable it should throw ItemNotFoundException"{
Set-Variable -Name foo1,foo2 hello -Exclude foo2 -EA Stop
try {
Get-Variable -Name foo2 -EA Stop
Throw "Execution OK"
}
catch {
$_.FullyQualifiedErrorId | Should be "VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand"
}
}
It "Set-Variable of existing ReadOnly variable without force should throw Exception"{
Set-Variable abcaVar bar -Description "new description" -Option ReadOnly
$var1=Get-Variable -Name abcaVar
$var1.Name|Should Be "abcaVar"
$var1.Value|Should Be "bar"
$var1.Options|Should Be "ReadOnly"
$var1.Description|Should Be "new description"
try {
Set-Variable abcaVar -Option None -EA Stop
Throw "Execution OK"
}
catch {
$_.FullyQualifiedErrorId | Should be "VariableNotWritable,Microsoft.PowerShell.Commands.SetVariableCommand"
}
}
It "Set-Variable of ReadOnly variable with private scope should work"{
Set-Variable foo bar -Description "new description" -Option ReadOnly -scope "private"
$var1=Get-Variable -Name foo
$var1.Name|Should Be "foo"
$var1.Value|Should Be "bar"
$var1.Options|Should Be "ReadOnly, Private"
$var1.Description|Should Be "new description"
}
It "Set-Variable pipeline with Get-Variable should work"{
$footest1="bar"
${Get-Variable footest1 -valueonly|Set-Variable bootest1 -passthru}
$var1=Get-Variable -Name footest1
$var1.Name|Should Be "footest1"
$var1.Value|Should Be "bar"
$var1.Options|Should Be "None"
$var1.Description|Should Be ""
}
}
Describe "Set-Variable" {
${nl} = [Environment]::Newline