From 7a96a399e9c2fe46f1f66286bc374c484f94ed16 Mon Sep 17 00:00:00 2001 From: Chris Dent Date: Wed, 8 Jan 2020 18:14:43 +0000 Subject: [PATCH 1/7] Debugging ideas --- PSKoans/Koans/Katas/Debugging.Koans.ps1 | 108 ++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 PSKoans/Koans/Katas/Debugging.Koans.ps1 diff --git a/PSKoans/Koans/Katas/Debugging.Koans.ps1 b/PSKoans/Koans/Katas/Debugging.Koans.ps1 new file mode 100644 index 000000000..e418af3c2 --- /dev/null +++ b/PSKoans/Koans/Katas/Debugging.Koans.ps1 @@ -0,0 +1,108 @@ +using module PSKoans +[Koan(Position = 152)] +param() + +<# + Kata: Debugging + + The debugging challenges below are based on some real world examples. Debugging is a critical skill and not one + which is confined break points. +#> +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 { + $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' + } + } + } + + It 'should not be broken' { + { Debug-Me } | Should -Not -Throw + } + } + + Context 'Something is leaking' { + + } +} From e2b5787b1799cf56a4aab7c2e409f91f33eead69 Mon Sep 17 00:00:00 2001 From: Chris Dent Date: Wed, 8 Jan 2020 18:17:38 +0000 Subject: [PATCH 2/7] Renames koan --- .../Koans/Katas/{Debugging.Koans.ps1 => Bugfixing.Koans.ps1} | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) rename PSKoans/Koans/Katas/{Debugging.Koans.ps1 => Bugfixing.Koans.ps1} (93%) diff --git a/PSKoans/Koans/Katas/Debugging.Koans.ps1 b/PSKoans/Koans/Katas/Bugfixing.Koans.ps1 similarity index 93% rename from PSKoans/Koans/Katas/Debugging.Koans.ps1 rename to PSKoans/Koans/Katas/Bugfixing.Koans.ps1 index e418af3c2..e17bab2c7 100644 --- a/PSKoans/Koans/Katas/Debugging.Koans.ps1 +++ b/PSKoans/Koans/Katas/Bugfixing.Koans.ps1 @@ -3,10 +3,9 @@ using module PSKoans param() <# - Kata: Debugging + Kata: Bug fixing - The debugging challenges below are based on some real world examples. Debugging is a critical skill and not one - which is confined break points. + The bug fixing challenges below are based on some real world examples. #> Describe 'Debugging' { Context 'The sticky one' { From 0ab501ab4ccc2d06b7eaca1b0310b4f135b586d5 Mon Sep 17 00:00:00 2001 From: Chris Dent Date: Mon, 13 Jan 2020 18:03:19 +0000 Subject: [PATCH 3/7] Update PSKoans/Koans/Katas/Bugfixing.Koans.ps1 Co-Authored-By: Joel Sallow (/u/ta11ow) <32407840+vexx32@users.noreply.github.com> --- PSKoans/Koans/Katas/Bugfixing.Koans.ps1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/PSKoans/Koans/Katas/Bugfixing.Koans.ps1 b/PSKoans/Koans/Katas/Bugfixing.Koans.ps1 index e17bab2c7..b8d4a0528 100644 --- a/PSKoans/Koans/Katas/Bugfixing.Koans.ps1 +++ b/PSKoans/Koans/Katas/Bugfixing.Koans.ps1 @@ -24,7 +24,8 @@ Describe 'Debugging' { if ($ObjectData.Name -eq 'Value') { $true - } else { + } + else { $ObjectData } } From a24164ef5c0deb349e89914f1be61a5ce4a530e9 Mon Sep 17 00:00:00 2001 From: Chris Dent Date: Mon, 13 Jan 2020 18:06:40 +0000 Subject: [PATCH 4/7] tweaks example --- PSKoans/Koans/Katas/Bugfixing.Koans.ps1 | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/PSKoans/Koans/Katas/Bugfixing.Koans.ps1 b/PSKoans/Koans/Katas/Bugfixing.Koans.ps1 index b8d4a0528..61a52f72a 100644 --- a/PSKoans/Koans/Katas/Bugfixing.Koans.ps1 +++ b/PSKoans/Koans/Katas/Bugfixing.Koans.ps1 @@ -91,8 +91,9 @@ Describe 'Debugging' { BeforeAll { function Debug-Me { $process = Get—Process -Id $PID - if ($process -and $process.Name -eq 'powershell') { - Write-Host 'you must be using PowerShell' + if ($process -and $process.Name -notin 'powershell', 'pwsh') { + Write-Warning 'you must be using PowerShell' + return } } } From 77bb15b7cf5273bd67611fc4526bdea66df002a1 Mon Sep 17 00:00:00 2001 From: Chris Dent Date: Mon, 13 Jan 2020 18:19:19 +0000 Subject: [PATCH 5/7] Fixes typo --- PSKoans/Koans/Katas/Bugfixing.Koans.ps1 | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/PSKoans/Koans/Katas/Bugfixing.Koans.ps1 b/PSKoans/Koans/Katas/Bugfixing.Koans.ps1 index 61a52f72a..99d1a542b 100644 --- a/PSKoans/Koans/Katas/Bugfixing.Koans.ps1 +++ b/PSKoans/Koans/Katas/Bugfixing.Koans.ps1 @@ -63,7 +63,7 @@ Describe 'Debugging' { } } - Context 'Pay attention to your surroudings' { + Context 'Pay attention to your surroundings' { BeforeAll { function Debug-Me { [CmdletBinding()] @@ -104,6 +104,10 @@ Describe 'Debugging' { } Context 'Something is leaking' { + BeforeAll { + function Debug-Me { + } + } } } From 87c40bdd7a7a4a2f050f45e86a97fe40505fc4f7 Mon Sep 17 00:00:00 2001 From: Chris Dent Date: Mon, 13 Jan 2020 18:52:36 +0000 Subject: [PATCH 6/7] Fixes the sticky one --- PSKoans/Koans/Katas/Bugfixing.Koans.ps1 | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/PSKoans/Koans/Katas/Bugfixing.Koans.ps1 b/PSKoans/Koans/Katas/Bugfixing.Koans.ps1 index 99d1a542b..ab12cddb4 100644 --- a/PSKoans/Koans/Katas/Bugfixing.Koans.ps1 +++ b/PSKoans/Koans/Katas/Bugfixing.Koans.ps1 @@ -32,7 +32,7 @@ Describe 'Debugging' { } It 'should return true' { - Debug-Me | Should -BeExactly $true + Debug-Me -ObjectData 'Value' | Should -BeExactly $true } } @@ -110,4 +110,21 @@ Describe 'Debugging' { } } } + + Context 'The little things matter' { + BeforeAll { + function Debug-Me { + $items = 1..10 + foreach ($item in $items) { + if ($item % 2) { + Write-Output $items + } + } + } + } + + It 'should return odd numbers only' { + Debug-Me | Should -Be 1, 3, 5, 7, 9 + } + } } From d3779866ddc1deaf3ff71c45e46ed5c56d9d6b48 Mon Sep 17 00:00:00 2001 From: Chris Dent Date: Mon, 13 Jan 2020 19:31:26 +0000 Subject: [PATCH 7/7] Improves The little things matter --- PSKoans/Koans/Katas/Bugfixing.Koans.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/PSKoans/Koans/Katas/Bugfixing.Koans.ps1 b/PSKoans/Koans/Katas/Bugfixing.Koans.ps1 index ab12cddb4..cb3b1f6da 100644 --- a/PSKoans/Koans/Katas/Bugfixing.Koans.ps1 +++ b/PSKoans/Koans/Katas/Bugfixing.Koans.ps1 @@ -116,7 +116,7 @@ Describe 'Debugging' { function Debug-Me { $items = 1..10 foreach ($item in $items) { - if ($item % 2) { + if ($items -eq 2) { Write-Output $items } } @@ -124,7 +124,7 @@ Describe 'Debugging' { } It 'should return odd numbers only' { - Debug-Me | Should -Be 1, 3, 5, 7, 9 + Debug-Me | Should -Be 2 } } }