2016-03-04 23:48:30 +01:00
|
|
|
Describe "Set-Variable" {
|
2015-12-01 19:44:32 +01:00
|
|
|
${nl} = [Environment]::Newline
|
|
|
|
|
2015-09-29 18:23:21 +02:00
|
|
|
It "Should create a new variable with no parameters" {
|
2016-03-04 23:52:27 +01:00
|
|
|
{ Set-Variable testVar } | Should Not Throw
|
2015-09-29 18:23:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should assign a value to a variable it has to create" {
|
2016-03-04 23:52:27 +01:00
|
|
|
Set-Variable -Name testVar -Value 4
|
2015-09-29 18:23:21 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Get-Variable testVar -ValueOnly | Should Be 4
|
2015-09-29 18:23:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should change the value of an already existing variable" {
|
2016-03-04 23:52:27 +01:00
|
|
|
$testVar=1
|
2015-09-29 18:23:21 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$testVar | Should Not Be 2
|
2015-09-29 18:23:21 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Set-Variable testVar -Value 2
|
2015-09-29 18:23:21 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$testVar | Should Be 2
|
2015-09-29 18:23:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should be able to be called with the set alias" {
|
2016-03-04 23:52:27 +01:00
|
|
|
set testVar -Value 1
|
2015-09-29 18:23:21 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$testVar | Should Be 1
|
2015-09-29 18:23:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should be able to be called with the sv alias" {
|
2016-03-04 23:52:27 +01:00
|
|
|
sv testVar -Value 2
|
2015-09-29 18:23:21 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$testVar | Should Be 2
|
2015-09-29 18:23:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should be able to set variable name using the Name parameter" {
|
2016-03-04 23:52:27 +01:00
|
|
|
Set-Variable -Name testVar -Value 1
|
2015-09-29 18:23:21 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$testVar | Should Be 1
|
2015-09-29 18:23:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should be able to set the value of a variable by piped input" {
|
2016-03-04 23:52:27 +01:00
|
|
|
$testValue = "piped input"
|
2015-09-29 18:23:21 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$testValue | Set-Variable -Name testVar
|
2015-09-29 18:23:21 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$testVar | Should Be $testValue
|
2015-09-29 18:23:21 +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:23:21 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$output = $in | Format-List -Property Description | Out-String
|
2015-09-29 18:23:21 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
# This will cause errors running these tests in Windows
|
|
|
|
$output | Should Be "${nl}${nl}Description : test description${nl}${nl}${nl}${nl}"
|
2015-09-29 18:23:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should be able to set the value using the value switch" {
|
2016-03-04 23:52:27 +01:00
|
|
|
Set-Variable -Name testVar -Value 4
|
2015-09-29 18:23:21 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$testVar | Should Be 4
|
2015-09-29 18:23:21 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Set-Variable -Name testVar -Value "test"
|
2015-09-29 18:23:21 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$testVar | Should Be "test"
|
2015-09-29 18:23:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Context "Scope Tests" {
|
2016-03-04 23:52:27 +01:00
|
|
|
It "Should be able to set a global scope variable using the global switch" {
|
|
|
|
{ Set-Variable globalVar -Value 1 -Scope global -Force } | Should Not Throw
|
|
|
|
}
|
2015-09-29 18:23:21 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
It "Should be able to set a global variable using the script scope switch" {
|
|
|
|
{ Set-Variable globalVar -Value 1 -Scope script -Force } | Should Not Throw
|
|
|
|
}
|
2015-09-29 18:23:21 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
It "Should be able to set an item locally using the local switch" {
|
|
|
|
{ Set-Variable globalVar -Value 1 -Scope local -Force } | Should Not Throw
|
|
|
|
}
|
2015-09-29 18:23:21 +02:00
|
|
|
}
|
|
|
|
}
|