This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Keeping a value from a mock call, or some other place | |
# while avoiding $script: to keep script scope clean | |
Describe "a" { | |
It 'Test' { | |
function Get-GitHubRelease ($Release) { } | |
# use a reference object instead of directly assiging to a | |
# variable to collect the values from a mock call | |
$container = @{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Getting and setting variables from within a module into the calling scope | |
# make sure we re-import the module when experimenting with this | |
# to get our functions updated | |
Get-Module m | Remove-Module | |
New-Module m -ScriptBlock { | |
function Get-CallerVariable { | |
[CmdletBinding()] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-Filter ($Predicate) { | |
# gets the property hashtable of an object | |
$properties = $Predicate.PSObject.Properties | |
# runs the code below in a scriptblock to ensure that we only capture | |
# the desired $Properties variable in case we would have more variables | |
# in this function. This is not strictly necessary, but closure only captures | |
# local variables so it is useful trick to limit the variables that we capture. | |
& { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$m = Get-Command Invoke-Pester -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Module | |
$psv = $PSVersionTable.PSVersion | |
$pre = $m.PrivateData -and $m.PrivateData.PSData -and $m.PrivateData.PSData.PreRelease | |
$pv = if ($pre) { "$($m.Version)-$($m.PrivateData.PSData.PreRelease)" } else { $m.Version } | |
"Pester version : " + $pv + " " + $m.Path | |
"PowerShell version : " + $psv | |
"OS version : " + [System.Environment]::OSVersion.VersionString |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$harmony = "$PSScriptRoot/0Harmony.dll" | |
Import-Module $harmony | |
$Script:Patches = @() | |
function Set-StaticPropertyGetter { | |
param ( | |
[Parameter(Mandatory)] | |
[Type] $Type, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Describe "a" { | |
$Sources = @( | |
[PSCustomObject]@{ | |
Advanced = @{ | |
Enabled = $true | |
} | |
} | |
[PSCustomObject]@{ | |
Advanced = @{ | |
Enabled = $true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Adds BeforeAll at the top of Tests file to make it follow Pester v5 recommendation of putting | |
# all code into Pester controlled blocks. | |
# DO this: | |
# BeforeAll { | |
# . $PSScriptRoot/Code.ps1 | |
# } | |
# DO this: | |
# BeforeAll { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Adds BeforeAll at the top of Tests file to make it follow Pester v5 recommendation of putting | |
# all code into Pester controlled blocks. | |
# DO this: | |
# BeforeAll { | |
# . $PSScriptRoot/Code.ps1 | |
# } | |
# DO this: | |
# BeforeAll { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Quickly measuring if it is better to call function in foreach loop or gerate the array and pass it in the function once | |
# and then iterate over the array inside of the function | |
"`n" | |
# cross function boundary 10,000 times | |
$script:b = 0 | |
function a ($a) { $script:b += $a } | |
$sw = [Diagnostics.StopWatch]::StartNew() | |
foreach ($_ in 1..10000) { | |
a 1 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# output | |
Pester v4.9.0 | |
Executing all tests in 'C:\Users\jajares\Dropbox\pester\parameters.tests.ps1' | |
Executing script C:\Users\jajares\Dropbox\pester\parameters.tests.ps1 | |
Describing a | |
[+] b 31ms | |
[+] c 15ms |