2016-03-04 23:48:30 +01:00
|
|
|
Describe "Clear-Variable" {
|
2015-09-29 18:27:30 +02:00
|
|
|
BeforeEach {
|
2016-03-04 23:52:27 +01:00
|
|
|
$var1 = 3
|
2015-09-29 18:27:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should be able to clear a variable using the Name switch" {
|
2016-03-04 23:52:27 +01:00
|
|
|
Clear-Variable -Name var1
|
|
|
|
$var1 | Should BeNullOrEmpty
|
|
|
|
{ Get-Variable var1 } | Should Not Throw
|
2015-09-29 18:27:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should be able to clear a variable without using the Name switch" {
|
2016-03-04 23:52:27 +01:00
|
|
|
Clear-Variable var1
|
|
|
|
$var1 | Should BeNullOrEmpty
|
|
|
|
{ Get-Variable var1 } | Should Not Throw
|
2015-09-29 18:27:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should have a null value after clearing the variable" {
|
2016-03-04 23:52:27 +01:00
|
|
|
Clear-Variable var1
|
|
|
|
$var1 | Should BeNullOrEmpty
|
2015-09-29 18:27:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should call without error using the clv alias" {
|
2016-03-04 23:52:27 +01:00
|
|
|
{ clv -Name var1 } | Should Not Throw
|
2015-09-29 18:27:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should be able to include a set of variables to clear" {
|
2016-03-04 23:52:27 +01:00
|
|
|
$var1 = 2
|
|
|
|
$var2 = 3
|
|
|
|
$var3 = 4
|
|
|
|
$variable1 = 23
|
|
|
|
$variable2 = 4
|
|
|
|
$variable3 = 2
|
|
|
|
$w = 3
|
2015-09-29 18:27:30 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Clear-Variable -Name w, vari* -Include w, vari*
|
2015-09-29 18:27:30 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$variable1 | Should BeNullOrEmpty
|
|
|
|
$variable2 | Should BeNullOrEmpty
|
|
|
|
$variable3 | Should BeNullOrEmpty
|
|
|
|
$w | Should BeNullOrEmpty
|
2015-09-29 18:27:30 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$var1 | Should Not BeNullOrEmpty
|
|
|
|
$var2 | Should Not BeNullOrEmpty
|
|
|
|
$var3 | Should Not BeNullOrEmpty
|
2015-09-29 18:27:30 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
It "Should be able to exclude a set of variables to clear" {
|
2016-03-04 23:52:27 +01:00
|
|
|
$var1 = 2
|
|
|
|
$var2 = 3
|
|
|
|
$var3 = 4
|
|
|
|
$variable1 = 23
|
|
|
|
$variable2 = 4
|
|
|
|
$variable3 = 2
|
|
|
|
$w = 3
|
|
|
|
|
|
|
|
Clear-Variable -Name w, vari* -Exclude var*
|
|
|
|
|
|
|
|
$variable1 | Should Not BeNullOrEmpty
|
|
|
|
$variable2 | Should Not BeNullOrEmpty
|
|
|
|
$variable3 | Should Not BeNullOrEmpty
|
|
|
|
$var1 | Should Not BeNullOrEmpty
|
|
|
|
$var2 | Should Not BeNullOrEmpty
|
|
|
|
$var3 | Should Not BeNullOrEmpty
|
|
|
|
|
|
|
|
$w | Should BeNullOrEmpty
|
2015-09-29 18:27:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should be able to pass the cleared object through the pipeline using the passthru switch" {
|
2016-03-04 23:52:27 +01:00
|
|
|
{ Clear-Variable -Name var1 -PassThru | Format-Wide -Property Value } | Should Not Throw
|
2015-09-29 18:27:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should not clear environment variables" {
|
2016-03-04 23:52:27 +01:00
|
|
|
$env:TEMPVARIABLE = "test data"
|
2015-09-29 18:27:30 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Clear-Variable -Name env:TEMPVARIABLE -ErrorAction SilentlyContinue | Should Throw
|
2015-09-29 18:27:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should clear variable even if it is read-only using the Force parameter" {
|
2016-03-04 23:52:27 +01:00
|
|
|
New-Variable -Name var2 -Option ReadOnly -Value 100
|
2015-09-29 18:27:30 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Clear-Variable -Name var1
|
|
|
|
Clear-Variable -Name var2 -Force
|
2015-09-29 18:27:30 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$var1 | Should BeNullOrEmpty
|
|
|
|
$var2 | Should BeNullOrEmpty
|
2015-09-29 18:27:30 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Remove-Variable -Name var2 -Force
|
2015-09-29 18:27:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should throw error when trying to clear variable that is read-only without using the Force parameter" {
|
2016-03-04 23:52:27 +01:00
|
|
|
New-Variable -Name var2 -Option ReadOnly -Value 100
|
2015-09-29 18:27:30 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Clear-Variable -Name var2 -ErrorAction SilentlyContinue | Should Throw
|
2015-09-29 18:27:30 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$var2 | Should Not BeNullOrEmpty
|
2015-09-29 18:27:30 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Remove-Variable -Name var2 -Force
|
2015-09-29 18:27:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Context "Scope Tests" {
|
2016-03-04 23:52:27 +01:00
|
|
|
# This will violate the DRY principle. Tread softly.
|
2015-09-29 18:27:30 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
It "Should be able to clear a global scope variable using the global switch" {
|
|
|
|
New-Variable globalVar -Value 1 -Scope global -Force
|
2015-09-29 18:27:30 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Clear-Variable -Name globalVar -Scope global
|
2015-09-29 18:27:30 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$globalVar | Should BeNullOrEmpty
|
|
|
|
}
|
2015-09-29 18:27:30 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
It "Should not be able to clear a global scope variable using the local switch" {
|
|
|
|
New-Variable globalVar -Value 1 -Scope global -Force
|
2015-09-29 18:27:30 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Clear-Variable -Name globalVar -Scope local -ErrorAction SilentlyContinue | Should Throw
|
|
|
|
}
|
2015-09-29 18:27:30 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
It "Should not be able to clear a global variable using the script scope switch" {
|
|
|
|
New-Variable globalVar -Value 1 -Scope global -Force
|
2015-09-29 18:27:30 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Clear-Variable -Name localVar -Scope script -ErrorAction SilentlyContinue | Should Throw
|
|
|
|
}
|
2015-09-29 18:27:30 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
It "Should be able to clear an item locally using the local switch" {
|
|
|
|
New-Variable localVar -Value 2 -Scope local -Force
|
2015-09-29 18:27:30 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Clear-Variable -Name localVar -Scope local
|
2015-09-29 18:27:30 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$localVar | Should BeNullOrEmpty
|
2015-09-29 18:27:30 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Clear-Variable -Name localVar -Scope script -ErrorAction SilentlyContinue | Should Throw
|
|
|
|
}
|
2015-09-29 18:27:30 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
It "Should not be able to clear an item locally using the global switch" {
|
|
|
|
New-Variable localVar -Value 2 -Scope local -Force
|
2015-09-29 18:27:30 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Clear-Variable -Name localVar -Scope global -ErrorAction SilentlyContinue | Should Throw
|
|
|
|
}
|
2015-09-29 18:27:30 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
It "Should not be able to clear a local variable using the script scope switch" {
|
|
|
|
New-Variable localVar -Value 2 -Scope local -Force
|
2015-09-29 18:27:30 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Clear-Variable -Name localVar -Scope script -ErrorAction SilentlyContinue | Should Throw
|
|
|
|
}
|
2015-09-29 18:27:30 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
It "Should be able to clear a script variable created using the script switch" {
|
|
|
|
{
|
|
|
|
New-Variable -Name derp2 -Value 3 -Scope script -Force
|
2015-09-29 18:27:30 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Clear-Variable -Name derp2 -Scope script
|
|
|
|
}| Should Not Throw
|
|
|
|
}
|
2015-09-29 18:27:30 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
It "Should be able to clear a global script variable that was created using the script scope switch" {
|
|
|
|
{
|
|
|
|
New-Variable -Name derpx -Value 4 -Scope script -Force
|
2015-09-29 18:27:30 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Clear-Variable -Name derpx -Scope script
|
|
|
|
} | Should Not Throw
|
|
|
|
}
|
2015-09-29 18:27:30 +02:00
|
|
|
}
|
|
|
|
}
|