From 1268ce5f24cf16bc0c2122fd33eaf655b047b957 Mon Sep 17 00:00:00 2001 From: Zachary Folwick Date: Tue, 29 Sep 2015 09:23:21 -0700 Subject: [PATCH 1/3] initial commit --- src/pester-tests/Test-Set-Variable.Tests.ps1 | 128 +++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 src/pester-tests/Test-Set-Variable.Tests.ps1 diff --git a/src/pester-tests/Test-Set-Variable.Tests.ps1 b/src/pester-tests/Test-Set-Variable.Tests.ps1 new file mode 100644 index 000000000..5a9821423 --- /dev/null +++ b/src/pester-tests/Test-Set-Variable.Tests.ps1 @@ -0,0 +1,128 @@ +Describe "Test-Set-Variable" { + It "Should create a new variable with no parameters" { + { Set-Variable testVar } | Should Not Throw + } + + It "Should assign a value to a variable it has to create" { + $testVar=4 + + Get-Variable testVar -ValueOnly | Should Be 4 + } + + It "Should change the value of an already existing variable" { + $testVar=1 + + $testVar | Should Not Be 2 + + Set-Variable testVar -Value 2 + + $testVar | Should Be 2 + } + + It "Should be able to be called with the set alias" { + set testVar -Value 1 + + $testVar | Should Be 1 + } + + It "Should be able to be called with the sv alias" { + sv testVar -Value 2 + + $testVar | Should Be 2 + } + + It "Should be able to set variable name using the Name parameter" { + Set-Variable -Name testVar -Value 1 + + $testVar | Should Be 1 + } + + It "Should be able to set the value of a variable by piped input" { + $testValue = "piped input" + + $testValue | Set-Variable -Name testVar + + $testVar | Should Be $testValue + } + + It "Should be able to pipe object properties to output using the PassThru switch" { + $input = Set-Variable -Name testVar -Value "test" -Description "test description" -PassThru + + $output = $input | Format-List -Property Description | Out-String + + $output | Should Be "`n`nDescription : test description`n`n`n`n" + } + + It "Should be able to set the value using the value switch" { + Set-Variable -Name testVar -Value 4 + + $testVar | Should Be 4 + + Set-Variable -Name testVar -Value "test" + + $testVar | Should Be "test" + } + + Context "Scope Tests" { + # This will violate the DRY principle. Tread softly. + + It "Should be able to get a global scope variable using the global switch" { + Set-Variable globalVar -Value 1 -Scope global -Force + + (Get-Variable -Name globalVar -Scope global)[0].Value | Should Be 1 + } + + It "Should not be able to set a global scope variable using the local switch" { + Set-Variable globalVar -Value 1 -Scope global -Force + + Get-Variable -Name globalVar -Scope local -ErrorAction SilentlyContinue | Should Throw + } + + It "Should be able to set a global variable using the script scope switch" { + { + Set-Variable globalVar -Value 1 -Scope global -Force + Get-Variable -Name globalVar -Scope script + } | Should Not Throw + } + + It "Should be able to get an item locally using the local switch" { + { + Set-Variable globalVar -Value 1 -Scope local -Force + + Get-Variable -Name globalVar -Scope local + } | Should Not Throw + } + + It "Should be able to get an item locally using the global switch" { + { + Set-Variable globalVar -Value 1 -Scope local -Force + + Get-Variable -Name globalVar -Scope global + } | Should Not Throw + } + + It "Should not be able to get a local variable using the script scope switch" { + { + Set-Variable globalVar -Value 1 -Scope local -Force + + Get-Variable -Name globalVar -Scope script + } | Should Not Throw + } + + It "Should be able to get a script variable created using the script switch" { + { + Set-Variable globalVar -Value 1 -Scope script -Force + + Get-Variable -Name globalVar -Scope script + } | Should Not Throw + } + + It "Should be able to set a global script variable that was created using the script scope switch" { + { + Set-Variable globalVar -Value 1 -Scope script -Force + + Get-Variable -Name globalVar -Scope script + } | Should Not Throw + } + } +} From 7b29702a27c7d6dbcad9b2c35b85167dff6f4fa8 Mon Sep 17 00:00:00 2001 From: Zachary Folwick Date: Tue, 29 Sep 2015 10:20:11 -0700 Subject: [PATCH 2/3] Added set-variable tests --- src/pester-tests/Test-Set-Variable.Tests.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/src/pester-tests/Test-Set-Variable.Tests.ps1 b/src/pester-tests/Test-Set-Variable.Tests.ps1 index 5a9821423..e683d120b 100644 --- a/src/pester-tests/Test-Set-Variable.Tests.ps1 +++ b/src/pester-tests/Test-Set-Variable.Tests.ps1 @@ -50,6 +50,7 @@ $output = $input | Format-List -Property Description | Out-String + # This will cause errors running these tests in windows $output | Should Be "`n`nDescription : test description`n`n`n`n" } From 73ee60efc1d876a4ed7b78dc5ad16d77e479f518 Mon Sep 17 00:00:00 2001 From: Zachary Folwick Date: Fri, 2 Oct 2015 15:21:56 -0700 Subject: [PATCH 3/3] fixed test according to code review --- src/pester-tests/Test-Set-Variable.Tests.ps1 | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/pester-tests/Test-Set-Variable.Tests.ps1 b/src/pester-tests/Test-Set-Variable.Tests.ps1 index e683d120b..66882beb4 100644 --- a/src/pester-tests/Test-Set-Variable.Tests.ps1 +++ b/src/pester-tests/Test-Set-Variable.Tests.ps1 @@ -1,10 +1,12 @@ Describe "Test-Set-Variable" { It "Should create a new variable with no parameters" { { Set-Variable testVar } | Should Not Throw + + { Get-Variable testVar } | Should Not Throw } It "Should assign a value to a variable it has to create" { - $testVar=4 + Set-Variable -Name testVar -Value 4 Get-Variable testVar -ValueOnly | Should Be 4 }