Skip to content
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions PSKoans/Koans/Katas/Bugfixing.Koans.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using module PSKoans
[Koan(Position = 152)]
param()

<#
Kata: Bug fixing

The bug fixing challenges below are based on some real world examples.
#>
Describe 'Debugging' {
Context 'The sticky one' {
BeforeAll {
function Debug-Me {
[CmdletBinding()]
param (
[Parameter()]
[string]
$ObjectData
)

$ObjectData = [PSCustomObject]@{
Name = $ObjectData
}

if ($ObjectData.Name -eq 'Value') {
$true
} else {
Comment thread
indented-automation marked this conversation as resolved.
Outdated
$ObjectData
}
}
}

It 'should return true' {
Debug-Me | Should -BeExactly $true
}
}

Context 'What about the iterator' {
BeforeAll {
function Debug-Me {
[CmdletBinding(DefaultParameterSetName = 'Default')]
param (
[Parameter()]
[string]
$InputObject,

[Parameter(ParameterSetName = 'Switch')]
[switch]
$Switch
)

switch ($InputObject) {
default {
'A value'
}
}
}
}

It 'is supposed to return a value' {
{ Debug-Me -InputObject 'Hello world' } | Should -Not -Throw
}
}

Context 'Pay attention to your surroudings' {
BeforeAll {
function Debug-Me {
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline)]
[string]
$InputObject
)

Write-Verbose 'Starting Debug-Me'

process
{
Write-Verbose "Working on $InputObject"
}
}
}

It 'should not be returning anything' {
Debug-Me -InputObject 'a value' | Should -BeNullOrEmpty
}
}

Context 'But the blog said this would work' {
BeforeAll {
function Debug-Me {
$process = Get—Process -Id $PID
if ($process -and $process.Name -eq 'powershell') {
Write-Host 'you must be using PowerShell'
}
}
Comment on lines +91 to +98

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't throw, will it? Might need to swap your Write-Host for a throw :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will throw (as-is) until the bug is fixed :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me tweak it just a little, to make it less obvious.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even the new example here doesn't look like it'll throw? Pester's Should -Throw will expect a terminating error and nothing else... what here is going to throw? XD

}

It 'should not be broken' {
{ Debug-Me } | Should -Not -Throw
}
}

Context 'Something is leaking' {

}
}