#Comment below because Pester has bug, it will show error for below code.
#Error:
#-] Clear-Variable ReadOnly variable Name should throw exception and force Clear-Variable should works 526ms
#The term 'Sort' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
#at line: 18 in /workspace/PowerShell/src/Microsoft.PowerShell.CoreConsoleHost/bin/Linux/netstandardapp1.5/ubuntu.14.04-x64/Modules/Pester/Functions/Assertions/Be.ps1
#Code:
#$_.FullyQualifiedErrorId | Should Be "VariableNotWritable,Microsoft.PowerShell.Commands.ClearAliasCommand"
}
Clear-Variable-Namefoo-Force
$var1=Get-Variable-Namefoo
$var1.Name|ShouldBe"foo"
$var1.Value|ShouldBe$null
$var1.Options|ShouldBe"ReadOnly"
$var1.Description|ShouldBe""
}
It"Clear-Variable normal variable Name with local scope should works"{
Set-Variablefoobar
&{
Set-Variablefoobaz
Clear-Variable-Namefoo-Scope"local"
$var1=Get-Variable-Namefoo-Scope"local"
$var1.Name|ShouldBe"foo"
$var1.Value|ShouldBe$null
$var1.Options|ShouldBe"None"
$var1.Description|ShouldBe""
}
$var1=Get-Variable-Namefoo
$var1.Name|ShouldBe"foo"
$var1.Value|ShouldBe"bar"
$var1.Options|ShouldBe"None"
$var1.Description|ShouldBe""
}
It"Clear-Variable Private variable Name should works and Get-Variable with local scope should throw exception"{
Set-Variablefoobar-OptionPrivate
{
Clear-Variable-Namefoo
try{
Get-Variable-Namefoo-Scopelocal
Throw"Execution OK"
}
catch{
$_.CategoryInfo|ShouldMatch"RuntimeException"
}
}
$var1=Get-Variable-Namefoo
$var1.Name|ShouldBe"foo"
$var1.Value|ShouldBe"bar"
$var1.Options|ShouldBe"Private"
$var1.Description|ShouldBe""
}
It"Clear-Variable normal variable Name with local scope should works in different scope"{
Set-Variablefoobar
&{
Set-Variablefoobaz
Clear-Variable-Namefoo-Scope"local"
$var1=Get-Variable-Namefoo-Scope"local"
$var1.Name|ShouldBe"foo"
$var1.Value|ShouldBe$null
$var1.Options|ShouldBe"None"
$var1.Description|ShouldBe""
}
$var1=Get-Variable-Namefoo
$var1.Name|ShouldBe"foo"
$var1.Value|ShouldBe"bar"
$var1.Options|ShouldBe"None"
$var1.Description|ShouldBe""
$var1=Get-Variable-Namefoo-Scope"local"
$var1.Name|ShouldBe"foo"
$var1.Value|ShouldBe"bar"
$var1.Options|ShouldBe"None"
$var1.Description|ShouldBe""
}
}
Describe"Clear-Variable"{
BeforeEach{
$var1=3
}
It"Should be able to clear a variable using the Name switch"{
Clear-Variable-Namevar1
$var1|ShouldBeNullOrEmpty
{Get-Variablevar1}|ShouldNotThrow
}
It"Should be able to clear a variable without using the Name switch"{
Clear-Variablevar1
$var1|ShouldBeNullOrEmpty
{Get-Variablevar1}|ShouldNotThrow
}
It"Should have a null value after clearing the variable"{
Clear-Variablevar1
$var1|ShouldBeNullOrEmpty
}
It"Should call without error using the clv alias"{
{clv -Namevar1}|ShouldNotThrow
}
It"Should be able to include a set of variables to clear"{
$var1=2
$var2=3
$var3=4
$variable1=23
$variable2=4
$variable3=2
$w=3
Clear-Variable-Namew,vari*-Includew,vari*
$variable1|ShouldBeNullOrEmpty
$variable2|ShouldBeNullOrEmpty
$variable3|ShouldBeNullOrEmpty
$w|ShouldBeNullOrEmpty
$var1|ShouldNotBeNullOrEmpty
$var2|ShouldNotBeNullOrEmpty
$var3|ShouldNotBeNullOrEmpty
}
It"Should be able to exclude a set of variables to clear"{
$var1=2
$var2=3
$var3=4
$variable1=23
$variable2=4
$variable3=2
$w=3
Clear-Variable-Namew,vari*-Excludevar*
$variable1|ShouldNotBeNullOrEmpty
$variable2|ShouldNotBeNullOrEmpty
$variable3|ShouldNotBeNullOrEmpty
$var1|ShouldNotBeNullOrEmpty
$var2|ShouldNotBeNullOrEmpty
$var3|ShouldNotBeNullOrEmpty
$w|ShouldBeNullOrEmpty
}
It"Should be able to pass the cleared object through the pipeline using the passthru switch"{