Skip to content

Commit

Permalink
🐛 Show-Karma - Properly handle completion of topics (#250)
Browse files Browse the repository at this point in the history
* 🐛 Fix Show-Karma not handling completed topics

Wasn't expecting completion at all and was completely mishandling the prompt parameter call.

* ✅ Add regression test

* 🐛 ✅ Fix logic and tests
Tests needed to be updated for Get-Karma as well
Show-Karma had its completion logic reversed, oops!

* 📝 add launch.json

* ♻️ Refactor Show-Karma logic
  • Loading branch information
vexx32 committed Sep 19, 2019
1 parent b6e1778 commit da66f06
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 33 deletions.
52 changes: 52 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "PowerShell Launch Current File",
"type": "PowerShell",
"request": "launch",
"script": "${file}",
"args": [],
"cwd": "${file}"
},
{
"name": "PowerShell Launch Current File in Temporary Console",
"type": "PowerShell",
"request": "launch",
"script": "${file}",
"args": [],
"cwd": "${file}",
"createTemporaryIntegratedConsole": true
},
{
"name": "PowerShell Launch Current File w/Args Prompt",
"type": "PowerShell",
"request": "launch",
"script": "${file}",
"args": [
"${command:SpecifyScriptArgs}"
],
"cwd": "${file}"
},
{
"name": "PowerShell Attach to Host Process",
"type": "PowerShell",
"request": "attach"
},
{
"name": "PowerShell Interactive Session",
"type": "PowerShell",
"request": "launch",
"cwd": ""
},
{
"name": "PowerShell Attach Interactive Session Runspace",
"type": "PowerShell",
"request": "attach",
"processId": "current"
}
]
}
36 changes: 23 additions & 13 deletions PSKoans/Public/Show-Karma.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function Show-Karma {
function Show-Karma {
[CmdletBinding(DefaultParameterSetName = 'Default',
HelpUri = 'https://github.com/vexx32/PSKoans/tree/master/docs/Show-Karma.md')]
[OutputType([void])]
Expand Down Expand Up @@ -73,19 +73,29 @@

$Results = if ($Topic) { Get-Karma -Topic $Topic } else { Get-Karma }

$Params = @{
DescribeName = $Results.Describe
ItName = $Results.It
Expectation = $Results.Expectation
Meditation = $Results.Meditation
KoansPassed = $Results.KoansPassed
TotalKoans = $Results.TotalKoans
CurrentTopic = $Results.CurrentTopic
Results = $PesterTests.TestResult
RequestedTopic = $Topic
if ($Results.Complete) {
$Params = @{
KoansPassed = $Results.KoansPassed
TotalKoans = $Results.TotalKoans
RequestedTopic = $Topic
Complete = $Results.Complete
}
}
if (-not $Detailed) {
$Params.Remove('Results')
else {
$Params = @{
DescribeName = $Results.Describe
ItName = $Results.It
Expectation = $Results.Expectation
Meditation = $Results.Meditation
KoansPassed = $Results.KoansPassed
TotalKoans = $Results.TotalKoans
CurrentTopic = $Results.CurrentTopic
RequestedTopic = $Topic
}

if ($Detailed) {
$Params.Add('Results', $PesterTests.TestResult)
}
}

Show-MeditationPrompt @Params
Expand Down
14 changes: 14 additions & 0 deletions Tests/Functions/Public/CompletedKoan/SelectedTopicTest.Koans.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using module PSKoans
[Koan(Position = 1)]
param()

Describe 'Koans Test' {

It 'is easy to solve' {
$true | Should -BeTrue
}

It 'is positively trivial' {
$false | Should -BeFalse
}
}
44 changes: 24 additions & 20 deletions Tests/Functions/Public/Get-Karma.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ Describe 'Get-Karma' {

Context 'With Nonexistent Koans Folder / No Koans Found' {
BeforeAll {
Mock Show-MeditationPrompt -ModuleName 'PSKoans' { }
Mock Measure-Koan -ModuleName 'PSKoans' { }
Mock Get-Koan -ModuleName 'PSKoans' { }
Mock Update-PSKoan -ModuleName 'PSKoans' { throw 'Prevent recursion' }
Expand Down Expand Up @@ -67,7 +66,6 @@ Describe 'Get-Karma' {

Context 'With -Topic Parameter' {
BeforeAll {
Mock Show-MeditationPrompt -ModuleName 'PSKoans' { }
Mock Invoke-Koan -ModuleName 'PSKoans' { }

$TestLocation = 'TestDrive:{0}PSKoans' -f [System.IO.Path]::DirectorySeparatorChar
Expand All @@ -87,32 +85,38 @@ Describe 'Get-Karma' {
Get-Karma -Topic $Topic
Assert-MockCalled Invoke-Koan -Times @($Topic).Count
}
}

It 'should not divide by zero if all Koans are completed' {
$KoansCompletedTestLocation = 'TestDrive:{0}PSKoansCompletedTest' -f [System.IO.Path]::DirectorySeparatorChar
$TestFile = Join-Path -Path $KoansCompletedTestLocation -ChildPath 'SingleTopicTest.Koans.Ps1'
Describe 'Behaviour When All Koans Are Completed' {
BeforeAll {
$StartLocation = Get-PSKoanLocation
Set-PSKoanLocation -Path "$PSScriptRoot/CompletedKoan"

New-Item $KoansCompletedTestLocation -ItemType Directory
New-Item $TestFile -ItemType File
$Result = Get-Karma -Topic SelectedTopicTest
}

Set-Content $TestFile -Value @'
using module PSKoans
[Koan(Position = 1)]
param()
It 'should output the result object' {
$Result | Should -Not -BeNullOrEmpty
$Result.PSTypeNames | Should -Contain 'PSKoans.CompleteResult'
}

Describe 'Koans Test' {
It 'is easy to solve' {
$true | should -be $true
}
}
'@
It 'should indicate completion' {
$Result.Complete | Should -BeTrue
}

Set-PSKoanLocation $KoansCompletedTestLocation
It 'should indicate number of koans passed' {
$Result.KoansPassed | Should -Be 2
}

{ Get-Karma -Topic SingleTopicTest } | Should -Not -Throw
It 'should indicate total number of koans' {
$Result.TotalKoans | Should -Be 2
}

Set-PSKoanLocation $TestLocation
It 'should indicate the requested topic' {
$Result.RequestedTopic | Should -Be 'SelectedTopicTest'
}

Set-PSKoanLocation -Path $StartLocation
}
}
}
38 changes: 38 additions & 0 deletions Tests/Functions/Public/Show-Karma.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,25 @@ Describe 'Show-Karma' {
}
}

Context 'With All Koans Completed' {
BeforeAll {
Mock Show-MeditationPrompt -ModuleName 'PSKoans' { $Complete }
Mock Get-Karma -ModuleName 'PSKoans' {
[PSCustomObject]@{
PSTypeName = 'PSKoans.CompleteResult'
KoansPassed = 10
TotalKoans = 10
RequestedTopci = $null
Complete = $true
}
}
}

It 'should not throw errors' {
{ Show-Karma } | Should -Not -Throw
}
}

Context 'With -ClearScreen Switch' {
BeforeAll {
Mock Clear-Host { }
Expand Down Expand Up @@ -142,6 +161,25 @@ Describe 'Show-Karma' {
}
}

Context 'With All Koans in a Single Topic Completed' {
BeforeAll {
Mock Show-MeditationPrompt -ModuleName 'PSKoans' { $Complete }
Mock Get-Karma -ModuleName 'PSKoans' {
[PSCustomObject]@{
PSTypeName = 'PSKoans.CompleteResult'
KoansPassed = 10
TotalKoans = 10
RequestedTopic = 'TestTopic'
Complete = $true
}
}
}

It 'should not throw errors' {
{ Show-Karma } | Should -Not -Throw
}
}

Context 'With -Contemplate Switch' {

Context 'VS Code Installed' {
Expand Down

0 comments on commit da66f06

Please sign in to comment.