This file contains hidden or 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
| Import-Module Pester -MaximumVersion 4.9.9 | |
| # use the fact that closures are dynamic modules with | |
| # their own session state, and that scriptblocks are bound | |
| # to the session state that created them, that makes | |
| # the call to `dot` transparent, because the scope | |
| # that the function creates is in a different sessionstate | |
| # and so the `$ScriptBlock` still dot-sources directly into | |
| # the calling session state, just like with the naked `.` | |
| $function:dot = { |
This file contains hidden or 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
| # Sets variable $a = 1000 in the local scope from which it was called from, like $matches in regex | |
| Get-Module m | Remove-Module | |
| New-Module m { | |
| function f { | |
| [CmdletBinding()] | |
| param() | |
| $sb = [ScriptBlock]::Create('param($Value); $a = $Value') | |
| $ExecutionContext.InvokeCommand.InvokeScript($PSCmdlet.SessionState, $sb, @(1000)) | |
| } |
This file contains hidden or 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
| # This example shows three versions of behavior, in the first the data binding only | |
| # picks up the property that is defined on the type, in the second it picks up ever | |
| # powershell adapted property Name (alias property), and in the third it picks up | |
| # all properties even though they are all powershell properties and not on the PSObjectType | |
| # is there some special handling for PSObject regarding WPF in Windows PowerShell? | |
| Add-Type -AssemblyName PresentationFramework | |
| [string]$xaml = @" |
This file contains hidden or 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
| # GOOD: | |
| # shares data among tests without polluting | |
| # the script scope | |
| Describe "I share data" { | |
| $container = @{ Value = 1 } | |
| It "I give data" { | |
| $container.Value = 5 | |
| } |
This file contains hidden or 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 Import-Script { | |
| [CmdletBinding()] | |
| param ( | |
| [Parameter(Mandatory)] | |
| [String] $Path, | |
| [Hashtable] $Parameters = @{}, | |
| [Object[]] $Arguments = @(), | |
| [String] $EntryPoint = 'Main' | |
| ) |
This file contains hidden or 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-Banner { | |
| param ( | |
| [Parameter(Mandatory)] | |
| [String] $Text | |
| ) | |
| if (-not $fonts) { | |
| $fonts = (Invoke-RestMethod -Method GET -Uri 'http://artii.herokuapp.com/fonts_list') -split "`n" | |
| } | |
This file contains hidden or 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 getOrUpdateValue { | |
| [CmdletBinding()] | |
| param( | |
| $Hashtable, | |
| $Key, | |
| $DefaultValue | |
| ) | |
| if ($Hashtable.ContainsKey($Key)) { | |
| # do not enumerate so we get the same thing back |
This file contains hidden or 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 a () { } | |
| describe "b" { mock a {} | |
| context "b" { | |
| describe "a" { | |
| a | |
| context "b" { | |
| a | |
| it "c" { |
This file contains hidden or 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
| get-module m,n,p | remove-module | |
| # some tested code | |
| New-Module m -ScriptBlock { | |
| New-Module n -ScriptBlock { | |
| function a { Write-Error "error abcd" } | |
| } | Import-Module | |
| function b { |
This file contains hidden or 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 "path trimmer" { | |
| it "does correct stuff" -TestCases @( | |
| @{ Actual = "c:\"; Expected = "c:\" } | |
| @{ Actual = "c:\abc"; Expected = "c:\abc" } | |
| @{ Actual = "c:\abc\"; Expected = "c:\abc" } | |
| @{ Actual = "\\abc\def"; Expected = "\\abc\def" } | |
| @{ Actual = "\\abc\def\"; Expected = "\\abc\def" } | |
| @{ Actual = "\\abc\def\gef\"; Expected = "\\abc\def\gef" } | |
| @{ Actual = "\"; Expected = "\" } | |
| ) { |