2016-03-04 23:48:30 +01:00
|
|
|
Describe "New-Variable" {
|
2015-12-01 00:11:11 +01:00
|
|
|
$nl = [Environment]::NewLine
|
|
|
|
|
2015-09-29 18:25:55 +02:00
|
|
|
It "Should create a new variable with no parameters" {
|
2016-03-04 23:52:27 +01:00
|
|
|
{ New-Variable var1 } | Should Not Throw
|
2015-09-29 18:25:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should be able to set variable name using the Name parameter" {
|
2016-03-04 23:52:27 +01:00
|
|
|
{ New-Variable -Name var1 } | Should Not Throw
|
2015-09-29 18:25:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should be able to assign a value to a variable using the value switch" {
|
2016-03-04 23:52:27 +01:00
|
|
|
New-Variable var1 -Value 4
|
2015-09-29 18:25:55 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$var1 | Should Be 4
|
2015-09-29 18:25:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should be able to assign a value to a new variable without using the value switch" {
|
2016-03-04 23:52:27 +01:00
|
|
|
New-Variable var1 "test"
|
2015-09-29 18:25:55 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$var1 | Should Be "test"
|
2015-09-29 18:25:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should assign a description to a new variable using the description switch" {
|
2016-03-04 23:52:27 +01:00
|
|
|
New-Variable var1 100 -Description "Test Description"
|
2015-09-29 18:25:55 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
(Get-Variable var1).Description | Should Be "Test Description"
|
2015-09-29 18:25:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
It "Should be able to be called with the nv alias" {
|
2016-03-04 23:52:27 +01:00
|
|
|
{ nv var1 } | Should Not Throw
|
|
|
|
{ nv var1 2 } | Should Not Throw
|
2015-09-29 18:25:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should not be able to set the name of a new variable to that of an old variable within same scope when the Force switch is missing" {
|
2016-03-04 23:52:27 +01:00
|
|
|
New-Variable var1
|
|
|
|
(New-Variable var1 -ErrorAction SilentlyContinue) | Should Throw
|
2015-09-29 18:25:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should change the value of an already existing variable using the Force switch" {
|
2016-03-04 23:52:27 +01:00
|
|
|
New-Variable var1 -Value 1
|
2015-09-29 18:25:55 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$var1 | Should Be 1
|
2015-09-29 18:25:55 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
New-Variable var1 -Value 2 -Force
|
2015-09-29 18:25:55 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$var1 | Should Be 2
|
|
|
|
$var1 | Should Not Be 1
|
2015-09-29 18:25:55 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
It "Should be able to set the value of a variable by piped input" {
|
2016-03-04 23:52:27 +01:00
|
|
|
$in = "value"
|
2015-09-29 18:25:55 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$in | New-Variable -Name var1
|
2015-09-29 18:25:55 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$var1 | Should Be $in
|
2015-09-29 18:25:55 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
It "Should be able to pipe object properties to output using the PassThru switch" {
|
2016-03-04 23:52:27 +01:00
|
|
|
$in = Set-Variable -Name testVar -Value "test" -Description "test description" -PassThru
|
2015-09-29 18:25:55 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$output = $in | Format-List -Property Description | Out-String
|
2015-09-29 18:25:55 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$output | Should Be "${nl}${nl}Description : test description${nl}${nl}${nl}${nl}"
|
2015-09-29 18:25:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should be able to set the value using the value switch" {
|
2016-03-04 23:52:27 +01:00
|
|
|
New-Variable -Name var1 -Value 2
|
2015-09-29 18:25:55 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$var1 | Should Be 2
|
2015-09-29 18:25:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Context "Option tests" {
|
2016-03-04 23:52:27 +01:00
|
|
|
BeforeEach {
|
|
|
|
# verify that the test variable doesn't exist
|
|
|
|
!!$var1 | Should Be $false
|
|
|
|
}
|
|
|
|
It "Should be able to use the options switch without error" {
|
|
|
|
{ New-Variable -Name var1 -Value 2 -Option Unspecified } | Should Not Throw
|
|
|
|
}
|
2015-09-29 18:25:55 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
It "Should default to none as the value for options" {
|
|
|
|
New-Variable -Name var2 -Value 4 -PassThru| Format-List | Out-String | Should Match "Options : None"
|
|
|
|
}
|
2015-09-29 18:25:55 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
It "Should be able to set ReadOnly option" {
|
|
|
|
{ New-Variable -Name var1 -Value 2 -Option ReadOnly } | Should Not Throw
|
|
|
|
}
|
2015-09-29 18:25:55 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
It "Should not be able to change variable created using the ReadOnly option when the Force switch is not used" {
|
|
|
|
New-Variable -Name var1 -Value 1 -Option ReadOnly
|
2015-09-29 18:25:55 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Set-Variable -Name var1 -Value 2 -ErrorAction SilentlyContinue
|
2015-09-29 18:25:55 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$var1 | Should Not Be 2
|
|
|
|
}
|
2015-09-29 18:25:55 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
It "Should be able to set a new variable to constant" {
|
|
|
|
{ New-Variable -Name var1 -Option Constant } | Should Not Throw
|
|
|
|
}
|
2015-09-29 18:25:55 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
It "Should not be able to change an existing variable to constant" {
|
|
|
|
New-Variable -Name var1 -Value 1 -PassThru
|
2015-09-29 18:25:55 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Set-Variable -Name var1 -Option Constant -ErrorAction SilentlyContinue
|
2015-09-29 18:25:55 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$var1 | Format-List | Out-String | Should Not Match "Options : None"
|
2015-09-29 18:25:55 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
}
|
2015-09-29 18:25:55 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
It "Should not be able to delete a constant variable" {
|
|
|
|
New-Variable -Name var1 -Value 2 -Option Constant
|
2015-09-29 18:25:55 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Remove-Variable -Name var1 -ErrorAction SilentlyContinue
|
2015-09-29 18:25:55 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
!!$var1 | Should Be $true
|
2015-09-29 18:25:55 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$var1 | Should Be 2
|
|
|
|
}
|
2015-09-29 18:25:55 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
It "Should not be able to change a constant variable" {
|
|
|
|
New-Variable -Name var1 -Value 1 -Option Constant
|
2015-09-29 18:25:55 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Set-Variable -Name var1 -Value 2 -ErrorAction SilentlyContinue
|
2015-09-29 18:25:55 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$var1 | Should Not Be 2
|
|
|
|
}
|
2015-09-29 18:25:55 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
It "Should be able to create a variable as private without error" {
|
|
|
|
{ New-Variable -Name var1 -Option Private } | Should Not Throw
|
|
|
|
}
|
2015-09-29 18:25:55 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
It "Should be able to see the value of a private variable when within scope" {
|
2015-09-29 18:25:55 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
New-Variable -Name var1 -Value 100 -Option Private
|
2015-09-29 18:25:55 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$var1 | Should Be 100
|
2015-09-29 18:25:55 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
}
|
2015-09-29 18:25:55 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
It "Should not be able to see the value of a private variable when out of scope" {
|
|
|
|
{
|
|
|
|
New-Variable -Name var1 -Value 1 -Option Private
|
|
|
|
}
|
2015-09-29 18:25:55 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$var1 | Should Be # Nothing, since it's not defined in scope
|
2015-09-29 18:25:55 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
}
|
2015-09-29 18:25:55 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
It "Should be able to use the AllScope switch without error" {
|
|
|
|
{ New-Variable -Name var1 -Option AllScope } | Should Not Throw
|
|
|
|
}
|
2015-09-29 18:25:55 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
It "Should be able to see variable created using the AllScope switch in a child scope" {
|
|
|
|
New-Variable -Name var1 -Value 1 -Option AllScope
|
|
|
|
function myFunction {
|
|
|
|
if ( $var1 -eq 1 ) {
|
|
|
|
$var1 = 2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
myFunction
|
2015-09-29 18:25:55 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$var1 | Should Be 2
|
2015-09-29 18:25:55 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
}
|
2015-09-29 18:25:55 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Context "Scope Tests" {
|
|
|
|
It "Should be able to create a global scope variable using the global switch" {
|
|
|
|
{ New-Variable globalVar -Value 1 -Scope global -Force } | Should Not Throw
|
|
|
|
}
|
2015-09-29 18:25:55 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
It "Should be able to create a local scope variable using the local switch" {
|
|
|
|
{ New-Variable localVar -Value 1 -Scope local -Force } | Should Not Throw
|
|
|
|
}
|
2015-09-29 18:25:55 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
It "Should be able to create a script scope variable using the script switch" {
|
|
|
|
{ New-Variable scriptVar -Value 1 -Scope script -Force } | Should Not Throw
|
|
|
|
}
|
2015-09-29 18:25:55 +02:00
|
|
|
}
|
|
|
|
}
|